Add comments to basic_use example and fix README example

This commit is contained in:
Niko 2025-08-13 01:40:43 -07:00
parent 9162a2c502
commit 68fabfc95d
No known key found for this signature in database
2 changed files with 26 additions and 19 deletions

View file

@ -27,7 +27,10 @@ fn main() {
).expect("failed to load model"); ).expect("failed to load model");
// create a params object // create a params object
let params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 }); let params = FullParams::new(SamplingStrategy::BeamSearch {
beam_size: 5,
patience: -1.0,
});
// 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
@ -40,20 +43,16 @@ fn main() {
.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 { // note start and end timestamps are in centiseconds
let segment = state // (10s of milliseconds)
.full_get_segment_text(i) segment.start_timestamp(),
.expect("failed to get segment"); segment.end_timestamp(),
let start_timestamp = state // the Display impl for WhisperSegment will replace invalid UTF-8 with the Unicode replacement character
.full_get_segment_t0(i) segment
.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);
} }
} }
``` ```

View file

@ -13,7 +13,6 @@ fn main() {
let wav_path = std::env::args() let wav_path = std::env::args()
.nth(2) .nth(2)
.expect("Please specify path to wav file as argument 2"); .expect("Please specify path to wav file as argument 2");
let language = "en";
let samples: Vec<i16> = hound::WavReader::open(wav_path) let samples: Vec<i16> = hound::WavReader::open(wav_path)
.unwrap() .unwrap()
@ -24,18 +23,24 @@ fn main() {
// load a context and model // load a context and model
let ctx = WhisperContext::new_with_params(&model_path, WhisperContextParameters::default()) let ctx = WhisperContext::new_with_params(&model_path, WhisperContextParameters::default())
.expect("failed to load model"); .expect("failed to load model");
// create a state attached to the model
let mut state = ctx.create_state().expect("failed to create state"); let mut state = ctx.create_state().expect("failed to create state");
// the sampling strategy will determine how accurate your final output is going to be
// typically BeamSearch is more accurate at the cost of significantly increased CPU time
let mut params = FullParams::new(SamplingStrategy::BeamSearch { let mut params = FullParams::new(SamplingStrategy::BeamSearch {
// whisper.cpp defaults to a beam size of 5, a reasonable default
beam_size: 5, beam_size: 5,
// this parameter is currently unused but defaults to -1.0
patience: -1.0, patience: -1.0,
}); });
// and set the language to translate to to english // and set the language to translate to as english
params.set_language(Some(&language)); params.set_language(Some("en"));
// we also explicitly disable anything that prints to stdout // we also explicitly disable anything that prints to stdout
// despite all of this you will still get things printing to stdout,
// be prepared to deal with it
params.set_print_special(false); params.set_print_special(false);
params.set_print_progress(false); params.set_print_progress(false);
params.set_print_realtime(false); params.set_print_realtime(false);
@ -61,8 +66,11 @@ fn main() {
for segment in state.as_iter() { for segment in state.as_iter() {
println!( println!(
"[{} - {}]: {}", "[{} - {}]: {}",
// these timestamps are in centiseconds (10s of milliseconds)
segment.start_timestamp(), segment.start_timestamp(),
segment.end_timestamp(), segment.end_timestamp(),
// this default Display implementation will result in any invalid UTF-8
// being converted into the Unicode replacement character, U+FFFD
segment segment
); );
} }