2026-01-04 09:00:51 +05:00
|
|
|
#[cfg(feature = "vosk")]
|
2023-06-11 19:17:50 +05:00
|
|
|
mod vosk;
|
|
|
|
|
|
|
|
|
|
use crate::config;
|
2025-12-11 23:43:50 +05:00
|
|
|
use once_cell::sync::OnceCell;
|
2023-06-11 19:17:50 +05:00
|
|
|
|
|
|
|
|
use crate::config::structs::SpeechToTextEngine;
|
2026-01-05 04:20:43 +05:00
|
|
|
pub use self::vosk::init_vosk;
|
|
|
|
|
pub use self::vosk::recognize_wake_word;
|
|
|
|
|
pub use self::vosk::recognize_speech;
|
|
|
|
|
pub use self::vosk::reset_speech_recognizer;
|
|
|
|
|
pub use self::vosk::reset_wake_recognizer;
|
2026-01-05 03:38:04 +05:00
|
|
|
|
2023-06-11 19:17:50 +05:00
|
|
|
static STT_TYPE: OnceCell<SpeechToTextEngine> = OnceCell::new();
|
|
|
|
|
|
2026-02-18 21:08:48 +05:00
|
|
|
pub fn init() -> Result<(), String> {
|
2026-01-04 06:14:05 +05:00
|
|
|
if STT_TYPE.get().is_some() {
|
2025-12-11 23:43:50 +05:00
|
|
|
return Ok(());
|
2026-02-18 21:08:48 +05:00
|
|
|
}
|
2023-06-11 19:17:50 +05:00
|
|
|
|
2026-02-18 21:08:48 +05:00
|
|
|
STT_TYPE.set(config::DEFAULT_SPEECH_TO_TEXT_ENGINE)
|
|
|
|
|
.map_err(|_| "STT type already set".to_string())?;
|
2023-06-11 19:17:50 +05:00
|
|
|
|
|
|
|
|
match STT_TYPE.get().unwrap() {
|
|
|
|
|
SpeechToTextEngine::Vosk => {
|
|
|
|
|
info!("Initializing Vosk STT backend.");
|
2026-02-18 21:08:48 +05:00
|
|
|
vosk::init_vosk()?;
|
2023-06-11 19:17:50 +05:00
|
|
|
info!("STT backend initialized.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-05 04:20:43 +05:00
|
|
|
pub fn recognize(data: &[i16], include_partial: bool) -> Option<String> {
|
|
|
|
|
if include_partial {
|
|
|
|
|
vosk::recognize_wake_word(data).map(|(text, _)| text)
|
|
|
|
|
} else {
|
|
|
|
|
vosk::recognize_speech(data)
|
2023-06-11 19:17:50 +05:00
|
|
|
}
|
2025-12-11 23:43:50 +05:00
|
|
|
}
|