From b5eb5b043153e95e7abbde071f9fce7f70791058 Mon Sep 17 00:00:00 2001 From: CJ Pais Date: Mon, 12 May 2025 12:22:46 -0700 Subject: [PATCH] some cleanup --- src-tauri/src/lib.rs | 38 +++---------------------- src-tauri/src/managers/audio.rs | 8 ++++-- src-tauri/src/managers/transcription.rs | 16 ++++++++--- src-tauri/src/utils.rs | 12 ++++---- 4 files changed, 28 insertions(+), 46 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 92f312b..972b4c7 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -6,27 +6,13 @@ mod utils; use managers::audio::AudioRecordingManager; use managers::transcription::TranscriptionManager; -use std::collections::HashMap; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use tauri::image::Image; use tauri::menu::{Menu, MenuItem}; use tauri::tray::TrayIconBuilder; use tauri::Manager; use tauri_plugin_autostart::MacosLauncher; -pub struct AppState { - pub active_bindings: Mutex>, -} - -impl AppState { - // Convenience method to create a new AppState - fn new() -> Self { - AppState { - active_bindings: Mutex::new(HashMap::new()), - } - } -} - #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { env_logger::init(); @@ -41,7 +27,6 @@ pub fn run() { .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_store::Builder::default().build()) .plugin(tauri_plugin_global_shortcut::Builder::new().build()) - .manage(AppState::new()) .setup(move |app| { let settings_i = MenuItem::with_id(app, "settings", "Settings", true, None::<&str>)?; let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?; @@ -66,27 +51,12 @@ pub fn run() { .build(app)?; app.manage(tray); - let vad_path = app.path().resolve( - "resources/models/silero_vad_v4.onnx", - tauri::path::BaseDirectory::Resource, - )?; - - let whisper_path = app.path().resolve( - "resources/models/ggml-small.bin", - tauri::path::BaseDirectory::Resource, - )?; - let recording_manager = Arc::new( - AudioRecordingManager::new(&vad_path) - .expect("Failed to initialize recording manager"), + AudioRecordingManager::new(app).expect("Failed to initialize recording manager"), ); let transcription_manager = Arc::new( - TranscriptionManager::new( - whisper_path - .to_str() - .expect("Path contains invalid UTF-8 Chars"), - ) - .expect("Failed to initialize transcription manager"), + TranscriptionManager::new(&app) + .expect("Failed to initialize transcription manager"), ); // Add managers to Tauri's managed state diff --git a/src-tauri/src/managers/audio.rs b/src-tauri/src/managers/audio.rs index 25f4cef..010beee 100644 --- a/src-tauri/src/managers/audio.rs +++ b/src-tauri/src/managers/audio.rs @@ -1,8 +1,8 @@ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use rubato::{FftFixedIn, Resampler}; -use std::path::PathBuf; use std::sync::{Arc, Mutex}; use std::vec::Vec; +use tauri::{App, Manager}; use vad_rs::Vad; #[derive(Clone, Debug)] @@ -17,7 +17,11 @@ pub struct AudioRecordingManager { } impl AudioRecordingManager { - pub fn new(vad_path: &PathBuf) -> Result { + pub fn new(app: &App) -> Result { + let vad_path = app.path().resolve( + "resources/models/silero_vad_v4.onnx", + tauri::path::BaseDirectory::Resource, + )?; let host = cpal::default_host(); let device = host .default_input_device() diff --git a/src-tauri/src/managers/transcription.rs b/src-tauri/src/managers/transcription.rs index cc9d3fd..f0ae2cd 100644 --- a/src-tauri/src/managers/transcription.rs +++ b/src-tauri/src/managers/transcription.rs @@ -1,5 +1,6 @@ use anyhow::Result; use std::sync::Mutex; +use tauri::{App, Manager}; use whisper_rs::install_whisper_log_trampoline; use whisper_rs::{ FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters, WhisperState, @@ -11,12 +12,19 @@ pub struct TranscriptionManager { } impl TranscriptionManager { - pub fn new(whisper_path: &str) -> Result { + pub fn new(app: &App) -> Result { + let whisper_path = app.path().resolve( + "resources/models/ggml-small.bin", + tauri::path::BaseDirectory::Resource, + )?; + let path = whisper_path + .to_str() + .expect("Path contains invalid UTF-8 Chars"); + install_whisper_log_trampoline(); // Load the model - let context = - WhisperContext::new_with_params(whisper_path, WhisperContextParameters::default()) - .map_err(|e| anyhow::anyhow!("Failed to load whisper model: {}", e))?; + let context = WhisperContext::new_with_params(path, WhisperContextParameters::default()) + .map_err(|e| anyhow::anyhow!("Failed to load whisper model: {}", e))?; // Create state let state = context.create_state().expect("failed to create state"); diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index 4c0b97d..2955600 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -10,9 +10,9 @@ fn try_send_event(event: &EventType) { } } -fn send(event: EventType) { +fn send_with_delay(event: EventType, delay_ms: u64) { try_send_event(&event); - thread::sleep(time::Duration::from_millis(60)); + thread::sleep(time::Duration::from_millis(delay_ms)); } fn send_paste() { @@ -23,12 +23,12 @@ fn send_paste() { let modifier_key = Key::ControlLeft; // Control key on other systems // Press both keys - send(EventType::KeyPress(modifier_key)); - send(EventType::KeyPress(Key::KeyV)); + send_with_delay(EventType::KeyPress(modifier_key), 100); + send_with_delay(EventType::KeyPress(Key::KeyV), 100); // Release both keys - send(EventType::KeyRelease(Key::KeyV)); - send(EventType::KeyRelease(modifier_key)); + send_with_delay(EventType::KeyRelease(Key::KeyV), 0); + send_with_delay(EventType::KeyRelease(modifier_key), 0); } pub fn paste(text: String, app_handle: AppHandle) {