found the type needed to pass closures with local variables

master v0.1.1
Bel LaPointe 2023-12-19 21:39:53 -05:00
parent 03370f362e
commit 11b5091872
1 changed files with 13 additions and 13 deletions

View File

@ -39,7 +39,7 @@ fn main() {
flags.threads,
flags.stream_head,
flags.stream_tail,
|result| {
|result: Result<Whispered, String>| {
match result {
Ok(whispered) => {
eprintln!("{}: {:?}", chrono::Local::now(), whispered);
@ -104,10 +104,10 @@ struct WhisperService {
jobs: std::sync::mpsc::SyncSender<AWhisper>,
}
fn new_whisper_service(model_path: String, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: fn(Result<Whispered, String>)) -> Result<WhisperService, String> {
fn new_whisper_service<F>(model_path: String, threads: i32, stream_head: f32, stream_tail: f32, handler_fn: F) -> Result<WhisperService, String> where F: FnMut(Result<Whispered, String>) + 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<Whispered, String>),
handler_fn: Option<Box<dyn FnMut(Result<Whispered, String>) + Send + 'static>>
}
fn new_whisper_impl(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler_fn: fn(Result<Whispered, String>)) -> WhisperImpl {
fn new_whisper_impl<F>(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler_fn: F) -> WhisperImpl where F: FnMut(Result<Whispered, String>) + 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<AWhisper>) {
fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver<AWhisper>) {
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;