fix bugs on linux (mix two channels down to 1, build with vulkan)

This commit is contained in:
CJ Pais 2025-05-15 13:00:16 -07:00
parent ef607db260
commit e307da8544
3 changed files with 36 additions and 6 deletions

View file

@ -42,6 +42,12 @@ tauri-plugin-store = "2"
[target.'cfg(target_os = "macos")'.dependencies]
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]
tauri-plugin-autostart = "2"
tauri-plugin-global-shortcut = "2"

View file

@ -12,6 +12,7 @@ pub enum RecordingState {
Recording { binding_id: String },
}
#[derive(Clone)]
pub struct AudioRecordingManager {
state: Arc<Mutex<RecordingState>>,
buffer: Arc<Mutex<Vec<f32>>>,
@ -84,6 +85,7 @@ impl AudioRecordingManager {
// Generic function to process audio data
fn process_audio<T: SampleToF32 + Send + 'static>(
data: &[T],
channels: usize,
state_clone: Arc<Mutex<RecordingState>>,
temp_buffer_clone: Arc<Mutex<Vec<f32>>>,
resampler_clone: Arc<Mutex<FftFixedIn<f32>>>,
@ -95,9 +97,20 @@ impl AudioRecordingManager {
if let RecordingState::Recording { .. } = *state_guard {
let mut temp_buffer = temp_buffer_clone.lock().unwrap();
// Convert incoming data to f32
let f32_data: Vec<f32> = data.iter().map(|sample| sample.to_f32()).collect();
temp_buffer.extend_from_slice(&f32_data);
// Handle multichannel audio by mixing down to mono
if channels > 1 {
// 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
while temp_buffer.len() >= 1024 {
@ -142,12 +155,17 @@ impl AudioRecordingManager {
let err_fn = |err| eprintln!("Error in stream: {}", err);
// 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() {
SampleFormat::I8 => device.build_input_stream(
&config.into(),
move |data: &[i8], _| {
process_audio(
data,
channels,
Arc::clone(&state_clone),
Arc::clone(&temp_buffer_clone),
Arc::clone(&resampler_clone),
@ -164,6 +182,7 @@ impl AudioRecordingManager {
move |data: &[i16], _| {
process_audio(
data,
channels,
Arc::clone(&state_clone),
Arc::clone(&temp_buffer_clone),
Arc::clone(&resampler_clone),
@ -180,6 +199,7 @@ impl AudioRecordingManager {
move |data: &[i32], _| {
process_audio(
data,
channels,
Arc::clone(&state_clone),
Arc::clone(&temp_buffer_clone),
Arc::clone(&resampler_clone),
@ -196,6 +216,7 @@ impl AudioRecordingManager {
move |data: &[f32], _| {
process_audio(
data,
channels,
Arc::clone(&state_clone),
Arc::clone(&temp_buffer_clone),
Arc::clone(&resampler_clone),
@ -268,7 +289,7 @@ impl AudioRecordingManager {
} else {
// Pad to minimum 1000ms if needed
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;
padded_audio.resize(target_samples, 0.0); // Pad with silence (zeros)
Some(padded_audio)

View file

@ -1,5 +1,6 @@
use anyhow::Result;
use std::sync::Mutex;
use std::thread;
use tauri::{App, Manager};
use whisper_rs::install_whisper_log_trampoline;
use whisper_rs::{
@ -7,7 +8,6 @@ use whisper_rs::{
};
pub struct TranscriptionManager {
context: WhisperContext,
state: Mutex<WhisperState>,
}
@ -30,7 +30,6 @@ impl TranscriptionManager {
let state = context.create_state().expect("failed to create state");
Ok(Self {
context,
state: Mutex::new(state),
})
}
@ -47,6 +46,10 @@ impl TranscriptionManager {
return Ok(result);
}
let num_threads = thread::available_parallelism()
.map(|count| count.get() as i32)
.unwrap_or(1);
let mut state = self.state.lock().unwrap();
// Initialize parameters
let mut params = FullParams::new(SamplingStrategy::default());