diff --git a/src/main.rs b/src/main.rs index 0b3f624..81e8f37 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,7 @@ fn main() { flags.threads, flags.stream_head, flags.stream_tail, - |result| { + |result: Result| { match result { Ok(whispered) => { eprintln!("{}: {:?}", chrono::Local::now(), whispered); @@ -104,10 +104,10 @@ struct WhisperService { jobs: std::sync::mpsc::SyncSender, } -fn new_whisper_service(model_path: String, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: fn(Result)) -> Result { +fn new_whisper_service(model_path: String, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: F) -> Result where F: FnMut(Result) + Send + 'static { match new_whisper_engine(model_path, threads) { Ok(engine) => { - let whisper = new_whisper_impl(engine, stream_head, stream_tail, handler_fn); + let mut whisper = new_whisper_impl(engine, stream_head, stream_tail, handler_fn); let (send, recv) = std::sync::mpsc::sync_channel(100); thread::spawn(move || { whisper.transcribe_asyncs(recv); }); Ok(WhisperService{jobs: send}) @@ -142,20 +142,20 @@ struct WhisperImpl { engine: WhisperEngine, stream_head: f32, stream_tail: f32, - handler_fn: fn(Result), + handler_fn: Option) + Send + 'static>> } -fn new_whisper_impl(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler_fn: fn(Result)) -> WhisperImpl { +fn new_whisper_impl(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler_fn: F) -> WhisperImpl where F: FnMut(Result) + Send + 'static { WhisperImpl { engine: engine, stream_head: stream_head, stream_tail: stream_tail, - handler_fn: handler_fn, + handler_fn: Some(Box::new(handler_fn)), } } impl WhisperImpl { - fn transcribe_asyncs(&self, recv: std::sync::mpsc::Receiver) { + fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver) { loop { match recv.recv() { Ok(job) => { @@ -172,7 +172,7 @@ impl WhisperImpl { } } - fn transcribe(&self, a_whisper: &AWhisper) -> Result<(), ()> { + fn transcribe(&mut self, a_whisper: &AWhisper) -> Result<(), ()> { match self.engine.transcribe(&a_whisper.data) { Ok(result) => { self.on_success(&result); @@ -185,15 +185,15 @@ impl WhisperImpl { } } - fn on_success(&self, whispered: &Whispered) { + fn on_success(&mut self, whispered: &Whispered) { let result = whispered .after(&(self.stream_head * 100.0)) .before(&(self.stream_tail * 100.0)); - (self.handler_fn)(Ok(result)); + (self.handler_fn.as_mut().unwrap())(Ok(result)); } - fn on_error(&self, msg: String) { - (self.handler_fn)(Err(format!("failed to transcribe: {}", &msg))); + fn on_error(&mut self, msg: String) { + (self.handler_fn.as_mut().unwrap())(Err(format!("failed to transcribe: {}", &msg))); } } @@ -342,7 +342,7 @@ impl Listener { let downsample_ratio = cfg.channels() as f32 * (cfg.sample_rate().0 as f32 / 16_000.0); let stream = device.build_input_stream( &cfg.clone().into(), - move |data: &[f32], _: &cpal::InputCallbackInfo| { // TODO why cant i do this... + move |data: &[f32], _: &cpal::InputCallbackInfo| { let mut downsampled_data = vec![]; for i in 0..(data.len() as f32 / downsample_ratio) as usize { let mut upsampled = i as f32 * downsample_ratio;