some cleanup

This commit is contained in:
CJ Pais 2025-05-12 12:22:46 -07:00
parent d4ac035b65
commit b5eb5b0431
4 changed files with 28 additions and 46 deletions

View file

@ -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<HashMap<String, String>>,
}
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

View file

@ -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<Self, anyhow::Error> {
pub fn new(app: &App) -> Result<Self, anyhow::Error> {
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()

View file

@ -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<Self> {
pub fn new(app: &App) -> Result<Self> {
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");

View file

@ -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) {