fix bugs on linux (mix two channels down to 1, build with vulkan)
This commit is contained in:
parent
ef607db260
commit
e307da8544
3 changed files with 36 additions and 6 deletions
|
|
@ -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"
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use std::thread;
|
||||||
use tauri::{App, Manager};
|
use tauri::{App, Manager};
|
||||||
use whisper_rs::install_whisper_log_trampoline;
|
use whisper_rs::install_whisper_log_trampoline;
|
||||||
use whisper_rs::{
|
use whisper_rs::{
|
||||||
|
|
@ -7,7 +8,6 @@ use whisper_rs::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct TranscriptionManager {
|
pub struct TranscriptionManager {
|
||||||
context: WhisperContext,
|
|
||||||
state: Mutex<WhisperState>,
|
state: Mutex<WhisperState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,7 +30,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),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -47,6 +46,10 @@ impl TranscriptionManager {
|
||||||
return Ok(result);
|
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();
|
let mut state = self.state.lock().unwrap();
|
||||||
// Initialize parameters
|
// Initialize parameters
|
||||||
let mut params = FullParams::new(SamplingStrategy::default());
|
let mut params = FullParams::new(SamplingStrategy::default());
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue