parent
03370f362e
commit
11b5091872
26
src/main.rs
26
src/main.rs
|
|
@ -39,7 +39,7 @@ fn main() {
|
||||||
flags.threads,
|
flags.threads,
|
||||||
flags.stream_head,
|
flags.stream_head,
|
||||||
flags.stream_tail,
|
flags.stream_tail,
|
||||||
|result| {
|
|result: Result<Whispered, String>| {
|
||||||
match result {
|
match result {
|
||||||
Ok(whispered) => {
|
Ok(whispered) => {
|
||||||
eprintln!("{}: {:?}", chrono::Local::now(), whispered);
|
eprintln!("{}: {:?}", chrono::Local::now(), whispered);
|
||||||
|
|
@ -104,10 +104,10 @@ struct WhisperService {
|
||||||
jobs: std::sync::mpsc::SyncSender<AWhisper>,
|
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) {
|
match new_whisper_engine(model_path, threads) {
|
||||||
Ok(engine) => {
|
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);
|
let (send, recv) = std::sync::mpsc::sync_channel(100);
|
||||||
thread::spawn(move || { whisper.transcribe_asyncs(recv); });
|
thread::spawn(move || { whisper.transcribe_asyncs(recv); });
|
||||||
Ok(WhisperService{jobs: send})
|
Ok(WhisperService{jobs: send})
|
||||||
|
|
@ -142,20 +142,20 @@ struct WhisperImpl {
|
||||||
engine: WhisperEngine,
|
engine: WhisperEngine,
|
||||||
stream_head: f32,
|
stream_head: f32,
|
||||||
stream_tail: 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 {
|
WhisperImpl {
|
||||||
engine: engine,
|
engine: engine,
|
||||||
stream_head: stream_head,
|
stream_head: stream_head,
|
||||||
stream_tail: stream_tail,
|
stream_tail: stream_tail,
|
||||||
handler_fn: handler_fn,
|
handler_fn: Some(Box::new(handler_fn)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WhisperImpl {
|
impl WhisperImpl {
|
||||||
fn transcribe_asyncs(&self, recv: std::sync::mpsc::Receiver<AWhisper>) {
|
fn transcribe_asyncs(&mut self, recv: std::sync::mpsc::Receiver<AWhisper>) {
|
||||||
loop {
|
loop {
|
||||||
match recv.recv() {
|
match recv.recv() {
|
||||||
Ok(job) => {
|
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) {
|
match self.engine.transcribe(&a_whisper.data) {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
self.on_success(&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
|
let result = whispered
|
||||||
.after(&(self.stream_head * 100.0))
|
.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_fn.as_mut().unwrap())(Ok(result));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_error(&self, msg: String) {
|
fn on_error(&mut self, msg: String) {
|
||||||
(self.handler_fn)(Err(format!("failed to transcribe: {}", &msg)));
|
(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 downsample_ratio = cfg.channels() as f32 * (cfg.sample_rate().0 as f32 / 16_000.0);
|
||||||
let stream = device.build_input_stream(
|
let stream = device.build_input_stream(
|
||||||
&cfg.clone().into(),
|
&cfg.clone().into(),
|
||||||
move |data: &[f32], _: &cpal::InputCallbackInfo| { // TODO why cant i do this...
|
move |data: &[f32], _: &cpal::InputCallbackInfo| {
|
||||||
let mut downsampled_data = vec![];
|
let mut downsampled_data = vec![];
|
||||||
for i in 0..(data.len() as f32 / downsample_ratio) as usize {
|
for i in 0..(data.len() as f32 / downsample_ratio) as usize {
|
||||||
let mut upsampled = i as f32 * downsample_ratio;
|
let mut upsampled = i as f32 * downsample_ratio;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue