From 4ef419e6c0bf2bbe5c62fa3bfc1704da6df46489 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 28 Nov 2023 21:22:26 -0700 Subject: [PATCH] successfully confirmed audio is k with sox -r 44100 -t f32 /tmp/transcribed.pcm --default-device --- rust-whisper.d/Cargo.lock | 7 +++++++ rust-whisper.d/Cargo.toml | 1 + rust-whisper.d/src/main.rs | 13 ++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/rust-whisper.d/Cargo.lock b/rust-whisper.d/Cargo.lock index e83b151..5afae11 100644 --- a/rust-whisper.d/Cargo.lock +++ b/rust-whisper.d/Cargo.lock @@ -130,6 +130,12 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.5.0" @@ -713,6 +719,7 @@ checksum = "b9b1a3d5f46d53f4a3478e2be4a5a5ce5108ea58b100dcd139830eae7f79a3a1" name = "rust-whisper" version = "0.1.0" dependencies = [ + "byteorder", "cpal", "signal-hook", "tokio", diff --git a/rust-whisper.d/Cargo.toml b/rust-whisper.d/Cargo.toml index 57c778a..3bc164f 100644 --- a/rust-whisper.d/Cargo.toml +++ b/rust-whisper.d/Cargo.toml @@ -11,3 +11,4 @@ wav = "1" tokio = "1.27" cpal = "0.15.2" signal-hook = "0.3.17" +byteorder = "1.5.0" diff --git a/rust-whisper.d/src/main.rs b/rust-whisper.d/src/main.rs index 72d5dd2..1c26795 100644 --- a/rust-whisper.d/src/main.rs +++ b/rust-whisper.d/src/main.rs @@ -2,6 +2,9 @@ use whisper_rs::{WhisperContext, FullParams, SamplingStrategy, WhisperError}; use cpal::traits::{HostTrait, DeviceTrait, StreamTrait}; use signal_hook::{iterator::Signals, consts::signal::SIGINT}; use std::time::{Duration, Instant}; +use std::fs; +use std::io::Write; +use byteorder::WriteBytesExt; fn main() { let w = new_whisper( @@ -59,9 +62,9 @@ fn main() { eprintln!("output error: {}", err) }, None, - ).unwrap().play().unwrap(); + ); //.unwrap().play().unwrap(); let stream = device.build_input_stream( - &cfg.into(), + &cfg.clone().into(), move |data: &[f32], _: &cpal::InputCallbackInfo| { data.iter() .map(|x| *x) @@ -69,7 +72,11 @@ fn main() { .for_each(|x| buffer.push(x)); if Instant::now() - last > five_seconds { let result = w.transcribe(&buffer).unwrap(); - println!("({}) {}", buffer.len(), result); + println!("({} from {:?}) {}", buffer.len(), cfg, result); + let mut f = fs::File::create("/tmp/transcribed.pcm").unwrap(); + for i in &buffer { + f.write_f32::(*i).unwrap(); + } let retain = buffer.len() - buffer.len() / 10; for i in retain..buffer.len() {