wait i just needed an option? f off
parent
367838ac23
commit
5f47b2c88b
27
src/main.rs
27
src/main.rs
|
|
@ -63,7 +63,10 @@ fn main() {
|
|||
new_listener().listen(move |data: Vec<f32>| {
|
||||
data.iter().for_each(|x| buffer.push(*x));
|
||||
if Instant::now() - last > stream_step {
|
||||
match w.transcribe_async(&buffer) {
|
||||
//match w.transcribe_async(&buffer) {
|
||||
match w.transcribe_callback(&buffer, |x| {
|
||||
println!("CALLBACK: {:?}", &x);
|
||||
}) {
|
||||
Ok(_) => (),
|
||||
Err(msg) => eprintln!("{}", msg),
|
||||
};
|
||||
|
|
@ -117,18 +120,23 @@ fn new_whisper(model_path: String, threads: i32, handler: Handler) -> Result<Whi
|
|||
impl Whisper {
|
||||
fn transcribe(&self, data: &Vec<f32>) {
|
||||
let (send, recv) = std::sync::mpsc::sync_channel(0);
|
||||
self._transcribe_async(data, Some(send)).unwrap();
|
||||
self._transcribe_async(data, Some(send), None).unwrap();
|
||||
recv.recv().unwrap();
|
||||
}
|
||||
|
||||
fn transcribe_async(&self, data: &Vec<f32>) -> Result<(), String> {
|
||||
self._transcribe_async(data, None)
|
||||
fn transcribe_callback(&self, data: &Vec<f32>, callback: fn(&Whispered)) -> Result<(), String> {
|
||||
self._transcribe_async(data, None, Some(callback))
|
||||
}
|
||||
|
||||
fn _transcribe_async(&self, data: &Vec<f32>, ack: Option<std::sync::mpsc::SyncSender<bool>>) -> Result<(), String> {
|
||||
fn transcribe_async(&self, data: &Vec<f32>) -> Result<(), String> {
|
||||
self._transcribe_async(data, None, None)
|
||||
}
|
||||
|
||||
fn _transcribe_async(&self, data: &Vec<f32>, ack: Option<std::sync::mpsc::SyncSender<bool>>, callback: Option<fn(&Whispered)>) -> Result<(), String> {
|
||||
match self.jobs.try_send(AWhisper{
|
||||
data: data.clone().to_vec(),
|
||||
ack: ack,
|
||||
callback: callback,
|
||||
}) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(msg) => Err(format!("failed to enqueue transcription: {}", msg)),
|
||||
|
|
@ -150,11 +158,15 @@ impl WhisperEngine {
|
|||
Ok(job) => {
|
||||
match self.transcribe(&job.data) {
|
||||
Ok(result) => {
|
||||
self.handler.on_success(result);
|
||||
self.handler.on_success(&result);
|
||||
match job.ack {
|
||||
Some(ack) => { let _ = ack.send(true); },
|
||||
None => (),
|
||||
};
|
||||
match job.callback {
|
||||
Some(foo) => foo(&result),
|
||||
None => (),
|
||||
};
|
||||
},
|
||||
Err(msg) => {
|
||||
self.handler.on_error(format!("failed to transcribe: {}", msg));
|
||||
|
|
@ -201,6 +213,7 @@ impl WhisperEngine {
|
|||
struct AWhisper {
|
||||
data: Vec<f32>,
|
||||
ack: Option<std::sync::mpsc::SyncSender<bool>>,
|
||||
callback: Option<fn(&Whispered)>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
|
@ -276,7 +289,7 @@ struct Handler {
|
|||
}
|
||||
|
||||
impl Handler {
|
||||
fn on_success(&self, result: Whispered) {
|
||||
fn on_success(&self, result: &Whispered) {
|
||||
eprintln!("{}: {:?}", chrono::Local::now(), &result);
|
||||
println!("{}", result
|
||||
.after(&(self.head * 100.0))
|
||||
|
|
|
|||
Loading…
Reference in New Issue