2023-06-11 19:17:50 +05:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
2026-01-05 01:22:45 +05:00
|
|
|
use jarvis_core::{audio, commands, config, listener, recorder, stt, COMMANDS_LIST, intent};
|
2025-12-12 01:36:39 +05:00
|
|
|
use rand::prelude::*;
|
2023-06-11 19:17:50 +05:00
|
|
|
|
|
|
|
|
pub fn start() -> Result<(), ()> {
|
|
|
|
|
// start the loop
|
|
|
|
|
main_loop()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main_loop() -> Result<(), ()> {
|
2026-01-05 01:22:45 +05:00
|
|
|
let rt = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime");
|
2023-06-11 19:17:50 +05:00
|
|
|
let mut start: SystemTime;
|
|
|
|
|
let sounds_directory = audio::get_sound_directory().unwrap();
|
|
|
|
|
let frame_length: usize = 512; // default for every wake-word engine
|
|
|
|
|
let mut frame_buffer: Vec<i16> = vec![0; frame_length];
|
|
|
|
|
|
|
|
|
|
// play some run phrase
|
|
|
|
|
// @TODO. Different sounds? Or better make it via commands or upcoming events system.
|
|
|
|
|
audio::play_sound(&sounds_directory.join("run.wav"));
|
|
|
|
|
|
|
|
|
|
// start recording
|
|
|
|
|
match recorder::start_recording() {
|
|
|
|
|
Ok(_) => info!("Recording started."),
|
|
|
|
|
Err(_) => {
|
|
|
|
|
error!("Cannot start recording.");
|
|
|
|
|
return Err(()); // quit
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the loop
|
|
|
|
|
'wake_word: loop {
|
|
|
|
|
// read from microphone
|
|
|
|
|
recorder::read_microphone(&mut frame_buffer);
|
|
|
|
|
|
|
|
|
|
// recognize wake-word
|
|
|
|
|
match listener::data_callback(&frame_buffer) {
|
2026-01-05 01:22:45 +05:00
|
|
|
Some(_keyword_index) => {
|
2026-01-05 04:20:43 +05:00
|
|
|
// reset speech recognizer
|
|
|
|
|
stt::reset_wake_recognizer();
|
|
|
|
|
stt::reset_speech_recognizer();
|
|
|
|
|
|
2023-06-11 19:17:50 +05:00
|
|
|
// wake-word activated, process further commands
|
|
|
|
|
// capture current time
|
|
|
|
|
start = SystemTime::now();
|
|
|
|
|
|
|
|
|
|
// play some greet phrase
|
|
|
|
|
// @TODO. Make it via commands or upcoming events system.
|
2025-12-11 23:43:50 +05:00
|
|
|
audio::play_sound(&sounds_directory.join(format!(
|
|
|
|
|
"{}.wav",
|
|
|
|
|
config::ASSISTANT_GREET_PHRASES
|
|
|
|
|
.choose(&mut rand::thread_rng())
|
|
|
|
|
.unwrap()
|
|
|
|
|
)));
|
2023-06-11 19:17:50 +05:00
|
|
|
|
|
|
|
|
// wait for voice commands
|
|
|
|
|
'voice_recognition: loop {
|
|
|
|
|
// read from microphone
|
|
|
|
|
recorder::read_microphone(&mut frame_buffer);
|
|
|
|
|
|
|
|
|
|
// stt part (without partials)
|
|
|
|
|
if let Some(mut recognized_voice) = stt::recognize(&frame_buffer, false) {
|
|
|
|
|
// something was recognized
|
|
|
|
|
info!("Recognized voice: {}", recognized_voice);
|
|
|
|
|
|
|
|
|
|
// filter recognized voice
|
|
|
|
|
// @TODO. Better recognized voice filtration.
|
|
|
|
|
recognized_voice = recognized_voice.to_lowercase();
|
2026-01-05 04:20:43 +05:00
|
|
|
|
|
|
|
|
// answer again if it's activation phrase repeated
|
|
|
|
|
if recognized_voice.contains(config::VOSK_FETCH_PHRASE) {
|
|
|
|
|
info!("Wake word detected during chaining, reactivating...");
|
|
|
|
|
|
|
|
|
|
// play greet sound
|
|
|
|
|
audio::play_sound(&sounds_directory.join(format!(
|
|
|
|
|
"{}.wav",
|
|
|
|
|
config::ASSISTANT_GREET_PHRASES
|
|
|
|
|
.choose(&mut rand::thread_rng())
|
|
|
|
|
.unwrap()
|
|
|
|
|
)));
|
|
|
|
|
|
|
|
|
|
// reset timer and continue listening
|
|
|
|
|
start = SystemTime::now();
|
|
|
|
|
stt::reset_speech_recognizer();
|
|
|
|
|
continue 'voice_recognition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// filter out activation phrase from command
|
2023-06-11 19:17:50 +05:00
|
|
|
for tbr in config::ASSISTANT_PHRASES_TBR {
|
|
|
|
|
recognized_voice = recognized_voice.replace(tbr, "");
|
|
|
|
|
}
|
|
|
|
|
recognized_voice = recognized_voice.trim().into();
|
|
|
|
|
|
2026-01-05 04:20:43 +05:00
|
|
|
// skip if nothing left after filtering (*evil laugh*)
|
|
|
|
|
if recognized_voice.is_empty() {
|
|
|
|
|
continue 'voice_recognition;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-05 01:22:45 +05:00
|
|
|
// infer command (try intent recognition first, fallback to levenshtein)
|
|
|
|
|
let cmd_result = if let Some((intent_id, confidence)) =
|
|
|
|
|
rt.block_on(intent::classify(&recognized_voice))
|
|
|
|
|
{
|
|
|
|
|
info!("Intent recognized: {} (confidence: {:.2})", intent_id, confidence);
|
|
|
|
|
intent::get_command_by_intent(COMMANDS_LIST.get().unwrap(), &intent_id)
|
|
|
|
|
} else {
|
2026-01-05 04:20:43 +05:00
|
|
|
info!("Intent not recognized, trying levenshtein fallback ...");
|
2026-01-05 01:22:45 +05:00
|
|
|
commands::fetch_command(&recognized_voice, COMMANDS_LIST.get().unwrap())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some((cmd_path, cmd_config)) = cmd_result {
|
2023-06-11 19:17:50 +05:00
|
|
|
info!("Command found: {:?}", cmd_path);
|
|
|
|
|
info!("Executing!");
|
|
|
|
|
|
|
|
|
|
// execute the command
|
|
|
|
|
match commands::execute_command(&cmd_path, &cmd_config) {
|
|
|
|
|
Ok(chain) => {
|
|
|
|
|
// success
|
|
|
|
|
info!("Command executed successfully.");
|
|
|
|
|
|
|
|
|
|
if chain {
|
|
|
|
|
// chain commands
|
|
|
|
|
start = SystemTime::now();
|
|
|
|
|
} else {
|
|
|
|
|
// skip, if chaining is not required
|
2025-12-11 23:43:50 +05:00
|
|
|
start = start
|
|
|
|
|
.checked_sub(core::time::Duration::from_secs(1000))
|
|
|
|
|
.unwrap();
|
2023-06-11 19:17:50 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue 'voice_recognition; // continue voice recognition
|
2025-12-11 23:43:50 +05:00
|
|
|
}
|
2023-06-11 19:17:50 +05:00
|
|
|
Err(msg) => {
|
|
|
|
|
// fail
|
|
|
|
|
error!("Error executing command: {}", msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// return to wake-word listening after command execution (no matter successful or not)
|
|
|
|
|
break 'voice_recognition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only recognize voice for a certain period of time
|
|
|
|
|
match start.elapsed() {
|
|
|
|
|
Ok(elapsed) if elapsed > config::CMS_WAIT_DELAY => {
|
|
|
|
|
// return to wake-word listening after N seconds
|
|
|
|
|
break 'voice_recognition;
|
2025-12-11 23:43:50 +05:00
|
|
|
}
|
|
|
|
|
_ => (),
|
2023-06-11 19:17:50 +05:00
|
|
|
}
|
2026-01-05 04:20:43 +05:00
|
|
|
|
|
|
|
|
// reset wake recognizer
|
|
|
|
|
stt::reset_wake_recognizer();
|
2023-06-11 19:17:50 +05:00
|
|
|
}
|
2025-12-11 23:43:50 +05:00
|
|
|
}
|
|
|
|
|
None => (),
|
2023-06-11 19:17:50 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 23:43:50 +05:00
|
|
|
fn keyword_callback(keyword_index: i32) {}
|
2023-06-11 19:17:50 +05:00
|
|
|
|
|
|
|
|
pub fn close(code: i32) {
|
|
|
|
|
info!("Closing application.");
|
|
|
|
|
std::process::exit(code);
|
2025-12-11 23:43:50 +05:00
|
|
|
}
|