diff --git a/Cargo.lock b/Cargo.lock index 72d44e3..a9173e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4846,7 +4846,7 @@ dependencies = [ [[package]] name = "rustpotter" version = "3.0.2" -source = "git+https://github.com/Priler/rustpotter#5604a094b4a21ad12645e5aac5ffc463a2892af8" +source = "git+https://github.com/Priler/rustpotter#f0d5c4d6b63aa1ebb6e0e81089993e4ae7aef3d8" dependencies = [ "candle-core", "candle-nn", diff --git a/crates/jarvis-app/src/main.rs b/crates/jarvis-app/src/main.rs index b00a3e1..1c94b35 100644 --- a/crates/jarvis-app/src/main.rs +++ b/crates/jarvis-app/src/main.rs @@ -75,7 +75,7 @@ fn main() -> Result<(), String> { // start the app (in the background thread) std::thread::spawn(|| { - app::start(); + let _ = app::start(); }); tray::init_blocking(); diff --git a/crates/jarvis-core/src/audio.rs b/crates/jarvis-core/src/audio.rs index 30786c2..658638f 100644 --- a/crates/jarvis-core/src/audio.rs +++ b/crates/jarvis-core/src/audio.rs @@ -11,7 +11,7 @@ use crate::{config, DB, SOUND_DIR}; static AUDIO_TYPE: OnceCell = OnceCell::new(); pub fn init() -> Result<(), ()> { - if !AUDIO_TYPE.get().is_none() { + if AUDIO_TYPE.get().is_some() { return Ok(()); } // already initialized @@ -29,7 +29,7 @@ pub fn init() -> Result<(), ()> { Ok(_) => { info!("Successfully initialized Rodio audio backend."); } - Err(msg) => { + Err(()) => { error!("Failed to initialize Rodio audio backend."); return Err(()); diff --git a/crates/jarvis-core/src/audio/rodio.rs b/crates/jarvis-core/src/audio/rodio.rs index c8e6073..6b3257d 100644 --- a/crates/jarvis-core/src/audio/rodio.rs +++ b/crates/jarvis-core/src/audio/rodio.rs @@ -17,7 +17,7 @@ static STREAM_HANDLE: OnceCell = OnceCell::new(); static SINK: OnceCell = OnceCell::new(); pub fn init() -> Result<(), ()> { - if !STREAM_HANDLE.get().is_none() { + if STREAM_HANDLE.get().is_some() { return Ok(()); } // already initialized diff --git a/crates/jarvis-core/src/commands.rs b/crates/jarvis-core/src/commands.rs index da11864..544f0af 100644 --- a/crates/jarvis-core/src/commands.rs +++ b/crates/jarvis-core/src/commands.rs @@ -23,7 +23,13 @@ pub fn parse_commands() -> Result, String> { if let Ok(cpaths) = fs::read_dir(config::COMMANDS_PATH) { for cpath in cpaths { // validate this command, check if required files exists - let _cpath = cpath.unwrap().path(); + let _cpath = match cpath { + Ok(entry) => entry.path(), + Err(e) => { + warn!("Failed to read command directory entry: {}", e); + continue; + } + }; let cc_file = Path::new(&_cpath).join("command.yaml"); if cc_file.exists() { @@ -53,7 +59,7 @@ pub fn parse_commands() -> Result, String> { } } - if commands.len() > 0 { + if !commands.is_empty() { Ok(commands) } else { error!("No commands were found"); @@ -101,7 +107,7 @@ pub fn fetch_command<'a>( } if let Some((cmd_path, scmd)) = result_scmd { - println!("Ratio is: {}", current_max_ratio); + debug!("Ratio is: {}", current_max_ratio); info!( "CMD is: {cmd_path:?}, SCMD is: {scmd:?}, Ratio is: {}", current_max_ratio @@ -118,7 +124,7 @@ pub fn execute_exe(exe: &str, args: &Vec) -> std::io::Result { } pub fn execute_cli(cmd: &str, args: &Vec) -> std::io::Result { - println!("Spawning cmd as: cmd /C {} {:?}", cmd, args); + debug!("Spawning cmd as: cmd /C {} {:?}", cmd, args); if cfg!(target_os = "windows") { Command::new("cmd").arg("/C").arg(cmd).args(args).spawn() diff --git a/crates/jarvis-core/src/listener.rs b/crates/jarvis-core/src/listener.rs index 19f96ab..c9aea47 100644 --- a/crates/jarvis-core/src/listener.rs +++ b/crates/jarvis-core/src/listener.rs @@ -19,7 +19,7 @@ static WAKE_WORD_ENGINE: OnceCell = OnceCell::new(); static LISTENING: AtomicBool = AtomicBool::new(false); pub fn init() -> Result<(), ()> { - if !WAKE_WORD_ENGINE.get().is_none() { + if WAKE_WORD_ENGINE.get().is_some() { return Ok(()); } // already initialized diff --git a/crates/jarvis-core/src/listener/rustpotter.rs b/crates/jarvis-core/src/listener/rustpotter.rs index 768ec75..12c53b7 100644 --- a/crates/jarvis-core/src/listener/rustpotter.rs +++ b/crates/jarvis-core/src/listener/rustpotter.rs @@ -33,7 +33,10 @@ pub fn init() -> Result<(), ()> { // load wake word files for rpw in rustpotter_wake_word_files { - rinstance.add_wakeword_from_file(rpw, rpw).unwrap(); // @TODO: Change wakeword key to something else? + // @TODO: Change wakeword key to something else? + if let Err(e) = rinstance.add_wakeword_from_file(rpw, rpw) { + error!("Failed to load wakeword file '{}': {}", rpw, e); + } } // store @@ -52,7 +55,8 @@ pub fn init() -> Result<(), ()> { pub fn data_callback(frame_buffer: &[i16]) -> Option { let mut lock = RUSTPOTTER.get().unwrap().lock(); let rustpotter = lock.as_mut().unwrap(); - let detection = rustpotter.process_samples(frame_buffer.to_vec()); // @TODO. Temp crutch. Fix optimization issue, frame_buffer should not be copied to a new vector! + // let detection = rustpotter.process_samples(frame_buffer.to_vec()); // @TODO. Temp crutch. Fix optimization issue, frame_buffer should not be copied to a new vector! + let detection = rustpotter.process_samples(frame_buffer); if let Some(detection) = detection { if detection.score > config::RUSPOTTER_MIN_SCORE { diff --git a/crates/jarvis-core/src/recorder/cpal.rs b/crates/jarvis-core/src/recorder/cpal.rs index 8b4d0ed..9692a9d 100644 --- a/crates/jarvis-core/src/recorder/cpal.rs +++ b/crates/jarvis-core/src/recorder/cpal.rs @@ -175,7 +175,7 @@ pub fn start_recording(device_index: i32, frame_length: u32) { pub fn stop_recording() { // ensure microphone is initialized RECORDER.with(|recorder| { - if !recorder.get().is_none() && IS_RECORDING.load(Ordering::SeqCst) { + if recorder.get().is_some() && IS_RECORDING.load(Ordering::SeqCst) { // pause instead of stop match recorder.get().unwrap().load().pause() { Err(msg) => { diff --git a/crates/jarvis-core/src/recorder/portaudio.rs b/crates/jarvis-core/src/recorder/portaudio.rs index 36a995a..4bb8ce6 100644 --- a/crates/jarvis-core/src/recorder/portaudio.rs +++ b/crates/jarvis-core/src/recorder/portaudio.rs @@ -159,7 +159,7 @@ where pub fn read_microphone(frame_buffer: &mut [i16]) { // ensure microphone is initialized RECORDER.with(|r| { - if !r.get().is_none() { + if r.get().is_some() { let cell = r.get().unwrap().load(); let mut lock = cell.lock(); let stream = lock.as_mut().unwrap(); @@ -194,7 +194,7 @@ pub fn start_recording(device_index: i32, frame_length: u32) { pub fn stop_recording() { RECORDER.with(|r| { - if !r.get().is_none() && IS_RECORDING.load(Ordering::SeqCst) { + if r.get().is_some() && IS_RECORDING.load(Ordering::SeqCst) { // stop recording let pa = r.get().unwrap().load(); r.get().unwrap().load().lock().unwrap().stop().expect("Failed to stop audio recording!"); diff --git a/crates/jarvis-core/src/recorder/pvrecorder.rs b/crates/jarvis-core/src/recorder/pvrecorder.rs index bffbf50..669ce39 100644 --- a/crates/jarvis-core/src/recorder/pvrecorder.rs +++ b/crates/jarvis-core/src/recorder/pvrecorder.rs @@ -6,36 +6,36 @@ static RECORDER: OnceCell = OnceCell::new(); static IS_RECORDING: AtomicBool = AtomicBool::new(false); pub fn init_microphone(device_index: i32, frame_length: u32) -> bool { - match RECORDER.get().is_none() { - true => { - let pv_recorder = PvRecorderBuilder::new(frame_length as i32) - .device_index(device_index) - // .frame_length(frame_length as i32) - .init(); + if RECORDER.get().is_some() { + return true; // already initialized + } + + // initialize + let pv_recorder = PvRecorderBuilder::new(frame_length as i32) + .device_index(device_index) + // .frame_length(frame_length as i32) + .init(); - match pv_recorder { - Ok(pv) => { - // store - RECORDER.set(pv); + match pv_recorder { + Ok(pv) => { + // store + RECORDER.set(pv); - // success - true - } - Err(msg) => { - error!("Failed to initialize pvrecorder.\nError details: {:?}", msg); - - // fail - false - } - } + // success + true + } + Err(msg) => { + error!("Failed to initialize pvrecorder.\nError details: {:?}", msg); + + // fail + false } - _ => true, // already initialized } } pub fn read_microphone(frame_buffer: &mut [i16]) { // ensure microphone is initialized - if !RECORDER.get().is_none() { + if RECORDER.get().is_some() { // read to frame buffer let frame = RECORDER.get().unwrap().read(); @@ -68,7 +68,7 @@ pub fn start_recording(device_index: i32, frame_length: u32) -> Result<(), ()> { Ok(()) } Err(msg) => { - error!("Failed to start audio recording!"); + error!("Failed to START audio recording: {}", msg); // fail Err(()) @@ -78,7 +78,7 @@ pub fn start_recording(device_index: i32, frame_length: u32) -> Result<(), ()> { pub fn stop_recording() -> Result<(), ()> { // ensure microphone is initialized & recording is in process - if !RECORDER.get().is_none() && IS_RECORDING.load(Ordering::SeqCst) { + if RECORDER.get().is_some() && IS_RECORDING.load(Ordering::SeqCst) { // stop recording match RECORDER.get().unwrap().stop() { Ok(_) => { @@ -91,7 +91,7 @@ pub fn stop_recording() -> Result<(), ()> { return Ok(()); } Err(msg) => { - error!("Failed to stop audio recording!"); + error!("Failed to STOP audio recording: {}", msg); // fail return Err(()); @@ -106,7 +106,10 @@ pub fn list_audio_devices() -> Vec { let audio_devices = PvRecorderBuilder::default().get_available_devices(); match audio_devices { Ok(audio_devices) => audio_devices, - Err(err) => panic!("Failed to get audio devices: {}", err), + Err(err) => { + error!("Failed to get audio devices: {}", err); + Vec::new() + }, } } diff --git a/crates/jarvis-core/src/stt.rs b/crates/jarvis-core/src/stt.rs index 288c402..02a4957 100644 --- a/crates/jarvis-core/src/stt.rs +++ b/crates/jarvis-core/src/stt.rs @@ -8,7 +8,7 @@ use crate::config::structs::SpeechToTextEngine; static STT_TYPE: OnceCell = OnceCell::new(); pub fn init() -> Result<(), ()> { - if !STT_TYPE.get().is_none() { + if STT_TYPE.get().is_some() { return Ok(()); } // already initialized diff --git a/crates/jarvis-core/src/stt/vosk.rs b/crates/jarvis-core/src/stt/vosk.rs index 3b3aa25..c716e03 100644 --- a/crates/jarvis-core/src/stt/vosk.rs +++ b/crates/jarvis-core/src/stt/vosk.rs @@ -9,7 +9,7 @@ static MODEL: OnceCell = OnceCell::new(); static RECOGNIZER: OnceCell> = OnceCell::new(); pub fn init_vosk() { - if !RECOGNIZER.get().is_none() { + if RECOGNIZER.get().is_some() { return; } // already initialized @@ -53,21 +53,14 @@ pub fn recognize(data: &[i16], include_partial: bool) -> Option { } DecodingState::Finalized => { // Result will always be multiple because we called set_max_alternatives - Some( - RECOGNIZER - .get() - .unwrap() - .lock() - .unwrap() - .result() - .multiple() - .unwrap() - .alternatives - .first() - .unwrap() - .text - .into(), - ) + RECOGNIZER + .get() + .unwrap() + .lock() + .unwrap() + .result() + .multiple() + .and_then(|m| m.alternatives.first().map(|a| a.text.to_string())) } DecodingState::Failed => None, }