From 2936fec1e4df7fb1ee12cfc6c7ba9fda5ccf893f Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Wed, 29 Nov 2023 05:33:27 -0700 Subject: [PATCH] dont need to choose 1 channel since downsampling should randomly choose from all --- rust-whisper.d/src/main.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/rust-whisper.d/src/main.rs b/rust-whisper.d/src/main.rs index 776dc75..379fe74 100644 --- a/rust-whisper.d/src/main.rs +++ b/rust-whisper.d/src/main.rs @@ -50,21 +50,19 @@ fn main() { .unwrap() .with_max_sample_rate(); - let channels = cfg.channels(); - let downsample_ratio = cfg.sample_rate().0 as f32 / 16000.0; + let downsample_ratio = cfg.channels() as f32 * (cfg.sample_rate().0 as f32 / 16000.0); let mut buffer = vec![]; let mut last = Instant::now(); let stream = device.build_input_stream( &cfg.clone().into(), move |data: &[f32], _: &cpal::InputCallbackInfo| { - let mono_data: Vec = data.iter().map(|x| *x).step_by(channels.into()).collect(); let mut downsampled_data = vec![]; - for i in 0..(mono_data.len() as f32 / downsample_ratio) as usize { + for i in 0..(data.len() as f32 / downsample_ratio) as usize { let mut upsampled = i as f32 * downsample_ratio; - if upsampled > (mono_data.len()-1) as f32 { - upsampled = (mono_data.len()-1) as f32 + if upsampled > (data.len()-1) as f32 { + upsampled = (data.len()-1) as f32 } - downsampled_data.push(mono_data[upsampled as usize]); + downsampled_data.push(data[upsampled as usize]); } downsampled_data.iter().for_each(|x| buffer.push(*x)); if Instant::now() - last > stream_step {