//! Plain egui settings pages: speech, microphone, voice model, voice effects, music listening, call audio. use capri_core::{Section, SettingsStore}; use crate::install::Phase; use crate::settings::{CallSettings, MusicSettings, SPEECH_LANGUAGES, SpeechSettings, VoiceEffects, VoiceSettings}; use crate::{VoiceHandle, effects}; use capri_core::i18n::t; // TODO: saves on every slider step; debounce if disk writes start to matter. /// Saves a section when the page changed it. fn edit(store: &SettingsStore, ui: &mut egui::Ui, f: impl FnOnce(&mut egui::Ui, &mut T)) { let before = store.section::(); let mut value = before.clone(); f(ui, &mut value); if value != before && let Err(e) = store.set_section(&value) { report(ui, Err(e)); } } fn error_id() -> egui::Id { egui::Id::unique("capri-voice-error") } fn report(ui: &mut egui::Ui, result: anyhow::Result<()>) { match result { Ok(()) => ui.data_mut(|d| { d.remove::(error_id()); }), Err(e) => ui.data_mut(|d| { d.insert_temp(error_id(), e.to_string()); }), } } fn language_combo(ui: &mut egui::Ui, label: &str, value: &mut String, extra: &[&str], languages: &[&str]) { egui::ComboBox::from_label(label).selected_text(value.as_str()).show_ui(ui, |ui| { for l in extra.iter().chain(languages) { ui.selectable_value(value, l.to_string(), *l); } }); } /// Every voice page in one scrollable column. pub fn settings_ui(ui: &mut egui::Ui, store: &SettingsStore, voice: &VoiceHandle) { if let Some(error) = ui.data(|d| d.get_temp::(error_id())) { ui.colored_label(ui.visuals().error_fg_color, error); } install_status(ui, voice); egui::CollapsingHeader::new(t("Speech")).default_open(true).show(ui, |ui| speech(ui, store, voice)); egui::CollapsingHeader::new(t("Microphone")).default_open(true).show(ui, |ui| microphone(ui, store, voice)); egui::CollapsingHeader::new(t("Voice model")).show(ui, |ui| voice_model(ui, store, voice)); egui::CollapsingHeader::new(t("Voice effects")).show(ui, |ui| voice_effects(ui, store, voice)); egui::CollapsingHeader::new(t("Music listening")).show(ui, |ui| music(ui, store, voice)); egui::CollapsingHeader::new(t("Call audio")).show(ui, |ui| call(ui, store, voice)); } fn install_status(ui: &mut egui::Ui, voice: &VoiceHandle) { let status = voice.install_status(); match status.phase { Phase::Installing => { ui.horizontal(|ui| { ui.label(format!("Installing: {} {}", status.stage, status.detail)); if ui.button(t("Cancel")).clicked() { voice.cancel_install(); } }); ui.add(egui::ProgressBar::new(status.progress.unwrap_or(0.0)).show_percentage()); } Phase::Error | Phase::Cancelled => { ui.colored_label(ui.visuals().warn_fg_color, status.error.unwrap_or_default()); } Phase::Ready => { ui.label(t("Speech components are installed.")); } Phase::Idle => {} } } fn speech(ui: &mut egui::Ui, store: &SettingsStore, voice: &VoiceHandle) { edit::(store, ui, |ui, s| { language_combo(ui, "Recognition language", &mut s.recognition_language, &["follow", "auto"], SPEECH_LANGUAGES); egui::ComboBox::from_label(t("Recognition model")).selected_text(s.asr_model.as_str()).show_ui(ui, |ui| { for (id, label, ..) in crate::asr::MODELS { ui.selectable_value(&mut s.asr_model, id.to_string(), *label); } }); ui.add(egui::Slider::new(&mut s.trailing_silence_seconds, 0.5..=5.0).step_by(0.1).suffix(" s").text(t("End-of-speech pause"))); if ui.button(t("Reset pause")).clicked() { s.trailing_silence_seconds = 1.0; } ui.checkbox(&mut s.wake_word, t("Wake phrase: Capri / Hey, Capri")); ui.checkbox(&mut s.auto_send, t("Send recognized speech automatically")); ui.checkbox(&mut s.barge_in, t("Interrupt Capri by speaking")); ui.checkbox(&mut s.follow_up, t("Keep listening after Capri asks a question")); ui.add_enabled(s.follow_up, egui::Slider::new(&mut s.follow_up_seconds, 5..=30).suffix(" s").text(t("Follow-up window"))); egui::ComboBox::from_label(t("Wake sound")).selected_text(s.wake_sound.as_str()).show_ui(ui, |ui| { ui.selectable_value(&mut s.wake_sound, "tone".to_string(), "Tone"); ui.selectable_value(&mut s.wake_sound, "off".to_string(), "Off"); }); ui.add(egui::Slider::new(&mut s.wake_volume, 0.0..=1.0).text(t("Wake volume"))); ui.checkbox(&mut s.asr_gpu, t("Use the GPU for recognition (needs a CUDA build)")); ui.add(egui::Slider::new(&mut s.cpu_threads, 1..=8).text(t("CPU threads"))); }); let installed = voice.recognition_installed(); ui.label(if installed { "Recognition model installed." } else { "Recognition model not installed." }); if !installed && ui.button(t("Install speech recognition")).clicked() { report(ui, voice.install_speech(true, false)); } } fn microphone(ui: &mut egui::Ui, store: &SettingsStore, voice: &VoiceHandle) { let mut on = voice.microphone_on(); if ui.checkbox(&mut on, t("Microphone (off at every launch)")).changed() { report(ui, voice.set_microphone(on)); } ui.add(egui::ProgressBar::new(voice.microphone_level()).desired_width(160.0).text(t("Level"))); if ui.button(t("Refresh devices")).clicked() { voice.refresh_devices(); } let devices = voice.input_devices(); edit::(store, ui, |ui, s| { let selected = if s.microphone_device.is_empty() { "System default".to_string() } else if devices.iter().any(|d| d.id == s.microphone_device) { s.microphone_label.clone() } else { format!("Unavailable — {}", s.microphone_label) }; egui::ComboBox::from_label(t("Input device")).selected_text(selected).show_ui(ui, |ui| { if ui.selectable_label(s.microphone_device.is_empty(), t("System default")).clicked() { (s.microphone_device, s.microphone_label) = (String::new(), String::new()); } for d in &devices { if ui.selectable_label(s.microphone_device == d.id, &d.label).clicked() { (s.microphone_device, s.microphone_label) = (d.id.clone(), d.label.clone()); } } }); }); ui.label(t("Changing the device or pause applies the next time the microphone starts.")); } fn voice_model(ui: &mut egui::Ui, store: &SettingsStore, voice: &VoiceHandle) { let mut on = voice.voice_enabled(); if ui.checkbox(&mut on, t("Speak replies (off at every launch)")).changed() { report(ui, voice.set_voice_enabled(on)); } edit::(store, ui, |ui, v| { language_combo(ui, "Voice language", &mut v.speech_language, &["follow"], SPEECH_LANGUAGES); egui::ComboBox::from_label(t("Quality")).selected_text(v.quality.as_str()).show_ui(ui, |ui| { ui.selectable_value(&mut v.quality, "balanced".to_string(), "Balanced (quantized, ~1.2 GB)"); ui.selectable_value(&mut v.quality, "full".to_string(), "Full (float32, ~3.3 GB)"); }); ui.add(egui::Slider::new(&mut v.cpu_threads, 1..=16).text(t("CPU threads"))); }); ui.label(t("English: Chatterbox Turbo. Other languages: Chatterbox Multilingual. Both use Capri's reference voice.")); if voice.voice_installed() { ui.label(t("Voice installed for this language.")); } else if ui.button(t("Install Capri's voice for this language")).clicked() { report(ui, voice.install_speech(false, true)); } } fn voice_effects(ui: &mut egui::Ui, store: &SettingsStore, voice: &VoiceHandle) { edit::(store, ui, |ui, e| { ui.checkbox(&mut e.enabled, t("Effects")); let mut preset = e.preset.clone(); egui::ComboBox::from_label(t("Preset")).selected_text(preset.as_str()).show_ui(ui, |ui| { for p in effects::PRESETS.iter().chain(&["custom"]) { ui.selectable_value(&mut preset, p.to_string(), *p); } }); if preset != e.preset && let Some(p) = effects::preset(&preset) { *e = p; } let before = e.clone(); for (label, value, max) in [ ("Intensity", &mut e.intensity, 200), ("Mix", &mut e.mix, 100), ("Reverb", &mut e.reverb, 200), ("Delay", &mut e.delay, 200), ("Filter", &mut e.filter, 200), ("Glitch", &mut e.glitch, 200), ("Electronic mix", &mut e.electronic_mix, 100), ] { ui.add(egui::Slider::new(value, 0..=max).text(label)); } ui.add(egui::Slider::new(&mut e.carrier_hz, 20..=200).suffix(" Hz").text(t("Carrier"))); if *e != before { e.preset = "custom".into(); } if ui.button(t("Reset")).clicked() { *e = VoiceEffects::default(); } }); if ui.button(t("Preview voice")).clicked() { report(ui, voice.preview_voice(None)); } } fn music(ui: &mut egui::Ui, store: &SettingsStore, voice: &VoiceHandle) { let state = voice.music_state(); let label = if state.preparing { "Preparing music detection…".to_string() } else if state.playing { format!("Music detected · BPM {}", if state.bpm > 0.0 { format!("{:.0}", state.bpm) } else { "…".into() }) } else if state.listening && state.speech >= 0.6 { "Speech detected".into() } else if state.listening { "Listening".into() } else { "Off".into() }; ui.label(label); if state.listening { if ui.button(t("Stop music listening")).clicked() { voice.set_music(false); } } else if ui.button(t("Start music listening")).clicked() { voice.set_music(true); } edit::(store, ui, |ui, m| { if ui.checkbox(&mut m.enabled, t("Music animations")).changed() && m.enabled && !state.listening { voice.set_music(true); } ui.checkbox(&mut m.notes, t("Floating notes")); ui.checkbox(&mut m.motion, t("Beat motion")); ui.checkbox(&mut m.auto_bpm, t("Follow the detected tempo (off: 110 BPM)")); }); ui.label(t("System playback is analysed in memory only; nothing is recorded or sent.")); } fn call(ui: &mut egui::Ui, store: &SettingsStore, voice: &VoiceHandle) { if voice.call_active() { ui.label(t("Virtual cable active: choose \"Capri-call-microphone\" as the microphone in your call app.")); let mut share = voice.share_replies(); if ui.checkbox(&mut share, t("Share Capri's replies in the call")).changed() { voice.set_share_replies(share); } if ui.button(t("Remove the virtual cable")).clicked() { voice.remove_call_cable(); } } else if ui.button(t("Create the virtual call cable")).clicked() { report(ui, voice.create_call_cable()); } edit::(store, ui, |ui, c| { ui.checkbox(&mut c.mix_microphone, t("Mix my microphone into the call")); ui.checkbox(&mut c.allow_model, t("Allow model-selected replies in calls")); ui.add(egui::Slider::new(&mut c.call_voice_volume, 0..=100).text(t("Capri's level in the call"))); }); } #[cfg(test)] mod tests { use super::*; use capri_core::{Paths, Settings}; #[tokio::test] async fn pages_render_headless() { let dir = tempfile::tempdir().unwrap(); let paths = Paths::under(dir.path().to_path_buf()); paths.ensure().unwrap(); let store = SettingsStore::new(paths, Settings::default()); let (voice, _rx) = VoiceHandle::start(store.clone()); let ctx = egui::Context::default(); for _ in 0..2 { let mut output = ctx.run_ui(egui::RawInput::default(), |ui| settings_ui(ui, &store, &voice)); output.textures_delta.clear(); } assert_eq!(store.section::(), SpeechSettings::default(), "rendering alone changes nothing"); } }