Skip to content

Commit

Permalink
styled: format code with fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ckaznable committed May 22, 2023
1 parent 6a1f123 commit 0f0403f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
10 changes: 2 additions & 8 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,8 @@ pub fn resample_to_16k(input: &[f32], input_sample_rate: f64) -> Vec<f32> {
window: WindowFunction::BlackmanHarris2,
};

let mut resampler = SincFixedIn::<f32>::new(
16000. / input_sample_rate,
2.0,
params,
input.len(),
1,
)
.unwrap();
let mut resampler =
SincFixedIn::<f32>::new(16000. / input_sample_rate, 2.0, params, input.len(), 1).unwrap();

let waves_in = vec![input.to_vec()];
let mut output = resampler.process(&waves_in, None).unwrap();
Expand Down
21 changes: 14 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use audio::{YOUTUBE_TS_SAMPLE_RATE, resample_to_16k};
use audio::{resample_to_16k, YOUTUBE_TS_SAMPLE_RATE};
use clap::Parser;
use ringbuf::{Consumer, LocalRb, SharedRb, HeapRb, Producer, Rb};
use ringbuf::{Consumer, HeapRb, LocalRb, Producer, Rb, SharedRb};
use speech::{SpeechConfig, WhisperPayload};
use vad::{VadState, WINDOW_SIZE_SAMPLES, split_audio_data_with_window_size};
use std::{
error::Error,
ffi::c_int,
Expand All @@ -16,6 +15,7 @@ use std::{
thread::{self, JoinHandle},
time::Instant,
};
use vad::{split_audio_data_with_window_size, VadState, WINDOW_SIZE_SAMPLES};
use whisper_rs::WhisperContext;

use util::Log;
Expand All @@ -26,8 +26,10 @@ mod util;
mod vad;

type F32Consumer = Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>;
type SegmentProducer = Producer<vad::VadSegment, Arc<SharedRb<vad::VadSegment, Vec<MaybeUninit<vad::VadSegment>>>>>;
type SegmentConsumer = Consumer<vad::VadSegment, Arc<SharedRb<vad::VadSegment, Vec<MaybeUninit<vad::VadSegment>>>>>;
type SegmentProducer =
Producer<vad::VadSegment, Arc<SharedRb<vad::VadSegment, Vec<MaybeUninit<vad::VadSegment>>>>>;
type SegmentConsumer =
Consumer<vad::VadSegment, Arc<SharedRb<vad::VadSegment, Vec<MaybeUninit<vad::VadSegment>>>>>;

enum ThreadState {
End,
Expand Down Expand Up @@ -77,7 +79,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// shared buffer for vad output in 20 segment
let rb = HeapRb::<vad::VadSegment>::new(20);
let (vad_prod, vad_cons ) = rb.split();
let (vad_prod, vad_cons) = rb.split();

let (tx, rx) = mpsc::sync_channel::<ThreadState>(1);
let (vad_tx, vad_rx) = mpsc::sync_channel::<ThreadState>(1);
Expand Down Expand Up @@ -189,7 +191,12 @@ fn evoke_vad_thread(
data.chunks(WINDOW_SIZE_SAMPLES).for_each(|data| {
let _ = vad::vad(&mut vad_state, data.to_vec(), &mut buf);
});
logger.verbose(format!("vad process time: {}s, detect {} segment", running_calc.elapsed().as_secs(), buf.len()));

logger.verbose(format!(
"vad process time: {}s, detect {} segment",
running_calc.elapsed().as_secs(),
buf.len()
));

if !buf.is_empty() {
prod.push_iter(&mut buf.into_iter());
Expand Down
6 changes: 3 additions & 3 deletions src/vad.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{mem::MaybeUninit, rc::Rc, io::Cursor};
use std::{io::Cursor, mem::MaybeUninit, rc::Rc};

use ringbuf::{Consumer, LocalRb, Producer};
use tract_onnx::{
Expand Down Expand Up @@ -95,9 +95,9 @@ pub fn vad(
state.init();
buf.push(VadSegment {
data: state.rb_cons.pop_iter().collect_vec(),
duration: 15.
duration: 15.,
});
return Ok(())
return Ok(());
}

let pcm = Array::from_shape_vec((1, audio_data.len()), audio_data).unwrap();
Expand Down

0 comments on commit 0f0403f

Please sign in to comment.