capri-voice: add ui module

This commit is contained in:
nocode committed 2026-11-11 20:20:48 +01:00
1 parent 5cd5828ecb
commit 1fa570bd54
1 file changed
+275
+275
View File
@@ -0,0 +1,275 @@
//! 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};
// TODO: saves on every slider step; debounce if disk writes start to matter.
/// Saves a section when the page changed it.
fn edit<T: Section>(store: &SettingsStore, ui: &mut egui::Ui, f: impl FnOnce(&mut egui::Ui, &mut T)) {
let before = store.section::<T>();
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::<String>(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::<String>(error_id())) {
ui.colored_label(ui.visuals().error_fg_color, error);
}
install_status(ui, voice);
egui::CollapsingHeader::new("Speech").default_open(true).show(ui, |ui| speech(ui, store, voice));
egui::CollapsingHeader::new("Microphone").default_open(true).show(ui, |ui| microphone(ui, store, voice));
egui::CollapsingHeader::new("Voice model").show(ui, |ui| voice_model(ui, store, voice));
egui::CollapsingHeader::new("Voice effects").show(ui, |ui| voice_effects(ui, store, voice));
egui::CollapsingHeader::new("Music listening").show(ui, |ui| music(ui, store, voice));
egui::CollapsingHeader::new("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("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("Speech components are installed.");
}
Phase::Idle => {}
}
}
fn speech(ui: &mut egui::Ui, store: &SettingsStore, voice: &VoiceHandle) {
edit::<SpeechSettings>(store, ui, |ui, s| {
language_combo(ui, "Recognition language", &mut s.recognition_language, &["follow", "auto"], SPEECH_LANGUAGES);
egui::ComboBox::from_label("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("End-of-speech pause"));
if ui.button("Reset pause").clicked() {
s.trailing_silence_seconds = 1.0;
}
ui.checkbox(&mut s.wake_word, "Wake phrase: Capri / Hey, Capri");
ui.checkbox(&mut s.auto_send, "Send recognized speech automatically");
ui.checkbox(&mut s.barge_in, "Interrupt Capri by speaking");
ui.checkbox(&mut s.follow_up, "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("Follow-up window"));
egui::ComboBox::from_label("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("Wake volume"));
ui.checkbox(&mut s.asr_gpu, "Use the GPU for recognition (needs a CUDA build)");
ui.add(egui::Slider::new(&mut s.cpu_threads, 1..=8).text("CPU threads"));
});
let installed = voice.recognition_installed();
ui.label(if installed { "Recognition model installed." } else { "Recognition model not installed." });
if !installed && ui.button("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, "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("Level"));
if ui.button("Refresh devices").clicked() {
voice.refresh_devices();
}
let devices = voice.input_devices();
edit::<SpeechSettings>(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("Input device").selected_text(selected).show_ui(ui, |ui| {
if ui.selectable_label(s.microphone_device.is_empty(), "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("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, "Speak replies (off at every launch)").changed() {
report(ui, voice.set_voice_enabled(on));
}
edit::<VoiceSettings>(store, ui, |ui, v| {
language_combo(ui, "Voice language", &mut v.speech_language, &["follow"], SPEECH_LANGUAGES);
egui::ComboBox::from_label("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("CPU threads"));
});
ui.label("English: Chatterbox Turbo. Other languages: Chatterbox Multilingual. Both use Capri's reference voice.");
if voice.voice_installed() {
ui.label("Voice installed for this language.");
} else if ui.button("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::<VoiceEffects>(store, ui, |ui, e| {
ui.checkbox(&mut e.enabled, "Effects");
let mut preset = e.preset.clone();
egui::ComboBox::from_label("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("Carrier"));
if *e != before {
e.preset = "custom".into();
}
if ui.button("Reset").clicked() {
*e = VoiceEffects::default();
}
});
if ui.button("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("Stop music listening").clicked() {
voice.set_music(false);
}
} else if ui.button("Start music listening").clicked() {
voice.set_music(true);
}
edit::<MusicSettings>(store, ui, |ui, m| {
if ui.checkbox(&mut m.enabled, "Music animations").changed() && m.enabled && !state.listening {
voice.set_music(true);
}
ui.checkbox(&mut m.notes, "Floating notes");
ui.checkbox(&mut m.motion, "Beat motion");
ui.checkbox(&mut m.auto_bpm, "Follow the detected tempo (off: 110 BPM)");
});
ui.label("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("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, "Share Capri's replies in the call").changed() {
voice.set_share_replies(share);
}
if ui.button("Remove the virtual cable").clicked() {
voice.remove_call_cable();
}
} else if ui.button("Create the virtual call cable").clicked() {
report(ui, voice.create_call_cable());
}
edit::<CallSettings>(store, ui, |ui, c| {
ui.checkbox(&mut c.mix_microphone, "Mix my microphone into the call");
ui.checkbox(&mut c.allow_model, "Allow model-selected replies in calls");
ui.add(egui::Slider::new(&mut c.call_voice_volume, 0..=100).text("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>(), SpeechSettings::default(), "rendering alone changes nothing");
}
}