use crate::{
Result,
backend::VadBackend,
options::SpeechOptions,
run::{Run, RunSegmenter},
};
pub type SpeechSegment = Run;
pub type SpeechSegmenter = RunSegmenter;
pub type SpeechDetector = SpeechSegmenter;
pub fn detect_speech_with<B: VadBackend>(
backend: &mut B,
samples: &[f32],
options: SpeechOptions,
) -> Result<Vec<SpeechSegment>> {
let hop = backend.frame_hop();
assert!(hop != 0, "VadBackend::frame_hop() must be non-zero");
let mut segmenter = SpeechSegmenter::new(options);
segmenter.set_sample_rate(backend.sample_rate());
segmenter.set_frame_hop(hop);
let mut segments = Vec::new();
backend
.push(samples, &mut |probability| {
if let Some(segment) = segmenter.push_probability(probability) {
segments.push(segment);
}
})
.map_err(Into::into)?;
backend
.finish(&mut |probability| {
if let Some(segment) = segmenter.push_probability(probability) {
segments.push(segment);
}
})
.map_err(Into::into)?;
if let Some(segment) = segmenter.finish() {
segments.push(segment);
}
Ok(segments)
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use crate::{SampleRate, SpeechOptions, VadBackend};
use super::{SpeechSegment, SpeechSegmenter, detect_speech_with};
fn frame_count(duration_ms: u32, sample_rate: SampleRate) -> usize {
let frame_ms = (sample_rate.chunk_samples() as u32 * 1_000) / sample_rate.hz();
(duration_ms / frame_ms) as usize
}
fn collect(segmenter: &mut SpeechSegmenter, probabilities: &[f32]) -> Vec<SpeechSegment> {
let mut segments = Vec::new();
for probability in probabilities {
if let Some(segment) = segmenter.push_probability(*probability) {
segments.push(segment);
}
}
if let Some(segment) = segmenter.finish() {
segments.push(segment);
}
segments
}
#[test]
fn closes_segment_after_confirmed_silence() {
let config = SpeechOptions::default();
let mut segmenter = SpeechSegmenter::new(config.clone());
let mut probabilities = vec![0.9; frame_count(320, SampleRate::Rate16k)];
probabilities.extend(vec![0.0; frame_count(128, SampleRate::Rate16k)]);
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(segments.len(), 1);
assert!(segments[0].start_sample() <= config.speech_pad_samples());
assert!(segments[0].sample_count() >= config.min_speech_samples());
}
#[test]
fn drops_short_bursts() {
let config = SpeechOptions::default();
let mut segmenter = SpeechSegmenter::new(config.clone());
let mut probabilities = vec![0.9; frame_count(64, SampleRate::Rate16k)];
probabilities.extend(vec![0.0; frame_count(160, SampleRate::Rate16k)]);
let segments = collect(&mut segmenter, &probabilities);
assert!(segments.is_empty());
}
#[test]
fn middle_band_frames_do_not_reset_tentative_end() {
let config = SpeechOptions::default()
.with_min_speech_duration(Duration::ZERO)
.with_speech_pad(Duration::ZERO)
.with_min_silence_duration(Duration::from_millis(100));
let mut segmenter = SpeechSegmenter::new(config);
let mut probabilities = vec![0.9; 4];
probabilities.extend([0.0, 0.4, 0.0, 0.0, 0.0]);
probabilities.extend(vec![0.9; 4]);
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(segments.len(), 2);
assert_eq!(segments[0].start_sample(), 0);
assert_eq!(segments[0].end_sample(), 2_048);
assert_eq!(segments[1].start_sample(), 4_608);
}
#[test]
fn min_speech_duration_is_checked_before_padding() {
let config = SpeechOptions::default();
let mut segmenter = SpeechSegmenter::new(config);
let mut probabilities = vec![0.0; 4];
probabilities.extend(vec![0.9; 6]);
probabilities.extend(vec![0.0; 5]);
let segments = collect(&mut segmenter, &probabilities);
assert!(segments.is_empty());
}
#[test]
fn finish_flushes_trailing_active_segment() {
let config = SpeechOptions::default();
let mut segmenter = SpeechSegmenter::new(config);
let probabilities = vec![0.9; frame_count(320, SampleRate::Rate16k)];
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(segments.len(), 1);
assert!(segments[0].end_sample() > segments[0].start_sample());
}
#[test]
fn reset_clears_runtime_state() {
let mut segmenter = SpeechSegmenter::new(SpeechOptions::default());
let _ = segmenter.push_probability(0.9);
assert!(segmenter.is_active());
segmenter.reset();
assert!(!segmenter.is_active());
}
#[test]
fn set_sample_rate_resets_runtime_state_and_updates_timeline_rate() {
let mut segmenter = SpeechSegmenter::new(SpeechOptions::default());
let _ = segmenter.push_probability(0.9);
assert!(segmenter.is_active());
segmenter.set_sample_rate(SampleRate::Rate8k);
assert_eq!(segmenter.sample_rate(), SampleRate::Rate8k);
assert!(!segmenter.is_active());
for _ in 0..frame_count(320, SampleRate::Rate8k) {
let _ = segmenter.push_probability(0.9);
}
let segment = segmenter.finish().expect("trailing segment");
assert_eq!(segment.sample_rate(), SampleRate::Rate8k);
}
#[test]
fn force_splits_long_speech_when_max_duration_is_reached() {
let config = SpeechOptions::default()
.with_min_speech_duration(Duration::ZERO)
.with_speech_pad(Duration::ZERO)
.with_max_speech_duration(Duration::from_millis(160));
let mut segmenter = SpeechSegmenter::new(config);
let probabilities = vec![0.9; 8];
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(segments.len(), 2);
assert_eq!(segments[0].start_sample(), 0);
assert_eq!(segments[0].end_sample(), 2_560);
assert_eq!(segments[1].start_sample(), 2_560);
assert_eq!(segments[1].end_sample(), 4_096);
}
#[test]
fn prefers_recorded_silence_when_splitting_long_speech() {
let config = SpeechOptions::default()
.with_min_speech_duration(Duration::ZERO)
.with_speech_pad(Duration::ZERO)
.with_min_silence_duration(Duration::from_millis(300))
.with_min_silence_at_max_speech(Duration::from_millis(64))
.with_max_speech_duration(Duration::from_millis(256));
let mut segmenter = SpeechSegmenter::new(config);
let mut probabilities = vec![0.9; 4];
probabilities.extend(vec![0.0; 4]);
probabilities.extend(vec![0.9; 4]);
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(segments.len(), 2);
assert_eq!(segments[0].start_sample(), 0);
assert_eq!(segments[0].end_sample(), 2_048);
assert_eq!(segments[1].start_sample(), 4_096);
assert_eq!(segments[1].end_sample(), 6_144);
}
#[test]
fn non_qualifying_silence_does_not_overwrite_next_start() {
let config = SpeechOptions::default()
.with_min_speech_duration(Duration::ZERO)
.with_speech_pad(Duration::ZERO)
.with_min_silence_duration(Duration::from_millis(10_000))
.with_min_silence_at_max_speech(Duration::from_millis(64))
.with_max_speech_duration(Duration::from_millis(512));
let mut segmenter = SpeechSegmenter::new(config);
let mut probabilities = vec![0.9; 4];
probabilities.extend(vec![0.0; 4]);
probabilities.extend(vec![0.9; 4]);
probabilities.extend(vec![0.0; 1]);
probabilities.extend(vec![0.9; 20]);
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(segments[0].end_sample(), 2_048);
assert_eq!(segments[1].start_sample(), 4_096);
}
#[test]
fn force_split_during_silence_closes_without_restarting() {
let config = SpeechOptions::default()
.with_min_speech_duration(Duration::ZERO)
.with_speech_pad(Duration::ZERO)
.with_min_silence_duration(Duration::from_millis(10_000))
.with_min_silence_at_max_speech(Duration::from_millis(64))
.with_max_speech_duration(Duration::from_millis(256));
let mut segmenter = SpeechSegmenter::new(config);
let mut probabilities = vec![0.9; 4];
probabilities.extend(vec![0.0; 8]);
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].start_sample(), 0);
assert_eq!(segments[0].end_sample(), 2_048);
}
#[test]
fn four_frame_silence_dip_does_not_close_segment_at_default_min_silence() {
let config = SpeechOptions::default();
let mut segmenter = SpeechSegmenter::new(config.clone());
let mut probabilities = vec![1.0; 30];
probabilities.extend(vec![0.0; 4]);
probabilities.extend(vec![1.0; 30]);
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(
segments.len(),
1,
"4-frame silence dip must be tolerated at default min_silence_duration_ms = 100; \
got {} segments",
segments.len()
);
assert_eq!(segments[0].start_sample(), 0);
assert_eq!(segments[0].end_sample(), 32_768);
}
#[test]
fn five_frame_silence_dip_closes_segment_at_default_min_silence() {
let config = SpeechOptions::default();
let mut segmenter = SpeechSegmenter::new(config);
let mut probabilities = vec![1.0; 30];
probabilities.extend(vec![0.0; 5]);
probabilities.extend(vec![1.0; 30]);
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(
segments.len(),
2,
"5-frame silence dip must close the segment at default \
min_silence_duration_ms = 100; got {} segments",
segments.len()
);
}
#[test]
fn force_split_applies_speech_pad_to_split_boundaries() {
let config = SpeechOptions::default()
.with_min_speech_duration(Duration::ZERO)
.with_speech_pad(Duration::from_millis(32))
.with_min_silence_duration(Duration::from_millis(10_000))
.with_min_silence_at_max_speech(Duration::from_millis(64))
.with_max_speech_duration(Duration::from_millis(512));
let mut segmenter = SpeechSegmenter::new(config);
let mut probabilities = vec![0.9; 4];
probabilities.extend(vec![0.0; 4]);
probabilities.extend(vec![0.9; 8]);
let segments = collect(&mut segmenter, &probabilities);
assert_eq!(segments[0].end_sample(), 2_560);
assert_eq!(segments[1].start_sample(), 3_584);
}
#[test]
fn finish_preserves_undrained_queued_segments() {
let config = SpeechOptions::default();
let mut segmenter = SpeechSegmenter::new(config);
let queued_a = SpeechSegment::new(0, 1_000, segmenter.sample_rate());
let queued_b = SpeechSegment::new(2_000, 3_000, segmenter.sample_rate());
segmenter.pending_segments.push_back(queued_a);
segmenter.pending_segments.push_back(queued_b);
assert_eq!(segmenter.finish(), Some(queued_a));
assert_eq!(segmenter.pending_segment_count(), 1);
assert_eq!(segmenter.finish(), Some(queued_b));
assert_eq!(segmenter.pending_segment_count(), 0);
assert_eq!(segmenter.finish(), None);
assert!(!segmenter.is_active());
}
#[derive(Debug)]
struct MockError(&'static str);
impl std::fmt::Display for MockError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0)
}
}
impl std::error::Error for MockError {}
impl From<MockError> for crate::Error {
fn from(error: MockError) -> Self {
crate::Error::Backend(Box::new(error))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EofPolicy {
Pad,
Snip,
}
struct MockBackend {
window: usize,
hop: usize,
sample_rate: SampleRate,
eof: EofPolicy,
tail: Vec<f32>,
probabilities: Vec<f32>,
cursor: usize,
fail_at: Option<usize>,
}
impl MockBackend {
fn new(window: usize, hop: usize, eof: EofPolicy, probabilities: Vec<f32>) -> Self {
assert!(window != 0 && hop != 0 && window >= hop);
Self {
window,
hop,
sample_rate: SampleRate::Rate16k,
eof,
tail: Vec::new(),
probabilities,
cursor: 0,
fail_at: None,
}
}
fn silero_like(frame: usize, probabilities: Vec<f32>) -> Self {
Self::new(frame, frame, EofPolicy::Pad, probabilities)
}
fn with_sample_rate(mut self, sample_rate: SampleRate) -> Self {
self.sample_rate = sample_rate;
self
}
fn with_fail_at(mut self, cursor: usize) -> Self {
self.fail_at = Some(cursor);
self
}
fn emit(&mut self, sink: &mut dyn FnMut(f32)) -> Result<(), MockError> {
if self.fail_at == Some(self.cursor) {
return Err(MockError("mock predict failure"));
}
let probability = self.probabilities.get(self.cursor).copied().unwrap_or(0.0);
self.cursor += 1;
sink(probability);
Ok(())
}
}
impl VadBackend for MockBackend {
type Error = MockError;
fn frame_hop(&self) -> usize {
self.hop
}
fn sample_rate(&self) -> SampleRate {
self.sample_rate
}
fn push(&mut self, samples: &[f32], sink: &mut dyn FnMut(f32)) -> Result<(), MockError> {
self.tail.extend_from_slice(samples);
while self.tail.len() >= self.window {
self.emit(sink)?;
self.tail.drain(..self.hop);
}
Ok(())
}
fn finish(&mut self, sink: &mut dyn FnMut(f32)) -> Result<(), MockError> {
match self.eof {
EofPolicy::Pad if !self.tail.is_empty() => {
self.tail.resize(self.window, 0.0);
self.emit(sink)?;
self.tail.clear();
}
_ => self.tail.clear(),
}
Ok(())
}
fn reset(&mut self) {
self.tail.clear();
self.cursor = 0;
}
}
#[test]
fn set_frame_hop_overrides_the_sample_rate_default() {
let mut segmenter = SpeechSegmenter::new(SpeechOptions::default());
assert_eq!(segmenter.frame_hop(), 512);
segmenter.set_frame_hop(4096);
assert_eq!(segmenter.frame_hop(), 4096);
}
#[test]
fn detect_speech_with_uses_backend_sample_rate_not_options() {
let mut backend =
MockBackend::silero_like(256, vec![0.9; 10]).with_sample_rate(SampleRate::Rate8k);
let samples = vec![0.0_f32; 10 * 256];
let segments =
detect_speech_with(&mut backend, &samples, SpeechOptions::default()).expect("detect");
assert_eq!(
segments.len(),
1,
"320 ms of speech at the backend's 8 kHz must yield one segment"
);
assert_eq!(
segments[0].sample_rate(),
SampleRate::Rate8k,
"segment must be stamped with the backend's rate"
);
}
#[test]
fn mock_geometry_closes_after_two_256ms_low_frames() {
let mut backend = MockBackend::silero_like(4096, vec![0.9, 0.9, 0.9, 0.0, 0.0]);
let samples = vec![0.0_f32; 5 * 4096];
let segments =
detect_speech_with(&mut backend, &samples, SpeechOptions::default()).expect("detect");
assert_eq!(
segments.len(),
1,
"two 256 ms low frames must close one segment"
);
assert_eq!(segments[0].start_sample(), 0);
assert_eq!(segments[0].end_sample(), 3 * 4096 + 480);
assert_eq!(backend.cursor, 5);
}
#[test]
fn mock_geometry_holds_open_through_one_256ms_low_frame() {
let mut backend = MockBackend::silero_like(4096, vec![0.9, 0.9, 0.9, 0.0]);
let samples = vec![0.0_f32; 4 * 4096];
let segments =
detect_speech_with(&mut backend, &samples, SpeechOptions::default()).expect("detect");
assert_eq!(
segments.len(),
1,
"one 256 ms low frame must not close the segment"
);
assert_eq!(segments[0].start_sample(), 0);
assert_eq!(segments[0].end_sample(), 4 * 4096);
}
#[test]
fn mock_geometry_max_speech_lookahead_is_frame_aware() {
let mut backend = MockBackend::silero_like(4096, vec![0.9; 6]);
let samples = vec![0.0_f32; 6 * 4096];
let options = SpeechOptions::default()
.with_min_speech_duration(Duration::ZERO)
.with_speech_pad(Duration::ZERO)
.with_max_speech_duration(Duration::from_millis(1_000));
let segments = detect_speech_with(&mut backend, &samples, options).expect("detect");
assert_eq!(segments[0].start_sample(), 0);
assert_eq!(
segments[0].end_sample(),
12_288,
"max-speech split must land at the hop-aware 12_288, not the \
chunk-based overshoot 16_384"
);
}
#[test]
fn mock_backend_error_bridges_through_backend_variant() {
let mut backend = MockBackend::silero_like(4096, vec![0.9, 0.9, 0.9]).with_fail_at(1);
let samples = vec![0.0_f32; 3 * 4096];
let error = detect_speech_with(&mut backend, &samples, SpeechOptions::default())
.expect_err("backend failure must propagate");
assert!(
matches!(error, crate::Error::Backend(_)),
"backend error must bridge through Error::Backend, got {error:?}"
);
assert_eq!(error.to_string(), "mock predict failure");
}
#[test]
fn firered_geometry_640_samples_emit_two_windows_through_detect_speech_with() {
let mut backend = MockBackend::new(400, 160, EofPolicy::Snip, vec![0.9, 0.9]);
let samples = vec![0.0_f32; 640];
let _ = detect_speech_with(&mut backend, &samples, SpeechOptions::default()).expect("detect");
assert_eq!(
backend.cursor, 2,
"640 samples must emit exactly two FireRed windows (at samples 0 and 160)"
);
}
#[test]
fn firered_geometry_500_sample_tail_is_snipped_through_detect_speech_with() {
let mut backend = MockBackend::new(400, 160, EofPolicy::Snip, vec![0.9, 0.9]);
let samples = vec![0.0_f32; 500];
let _ = detect_speech_with(&mut backend, &samples, SpeechOptions::default()).expect("detect");
assert_eq!(
backend.cursor, 1,
"500 samples must emit exactly one window; the 340-sample tail is snipped, not padded"
);
}
}