From 78b5dd58dc1240c9e8bbaa28c7c19762ec93fbf5 Mon Sep 17 00:00:00 2001 From: fkDead Date: Wed, 11 Nov 2026 20:20:48 +0100 Subject: [PATCH] capri-voice: add settings module --- crates/capri-voice/src/settings.rs | 214 +++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 crates/capri-voice/src/settings.rs diff --git a/crates/capri-voice/src/settings.rs b/crates/capri-voice/src/settings.rs new file mode 100644 index 0000000..2aff06c --- /dev/null +++ b/crates/capri-voice/src/settings.rs @@ -0,0 +1,214 @@ +//! Voice settings sections. Defaults follow Capri `config/companion-asr.json`, `companion-voice.json`, +//! `renderer/voice-effects-settings.mjs`, `music-listening-settings.mjs` and `call-audio.json`. +//! Microphone, voice output and music capture are session-only switches: never stored here. +use capri_core::{Section, Settings}; +use serde::{Deserialize, Serialize}; + +/// The 23 languages Capri's voices can speak. +pub const SPEECH_LANGUAGES: &[&str] = + &["en", "ar", "zh", "da", "nl", "fi", "fr", "de", "el", "he", "hi", "it", "ja", "ko", "ms", "no", "pl", "pt", "ru", "es", "sw", "sv", "tr"]; + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[serde(default, rename_all = "camelCase")] +pub struct SpeechSettings { + /// `follow` (conversation language), `auto`, or a language code. + pub recognition_language: String, + /// End-of-speech pause, 0.5..5.0 s in 0.1 steps. + pub trailing_silence_seconds: f32, + pub max_utterance_seconds: u32, + pub auto_send: bool, + pub barge_in: bool, + pub wake_word: bool, + pub command_window_ms: u32, + pub ambiguity_hold_ms: u32, + pub vad_threshold: f32, + pub min_speech_seconds: f32, + pub wake_probe_seconds: f32, + pub follow_up: bool, + pub follow_up_seconds: u32, + /// `tone`, `off`. + pub wake_sound: String, + pub wake_volume: f32, + /// Exact input id from [`crate::capture::input_devices`]; empty = system default. + pub microphone_device: String, + pub microphone_label: String, + /// Whisper model id from [`crate::asr::MODELS`]. + pub asr_model: String, + pub asr_gpu: bool, + pub cpu_threads: u32, +} + +impl Default for SpeechSettings { + fn default() -> Self { + Self { + recognition_language: "follow".into(), + trailing_silence_seconds: 1.0, + max_utterance_seconds: 30, + auto_send: true, + barge_in: true, + wake_word: true, + command_window_ms: 10_000, + ambiguity_hold_ms: 120, + vad_threshold: 0.5, + min_speech_seconds: 0.12, + wake_probe_seconds: 0.8, + follow_up: true, + follow_up_seconds: 12, + wake_sound: "tone".into(), + wake_volume: 0.7, + microphone_device: String::new(), + microphone_label: String::new(), + asr_model: crate::asr::DEFAULT_MODEL.into(), + asr_gpu: false, + cpu_threads: 8, + } + } +} + +impl Section for SpeechSettings { + const NAME: &'static str = "speech"; +} + +impl SpeechSettings { + /// Values clamped to Capri's validated ranges. + pub fn normalized(&self) -> Self { + let mut s = self.clone(); + s.trailing_silence_seconds = (s.trailing_silence_seconds.clamp(0.5, 5.0) * 10.0).round() / 10.0; + s.max_utterance_seconds = s.max_utterance_seconds.clamp(10, 60); + s.command_window_ms = s.command_window_ms.clamp(3000, 30000); + s.ambiguity_hold_ms = s.ambiguity_hold_ms.min(500); + s.vad_threshold = s.vad_threshold.clamp(0.2, 0.9); + s.min_speech_seconds = s.min_speech_seconds.clamp(0.06, 0.5); + s.wake_probe_seconds = s.wake_probe_seconds.clamp(0.5, 2.0); + s.follow_up_seconds = s.follow_up_seconds.clamp(5, 30); + s.wake_volume = s.wake_volume.clamp(0.0, 1.0); + s.cpu_threads = s.cpu_threads.clamp(1, 8); + s + } + + /// Recognition language for whisper: `auto` or a language code. + pub fn language(&self, settings: &Settings) -> String { + let l = &settings.language; + match self.recognition_language.as_str() { + "follow" if l.conversation == "custom" || l.conversation.is_empty() => "auto".into(), + "follow" => l.conversation.clone(), + other => other.to_string(), + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[serde(default, rename_all = "camelCase")] +pub struct VoiceSettings { + /// `follow` (conversation language when it is a speech language) or a speech language. + pub speech_language: String, + /// `balanced` (quantized language model, ~1.2 GB) or `full` (float32, ~3.3 GB English / ~3.3 GB multilingual). + pub quality: String, + pub cpu_threads: u32, +} + +impl Default for VoiceSettings { + fn default() -> Self { + Self { speech_language: "follow".into(), quality: "balanced".into(), cpu_threads: 8 } + } +} + +impl Section for VoiceSettings { + const NAME: &'static str = "voice"; +} + +impl VoiceSettings { + /// Spoken language, or `None` when the conversation language has no voice. + pub fn language(&self, settings: &Settings) -> Option { + let wanted = if self.speech_language == "follow" { settings.language.conversation.as_str() } else { self.speech_language.as_str() }; + SPEECH_LANGUAGES.contains(&wanted).then(|| wanted.to_string()) + } +} + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[serde(default, rename_all = "camelCase")] +pub struct VoiceEffects { + pub enabled: bool, + /// `clean`, `cybernetic`, `radio`, `holographic`, `synthetic`, `custom`. + pub preset: String, + pub intensity: u32, + pub mix: u32, + pub reverb: u32, + pub delay: u32, + pub filter: u32, + pub glitch: u32, + pub electronic_mix: u32, + pub carrier_hz: u32, +} + +impl Default for VoiceEffects { + fn default() -> Self { + Self { enabled: false, preset: "clean".into(), intensity: 40, mix: 25, reverb: 0, delay: 0, filter: 0, glitch: 0, electronic_mix: 0, carrier_hz: 60 } + } +} + +impl Section for VoiceEffects { + const NAME: &'static str = "voiceEffects"; +} + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[serde(default, rename_all = "camelCase")] +pub struct MusicSettings { + /// Music animations. + pub enabled: bool, + pub notes: bool, + pub motion: bool, + pub auto_bpm: bool, +} + +impl Default for MusicSettings { + fn default() -> Self { + Self { enabled: false, notes: true, motion: true, auto_bpm: true } + } +} + +impl Section for MusicSettings { + const NAME: &'static str = "music"; +} + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[serde(default, rename_all = "camelCase")] +pub struct CallSettings { + /// Permits "Ask Capri in call". + pub allow_model: bool, + /// Capri's level in the call, 0..100. + pub call_voice_volume: u32, + /// Also route the owner's microphone into the call cable. + pub mix_microphone: bool, +} + +impl Default for CallSettings { + fn default() -> Self { + Self { allow_model: false, call_voice_volume: 50, mix_microphone: true } + } +} + +impl Section for CallSettings { + const NAME: &'static str = "callAudio"; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn languages_and_ranges() { + let mut settings = Settings::default(); + settings.language.conversation = "fr".into(); + assert_eq!(SpeechSettings::default().language(&settings), "fr"); + assert_eq!(VoiceSettings::default().language(&settings).as_deref(), Some("fr")); + settings.language.conversation = "uk".into(); + assert_eq!(VoiceSettings::default().language(&settings), None, "Ukrainian has no voice"); + settings.language.conversation = "custom".into(); + assert_eq!(SpeechSettings::default().language(&settings), "auto"); + let s = SpeechSettings { trailing_silence_seconds: 9.04, follow_up_seconds: 1, ..Default::default() }.normalized(); + assert_eq!((s.trailing_silence_seconds, s.follow_up_seconds), (5.0, 5)); + let partial: SpeechSettings = serde_json::from_str(r#"{"wakeWord":false}"#).unwrap(); + assert!(!partial.wake_word && partial.auto_send); + } +}