Skip to content

Commit

Permalink
Add in a test for speech duration (#19)
Browse files Browse the repository at this point in the history
* Add in a test for speech duration

* Try to fix test

* Fix test and add a comment to clear up my own confusion
  • Loading branch information
xd009642 authored Aug 29, 2024
1 parent 00fccb2 commit 494d637
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ pub struct VadSession {
session_audio: Vec<f32>,
processed_samples: usize,
silent_samples: usize,
/// Current start of the speech in milliseconds
speech_start: Option<usize>,
/// Current end of the speech in milliseconds
speech_end: Option<usize>,
}

Expand Down Expand Up @@ -396,4 +398,29 @@ mod tests {
let bytes = std::fs::read("models/silero_vad.onnx").unwrap();
assert!(VadSession::new_from_bytes(&bytes, config).is_err());
}

/// Just a sanity test of speech duration to make sure the calculation seems roughly right in
/// terms of number of samples, sample rate and taking into account the speech starts/ends.
#[test]
fn simple_speech_duration() {
let mut config = VadConfig::default();
config.sample_rate = 8000;
let mut session = VadSession::new(config.clone()).unwrap();
session.session_audio.resize(16080, 0.0);

assert_eq!(session.current_speech_duration(), Duration::from_secs(0));

session.speech_start = Some(10);
assert_eq!(session.current_speech_duration(), Duration::from_secs(2));

session.speech_end = Some(1010);
assert_eq!(session.current_speech_duration(), Duration::from_secs(1));

session.config.sample_rate = 16000;
session.speech_end = Some(510);
assert_eq!(
session.current_speech_duration(),
Duration::from_millis(500)
);
}
}

0 comments on commit 494d637

Please sign in to comment.