dumb callbacks work

master
Bel LaPointe 2023-12-19 21:18:04 -05:00
parent 839487b99e
commit 1b96b132e1
1 changed files with 17 additions and 9 deletions

View File

@ -40,6 +40,12 @@ fn main() {
flags.stream_head,
flags.stream_tail,
new_handler(),
|result| {
match result {
Ok(whispered) => { println!("{}", whispered.to_string()); },
Err(msg) => { eprintln!("Error whispering: {}", msg); },
};
},
).unwrap();
let stream_retain = (flags.stream_retain * 16_000.0) as usize;
let stream_step = Duration::new(flags.stream_step, 0);
@ -96,10 +102,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: Handler) -> Result<WhisperService, String> {
fn new_whisper_service(model_path: String, threads: i32, stream_head: f32, stream_tail: f32, handler: Handler, handler_fn: fn(Result<&Whispered, String>)) -> Result<WhisperService, String> {
match new_whisper_engine(model_path, threads) {
Ok(engine) => {
let whisper = new_whisper_impl(engine, stream_head, stream_tail, handler);
let whisper = new_whisper_impl(engine, stream_head, stream_tail, handler, handler_fn);
let (send, recv) = std::sync::mpsc::sync_channel(100);
thread::spawn(move || { whisper.transcribe_asyncs(recv); });
Ok(WhisperService{jobs: send})
@ -135,14 +141,16 @@ struct WhisperImpl {
stream_head: f32,
stream_tail: f32,
handler: Handler,
handler_fn: fn(Result<&Whispered, String>),
}
fn new_whisper_impl(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler: Handler) -> WhisperImpl {
fn new_whisper_impl(engine: WhisperEngine, stream_head: f32, stream_tail: f32, handler: Handler, handler_fn: fn(Result<&Whispered, String>)) -> WhisperImpl {
WhisperImpl {
engine: engine,
stream_head: stream_head,
stream_tail: stream_tail,
handler: handler,
handler_fn: handler_fn,
}
}
@ -178,16 +186,16 @@ impl WhisperImpl {
}
fn on_success(&self, whispered: &Whispered) {
eprintln!("{}: {:?}", chrono::Local::now(), whispered);
self.handler.on_success(
&whispered
let result = whispered
.after(&(self.stream_head * 100.0))
.before(&(self.stream_tail * 100.0)),
);
.before(&(self.stream_tail * 100.0));
(self.handler_fn)(Ok(&result));
self.handler.on_success(&result);
}
fn on_error(&self, msg: String) {
self.handler.on_error(format!("failed to transcribe: {}", &msg));
(self.handler_fn)(Err(format!("failed to transcribe: {}", &msg)));
}
}