This commit is contained in:
CJ Pais 2025-05-15 13:21:52 -07:00
commit 5735b83cd2
4 changed files with 34 additions and 9 deletions

View file

@ -42,6 +42,12 @@ tauri-plugin-store = "2"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
whisper-rs = { version = "0.13.2", features = ["metal"] } whisper-rs = { version = "0.13.2", features = ["metal"] }
[target.'cfg(target_os = "windows")'.dependencies]
whisper-rs = { version = "0.13.2", features = ["vulkan"] }
[target.'cfg(target_os = "linux")'.dependencies]
whisper-rs = { version = "0.13.2", features = ["openblas", "vulkan"] }
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies] [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-autostart = "2" tauri-plugin-autostart = "2"
tauri-plugin-global-shortcut = "2" tauri-plugin-global-shortcut = "2"

View file

@ -98,6 +98,8 @@ pub fn run() {
}) })
.on_window_event(|window, event| match event { .on_window_event(|window, event| match event {
tauri::WindowEvent::CloseRequested { api, .. } => { tauri::WindowEvent::CloseRequested { api, .. } => {
api.prevent_close();
let _res = window.hide();
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
let res = window let res = window
@ -107,10 +109,8 @@ pub fn run() {
println!("Failed to set activation policy: {}", e); println!("Failed to set activation policy: {}", e);
} }
api.prevent_close();
// TODO may be different on windows, this works for macos // TODO may be different on windows, this works for macos
tauri::AppHandle::hide(window.app_handle()).unwrap(); // tauri::AppHandle::hide(window.app_handle()).unwrap();
} }
} }
_ => {} _ => {}

View file

@ -12,6 +12,7 @@ pub enum RecordingState {
Recording { binding_id: String }, Recording { binding_id: String },
} }
#[derive(Clone)]
pub struct AudioRecordingManager { pub struct AudioRecordingManager {
state: Arc<Mutex<RecordingState>>, state: Arc<Mutex<RecordingState>>,
buffer: Arc<Mutex<Vec<f32>>>, buffer: Arc<Mutex<Vec<f32>>>,
@ -84,6 +85,7 @@ impl AudioRecordingManager {
// Generic function to process audio data // Generic function to process audio data
fn process_audio<T: SampleToF32 + Send + 'static>( fn process_audio<T: SampleToF32 + Send + 'static>(
data: &[T], data: &[T],
channels: usize,
state_clone: Arc<Mutex<RecordingState>>, state_clone: Arc<Mutex<RecordingState>>,
temp_buffer_clone: Arc<Mutex<Vec<f32>>>, temp_buffer_clone: Arc<Mutex<Vec<f32>>>,
resampler_clone: Arc<Mutex<FftFixedIn<f32>>>, resampler_clone: Arc<Mutex<FftFixedIn<f32>>>,
@ -95,9 +97,20 @@ impl AudioRecordingManager {
if let RecordingState::Recording { .. } = *state_guard { if let RecordingState::Recording { .. } = *state_guard {
let mut temp_buffer = temp_buffer_clone.lock().unwrap(); let mut temp_buffer = temp_buffer_clone.lock().unwrap();
// Convert incoming data to f32 // Handle multichannel audio by mixing down to mono
let f32_data: Vec<f32> = data.iter().map(|sample| sample.to_f32()).collect(); if channels > 1 {
temp_buffer.extend_from_slice(&f32_data); // Process chunks of `channels` samples at a time (each chunk is one audio frame)
for chunk in data.chunks(channels) {
// Average the channels to create a mono sample
let mono_sample: f32 =
chunk.iter().map(|s| s.to_f32()).sum::<f32>() / channels as f32;
temp_buffer.push(mono_sample);
}
} else {
// Single channel processing
let f32_data: Vec<f32> = data.iter().map(|sample| sample.to_f32()).collect();
temp_buffer.extend_from_slice(&f32_data);
}
// Process when we have enough samples // Process when we have enough samples
while temp_buffer.len() >= 1024 { while temp_buffer.len() >= 1024 {
@ -142,12 +155,17 @@ impl AudioRecordingManager {
let err_fn = |err| eprintln!("Error in stream: {}", err); let err_fn = |err| eprintln!("Error in stream: {}", err);
// Build the appropriate stream based on the sample format // Build the appropriate stream based on the sample format
// Store the number of channels for use in the closure
let channels = config.channels() as usize;
println!("Using {} channel(s) for recording", channels);
let stream = match config.sample_format() { let stream = match config.sample_format() {
SampleFormat::I8 => device.build_input_stream( SampleFormat::I8 => device.build_input_stream(
&config.into(), &config.into(),
move |data: &[i8], _| { move |data: &[i8], _| {
process_audio( process_audio(
data, data,
channels,
Arc::clone(&state_clone), Arc::clone(&state_clone),
Arc::clone(&temp_buffer_clone), Arc::clone(&temp_buffer_clone),
Arc::clone(&resampler_clone), Arc::clone(&resampler_clone),
@ -164,6 +182,7 @@ impl AudioRecordingManager {
move |data: &[i16], _| { move |data: &[i16], _| {
process_audio( process_audio(
data, data,
channels,
Arc::clone(&state_clone), Arc::clone(&state_clone),
Arc::clone(&temp_buffer_clone), Arc::clone(&temp_buffer_clone),
Arc::clone(&resampler_clone), Arc::clone(&resampler_clone),
@ -180,6 +199,7 @@ impl AudioRecordingManager {
move |data: &[i32], _| { move |data: &[i32], _| {
process_audio( process_audio(
data, data,
channels,
Arc::clone(&state_clone), Arc::clone(&state_clone),
Arc::clone(&temp_buffer_clone), Arc::clone(&temp_buffer_clone),
Arc::clone(&resampler_clone), Arc::clone(&resampler_clone),
@ -196,6 +216,7 @@ impl AudioRecordingManager {
move |data: &[f32], _| { move |data: &[f32], _| {
process_audio( process_audio(
data, data,
channels,
Arc::clone(&state_clone), Arc::clone(&state_clone),
Arc::clone(&temp_buffer_clone), Arc::clone(&temp_buffer_clone),
Arc::clone(&resampler_clone), Arc::clone(&resampler_clone),
@ -268,7 +289,7 @@ impl AudioRecordingManager {
} else { } else {
// Pad to minimum 1000ms if needed // Pad to minimum 1000ms if needed
if duration_ms < 1000.0 { if duration_ms < 1000.0 {
let target_samples = (16400.0 * (1000.0 / 1000.0)) as usize; // 16000 samples for 1 second let target_samples = (16000.0 * (1000.0 / 1000.0)) as usize; // 16000 samples for 1 second
let mut padded_audio = audio_data; let mut padded_audio = audio_data;
padded_audio.resize(target_samples, 0.0); // Pad with silence (zeros) padded_audio.resize(target_samples, 0.0); // Pad with silence (zeros)
Some(padded_audio) Some(padded_audio)

View file

@ -7,7 +7,6 @@ use whisper_rs::{
}; };
pub struct TranscriptionManager { pub struct TranscriptionManager {
context: WhisperContext,
state: Mutex<WhisperState>, state: Mutex<WhisperState>,
} }
@ -30,7 +29,6 @@ impl TranscriptionManager {
let state = context.create_state().expect("failed to create state"); let state = context.create_state().expect("failed to create state");
Ok(Self { Ok(Self {
context,
state: Mutex::new(state), state: Mutex::new(state),
}) })
} }