super-voice-app-core/src/pages/recording_test.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! Speech to text's recording test, on its Behavior tab: Test recording |
3 | | //! records a take through the daemon, shows its words as they come, and then |
4 | | //! its transcript. |
5 | | //! |
6 | | //! The test shares the microphone's state with every page that records: it |
7 | | //! marks the microphone open the moment it starts, before the daemon says so, |
8 | | //! so a second press, or another page's Record, waits for it. |
9 | | |
10 | | use futures_util::{Stream, StreamExt}; |
11 | | use super_voice_shared::models::protocol::PreviewSource; |
12 | | |
13 | | use crate::daemon::client::{RecordEvent, record_command_stream, stop_record_command}; |
14 | | use crate::state::Microphone; |
15 | | use crate::state::recording_test::{Heard, RecordingTest}; |
16 | | |
17 | | /// What happened. |
18 | | #[derive(Debug, Clone, PartialEq, Eq)] |
19 | | pub enum Input { |
20 | | /// Test recording was pressed. |
21 | | Start, |
22 | | /// Stop recording was pressed. |
23 | | Stop, |
24 | | /// The take heard this much so far. |
25 | | Preview { |
26 | | text: String, |
27 | | source: Option<PreviewSource>, |
28 | | }, |
29 | | /// The take is over: its transcript, or why it failed. |
30 | | Transcribed(Result<String, String>), |
31 | | /// The daemon is back after the app lost it: what the test showed |
32 | | /// belonged to a session that is gone. |
33 | | Reconnected, |
34 | | } |
35 | | |
36 | | /// What the app should do. |
37 | | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
38 | | pub enum Effect { |
39 | | /// Record a take: run [`take`] and hand each of its inputs back. |
40 | | Take, |
41 | | /// Ask the daemon to stop it: [`run_stop`]. |
42 | | Stop, |
43 | | } |
44 | | |
45 | | /// Take in `input`, and say what to do. |
46 | 7 | pub fn update(test: &mut RecordingTest, microphone: &mut Microphone, input: Input) -> Vec<Effect> { |
47 | 7 | match input { |
48 | | Input::Start => { |
49 | | // The daemon records one take at a time. |
50 | 2 | if microphone.recording { |
51 | 1 | return Vec::new(); |
52 | 1 | } |
53 | 1 | microphone.recording = true; |
54 | 1 | *test = RecordingTest::default(); |
55 | 1 | vec![Effect::Take] |
56 | | } |
57 | 0 | Input::Stop => vec![Effect::Stop], |
58 | | // Previews have a line of their own; the transcript is left alone |
59 | | // until it arrives. |
60 | | Input::Preview { text, source } => { |
61 | 1 | test.preview = text; |
62 | 1 | test.preview_source = source; |
63 | 1 | Vec::new() |
64 | | } |
65 | | Input::Transcribed(result) => { |
66 | 3 | test.heard = Some(match result { |
67 | | // A blank transcript is a recording of nothing, not a failure. |
68 | 2 | Ok(words) if words.trim().is_empty() => Heard::Nothing1 , |
69 | 1 | Ok(words) => Heard::Words(words), |
70 | 1 | Err(e) => Heard::Failed(e), |
71 | | }); |
72 | | // The live line gives way to the transcript. |
73 | 3 | test.preview.clear(); |
74 | 3 | test.preview_source = None; |
75 | 3 | microphone.recording = false; |
76 | 3 | microphone.level = 0.0; |
77 | 3 | Vec::new() |
78 | | } |
79 | | Input::Reconnected => { |
80 | 1 | *test = RecordingTest::default(); |
81 | 1 | Vec::new() |
82 | | } |
83 | | } |
84 | 7 | } |
85 | | |
86 | | /// What the microphone is doing, as a message key: hearing speech, hearing |
87 | | /// silence, or closed. |
88 | | #[must_use] |
89 | 3 | pub const fn hearing_key(microphone: &Microphone) -> &'static str { |
90 | 3 | match (microphone.recording, microphone.speech) { |
91 | 1 | (true, true) => "hearing-speech", |
92 | 1 | (true, false) => "hearing-silence", |
93 | 1 | (false, _) => "microphone-closed", |
94 | | } |
95 | 3 | } |
96 | | |
97 | | /// What a preview spans, as a message key. |
98 | | #[must_use] |
99 | 0 | pub const fn spans_key(source: PreviewSource) -> &'static str { |
100 | 0 | match source { |
101 | 0 | PreviewSource::Stream => "everything-said-so-far", |
102 | 0 | PreviewSource::Window => "the-last-few-seconds", |
103 | | } |
104 | 0 | } |
105 | | |
106 | | /// What the test's transcript box says: the last take's words, that it heard |
107 | | /// nothing or failed, or what the box is for before any take. |
108 | | #[must_use] |
109 | 2 | pub fn heard_text(heard: Option<&Heard>) -> String { |
110 | 2 | match heard { |
111 | 0 | None => crate::i18n::text("test-transcript-shows-here", &[]), |
112 | 1 | Some(Heard::Words(words)) => words.clone(), |
113 | 0 | Some(Heard::Nothing) => crate::i18n::text("no-speech-detected", &[]), |
114 | 1 | Some(Heard::Failed(e)) => crate::i18n::text("the-recording-failed", &[("error", e)]) |
115 | 1 | .replace(['\u{2068}', '\u{2069}'], ""), |
116 | | } |
117 | 2 | } |
118 | | |
119 | | /// Record a take: what it hears so far, then its transcript. Dropping the |
120 | | /// stream stops the recording. |
121 | 0 | pub fn take() -> impl Stream<Item = Input> + Send + 'static { |
122 | 0 | record_command_stream().map(|event| match event { |
123 | 0 | RecordEvent::Preview { text, source } => Input::Preview { text, source }, |
124 | 0 | RecordEvent::Final(result) => Input::Transcribed(result), |
125 | 0 | }) |
126 | 0 | } |
127 | | |
128 | | /// Ask the daemon to stop the take. |
129 | 0 | pub async fn run_stop() { |
130 | 0 | if let Err(e) = stop_record_command().await { |
131 | 0 | log::warn!("Stopping the recording test failed: {e}"); |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | #[cfg(test)] |
136 | | mod tests { |
137 | | use super::{Effect, Input, update}; |
138 | | use crate::state::Microphone; |
139 | | use crate::state::recording_test::{Heard, RecordingTest}; |
140 | | |
141 | | #[test] |
142 | 1 | fn a_take_marks_the_microphone_open_at_once_and_ends_with_its_words() { |
143 | 1 | let mut test = RecordingTest { |
144 | 1 | heard: Some(Heard::Words("last time".into())), |
145 | 1 | ..RecordingTest::default() |
146 | 1 | }; |
147 | 1 | let mut microphone = Microphone::default(); |
148 | | |
149 | 1 | assert_eq!( |
150 | 1 | update(&mut test, &mut microphone, Input::Start), |
151 | 1 | [Effect::Take] |
152 | | ); |
153 | 1 | assert!(microphone.recording, "before the daemon says so"); |
154 | 1 | assert_eq!(test.heard, None, "the last take's words go"); |
155 | | |
156 | 1 | update( |
157 | 1 | &mut test, |
158 | 1 | &mut microphone, |
159 | 1 | Input::Preview { |
160 | 1 | text: "hello".into(), |
161 | 1 | source: None, |
162 | 1 | }, |
163 | 1 | ); |
164 | 1 | assert_eq!(test.preview, "hello"); |
165 | | |
166 | 1 | microphone.level = 0.7; |
167 | 1 | update( |
168 | 1 | &mut test, |
169 | 1 | &mut microphone, |
170 | 1 | Input::Transcribed(Ok("Hello there.".into())), |
171 | 1 | ); |
172 | 1 | assert_eq!(test.heard, Some(Heard::Words("Hello there.".into()))); |
173 | 1 | assert_eq!(test.preview, ""); |
174 | 1 | assert!(!microphone.recording); |
175 | 1 | assert!( |
176 | 1 | microphone.level.abs() < f32::EPSILON, |
177 | | "the meter goes quiet" |
178 | | ); |
179 | 1 | } |
180 | | |
181 | | #[test] |
182 | 1 | fn a_second_press_while_recording_does_nothing() { |
183 | 1 | let mut test = RecordingTest::default(); |
184 | 1 | let mut microphone = Microphone { |
185 | 1 | recording: true, |
186 | 1 | ..Microphone::default() |
187 | 1 | }; |
188 | 1 | assert!(update(&mut test, &mut microphone, Input::Start).is_empty()); |
189 | 1 | } |
190 | | |
191 | | #[test] |
192 | 1 | fn a_blank_transcript_is_nothing_heard_and_an_error_a_failure() { |
193 | 1 | let mut test = RecordingTest::default(); |
194 | 1 | let mut microphone = Microphone::default(); |
195 | 1 | update( |
196 | 1 | &mut test, |
197 | 1 | &mut microphone, |
198 | 1 | Input::Transcribed(Ok(" ".into())), |
199 | 1 | ); |
200 | 1 | assert_eq!(test.heard, Some(Heard::Nothing)); |
201 | 1 | update( |
202 | 1 | &mut test, |
203 | 1 | &mut microphone, |
204 | 1 | Input::Transcribed(Err("no model".into())), |
205 | 1 | ); |
206 | 1 | assert_eq!(test.heard, Some(Heard::Failed("no model".into()))); |
207 | 1 | } |
208 | | |
209 | | #[test] |
210 | 1 | fn the_test_says_what_it_hears_in_words_of_its_own() { |
211 | | use super::{heard_text, hearing_key}; |
212 | 1 | let mut microphone = Microphone::default(); |
213 | 1 | assert_eq!(hearing_key(µphone), "microphone-closed"); |
214 | 1 | microphone.recording = true; |
215 | 1 | assert_eq!(hearing_key(µphone), "hearing-silence"); |
216 | 1 | microphone.speech = true; |
217 | 1 | assert_eq!(hearing_key(µphone), "hearing-speech"); |
218 | 1 | assert_eq!(heard_text(Some(&Heard::Words("hi".into()))), "hi"); |
219 | 1 | let failed = heard_text(Some(&Heard::Failed("no model".into()))); |
220 | 1 | assert!(failed.contains("no model") && !failed.contains('\u{2068}')); |
221 | 1 | for key in [ |
222 | 1 | "hearing-speech", |
223 | 1 | "hearing-silence", |
224 | 1 | "microphone-closed", |
225 | 1 | "test-transcript-shows-here", |
226 | 1 | ] { |
227 | 4 | assert!(crate::i18n::LANGUAGE_LOADER.has(key), "no message {key}"); |
228 | | } |
229 | 1 | } |
230 | | |
231 | | #[test] |
232 | 1 | fn a_reconnect_clears_the_test() { |
233 | 1 | let mut test = RecordingTest { |
234 | 1 | preview: "half".into(), |
235 | 1 | heard: Some(Heard::Nothing), |
236 | 1 | ..RecordingTest::default() |
237 | 1 | }; |
238 | 1 | update(&mut test, &mut Microphone::default(), Input::Reconnected); |
239 | 1 | assert_eq!(test, RecordingTest::default()); |
240 | 1 | } |
241 | | } |