From 1eb4c4955271d2429d29914ba0de9e4ead115248 Mon Sep 17 00:00:00 2001 From: softmaxe <94467584+softmaxe@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:43:44 +0800 Subject: [PATCH 01/16] feat(text): remove filler words before dictation delivery --- README.md | 2 + README.zh-CN.md | 2 + docs/testing.md | 8 ++ locales/en.json | 4 +- locales/zh-CN.json | 4 +- src/pipeline.rs | 69 +++++++++- src/settings.rs | 3 + src/text/filler_words.rs | 229 +++++++++++++++++++++++++++++++ src/text/mod.rs | 1 + src/ui/settings_modal/servers.rs | 9 ++ 10 files changed, 325 insertions(+), 6 deletions(-) create mode 100644 src/text/filler_words.rs diff --git a/README.md b/README.md index 0047303..f63fc61 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ typing in. `/audio/transcriptions` endpoint; optional cleanup goes to a `/chat/completions` endpoint, with an editable prompt and a Prompt Studio to test it. Test Connection checks each server and lists its models. +- **Fewer hesitation sounds.** Optional local filler-word removal works even + with text cleanup off, while preserving the raw transcript in History. - **Gets better with use.** A dictionary supplies spelling hints, corrections you make right after a paste are learned, and Snippets expand short phrases into longer text. Removing a word or snippet can be undone. diff --git a/README.zh-CN.md b/README.zh-CN.md index cb858df..b286282 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -28,6 +28,8 @@ - **自托管模型。** 语音识别请求发送到你自己的 `/audio/transcriptions` 接口;可选的 文本整理请求发送到 `/chat/completions` 接口,提示词可编辑,并可在 Prompt Studio 中测试。“测试连接”可检查每个服务器并列出它的模型。 +- **减少犹豫音。** 可在本机移除“呃”、um、uh 等语气词,无需开启文本整理; + 历史记录仍保留服务器返回的原始转录。 - **越用越准。** 词典为识别提供拼写提示;粘贴后立即做出的修改会被自动学习; 片段(Snippets)可以把短语展开为更长的文本。删除词条或片段后可以撤销。 - **历史记录。** 用 Cmd+K 搜索、查看原始转录、用保存的音频重新转录、撤销删除, diff --git a/docs/testing.md b/docs/testing.md index 3aee28a..1845468 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -126,3 +126,11 @@ Do not reset TCC permissions, change system trust, or delete Keychain items duri - Tab through theme choices, buttons, switches, inputs, and dropdowns. Check focus, hover, selected, disabled, and destructive states. The filled action must retain a clearly contrasting label; switches must remain distinguishable by both thumb position and track color. - With `WHISPER_USER_DATA_DIR` pointing to a temporary directory, use `WHISPER_DEV_SCENE` in a debug build to inspect `pill:recording`, `pill:processing`, `pill:preparing`, `pill:error`, and `pill:fallback`. Tokyo Night uses the same indigo surface family as the workspace, muted blue voice activity, and distinct error/recovery colors. Light and Dark retain their dark floating HUD. - Automated tests cover preference serialization, system-appearance resolution, reading contrast, focus indicators, and selected/action text. They complement visual review; they do not prove native interaction or rendering quality. + +## Filler-word removal regression check + +- In Settings > Speech to text, toggle Remove filler words, restart, and verify the preference persists. Existing settings enable it by default. +- With cleanup off and English selected, dictate “Um, we should go”; delivery should read “We should go” while History's raw transcript retains the hesitation. With Auto selected, “um carro” must remain intact. +- Check Chinese hesitation runs and the exceptions 打呃 and 呃逆. Newlines, ordinary words, and the non-filler Chinese words 嗯, 啊, 额, 那个, 然后, and 就是 must remain. +- Check filler-only input produces no-audio feedback, and a whole snippet still expands when preceded by a filler. Turn removal off and verify the transcript is unchanged. File uploads must continue returning raw server text. +- Tab to the switch and activate it with the keyboard; verify its VoiceOver label in both interface languages and all three themes. diff --git a/locales/en.json b/locales/en.json index 1204a7a..d0f8dda 100644 --- a/locales/en.json +++ b/locales/en.json @@ -166,7 +166,9 @@ "serverTitle": "Server", "modelHelp": "The model your server should run. Test Connection lists the ones it offers.", "serverModels": "Models on this server", - "protocolTag": "OpenAI-compatible · no auth" + "protocolTag": "OpenAI-compatible · no auth", + "removeFillerWords": "Remove filler words", + "removeFillerWordsDescription": "Drops hesitation sounds such as “um” and “uh” from transcripts. Works without text cleanup." }, "llms": { "server": { diff --git a/locales/zh-CN.json b/locales/zh-CN.json index 2aa5669..f34f512 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -163,7 +163,9 @@ "serverTitle": "服务器", "modelHelp": "服务器要运行的模型。点“测试连接”可列出它提供的模型。", "serverModels": "此服务器上的模型", - "protocolTag": "OpenAI 兼容 · 无鉴权" + "protocolTag": "OpenAI 兼容 · 无鉴权", + "removeFillerWords": "移除语气词", + "removeFillerWordsDescription": "自动去掉转录中的“呃”、um、uh 等犹豫音,无需开启文本整理。" }, "llms": { "server": { diff --git a/src/pipeline.rs b/src/pipeline.rs index c2c783c..40cd849 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -173,6 +173,14 @@ fn cleanup_failure(err: &CleanupError) -> CleanupFailure { } } +fn filter_fillers(text: &str, settings: &Settings) -> String { + if settings.remove_filler_words { + crate::text::filler_words::remove_filler_words(text, &settings.preferred_language) + } else { + text.to_string() + } +} + /// The deterministic Chinese script pass, run on the cleaned text. pub fn finalize_script(text: &str, settings: &Settings) -> String { let target = resolve_chinese_script_target( @@ -207,15 +215,19 @@ pub async fn run(input: PipelineInput, cancel: CancellationToken) -> PipelineOut Err(err) => return PipelineOutcome::failed_from(&err), }; let raw = response.text; + let filtered = filter_fillers(&raw, &input.settings); + if filtered.trim().is_empty() { + return PipelineOutcome::NoAudio; + } if cancel.is_cancelled() { return PipelineOutcome::Cancelled; } // A whole trigger is intentional input even when it equals the ASR hint. // Deliver saved text verbatim, without letting cleanup rewrite the trigger. - if let Some(text) = whole_snippet_replacement(&raw, &input.snippets) { + if let Some(text) = whole_snippet_replacement(&filtered, &input.snippets) { return PipelineOutcome::Transcribed(Transcript { text, - spoken_text: raw.trim().to_string(), + spoken_text: filtered.trim().to_string(), raw_text: raw, cleanup_failure: None, }); @@ -234,13 +246,13 @@ pub async fn run(input: PipelineInput, cancel: CancellationToken) -> PipelineOut if cancel.is_cancelled() { return PipelineOutcome::Cancelled; } - let (cleaned, cleanup_failure) = clean_up(&raw, &input, &cancel).await; + let (cleaned, cleanup_failure) = clean_up(&filtered, &input, &cancel).await; if cancel.is_cancelled() { return PipelineOutcome::Cancelled; } // Mishearings were learned from delivered text, so they match it here. let spoken_text = apply_known_corrections( - &finalize_script(&cleaned, &input.settings), + &finalize_script(&filter_fillers(&cleaned, &input.settings), &input.settings), &input.known_corrections, ); if spoken_text.trim().is_empty() { @@ -492,6 +504,55 @@ mod tests { ); } + #[test] + fn filler_only_transcripts_are_no_audio_without_cleanup() { + let asr = asr("Um, uh, 呃,"); + assert_eq!( + run_test(run(served(&asr, None), CancellationToken::new())), + PipelineOutcome::NoAudio + ); + } + + #[test] + fn filler_removal_preserves_raw_text_and_can_be_disabled() { + for enabled in [true, false] { + let asr = asr("Um, we should go"); + let mut input = served(&asr, None); + input.settings.remove_filler_words = enabled; + let t = transcribed(run_test(run(input, CancellationToken::new()))); + assert_eq!(t.raw_text, "Um, we should go"); + assert_eq!( + t.text, + if enabled { + "We should go" + } else { + "Um, we should go" + } + ); + } + } + + #[test] + fn filler_removal_wraps_cleanup_and_precedes_whole_snippet_matching() { + let asr = asr("Um, we should go"); + let llm = llm(completion("Uh, we should go.")); + let t = transcribed(run_test(run( + served(&asr, Some(&llm)), + CancellationToken::new(), + ))); + assert_eq!(t.text, "We should go."); + assert!(String::from_utf8_lossy(&llm.requests()[0].body).contains("We should go")); + let asr = self::asr("Um, sig."); + let mut input = served(&asr, None); + input.snippets = vec![Snippet { + trigger: "sig".into(), + replacement: "Best regards".into(), + }]; + let t = transcribed(run_test(run(input, CancellationToken::new()))); + assert_eq!(t.text, "Best regards"); + assert_eq!(t.raw_text, "Um, sig."); + } + #[test] fn dictionary_echo_keeps_candidate_text_without_cleanup_or_delivery() { for (spoken, dictionary) in [ diff --git a/src/settings.rs b/src/settings.rs index 576bc75..9daa1a4 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -133,6 +133,7 @@ pub struct Settings { pub remote_transcription_url: String, pub remote_transcription_model: String, + pub remove_filler_words: bool, pub use_cleanup_model: bool, pub cleanup_model: String, pub cleanup_remote_url: String, @@ -170,6 +171,7 @@ impl Default for Settings { chinese_script_preference: ChineseScriptPreference::AsTranscribed, remote_transcription_url: String::new(), remote_transcription_model: String::new(), + remove_filler_words: true, use_cleanup_model: true, cleanup_model: String::new(), cleanup_remote_url: String::new(), @@ -359,5 +361,6 @@ mod tests { let s: Settings = serde_json::from_str(r#"{"theme":"dark"}"#).unwrap(); assert_eq!(s.theme, ThemePreference::Dark); assert_eq!(s.audio_retention_days, 30); + assert!(s.remove_filler_words); } } diff --git a/src/text/filler_words.rs b/src/text/filler_words.rs new file mode 100644 index 0000000..705679c --- /dev/null +++ b/src/text/filler_words.rs @@ -0,0 +1,229 @@ +//! Conservative, deterministic hesitation removal, independent of cleanup. + +const UNIVERSAL: &[&str] = &[ + "uh", "uhm", "umm", "uhh", "uhhh", "ehh", "ehm", "ahm", "hmm", "hm", "mmm", "хм", "ммм", +]; +const ENGLISH: &[&str] = &["um", "ah", "eh"]; + +fn word(c: char) -> bool { + c.is_alphanumeric() || c == '_' +} + +fn horizontal(c: char) -> bool { + matches!(c, ' ' | '\t') +} + +fn sentence_start(text: &str) -> bool { + text.trim_end_matches([' ', '\t', '"', '\'', '“', '‘', '(']) + .chars() + .next_back() + .is_none_or(|c| matches!(c, '.' | '!' | '?' | '\n' | '。' | '!' | '?')) +} + +/// A missing language is Auto; short English words then require punctuation +/// to avoid deleting ordinary words in other languages (Portuguese `um`). +pub fn remove_filler_words(text: &str, language: &str) -> String { + let base = language + .split(['-', '_']) + .next() + .unwrap_or("") + .to_lowercase(); + let auto = base.is_empty() || base == "auto"; + let specific: &[&str] = match base.as_str() { + "en" => ENGLISH, + "de" => &["äh", "ähm"], + "fr" => &["euh"], + _ => &[], + }; + let mut current = text.to_string(); + // Removing an adjacent filler can expose the punctuation qualifying an + // Auto English token. Finish that work here so the pass is idempotent. + loop { + let next = remove_pass(¤t, specific, auto); + if next == current { + return next; + } + current = next; + } +} + +fn remove_pass(text: &str, specific: &[&str], auto: bool) -> String { + let mut ranges: Vec<(usize, usize, bool)> = Vec::new(); + let mut cursor = 0; + while cursor < text.len() { + let Some(c) = text[cursor..].chars().next() else { + break; + }; + if c == '呃' { + let start = cursor; + while text[cursor..].starts_with('呃') { + cursor += '呃'.len_utf8(); + } + if text[..start].ends_with('打') || text[cursor..].starts_with('逆') { + continue; + } + if let Some(c @ (',' | '、' | ',')) = text[cursor..].chars().next() { + cursor += c.len_utf8(); + } + ranges.push((start, cursor, false)); + } else if word(c) { + let start = cursor; + cursor += c.len_utf8(); + while let Some(c) = text[cursor..] + .chars() + .next() + .filter(|c| word(*c) && *c != '呃') + { + cursor += c.len_utf8(); + } + let token = &text[start..cursor]; + let lower = token.to_lowercase(); + let following = text[cursor..].trim_start_matches(horizontal).chars().next(); + let hesitation = + following.is_none_or(|c| matches!(c, ',' | '.' | '…' | '-' | '–' | '—')); + let boundary = text[..start].chars().next_back().is_none_or(|c| !word(c)) + && text[cursor..].chars().next().is_none_or(|c| !word(c)); + if boundary + && (UNIVERSAL.contains(&lower.as_str()) + || specific.contains(&lower.as_str()) + || (auto && hesitation && ENGLISH.contains(&lower.as_str()))) + { + let capitalize = c.is_uppercase() && sentence_start(&text[..start]); + if text[cursor..].starts_with([',', '.']) { + cursor += 1; + } + ranges.push((start, cursor, capitalize)); + } + } else { + cursor += c.len_utf8(); + } + } + if ranges.is_empty() { + return text.to_string(); + } + let mut out = String::with_capacity(text.len()); + let mut previous = 0; + let mut capitalize = false; + let mut after_removal = false; + for (start, end, recase) in + ranges + .into_iter() + .chain(std::iter::once((text.len(), text.len(), false))) + { + let mut kept = &text[previous..start]; + if after_removal { + let whitespace = kept.len() - kept.trim_start_matches(horizontal).len(); + if out.ends_with(horizontal) { + let trimmed = out.trim_end_matches(horizontal).len(); + out.truncate(trimmed); + if !out.is_empty() && !out.ends_with('\n') && whitespace < kept.len() { + out.push(' '); + } + kept = &kept[whitespace..]; + } else if out.is_empty() || out.ends_with('\n') { + kept = &kept[whitespace..]; + } + if out.is_empty() || out.ends_with('\n') { + kept = kept + .trim_start_matches([',', '.', '…', '-', '–', '—', ',', '、']) + .trim_start_matches(horizontal); + } + } + for c in kept.chars() { + if capitalize && c.is_alphabetic() { + out.extend(c.to_uppercase()); + capitalize = false; + } else { + out.push(c); + } + } + capitalize |= recase; + previous = end; + after_removal = true; + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn universal_and_language_specific_lists() { + for language in ["auto", "en", "pt", "de", "fr", "zh-CN"] { + for token in UNIVERSAL { + assert_eq!( + remove_filler_words(&format!("{token}, hello"), language), + "hello", + "{token}/{language}" + ); + } + } + for (language, tokens) in [ + ("en-US", ENGLISH), + ("de-DE", &["äh", "ähm"][..]), + ("fr", &["euh"][..]), + ] { + for token in tokens { + assert_eq!( + remove_filler_words(&format!("{token} hello"), language), + "hello" + ); + } + } + } + + #[test] + fn boundaries_and_auto_preserve_ordinary_words() { + for text in [ + "umbrella", + "hmmm-like", + "ahead", + "uh_thing", + "1uh", + "éuh", + "um carro", + "ah bon", + "euh oui", + ] { + assert_eq!(remove_filler_words(text, "auto"), text); + } + assert_eq!(remove_filler_words("Um, I think", "auto"), "I think"); + assert_eq!( + remove_filler_words("Um, we should go", "en"), + "We should go" + ); + assert_eq!( + remove_filler_words("hello. Um, we should go", "auto"), + "hello. We should go" + ); + assert_eq!(remove_filler_words("um carro", "pt-BR"), "um carro"); + } + + #[test] + fn chinese_runs_and_exceptions() { + for language in ["en", "auto", "zh-CN"] { + assert_eq!( + remove_filler_words("呃呃,我们呃、走吧呃,好", language), + "我们走吧好" + ); + let text = "打呃 呃逆 嗯 啊 额 那个 然后 就是"; + assert_eq!(remove_filler_words(text, language), text); + } + } + + #[test] + fn whitespace_punctuation_and_idempotence() { + for (text, expected) in [ + ("a b\tc", "a b\tc"), + ("a uh, \t b\nUm, we go\n\nuh", "a b\nWe go\n\n"), + ("Um... we go", "We go"), + ("um uh,", ""), + ("uh, uhm. 呃,", ""), + ] { + let result = remove_filler_words(text, "auto"); + assert_eq!(result, expected, "{text:?}"); + assert_eq!(remove_filler_words(&result, "auto"), result); + } + } +} diff --git a/src/text/mod.rs b/src/text/mod.rs index 35d8da8..761ace2 100644 --- a/src/text/mod.rs +++ b/src/text/mod.rs @@ -5,6 +5,7 @@ pub mod chinese_script; pub mod correction_learner; pub mod dictionary_import; pub mod echo_filter; +pub mod filler_words; pub mod format; pub mod hotkeys; pub mod language; diff --git a/src/ui/settings_modal/servers.rs b/src/ui/settings_modal/servers.rs index 3877666..61308a2 100644 --- a/src/ui/settings_modal/servers.rs +++ b/src/ui/settings_modal/servers.rs @@ -283,6 +283,15 @@ impl SettingsModal { .into_any_element(), ); } + language_rows.push(toggle_row( + "remove-filler-words", + t("settings.speechToText.removeFillerWords"), + Some(t("settings.speechToText.removeFillerWordsDescription")), + settings(cx).remove_filler_words, + false, + |v, _, cx| update_settings(cx, |s| s.remove_filler_words = v), + &p, + )); let server = settings_section( t("settings.speechToText.serverTitle"), vec![ From ed235028be7df02af052c22d7882070a0a9ff0cf Mon Sep 17 00:00:00 2001 From: softmaxe <94467584+softmaxe@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:48:18 +0800 Subject: [PATCH 02/16] feat(dictation): skip cleanup without cancelling transcription --- README.md | 2 + README.zh-CN.md | 1 + docs/testing.md | 7 ++ locales/en.json | 6 +- locales/zh-CN.json | 6 +- src/app_state.rs | 1 + src/dictation.rs | 69 ++++++++++++++++- src/history.rs | 1 + src/pipeline.rs | 124 +++++++++++++++++++++++++++++- src/services/performance_tests.rs | 7 +- src/ui/dev_scene.rs | 27 ++++++- src/ui/pill.rs | 4 +- src/ui/pill/waveform.rs | 80 ++++++++++++++++++- 13 files changed, 320 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f63fc61..77f65c0 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ typing in. recording (key combinations toggle with one press), or hold to talk. A floating recording pill shows a live waveform and the elapsed time, with a cancel button beside it; click the pill to finish or press Escape to cancel. + During polishing, Skip inserts the recognized text immediately with local + corrections and snippets still applied. If the server fails, the recording is kept so you can resend it. - **Self-hosted models.** Speech recognition goes to your own `/audio/transcriptions` endpoint; optional cleanup goes to a diff --git a/README.zh-CN.md b/README.zh-CN.md index b286282..043bd9f 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -24,6 +24,7 @@ - **随处听写。** 可用 Globe/Fn 键、单个修饰键(如右 Command)或任意组合键触发。 单键双击进入免手持录音(组合键按一次即开始或结束),或按住说话。录音时会出现 悬浮条,显示实时波形和已录时长,旁边有取消按钮;点击悬浮条结束录音,按 Esc 取消。 + 润色时可点击“跳过”直接插入识别结果,本机纠错和片段展开仍会执行。 如果服务器出错,录音会被保留,可以重新发送。 - **自托管模型。** 语音识别请求发送到你自己的 `/audio/transcriptions` 接口;可选的 文本整理请求发送到 `/chat/completions` 接口,提示词可编辑,并可在 Prompt Studio diff --git a/docs/testing.md b/docs/testing.md index 1845468..56f5b25 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -134,3 +134,10 @@ Do not reset TCC permissions, change system trust, or delete Keychain items duri - Check Chinese hesitation runs and the exceptions 打呃 and 呃逆. Newlines, ordinary words, and the non-filler Chinese words 嗯, 啊, 额, 那个, 然后, and 就是 must remain. - Check filler-only input produces no-audio feedback, and a whole snippet still expands when preceded by a filler. Turn removal off and verify the transcript is unchanged. File uploads must continue returning raw server text. - Tab to the switch and activate it with the keyboard; verify its VoiceOver label in both interface languages and all three themes. + +## Skip cleanup regression check + +- Delay the cleanup server, dictate, and verify Transcribing becomes Polishing only after ASR completes. Skip must appear inside the capsule while Cancel remains beside it. Skip is unavailable during ASR. +- Click Skip and verify immediate insertion of the recognized text with filler removal, Chinese script conversion, known corrections, and snippets. There must be no cleanup failure notice and no second paste when the delayed reply arrives. Cancel must still discard the whole operation. +- Tab to Skip and activate it with Return or Space. Check VoiceOver's “Skip cleanup” label and the “Insert without cleanup” hover hint. Check smooth width changes and emergence, then repeat with Reduce motion for immediate changes. +- Use `WHISPER_DEV_SCENE=pill:cleanup` with isolated data to inspect Light, Dark and Tokyo Night, and both interface languages. diff --git a/locales/en.json b/locales/en.json index d0f8dda..f5256f5 100644 --- a/locales/en.json +++ b/locales/en.json @@ -370,7 +370,11 @@ "cancel": "Cancel", "cancelDictation": "Cancel Dictation", "dismiss": "Close", - "closesIn": "Closes in {{seconds}} s; hover to keep open" + "closesIn": "Closes in {{seconds}} s; hover to keep open", + "polishing": "Polishing", + "skip": "Skip", + "skipHint": "Insert without cleanup", + "skipCleanup": "Skip cleanup" }, "keptRecording": "Your {{time}} recording is kept until you close this card." }, diff --git a/locales/zh-CN.json b/locales/zh-CN.json index f34f512..3148d0b 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -366,7 +366,11 @@ "cancel": "取消", "cancelDictation": "取消听写", "dismiss": "关闭", - "closesIn": "{{seconds}} 秒后关闭;悬停可保持打开" + "closesIn": "{{seconds}} 秒后关闭;悬停可保持打开", + "polishing": "润色中", + "skip": "跳过", + "skipHint": "不润色直接插入", + "skipCleanup": "跳过润色" }, "keptRecording": "这段 {{time}} 的录音会一直保留,直到你关闭此卡片。" }, diff --git a/src/app_state.rs b/src/app_state.rs index 115b3d2..9412e04 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -28,6 +28,7 @@ use crate::ui::theme; pub struct DictationSnapshot { pub is_recording: bool, pub is_processing: bool, + pub is_cleaning: bool, pub is_preparing: bool, } diff --git a/src/dictation.rs b/src/dictation.rs index 9372696..da33aee 100644 --- a/src/dictation.rs +++ b/src/dictation.rs @@ -166,6 +166,8 @@ pub struct Dictation { generation: u64, start_lock: bool, cancel: Option, + skip_cleanup: Option, + is_cleaning: bool, target_pid: Option, started_at: Option<(Instant, DateTime)>, effects_held: bool, @@ -212,6 +214,8 @@ impl Dictation { generation: 0, start_lock: false, cancel: None, + skip_cleanup: None, + is_cleaning: false, target_pid: None, started_at: None, effects_held: false, @@ -248,6 +252,10 @@ impl Dictation { fn set_phase(&mut self, phase: Phase, cx: &mut Context) { let previous = self.phase; self.phase = phase; + if phase != Phase::Processing { + self.is_cleaning = false; + self.skip_cleanup = None; + } self.status.set(phase); if phase != Phase::Preparing { backend::clear_hotkey_start_request(); @@ -262,6 +270,7 @@ impl Dictation { let snapshot = DictationSnapshot { is_recording: self.phase == Phase::Recording, is_processing: self.phase == Phase::Processing, + is_cleaning: self.phase == Phase::Processing && self.is_cleaning, is_preparing: self.phase == Phase::Preparing, }; AppState::global(cx).update(cx, |s, cx| { @@ -446,6 +455,42 @@ impl Dictation { true } + pub fn skip_cleanup(&mut self, cx: &mut Context) { + if self.phase == Phase::Processing && self.is_cleaning { + if let Some(skip) = self.skip_cleanup.take() { + skip.cancel(); + } + self.is_cleaning = false; + self.publish(cx); + } + } + + fn cleanup_control( + &mut self, + cx: &mut Context, + ) -> (CancellationToken, async_channel::Sender) { + let skip = CancellationToken::new(); + self.skip_cleanup = Some(skip.clone()); + let (tx, rx) = async_channel::unbounded(); + let generation = self.generation; + cx.spawn(async move |this, cx| { + while let Ok(running) = rx.recv().await { + let _ = this.update(cx, |this, cx| { + if this.generation == generation && this.phase == Phase::Processing { + this.is_cleaning = running + && this + .skip_cleanup + .as_ref() + .is_some_and(|skip| !skip.is_cancelled()); + this.publish(cx); + } + }); + } + }) + .detach(); + (skip, tx) + } + pub fn cancel_processing(&mut self, cx: &mut Context) { if self.phase != Phase::Processing { return; @@ -541,6 +586,7 @@ impl Dictation { let generation = self.generation; let cancel = CancellationToken::new(); self.cancel = Some(cancel.clone()); + let (skip, stage) = self.cleanup_control(cx); let stopped = Instant::now(); let handle = crate::services::runtime::spawn(async move { let conversion_cancel = cancel.clone(); @@ -567,7 +613,8 @@ impl Dictation { } let mut input = input.await; input.audio = wav.clone(); - let outcome = run_prepared_pipeline(input, cancel).await; + input.cleanup_stage = Some(stage); + let outcome = run_prepared_pipeline_controlled(input, cancel, skip).await; log::info!( "[perf] operation={generation} stage=pipeline_complete elapsed_ms={:.3}", stopped.elapsed().as_secs_f64() * 1000.0 @@ -622,8 +669,11 @@ impl Dictation { let input = Self::pipeline_input(wav.clone(), cx); let cancel = CancellationToken::new(); self.cancel = Some(cancel.clone()); + let (skip, stage) = self.cleanup_control(cx); let handle = crate::services::runtime::spawn(async move { - run_prepared_pipeline(input.await, cancel).await + let mut input = input.await; + input.cleanup_stage = Some(stage); + run_prepared_pipeline_controlled(input, cancel, skip).await }); cx.spawn(async move |this, cx| { let outcome = handle.await.unwrap_or(PipelineOutcome::Failed { @@ -921,12 +971,18 @@ fn capture_pipeline_input( snippets: db.get_snippets().unwrap_or_default(), settings, cleanup_api_key: None, + cleanup_stage: None, }) } -async fn run_prepared_pipeline( +async fn run_prepared_pipeline(input: PipelineInput, cancel: CancellationToken) -> PipelineOutcome { + run_prepared_pipeline_controlled(input, cancel, CancellationToken::new()).await +} + +async fn run_prepared_pipeline_controlled( mut input: PipelineInput, cancel: CancellationToken, + skip_cleanup: CancellationToken, ) -> PipelineOutcome { if cancel.is_cancelled() { return PipelineOutcome::Cancelled; @@ -945,7 +1001,7 @@ async fn run_prepared_pipeline( if cancel.is_cancelled() { return PipelineOutcome::Cancelled; } - pipeline::run(input, cancel).await + pipeline::run(input, cancel, skip_cleanup).await } /// Re-transcribes a History row from its saved audio through the same @@ -1042,6 +1098,8 @@ mod tests { generation: 7, start_lock: false, cancel: None, + skip_cleanup: None, + is_cleaning: false, target_pid: None, started_at: None, effects_held: false, @@ -1181,6 +1239,7 @@ mod tests { spoken_text: "words worth keeping".into(), raw_text: "um words worth keeping".into(), cleanup_failure: None, + cleanup_skipped: false, }; for retention_enabled in [false, true] { let saved = history @@ -1248,6 +1307,8 @@ mod tests { generation: 7, start_lock: false, cancel: Some(cancel.clone()), + skip_cleanup: None, + is_cleaning: false, target_pid: None, started_at: None, effects_held: false, diff --git a/src/history.rs b/src/history.rs index 7475190..a42e7cf 100644 --- a/src/history.rs +++ b/src/history.rs @@ -217,6 +217,7 @@ mod tests { spoken_text: text.into(), raw_text: format!("um {text}"), cleanup_failure: None, + cleanup_skipped: false, } } diff --git a/src/pipeline.rs b/src/pipeline.rs index 40cd849..9bce9b5 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -36,6 +36,7 @@ pub struct PipelineInput { pub known_corrections: Vec, pub snippets: Vec, pub cleanup_api_key: Option, + pub cleanup_stage: Option>, } /// Why cleanup did not run on a transcript that was still delivered raw. @@ -57,6 +58,7 @@ pub struct Transcript { /// The server's transcript before cleanup. pub raw_text: String, pub cleanup_failure: Option, + pub cleanup_skipped: bool, } #[derive(Debug, Clone, PartialEq)] @@ -194,7 +196,11 @@ pub fn finalize_script(text: &str, settings: &Settings) -> String { /// Transcribes `input.audio`, filters dictionary echoes, cleans up, converts /// the Chinese script and expands Snippets. Cancelling `cancel` aborts only /// this run. -pub async fn run(input: PipelineInput, cancel: CancellationToken) -> PipelineOutcome { +pub async fn run( + input: PipelineInput, + cancel: CancellationToken, + skip_cleanup: CancellationToken, +) -> PipelineOutcome { let route = match transcription::resolve_transcription_route(&input.settings) { Ok(route) => route, Err(err) => return PipelineOutcome::failed_from(&TranscriptionError::Route(err)), @@ -230,6 +236,7 @@ pub async fn run(input: PipelineInput, cancel: CancellationToken) -> PipelineOut spoken_text: filtered.trim().to_string(), raw_text: raw, cleanup_failure: None, + cleanup_skipped: false, }); } // Check the dictionary alone as well as the full prompt: a script bias @@ -246,7 +253,25 @@ pub async fn run(input: PipelineInput, cancel: CancellationToken) -> PipelineOut if cancel.is_cancelled() { return PipelineOutcome::Cancelled; } - let (cleaned, cleanup_failure) = clean_up(&filtered, &input, &cancel).await; + let mut cleanup_skipped = false; + let (cleaned, cleanup_failure) = if cleanup_enabled(&input.settings) { + // A skip issued during ASR cannot arm a future cleanup stage. + let can_skip = !skip_cleanup.is_cancelled(); + let stage = CleanupStage::new(input.cleanup_stage.as_ref()); + let result = tokio::select! { + biased; + _ = cancel.cancelled() => return PipelineOutcome::Cancelled, + _ = skip_cleanup.cancelled(), if can_skip => { + cleanup_skipped = true; + (filtered.clone(), None) + } + result = clean_up(&filtered, &input, &cancel) => result, + }; + drop(stage); + result + } else { + (filtered.clone(), None) + }; if cancel.is_cancelled() { return PipelineOutcome::Cancelled; } @@ -263,9 +288,29 @@ pub async fn run(input: PipelineInput, cancel: CancellationToken) -> PipelineOut spoken_text, raw_text: raw, cleanup_failure, + cleanup_skipped, }) } +struct CleanupStage<'a>(Option<&'a async_channel::Sender>); + +impl<'a> CleanupStage<'a> { + fn new(sender: Option<&'a async_channel::Sender>) -> Self { + if let Some(sender) = sender { + let _ = sender.try_send(true); + } + Self(sender) + } +} + +impl Drop for CleanupStage<'_> { + fn drop(&mut self) { + if let Some(sender) = self.0 { + let _ = sender.try_send(false); + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -273,6 +318,10 @@ mod tests { use serde_json::json; use std::time::Duration; + async fn run(input: PipelineInput, cancel: CancellationToken) -> PipelineOutcome { + super::run(input, cancel, CancellationToken::new()).await + } + fn input(dictionary: &[&str]) -> PipelineInput { PipelineInput { audio: bytes::Bytes::new(), @@ -282,6 +331,7 @@ mod tests { known_corrections: Vec::new(), snippets: Vec::new(), cleanup_api_key: None, + cleanup_stage: None, } } @@ -592,6 +642,76 @@ mod tests { assert!(started.elapsed() < Duration::from_secs(2)); } + #[test] + fn skipping_cleanup_is_prompt_and_finishes_deterministic_steps_once() { + let asr = asr("Uh, 電腦 克劳德 sig"); + let llm = llm(completion("late reply").delayed(Duration::from_secs(3))); + let mut input = served(&asr, Some(&llm)); + input.settings.preferred_language = "zh-CN".into(); + input.settings.chinese_script_preference = + crate::settings::ChineseScriptPreference::Simplified; + input.known_corrections = corrections(); + input.snippets = vec![Snippet { + trigger: "sig".into(), + replacement: "uh saved text".into(), + }]; + let (tx, rx) = async_channel::unbounded(); + input.cleanup_stage = Some(tx); + let skip = CancellationToken::new(); + let started = std::time::Instant::now(); + let t = transcribed(run_test(async { + let run = super::run(input, CancellationToken::new(), skip.clone()); + let skipping = async { + assert_eq!(rx.recv().await, Ok(true)); + tokio::time::sleep(Duration::from_millis(30)).await; + skip.cancel(); + }; + let (outcome, ()) = tokio::join!(run, skipping); + outcome + })); + assert!(started.elapsed() < Duration::from_secs(1)); + assert_eq!(t.raw_text, "Uh, 電腦 克劳德 sig"); + assert!(t.text.contains("电脑"), "{}", t.text); + assert!(t.text.contains("Claude"), "{}", t.text); + assert!(t.text.ends_with("uh saved text"), "{}", t.text); + assert!(t.cleanup_skipped); + assert_eq!(t.cleanup_failure, None); + assert_eq!(rx.try_recv(), Ok(false)); + assert!(rx.try_recv().is_err()); + assert_eq!(llm.requests().len(), 1); + } + + #[test] + fn early_and_late_cleanup_skips_are_harmless() { + for early in [true, false] { + let asr = asr("hello there"); + let llm = llm(completion("Hello there.")); + let mut input = served(&asr, Some(&llm)); + let (tx, rx) = async_channel::unbounded(); + input.cleanup_stage = Some(tx); + let skip = CancellationToken::new(); + if early { + skip.cancel(); + } + let t = transcribed(run_test(super::run( + input, + CancellationToken::new(), + skip.clone(), + ))); + skip.cancel(); + assert_eq!(t.text, "Hello there."); + assert!(!t.cleanup_skipped); + assert_eq!(rx.try_recv(), Ok(true)); + assert_eq!(rx.try_recv(), Ok(false)); + } + let asr = asr("hello there"); + let mut input = served(&asr, None); + let (tx, rx) = async_channel::unbounded(); + input.cleanup_stage = Some(tx); + let _ = run_test(run(input, CancellationToken::new())); + assert!(rx.try_recv().is_err()); + } + /// Live check against real servers (see [`crate::services::live_tests`]): /// `cargo test -- --ignored pipeline`. #[test] diff --git a/src/services/performance_tests.rs b/src/services/performance_tests.rs index 8334589..6f6f848 100644 --- a/src/services/performance_tests.rs +++ b/src/services/performance_tests.rs @@ -55,9 +55,14 @@ fn performance_live_pipeline() { known_corrections: Vec::new(), snippets: Vec::new(), cleanup_api_key: None, + cleanup_stage: None, }; let pipeline_started = Instant::now(); - let outcome = super::runtime::block_on(pipeline::run(input, CancellationToken::new())); + let outcome = super::runtime::block_on(pipeline::run( + input, + CancellationToken::new(), + CancellationToken::new(), + )); let pipeline_ms = pipeline_started.elapsed().as_secs_f64() * 1000.0; let total_ms = started.elapsed().as_secs_f64() * 1000.0; let PipelineOutcome::Transcribed(transcript) = outcome else { diff --git a/src/ui/dev_scene.rs b/src/ui/dev_scene.rs index c9d45f5..66d302f 100644 --- a/src/ui/dev_scene.rs +++ b/src/ui/dev_scene.rs @@ -4,7 +4,7 @@ //! Values: `home`, `insights`, `upload`, `dictionary`, `search`, //! `settings[:general|language|hotkeys|speech|llms|privacy]`, and the pill states //! `pill:recording`, `pill:processing`, `pill:preparing`, `pill:transition`, -//! `pill:error`, `pill:fallback`. +//! `pill:cleanup`, `pill:cleanup-transition`, `pill:error`, `pill:fallback`. //! `WHISPER_DEV_SCROLL_FRACTION=0.5` or `1` captures the middle or bottom of Settings. use std::time::Duration; @@ -45,6 +45,8 @@ pub fn scene() -> Option { "preparing" => "preparing", "transition" => "transition", "processing" => "processing", + "cleanup" => "cleanup", + "cleanup-transition" => "cleanup-transition", "error" => "error", "fallback" => "fallback", _ => "recording", @@ -68,6 +70,24 @@ pub fn apply_pill(cx: &mut App) { return; }; match state { + "cleanup-transition" => { + AppState::global(cx).update(cx, |s, cx| { + s.dictation.is_processing = true; + cx.notify(); + }); + cx.spawn(async |cx| { + loop { + cx.background_executor().timer(Duration::from_secs(2)).await; + cx.update(|cx| { + AppState::global(cx).update(cx, |s, cx| { + s.dictation.is_cleaning = !s.dictation.is_cleaning; + cx.notify(); + }); + }); + } + }) + .detach(); + } "preparing" | "transition" => { AppState::global(cx).update(cx, |s, cx| { s.dictation.is_preparing = true; @@ -105,10 +125,11 @@ pub fn apply_pill(cx: &mut App) { .detach(); } } - "recording" | "processing" => { + "recording" | "processing" | "cleanup" => { AppState::global(cx).update(cx, |s, cx| { s.dictation.is_recording = state == "recording"; - s.dictation.is_processing = state == "processing"; + s.dictation.is_processing = state == "processing" || state == "cleanup"; + s.dictation.is_cleaning = state == "cleanup"; cx.notify(); }); } diff --git a/src/ui/pill.rs b/src/ui/pill.rs index dbe4c84..d2e700b 100644 --- a/src/ui/pill.rs +++ b/src/ui/pill.rs @@ -193,6 +193,7 @@ pub struct PillView { /// Whether the pointer rests on the cancel control, so the hint above /// the pill speaks for it instead of for the pill. cancel_hovered: bool, + skip_hovered: bool, } impl PillView { @@ -236,6 +237,7 @@ impl PillView { ready_at: None, recorded: Duration::ZERO, cancel_hovered: false, + skip_hovered: false, } } @@ -747,7 +749,7 @@ impl Render for PillView { // The hint sits above the pill, aligned with it; toasts take // that spot when present. let hint = (self.hovered && active && self.toasts.is_empty()) - .then(|| Self::render_hint(&snapshot, self.cancel_hovered, cx)) + .then(|| Self::render_hint(&snapshot, self.cancel_hovered, self.skip_hovered, cx)) .flatten(); if let Some(hint) = hint { let hint = diff --git a/src/ui/pill/waveform.rs b/src/ui/pill/waveform.rs index 2a8d787..ef77cc4 100644 --- a/src/ui/pill/waveform.rs +++ b/src/ui/pill/waveform.rs @@ -339,8 +339,44 @@ impl PillView { // Transcribing names itself beside the dots, so the state reads // without hovering. It keeps the timed recording pill's width, so // the capsule does not jump sideways when recording ends. - let think_label = t("dictation.pill.transcribing"); - let think_w = Self::processing_width(&think_label, timed_w, window); + let polishing = processing && snapshot.is_cleaning; + if !polishing { + self.skip_hovered = false; + } + let think_label = t(if polishing { + "dictation.pill.polishing" + } else { + "dictation.pill.transcribing" + }); + let skip_label = t("dictation.pill.skip"); + let skip_w = Self::label_width(&skip_label, window) + 8.; + let skip_t = f32_tx( + "skip-emerge", + if polishing { 1. } else { 0. }, + if reduce { + 0 + } else if polishing { + DUR_BASE + } else { + DUR_FAST + }, + ease_out, + window, + cx, + ); + let think_w = f32_tx( + "pill-processing-width", + Self::processing_width(&think_label, timed_w, window) + + if polishing { + THINK_LABEL_GAP + skip_w + } else { + 0. + }, + if reduce { 0 } else { DUR_BASE }, + ease_out, + window, + cx, + ); let think_alpha = thinking * motion_fx.bars; let think = (think_alpha > 0.001).then(|| { let mut dots = div().flex().items_center().gap(px(THINK_GAP)); @@ -367,6 +403,43 @@ impl PillView { .text_color(p.foreground.opacity(PILL_LABEL * think_alpha)) .child(think_label.clone()), ) + .when(polishing, |d| { + let scale = if reduce { 1. } else { 0.8 + 0.2 * skip_t }; + d.child( + div() + .id("pill-skip-cleanup") + .keyboard_focusable( + "pill-skip-cleanup", + Role::Button, + t("dictation.pill.skipCleanup"), + cx, + ) + .w(px(skip_w)) + .h(px(24.)) + .flex_none() + .flex() + .items_center() + .justify_center() + .rounded(px(RADIUS_MD)) + .text_size(px(TEXT_SMALL)) + .text_color(p.muted_foreground) + .hover(move |s| s.text_color(p.foreground)) + .opacity(skip_t) + .text_size(px(TEXT_SMALL * scale)) + .cursor_pointer() + .on_hover(cx.listener(|this, hovered: &bool, _, cx| { + this.skip_hovered = *hovered; + cx.notify(); + })) + .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) + .on_click(cx.listener(|this, _, _, cx| { + this.drag_origin = None; + cx.stop_propagation(); + Dictation::global(cx).update(cx, |d, cx| d.skip_cleanup(cx)); + })) + .child(skip_label.clone()), + ) + }) }); let check = motion_fx.check.map(|(scale, alpha)| { @@ -582,6 +655,7 @@ impl PillView { pub(super) fn render_hint( snapshot: &DictationSnapshot, on_cancel: bool, + on_skip: bool, cx: &App, ) -> Option
{ let p = hud(cx); @@ -605,6 +679,8 @@ impl PillView { if on_cancel && (snapshot.is_recording || snapshot.is_processing) { row.child(caption(t("dictation.pill.cancelDictation"), p.foreground)) .children(esc) + } else if on_skip && snapshot.is_cleaning { + row.child(caption(t("dictation.pill.skipHint"), p.foreground)) } else if snapshot.is_recording { row.child(caption(t("dictation.pill.finish"), p.foreground)) .child(caption("·".into(), p.subtle_foreground)) From 2747b4d84fbabacca4fbfd7408c5cb9c98df96f4 Mon Sep 17 00:00:00 2001 From: softmaxe <94467584+softmaxe@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:54:59 +0800 Subject: [PATCH 03/16] feat(audio): add private microphone recording and playback test --- README.md | 2 + README.zh-CN.md | 2 + docs/testing.md | 10 + locales/en.json | 15 ++ locales/zh-CN.json | 15 ++ src/audio/capture.rs | 7 + src/audio/cues.rs | 63 +---- src/audio/microphone_test.rs | 392 ++++++++++++++++++++++++++++ src/audio/mod.rs | 2 + src/audio/playback.rs | 167 ++++++++++++ src/audio/wav.rs | 1 - src/dictation.rs | 14 + src/ui/dev_scene.rs | 4 +- src/ui/pill.rs | 2 + src/ui/pill/waveform.rs | 15 ++ src/ui/settings_modal.rs | 51 ++++ src/ui/settings_modal/general.rs | 3 + src/ui/settings_modal/microphone.rs | 214 +++++++++++++++ 18 files changed, 920 insertions(+), 59 deletions(-) create mode 100644 src/audio/microphone_test.rs create mode 100644 src/audio/playback.rs diff --git a/README.md b/README.md index 77f65c0..c3b8c76 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ typing in. sets the period beside the one before it; highlights such as active days, busiest day and time spoken; and a Habits section with your streak calendar and weekly rhythm. Everything is computed on device. +- **Test your microphone.** Record up to ten seconds and play it back from + Settings > Dictation. The clip stays in memory and is never saved or sent. - **Fits into macOS.** Menu bar icon, standard app menu, start at login (macOS 13+), optional media pause while recording, English and Simplified Chinese UI, and light, dark, Tokyo Night or automatic themes. diff --git a/README.zh-CN.md b/README.zh-CN.md index 043bd9f..ee6f7be 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -39,6 +39,8 @@ - **统计。** 最近 7 天、30 天或全部时间的字数、听写次数和每分钟字数,每项都与 上一周期对比;趋势图把当前周期与上一周期并排显示;亮点包括活跃天数、最忙的一天 和说话时长;“习惯”部分展示连续天数日历和每周节奏。全部在本机计算。 +- **测试麦克风。** 在“设置 > 听写”录制最长十秒的声音并试听,音频只保留在内存中, + 不会保存、转写或发送到服务器。 - **融入 macOS。** 菜单栏图标、标准应用菜单、开机启动(macOS 13+)、可选在录音时暂停媒体播放、 英文和简体中文界面,以及浅色、深色、Tokyo Night 或自动主题。 - **键盘与旁白。** 所有控件都能用 Tab 访问并带有旁白(VoiceOver)名称,列表和图表 diff --git a/docs/testing.md b/docs/testing.md index 56f5b25..c142507 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -141,3 +141,13 @@ Do not reset TCC permissions, change system trust, or delete Keychain items duri - Click Skip and verify immediate insertion of the recognized text with filler removal, Chinese script conversion, known corrections, and snippets. There must be no cleanup failure notice and no second paste when the delayed reply arrives. Cancel must still discard the whole operation. - Tab to Skip and activate it with Return or Space. Check VoiceOver's “Skip cleanup” label and the “Insert without cleanup” hover hint. Check smooth width changes and emergence, then repeat with Reduce motion for immediate changes. - Use `WHISPER_DEV_SCENE=pill:cleanup` with isolated data to inspect Light, Dark and Tokyo Night, and both interface languages. + +## Microphone loopback regression check + +- In Settings > Dictation, check the microphone control order: Test, Refresh, device picker. Test must use the same resolved device as the next dictation, including Auto, lid changes, unavailable saved devices, and the system-default fallback. +- Test records for at most ten seconds with a live level meter, countdown, and Stop. Stop early, play, stop playback, replay, and Record again. The progress bar and duration should reflect the memory clip. A silent clip remains playable and shows a warning. +- Deny microphone permission, disconnect the selected device, and test a device delivering no frames. Check the named error and Try again. Disconnect output during playback and check its error as well. +- Start dictation with the hotkey during recording and playback. The test must close and yield audio before dictation begins. While dictation records, prepares, or processes, Test is disabled with an explanation. No test may play dictation cues or pause media. +- Close Settings, change section, change device, and drop/reopen the sheet during microphone startup, recording and playback. Each must release the streams and clip, and no old result may reopen the strip. No test audio may appear in History, recovery, storage or requests. +- Check Tab, Return/Space, VoiceOver names, Light/Dark/Tokyo Night, both languages, and Reduce motion. Use `settings:mic-recording`, `settings:mic-ready` and `settings:mic-error` dev scenes with isolated data for visual checks. +- `cargo test -- --ignored live_microphone_loopback` checks real capture and output with microphone authorization; ordinary lifecycle tests use an injected clock and hardware-free audio. diff --git a/locales/en.json b/locales/en.json index f5256f5..639ecd5 100644 --- a/locales/en.json +++ b/locales/en.json @@ -763,5 +763,20 @@ "copy": "Copy Text", "copiedHint": "Copied. Press ⌘V to paste." } + }, + "microphoneTest": { + "test": "Test", + "stop": "Stop", + "play": "Play", + "recordAgain": "Record again", + "close": "Close microphone test", + "tryAgain": "Try again", + "privacy": "Stays on this Mac. Never transcribed or saved.", + "silent": "No sound was picked up. Check the selected microphone.", + "permission": "Microphone permission is missing.", + "device": "The microphone is unavailable or disconnected.", + "noSignal": "The microphone did not deliver an input signal.", + "output": "Audio output is unavailable.", + "busy": "Finish or cancel dictation before testing the microphone." } } diff --git a/locales/zh-CN.json b/locales/zh-CN.json index 3148d0b..3b346b0 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -741,5 +741,20 @@ "copy": "复制文字", "copiedHint": "已复制,按 ⌘V 粘贴。" } + }, + "microphoneTest": { + "test": "测试", + "stop": "停止", + "play": "播放", + "recordAgain": "重新录音", + "close": "关闭麦克风测试", + "tryAgain": "重试", + "privacy": "仅保留在这台 Mac 上,不会被转写或保存。", + "silent": "没有录到声音,请检查所选麦克风。", + "permission": "尚未获得麦克风权限。", + "device": "麦克风不可用或已断开连接。", + "noSignal": "麦克风没有传入音频信号。", + "output": "音频输出设备不可用。", + "busy": "请先结束或取消听写,再测试麦克风。" } } diff --git a/src/audio/capture.rs b/src/audio/capture.rs index f3027ba..19c9bd5 100644 --- a/src/audio/capture.rs +++ b/src/audio/capture.rs @@ -37,6 +37,9 @@ pub const PRE_ROLL_MAX_AGE: Duration = Duration::from_millis(2_000); pub const FIRST_AUDIO_TIMEOUT: Duration = Duration::from_millis(10_000); /// No frames for this long during a recording means the device is gone. pub const STALL_TIMEOUT: Duration = Duration::from_millis(1_500); +// The lease includes stream teardown, so a test can yield capture to dictation +// without overlapping inputs or feeding its playback into a new recording. +pub(crate) static AUDIO_INPUT_LEASE: Mutex<()> = Mutex::new(()); const TICK: Duration = Duration::from_millis(50); const LEVEL_WINDOW: usize = ANALYSER_WINDOW; @@ -491,6 +494,10 @@ fn capture_thread( commands: mpsc::Receiver, events: Option, ) { + let _lease = AUDIO_INPUT_LEASE.lock(); + if core.state.lock().phase == Phase::Finished { + return; + } let stream = match open_stream(selector.as_deref(), &core) { Ok(stream) => stream, Err(err) => { diff --git a/src/audio/cues.rs b/src/audio/cues.rs index feb9333..6749840 100644 --- a/src/audio/cues.rs +++ b/src/audio/cues.rs @@ -11,9 +11,6 @@ use std::f64::consts::PI; use std::time::Duration; -use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use cpal::{FromSample, SampleFormat, SizedSample}; - use super::AudioError; pub const START_NOTES: [f64; 2] = [523.25, 659.25]; @@ -116,60 +113,12 @@ pub fn play(cue: Cue) { /// Plays a cue and returns once it has finished. pub fn play_blocking(cue: Cue) -> Result<(), AudioError> { - let host = cpal::default_host(); - let device = host - .default_output_device() - .ok_or_else(|| AudioError::Output("no output device".into()))?; - let supported = device - .default_output_config() - .map_err(|e| AudioError::Output(e.to_string()))?; - let config = supported.config(); - let samples = render_cue(cue.notes(), config.sample_rate); - let stream = match supported.sample_format() { - SampleFormat::F32 => build::(&device, config, samples), - SampleFormat::F64 => build::(&device, config, samples), - SampleFormat::I16 => build::(&device, config, samples), - SampleFormat::I32 => build::(&device, config, samples), - SampleFormat::U16 => build::(&device, config, samples), - SampleFormat::U8 => build::(&device, config, samples), - other => Err(AudioError::Output(format!( - "unsupported sample format {other:?}" - ))), - }?; - stream - .play() - .map_err(|e| AudioError::Output(e.to_string()))?; - std::thread::sleep(cue_duration(cue.notes()) + Duration::from_millis(80)); - drop(stream); - Ok(()) -} - -fn build( - device: &cpal::Device, - config: cpal::StreamConfig, - samples: Vec, -) -> Result -where - T: SizedSample + FromSample, -{ - let channels = usize::from(config.channels.max(1)); - let mut position = 0usize; - device - .build_output_stream::( - config, - move |out: &mut [T], _| { - for frame in out.chunks_mut(channels) { - let value = samples.get(position).copied().unwrap_or(0.0); - position += 1; - for slot in frame { - *slot = >::from_sample_(value); - } - } - }, - |err| log::debug!("cue output error: {err}"), - None, - ) - .map_err(|e| AudioError::Output(e.to_string())) + super::playback::Playback::start( + std::sync::Arc::new(render_cue(cue.notes(), 48_000)), + 48_000, + false, + )? + .wait() } #[cfg(test)] diff --git a/src/audio/microphone_test.rs b/src/audio/microphone_test.rs new file mode 100644 index 0000000..9ffc7e9 --- /dev/null +++ b/src/audio/microphone_test.rs @@ -0,0 +1,392 @@ +//! Memory-only loopback lifecycle. The clock is supplied by the caller so the +//! ten-second limit and cancellation can be exercised without audio hardware. + +use std::sync::{Arc, Weak}; +use std::time::{Duration, Instant}; + +use futures::future::BoxFuture; +use parking_lot::Mutex; + +use super::AudioError; +use super::capture::{CaptureEvent, RecordedAudio, Recorder}; +#[cfg(any(debug_assertions, test))] +use super::gate::SpeechGate; +use super::gate::{ANALYSER_WINDOW, window_rms}; +use super::playback::Playback; + +pub const RECORDING_LIMIT: Duration = Duration::from_secs(10); + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Problem { + Permission, + Device, + NoSignal, + Output, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum State { + Closed, + Recording, + Ready, + Playing, + Error(Problem), +} + +struct Clip { + samples: Arc>, + rate: u32, + duration: Duration, + silent: bool, +} + +pub struct MicrophoneTest { + state: State, + recorder: Recorder, + events: async_channel::Receiver, + clip: Option, + playback: Option, + started: Instant, + generation: u64, + progress: f32, +} + +impl Default for MicrophoneTest { + fn default() -> Self { + let (tx, events) = async_channel::unbounded(); + Self { + state: State::Closed, + recorder: Recorder::new(Some(Arc::new(move |event| { + let _ = tx.try_send(event); + }))), + events, + clip: None, + playback: None, + started: Instant::now(), + generation: 0, + progress: 0., + } + } +} + +impl MicrophoneTest { + pub fn state(&self) -> State { + self.state + } + pub fn generation(&self) -> u64 { + self.generation + } + pub fn level(&self) -> f32 { + self.recorder.level().unwrap_or(0.) + } + pub fn duration(&self) -> Duration { + self.clip + .as_ref() + .map_or(Duration::ZERO, |clip| clip.duration) + } + pub fn silent(&self) -> bool { + self.clip.as_ref().is_some_and(|clip| clip.silent) + } + pub fn progress(&self) -> f32 { + self.playback + .as_ref() + .map_or(self.progress, Playback::progress) + } + pub fn remaining(&self, now: Instant) -> Duration { + RECORDING_LIMIT.saturating_sub(now.saturating_duration_since(self.started)) + } + + pub fn start( + &mut self, + selector: Option<&str>, + now: Instant, + ) -> (u64, BoxFuture<'static, Result<(), AudioError>>) { + self.begin(now); + (self.generation, self.recorder.start(selector)) + } + + fn begin(&mut self, now: Instant) { + self.close(); + self.started = now; + self.state = State::Recording; + } + + pub fn capture_started( + &mut self, + generation: u64, + result: Result<(), AudioError>, + now: Instant, + ) { + if generation != self.generation || self.state != State::Recording { + return; + } + match result { + Ok(()) => self.started = now, + Err(error) => self.fail(&error), + } + } + + pub fn stop(&mut self) { + if self.state != State::Recording { + return; + } + match self.recorder.stop() { + Some(audio) => self.finish_audio(audio), + None => self.fail(&AudioError::FirstAudioUnavailable), + } + } + + fn finish_audio(&mut self, mut audio: RecordedAudio) { + if !audio.received_audio_data || audio.samples.is_empty() || audio.sample_rate == 0 { + self.fail(&AudioError::FirstAudioUnavailable); + return; + } + // A short clip can finish before capture has measured its first gate. + if audio.speech_gate.window_count == 0 { + for window in audio.samples.chunks(ANALYSER_WINDOW) { + audio + .speech_gate + .record_window(window_rms(window, ANALYSER_WINDOW)); + } + } + let duration = + Duration::from_secs_f64(audio.samples.len() as f64 / f64::from(audio.sample_rate)); + self.clip = Some(Clip { + samples: Arc::new(audio.samples), + rate: audio.sample_rate, + duration, + silent: audio.speech_gate.is_silence(), + }); + self.state = State::Ready; + self.progress = 0.; + } + + pub fn toggle_playback(&mut self) { + self.toggle_playback_with(|clip| { + Playback::start(clip.samples.clone(), clip.rate, true).map(Some) + }); + } + + fn toggle_playback_with( + &mut self, + start: impl FnOnce(&Clip) -> Result, AudioError>, + ) { + if self.state == State::Playing { + self.playback = None; + self.state = State::Ready; + return; + } + if self.state != State::Ready { + return; + } + let Some(clip) = &self.clip else { + return; + }; + match start(clip) { + Ok(playback) => { + self.playback = playback; + self.state = State::Playing; + self.progress = 0.; + } + Err(error) => self.fail(&error), + } + } + + pub fn tick(&mut self, now: Instant) { + while let Ok(CaptureEvent::DeviceLost { capture_id, .. }) = self.events.try_recv() { + if self.state == State::Recording + && self.recorder.active_capture_id() == Some(capture_id) + { + self.fail(&AudioError::DeviceLost(String::new())); + } + } + if self.state == State::Recording && self.remaining(now).is_zero() { + self.stop(); + } + if self.state == State::Playing && self.playback.as_ref().is_some_and(Playback::is_done) { + let error = self.playback.as_ref().and_then(Playback::error); + self.playback = None; + self.playback_finished(error); + } + } + + fn playback_finished(&mut self, error: Option) { + if let Some(error) = error { + self.fail(&error); + } else { + self.state = State::Ready; + self.progress = 1.; + } + } + + fn fail(&mut self, error: &AudioError) { + self.recorder.cancel(); + self.playback = None; + self.state = State::Error(match error { + AudioError::PermissionDenied => Problem::Permission, + AudioError::FirstAudioUnavailable => Problem::NoSignal, + AudioError::Output(_) => Problem::Output, + _ => Problem::Device, + }); + } + + pub fn close(&mut self) { + self.generation = self.generation.wrapping_add(1); + self.recorder.cancel(); + self.playback = None; + self.clip = None; + self.state = State::Closed; + self.progress = 0.; + } + + #[cfg(debug_assertions)] + pub fn dev_scene(&mut self, scene: &str) { + self.begin(Instant::now() - Duration::from_secs(3)); + if scene == "ready" { + self.finish_audio(RecordedAudio { + samples: vec![0.1; 48_000], + sample_rate: 16_000, + received_audio_data: true, + speech_gate: SpeechGate { + peak_rms: 0.1, + window_count: 30, + }, + }); + } else if scene == "error" { + self.fail(&AudioError::PermissionDenied); + } + } +} + +impl Drop for MicrophoneTest { + fn drop(&mut self) { + self.close(); + } +} + +/// Called before dictation opens or adopts a microphone, including prewarming. +pub fn cancel_for_dictation(test: &Weak>) { + if let Some(test) = test.upgrade() { + test.lock().close(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn recording(now: Instant, amplitude: f32) -> MicrophoneTest { + let mut test = MicrophoneTest::default(); + test.begin(now); + test.recorder = Recorder::with_test_audio(vec![amplitude; 16_000]); + test + } + + #[test] + fn injected_clock_auto_stops_at_ten_seconds_and_manual_stop_is_ready() { + let now = Instant::now(); + let mut test = recording(now, 0.1); + test.tick(now + Duration::from_millis(9999)); + assert_eq!(test.state(), State::Recording); + test.tick(now + RECORDING_LIMIT); + assert_eq!(test.state(), State::Ready); + assert_eq!(test.duration(), Duration::from_secs(1)); + assert!(!test.silent()); + let mut test = recording(now, 0.1); + test.stop(); + assert_eq!(test.state(), State::Ready); + test.begin(now); + assert_eq!(test.state(), State::Recording); + assert!(test.clip.is_none()); + } + + #[test] + fn silent_clips_still_allow_playback_and_replay() { + let mut test = recording(Instant::now(), 0.); + test.stop(); + assert!(test.silent()); + test.toggle_playback_with(|_| Ok(None)); + assert_eq!(test.state(), State::Playing); + test.playback_finished(None); + assert_eq!(test.state(), State::Ready); + assert_eq!(test.progress(), 1.); + test.toggle_playback_with(|_| Ok(None)); + test.toggle_playback(); + assert_eq!(test.state(), State::Ready); + assert!(test.clip.is_some()); + test.toggle_playback_with(|_| Err(AudioError::Output("disconnected".into()))); + assert_eq!(test.state(), State::Error(Problem::Output)); + test.close(); + assert_eq!(test.state(), State::Closed); + assert!(test.clip.is_none()); + } + + #[test] + fn errors_are_named_and_late_capture_results_cannot_reopen_a_test() { + for (error, problem) in [ + (AudioError::PermissionDenied, Problem::Permission), + ( + AudioError::DeviceLost("disconnected".into()), + Problem::Device, + ), + (AudioError::FirstAudioUnavailable, Problem::NoSignal), + (AudioError::Output("unavailable".into()), Problem::Output), + ] { + let now = Instant::now(); + let mut test = recording(now, 0.1); + let generation = test.generation(); + test.capture_started(generation, Err(error), now); + assert_eq!(test.state(), State::Error(problem)); + assert!(!test.recorder.is_capture_active()); + test.begin(now); + let generation = test.generation(); + test.close(); + test.capture_started(generation, Ok(()), now); + assert_eq!(test.state(), State::Closed); + } + let mut test = recording(Instant::now(), 0.1); + test.finish_audio(RecordedAudio { + samples: Vec::new(), + sample_rate: 16_000, + received_audio_data: false, + speech_gate: SpeechGate::default(), + }); + assert_eq!(test.state(), State::Error(Problem::NoSignal)); + } + + #[test] + fn dictation_and_drop_cancel_capture_and_free_the_memory_clip() { + let now = Instant::now(); + let test = Arc::new(Mutex::new(recording(now, 0.1))); + let recorder = test.lock().recorder.clone(); + cancel_for_dictation(&Arc::downgrade(&test)); + assert!(!recorder.is_capture_active()); + assert_eq!(test.lock().state(), State::Closed); + let mut test = recording(now, 0.1); + let recorder = test.recorder.clone(); + test.stop(); + let clip = Arc::downgrade(&test.clip.as_ref().unwrap().samples); + drop(test); + assert!(!recorder.is_capture_active()); + assert!(clip.upgrade().is_none()); + } + + #[test] + #[ignore] + fn live_microphone_loopback() { + let mut test = MicrophoneTest::default(); + let (generation, start) = test.start(None, Instant::now()); + let result = futures::executor::block_on(start); + test.capture_started(generation, result, Instant::now()); + assert_eq!(test.state(), State::Recording); + std::thread::sleep(Duration::from_secs(2)); + test.stop(); + assert_eq!(test.state(), State::Ready); + test.toggle_playback(); + while test.state() == State::Playing { + test.tick(Instant::now()); + std::thread::sleep(Duration::from_millis(20)); + } + assert_eq!(test.state(), State::Ready); + } +} diff --git a/src/audio/mod.rs b/src/audio/mod.rs index 28dbb25..9459b7c 100644 --- a/src/audio/mod.rs +++ b/src/audio/mod.rs @@ -7,6 +7,8 @@ pub mod capture; pub mod cues; pub mod devices; pub mod gate; +pub mod microphone_test; +pub mod playback; pub mod validation; pub mod wav; diff --git a/src/audio/playback.rs b/src/audio/playback.rs new file mode 100644 index 0000000..a937203 --- /dev/null +++ b/src/audio/playback.rs @@ -0,0 +1,167 @@ +//! Shared in-process output for cues and memory-only microphone tests. + +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::{Arc, mpsc}; +use std::time::Duration; + +use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use cpal::{FromSample, SampleFormat, SizedSample}; +use parking_lot::Mutex; + +use super::{AudioError, wav::resample}; + +#[derive(Default)] +struct Status { + position: AtomicUsize, + length: AtomicUsize, + done: AtomicBool, + error: Mutex>, +} + +pub struct Playback { + cancel: mpsc::Sender<()>, + status: Arc, + thread: Option>, +} + +impl Playback { + pub fn start(samples: Arc>, rate: u32, exclusive: bool) -> Result { + let (tx, rx) = mpsc::channel(); + let status = Arc::new(Status::default()); + let progress = status.clone(); + let thread = std::thread::Builder::new() + .name("whisper-playback".into()) + .spawn(move || { + // Test playback and input share a lease. Cancellation can be + // requested while a previous stream is still closing. + let _lease = exclusive.then(|| super::capture::AUDIO_INPUT_LEASE.lock()); + if rx.try_recv().is_ok() { + progress.done.store(true, Ordering::Release); + return; + } + if let Err(error) = play(&samples, rate, &rx, progress.clone()) { + *progress.error.lock() = Some(error); + } + progress.done.store(true, Ordering::Release); + }) + .map_err(|e| AudioError::Output(e.to_string()))?; + Ok(Self { + cancel: tx, + status, + thread: Some(thread), + }) + } + + pub fn progress(&self) -> f32 { + let length = self.status.length.load(Ordering::Relaxed); + if length == 0 { + return 0.; + } + (self.status.position.load(Ordering::Relaxed) as f32 / length as f32).min(1.) + } + + pub fn is_done(&self) -> bool { + self.status.done.load(Ordering::Acquire) + } + + pub fn error(&self) -> Option { + self.status.error.lock().clone() + } + + pub fn wait(mut self) -> Result<(), AudioError> { + if let Some(thread) = self.thread.take() { + thread + .join() + .map_err(|_| AudioError::Output("output worker failed".into()))?; + } + self.error().map_or(Ok(()), Err) + } +} + +impl Drop for Playback { + fn drop(&mut self) { + let _ = self.cancel.send(()); + } +} + +fn play( + samples: &[f32], + rate: u32, + cancel: &mpsc::Receiver<()>, + status: Arc, +) -> Result<(), AudioError> { + let device = cpal::default_host() + .default_output_device() + .ok_or_else(|| AudioError::Output("no output device".into()))?; + let supported = device + .default_output_config() + .map_err(|e| AudioError::Output(e.to_string()))?; + let config = supported.config(); + let samples = resample(samples, rate, config.sample_rate); + status.length.store(samples.len(), Ordering::Relaxed); + let stream = match supported.sample_format() { + SampleFormat::F32 => build::(&device, config, samples, status.clone()), + SampleFormat::F64 => build::(&device, config, samples, status.clone()), + SampleFormat::I16 => build::(&device, config, samples, status.clone()), + SampleFormat::I32 => build::(&device, config, samples, status.clone()), + SampleFormat::U16 => build::(&device, config, samples, status.clone()), + SampleFormat::U8 => build::(&device, config, samples, status.clone()), + other => Err(AudioError::Output(format!( + "unsupported sample format {other:?}" + ))), + }?; + stream + .play() + .map_err(|e| AudioError::Output(e.to_string()))?; + loop { + match cancel.recv_timeout(Duration::from_millis(10)) { + Ok(()) | Err(mpsc::RecvTimeoutError::Disconnected) => break, + Err(mpsc::RecvTimeoutError::Timeout) => {} + } + if status.error.lock().is_some() { + break; + } + if status.position.load(Ordering::Relaxed) >= status.length.load(Ordering::Relaxed) { + // The callback has filled the last output buffer; let it drain. + let _ = cancel.recv_timeout(Duration::from_millis(80)); + break; + } + } + drop(stream); + Ok(()) +} + +fn build( + device: &cpal::Device, + config: cpal::StreamConfig, + samples: Vec, + status: Arc, +) -> Result +where + T: SizedSample + FromSample, +{ + let channels = usize::from(config.channels.max(1)); + let mut position = 0; + let progress = status.clone(); + device + .build_output_stream::( + config, + move |out, _| { + for frame in out.chunks_mut(channels) { + let value = samples.get(position).copied().unwrap_or(0.); + position += 1; + for slot in frame { + *slot = T::from_sample_(value); + } + } + progress + .position + .store(position.min(samples.len()), Ordering::Relaxed); + }, + move |error| { + *status.error.lock() = Some(AudioError::Output(error.to_string())); + }, + None, + ) + .map_err(|e| AudioError::Output(e.to_string())) +} diff --git a/src/audio/wav.rs b/src/audio/wav.rs index cfdd540..080d447 100644 --- a/src/audio/wav.rs +++ b/src/audio/wav.rs @@ -202,7 +202,6 @@ fn resampled_len(input_len: usize, from_rate: u32, to_rate: u32) -> usize { /// Windowed-sinc (Kaiser) resampler. Downsampling low-passes below the /// target Nyquist so nothing aliases into the speech band. -#[cfg(test)] pub fn resample(input: &[f32], from_rate: u32, to_rate: u32) -> Vec { if from_rate == to_rate || input.is_empty() || from_rate == 0 || to_rate == 0 { return input.to_vec(); diff --git a/src/dictation.rs b/src/dictation.rs index da33aee..1127ece 100644 --- a/src/dictation.rs +++ b/src/dictation.rs @@ -166,6 +166,7 @@ pub struct Dictation { generation: u64, start_lock: bool, cancel: Option, + mic_test: std::sync::Weak>, skip_cleanup: Option, is_cleaning: bool, target_pid: Option, @@ -214,6 +215,7 @@ impl Dictation { generation: 0, start_lock: false, cancel: None, + mic_test: Default::default(), skip_cleanup: None, is_cleaning: false, target_pid: None, @@ -234,6 +236,14 @@ impl Dictation { cx.global::().0.clone() } + pub fn attach_microphone_test( + &mut self, + test: &Arc>, + ) { + crate::audio::microphone_test::cancel_for_dictation(&self.mic_test); + self.mic_test = Arc::downgrade(test); + } + pub fn status(&self) -> StatusMirror { self.status.clone() } @@ -318,6 +328,7 @@ impl Dictation { if self.start_lock || self.phase != Phase::Idle { return; } + crate::audio::microphone_test::cancel_for_dictation(&self.mic_test); // A new Dictation replaces the one that failed. self.failed = None; let device = backend::preferred_input_device(&AppState::settings(cx)); @@ -358,6 +369,7 @@ impl Dictation { if self.start_lock || matches!(self.phase, Phase::Recording | Phase::Processing) { return; } + crate::audio::microphone_test::cancel_for_dictation(&self.mic_test); self.start_lock = true; self.push_force_stopped = false; self.failed = None; @@ -1098,6 +1110,7 @@ mod tests { generation: 7, start_lock: false, cancel: None, + mic_test: Default::default(), skip_cleanup: None, is_cleaning: false, target_pid: None, @@ -1307,6 +1320,7 @@ mod tests { generation: 7, start_lock: false, cancel: Some(cancel.clone()), + mic_test: Default::default(), skip_cleanup: None, is_cleaning: false, target_pid: None, diff --git a/src/ui/dev_scene.rs b/src/ui/dev_scene.rs index 66d302f..2b2fa51 100644 --- a/src/ui/dev_scene.rs +++ b/src/ui/dev_scene.rs @@ -35,7 +35,9 @@ pub fn scene() -> Option { "dictionary" => Scene::View(PanelView::Dictionary), "search" => Scene::Search, "settings" => Scene::Settings(match arg { - "hotkeys" => SettingsSection::Hotkeys, + "hotkeys" | "mic-recording" | "mic-ready" | "mic-error" | "mic-transition" => { + SettingsSection::Hotkeys + } "speech" => SettingsSection::SpeechToText, "llms" => SettingsSection::Llms, "privacy" => SettingsSection::PrivacyData, diff --git a/src/ui/pill.rs b/src/ui/pill.rs index d2e700b..3a2cee1 100644 --- a/src/ui/pill.rs +++ b/src/ui/pill.rs @@ -20,6 +20,8 @@ mod waveform; use state::*; use waveform::*; +pub(crate) use waveform::microphone_level_meter; + use std::cell::RefCell; use std::f32::consts::{FRAC_PI_2, TAU}; use std::rc::Rc; diff --git a/src/ui/pill/waveform.rs b/src/ui/pill/waveform.rs index ef77cc4..be20a81 100644 --- a/src/ui/pill/waveform.rs +++ b/src/ui/pill/waveform.rs @@ -57,6 +57,21 @@ fn live_target(rms: f32, index: usize, now_ms: f32, reduce: bool) -> f32 { speech + (1. - level) * BREATH * ENVELOPE[index] * swell } +pub(crate) fn microphone_level_meter(rms: f32, reduce: bool, palette: &Palette) -> Div { + div() + .h(px(24.)) + .flex() + .items_center() + .gap(px(BAR_GAP)) + .children((0..BARS).map(|i| { + div() + .w(px(BAR_W)) + .h(px(BAR_MIN + live_target(rms, i, 0., reduce) * 12.)) + .rounded_full() + .bg(palette.voice) + })) +} + /// A zero-velocity handoff at both ends keeps the dots and bars continuous. fn prepare_ease(t: f32) -> f32 { let t = t.clamp(0., 1.); diff --git a/src/ui/settings_modal.rs b/src/ui/settings_modal.rs index c25080f..353f855 100644 --- a/src/ui/settings_modal.rs +++ b/src/ui/settings_modal.rs @@ -186,6 +186,8 @@ pub struct SettingsModal { audio_retention: Entity, microphone: Entity