From 931801d65501199d2c5aa803cb3d64313eceee2e Mon Sep 17 00:00:00 2001 From: fkDead Date: Wed, 11 Nov 2026 20:20:48 +0100 Subject: [PATCH] capri-voice: add crate root --- crates/capri-voice/src/lib.rs | 105 ++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 crates/capri-voice/src/lib.rs diff --git a/crates/capri-voice/src/lib.rs b/crates/capri-voice/src/lib.rs new file mode 100644 index 0000000..99c0a2c --- /dev/null +++ b/crates/capri-voice/src/lib.rs @@ -0,0 +1,105 @@ +//! Microphone, speech recognition (whisper.cpp), Capri's voice (Chatterbox ONNX), effects, subtitle timing, +//! music listening and call audio. See README.md for how to wire it in. +pub mod asr; +pub mod call; +pub mod capture; +pub mod download; +pub mod echo; +pub mod effects; +pub mod install; +pub mod mic; +pub mod music; +pub mod player; +pub mod segment; +pub mod service; +pub mod settings; +pub mod text; +pub mod tts; +mod ui; +pub mod wake; + +pub use install::{InstallStatus, Phase as InstallPhase}; +pub use player::Mouth; +pub use service::{VoiceHandle, VoiceService, preview_text}; +pub use settings::{CallSettings, MusicSettings, SpeechSettings, VoiceEffects, VoiceSettings}; +pub use ui::settings_ui; + +/// One ONNX Runtime environment for the process (VAD, music classifier and voice share it; a second one fails). +pub(crate) fn ort_env() -> anyhow::Result<&'static ort::environment::Environment> { + static ENV: std::sync::OnceLock = std::sync::OnceLock::new(); + static INIT: parking_lot::Mutex<()> = parking_lot::Mutex::new(()); + if let Some(env) = ENV.get() { + return Ok(env); + } + let _guard = INIT.lock(); + if let Some(env) = ENV.get() { + return Ok(env); + } + let env = ort::init().with_name("capri").build()?; + Ok(ENV.get_or_init(|| env)) +} + +#[derive(Clone, Debug, PartialEq)] +pub struct MicStatus { + pub on: bool, + /// The recognizer is loaded and audio is being processed. + pub listening: bool, + pub wake_state: wake::GateState, + pub phase: segment::Phase, + pub language: String, + /// Why the microphone stopped, when it stopped by itself. + pub error: Option, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct VoiceStatus { + pub enabled: bool, + pub ready: bool, + pub loading: bool, + pub language: Option, + pub error: Option, +} + +/// Music listening state for the avatar dance (`playing`/`bpm` stay 0 when music animations are off). +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct MusicState { + pub listening: bool, + pub preparing: bool, + pub playing: bool, + pub bpm: f64, + pub confidence: f64, + /// Epoch milliseconds of a beat; beats repeat every `60000 / bpm` ms. + pub beat_at_ms: f64, + pub energy: f64, + pub speech: f64, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum VoiceEvent { + Microphone(MicStatus), + /// Capri was addressed (wake phrase), or a follow-up / manual command window opened. + Wake { utterance: Option, follow_up: bool, manual: bool }, + /// Final recognized text for the composer preview. `is_final: false` with empty text clears the preview. + /// Provisional words are never emitted (Capri rule). + Transcript { utterance: i64, revision: u64, text: String, is_final: bool, auto_send: bool }, + /// Send this text to the conversation (`Engine::submit`), then call `begin_turn(id, true)`. + Submit { utterance: i64, text: String }, + /// "Stop", "be quiet": handled locally (speech already stopped), not sent to the model. + LocalCommand { text: String }, + /// Barge-in: cancel the running reply (`Engine::cancel`). Speech is already stopped. + Interrupt, + /// "Microphone off" was spoken; the microphone stops itself. + StopRequest, + /// Every 50 ms while speaking, then once with level 0. + Speaking { turn_id: Option, level: f32, mouth: Mouth }, + /// A sentence starts playing: show it, page subtitles over `duration_ms`. + Subtitle { turn_id: String, sequence: u32, text: String, duration_ms: u64 }, + SubtitleEnded { turn_id: String, sequence: u32 }, + PlaybackStopped, + /// Every sentence of the turn is synthesized. + Generated { turn_id: String }, + Voice(VoiceStatus), + MusicState(MusicState), + Install(InstallStatus), + Error { area: &'static str, message: String }, +}