super-voice-app-core/src/state/microphone.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! The microphone, as the daemon's events report it. Every page that records |
3 | | //! reads it: whether something else holds the microphone, how loud it is, and |
4 | | //! whether that sounds like speech. |
5 | | |
6 | | use crate::events::Event; |
7 | | |
8 | | /// What the daemon last said about its microphone. |
9 | | #[derive(Debug, Clone, Copy, Default, PartialEq)] |
10 | | pub struct Microphone { |
11 | | /// Whether the microphone is open: a take of any app, or dictation. |
12 | | pub recording: bool, |
13 | | /// How loud it is, 0 to 1. |
14 | | pub level: f32, |
15 | | /// Whether that sounds like speech. |
16 | | pub speech: bool, |
17 | | } |
18 | | |
19 | | impl Microphone { |
20 | | /// Take in `event`. Whether anything changed: most events are about |
21 | | /// something else, and a meter's reading often repeats. |
22 | 7 | pub fn follow(&mut self, event: &Event) -> bool { |
23 | 7 | let next = match *event { |
24 | 1 | Event::Recording(true) => Self { |
25 | 1 | recording: true, |
26 | 1 | ..*self |
27 | 1 | }, |
28 | | // A closed microphone is a quiet one, whatever its last reading. |
29 | | // A stream live again missed what happened while it was down, so |
30 | | // nothing is taken to be recording until the daemon says so. |
31 | 2 | Event::Recording(false) | Event::Connected => Self::default(), |
32 | | Event::MicrophoneLevel { |
33 | | level, is_speech, .. |
34 | 3 | } => Self { |
35 | 3 | level, |
36 | 3 | speech: is_speech, |
37 | 3 | ..*self |
38 | 3 | }, |
39 | 1 | _ => return false, |
40 | | }; |
41 | 6 | let changed = next != *self; |
42 | 6 | *self = next; |
43 | 6 | changed |
44 | 7 | } |
45 | | } |
46 | | |
47 | | #[cfg(test)] |
48 | | mod tests { |
49 | | use super::Microphone; |
50 | | use crate::events::Event; |
51 | | |
52 | | #[test] |
53 | 1 | fn opens_reads_and_closes() { |
54 | 1 | let mut microphone = Microphone::default(); |
55 | 1 | assert!(microphone.follow(&Event::Recording(true))); |
56 | 1 | assert!(microphone.follow(&Event::MicrophoneLevel { |
57 | 1 | level: 0.6, |
58 | 1 | is_speech: true, |
59 | 1 | bands: Vec::new(), |
60 | 1 | total_energy: 0.0, |
61 | 1 | })); |
62 | 1 | assert_eq!( |
63 | 1 | microphone, |
64 | 1 | Microphone { |
65 | 1 | recording: true, |
66 | 1 | level: 0.6, |
67 | 1 | speech: true, |
68 | 1 | } |
69 | | ); |
70 | 1 | assert!(microphone.follow(&Event::Recording(false))); |
71 | 1 | assert_eq!(microphone, Microphone::default()); |
72 | 1 | } |
73 | | |
74 | | #[test] |
75 | 1 | fn a_repeated_reading_or_another_event_changes_nothing() { |
76 | 1 | let mut microphone = Microphone::default(); |
77 | 1 | let reading = Event::MicrophoneLevel { |
78 | 1 | level: 0.2, |
79 | 1 | is_speech: false, |
80 | 1 | bands: Vec::new(), |
81 | 1 | total_energy: 0.0, |
82 | 1 | }; |
83 | 1 | assert!(microphone.follow(&reading)); |
84 | 1 | assert!(!microphone.follow(&reading)); |
85 | 1 | assert!(!microphone.follow(&Event::Speaking { |
86 | 1 | is_speaking: true, |
87 | 1 | utterance_id: None, |
88 | 1 | })); |
89 | 1 | } |
90 | | |
91 | | /// The daemon may have closed the microphone while the stream was down. |
92 | | #[test] |
93 | 1 | fn a_reconnect_forgets_the_microphone() { |
94 | 1 | let mut microphone = Microphone { |
95 | 1 | recording: true, |
96 | 1 | level: 0.4, |
97 | 1 | speech: true, |
98 | 1 | }; |
99 | 1 | assert!(microphone.follow(&Event::Connected)); |
100 | 1 | assert_eq!(microphone, Microphone::default()); |
101 | 1 | } |
102 | | } |