RUST RUNS WOO

master
Bel LaPointe 2023-03-31 10:10:59 -06:00
parent 2699d73c9c
commit 8a71756b89
3 changed files with 30 additions and 13 deletions

View File

@ -172,6 +172,12 @@ version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "riff"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9b1a3d5f46d53f4a3478e2be4a5a5ce5108ea58b100dcd139830eae7f79a3a1"
[[package]] [[package]]
name = "rustc-hash" name = "rustc-hash"
version = "1.1.0" version = "1.1.0"
@ -201,6 +207,15 @@ version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
[[package]]
name = "wav"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a65e199c799848b4f997072aa4d673c034f80f40191f97fe2f0a23f410be1609"
dependencies = [
"riff",
]
[[package]] [[package]]
name = "which" name = "which"
version = "4.4.0" version = "4.4.0"
@ -216,6 +231,7 @@ dependencies = [
name = "whisper-cpp-er" name = "whisper-cpp-er"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"wav",
"whisper-rs", "whisper-rs",
] ]

View File

@ -7,3 +7,4 @@ edition = "2021"
[dependencies] [dependencies]
whisper-rs = "0.5" whisper-rs = "0.5"
wav = "1"

View File

@ -1,26 +1,26 @@
use whisper_rs::{WhisperContext, FullParams, SamplingStrategy}; use whisper_rs::{WhisperContext, FullParams, SamplingStrategy};
use std::io::Read;
fn main() { fn main() {
let mut ctx = WhisperContext::new("../models/ggml-tiny.en.bin").expect("failed to load model"); let mut ctx = WhisperContext::new("../models/ggml-tiny.en.bin").expect("failed to load model");
// create a params object // create a params object
let params = FullParams::new(SamplingStrategy::Greedy { best_of: 0 }); let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 0 });
params.set_n_threads(4);
params.set_translate(false);
params.set_language(Some("en"));
params.set_print_special(false);
params.set_print_progress(false);
params.set_print_realtime(false);
params.set_print_timestamps(false);
// assume we have a buffer of audio data // assume we have a buffer of audio data
// here we'll make a fake one, floating point samples, 32 bit, 16KHz, mono // here we'll make a fake one, floating point samples, 32 bit, 16KHz, mono
//let audio_data = vec![0_f32; 16000 * 2]; //let audio_data = vec![0_f32; 16000 * 2];
let mut audio_data = Vec::new(); let (header, data) = wav::read(&mut std::fs::File::open("../git.d/samples/jfk.wav").expect("failed to open .wav")).expect("failed to decode .wav");
let mut input = std::io::BufReader::new(std::fs::File::open("../git.d/samples/jfk.wav").expect("cannot open jfk.wav")); assert!(header.channel_count == 1);
loop { assert!(header.sampling_rate == 16000);
let mut buff = [0u8; std::mem::size_of::<f32>()]; let data16 = data.as_sixteen().expect("wav is not 32bit floats");
let res = input.read_exact(&mut buff); let audio_data = &whisper_rs::convert_integer_to_float_audio(&data16);
match res {
Err(_) => break,
_ => {},
};
audio_data.push(f32::from_le_bytes(buff));
}
// now we can run the model // now we can run the model
ctx.full(params, &audio_data[..]) ctx.full(params, &audio_data[..])