Fix up basic_use example

This commit is contained in:
Niko 2025-08-02 19:39:05 -07:00
parent e0b83e8248
commit 3d70621a51
No known key found for this signature in database

View file

@ -9,10 +9,10 @@ use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextPar
fn main() { fn main() {
let model_path = std::env::args() let model_path = std::env::args()
.nth(1) .nth(1)
.expect("Please specify path to model"); .expect("Please specify path to model as argument 1");
let wav_path = std::env::args() let wav_path = std::env::args()
.nth(2) .nth(2)
.expect("Please specify path to wav file"); .expect("Please specify path to wav file as argument 2");
let language = "en"; let language = "en";
let samples: Vec<i16> = hound::WavReader::open(wav_path) let samples: Vec<i16> = hound::WavReader::open(wav_path)
@ -27,7 +27,10 @@ fn main() {
let mut state = ctx.create_state().expect("failed to create state"); let mut state = ctx.create_state().expect("failed to create state");
let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 }); let mut params = FullParams::new(SamplingStrategy::BeamSearch {
beam_size: 5,
patience: -1.0,
});
// and set the language to translate to to english // and set the language to translate to to english
params.set_language(Some(&language)); params.set_language(Some(&language));
@ -42,7 +45,6 @@ fn main() {
// some utilities exist for this // some utilities exist for this
// note that you don't need to use these, you can do it yourself or any other way you want // note that you don't need to use these, you can do it yourself or any other way you want
// these are just provided for convenience // these are just provided for convenience
// SIMD variants of these functions are also available, but only on nightly Rust: see the docs
let mut inter_samples = vec![Default::default(); samples.len()]; let mut inter_samples = vec![Default::default(); samples.len()];
whisper_rs::convert_integer_to_float_audio(&samples, &mut inter_samples) whisper_rs::convert_integer_to_float_audio(&samples, &mut inter_samples)
@ -51,25 +53,17 @@ fn main() {
.expect("failed to convert audio data"); .expect("failed to convert audio data");
// now we can run the model // now we can run the model
// note the key we use here is the one we created above
state state
.full(params, &samples[..]) .full(params, &samples[..])
.expect("failed to run model"); .expect("failed to run model");
// fetch the results // fetch the results
let num_segments = state for segment in state.as_iter() {
.full_n_segments() println!(
.expect("failed to get number of segments"); "[{} - {}]: {}",
for i in 0..num_segments { segment.start_timestamp(),
let segment = state segment.end_timestamp(),
.full_get_segment_text(i) segment
.expect("failed to get segment"); );
let start_timestamp = state
.full_get_segment_t0(i)
.expect("failed to get segment start timestamp");
let end_timestamp = state
.full_get_segment_t1(i)
.expect("failed to get segment end timestamp");
println!("[{} - {}]: {}", start_timestamp, end_timestamp, segment);
} }
} }