firered-vad 0.1.0

Streaming Voice Activity Detection wrapping the FireRedVAD model via ONNX Runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! The Sans-I/O `Vad` engine.

use std::{collections::VecDeque, path::Path};

use crate::{
  detector::Postprocessor,
  error::Result,
  event::{FrameResult, SpeechSegment},
  features::{FeatureExtractor, NUM_MEL_BINS},
  inference::OrtRunner,
  options::VadOptions,
};

/// Bundled FireRedVAD streaming ONNX (Apache-2.0; see `THIRD_PARTY_NOTICES.md`).
#[cfg(feature = "bundled")]
#[cfg_attr(docsrs, doc(cfg(feature = "bundled")))]
pub const BUNDLED_MODEL: &[u8] = include_bytes!(concat!(
  env!("CARGO_MANIFEST_DIR"),
  "/models/fireredvad_stream_vad_with_cache.onnx"
));

/// Bundled CMVN stats (Apache-2.0).
#[cfg(feature = "bundled")]
#[cfg_attr(docsrs, doc(cfg(feature = "bundled")))]
pub const BUNDLED_CMVN: &[u8] =
  include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/models/cmvn.ark"));

/// Streaming Voice Activity Detector for the FireRedVAD model.
///
/// `Vad` is a Sans-I/O state machine: callers push 16 kHz f32 PCM in
/// `[-1.0, 1.0]` via [`Self::push_samples`], which returns the next
/// available closed [`SpeechSegment`] (or `None`). See the crate-level
/// docs for the canonical streaming loop.
///
/// # Thread safety
///
/// `Vad` implements [`Send`] but **not** [`Sync`]. The underlying
/// `ort::Session` is `Send` but not `Sync` (ONNX Runtime sessions
/// permit cross-thread move but not concurrent calls). To use one
/// `Vad` instance across multiple threads, wrap it in a `Mutex` or
/// equivalent. Consumers needing parallel inference typically
/// construct one `Vad` per worker thread (cheap once the model bytes
/// are bundled) rather than synchronising a single instance.
pub struct Vad {
  runner: OrtRunner,
  features: FeatureExtractor,
  detector: Postprocessor,
  pending_segments: VecDeque<SpeechSegment>,
  feature_scratch: Vec<f32>,
  /// Per-frame snapshots produced by the most recent non-empty
  /// `push_samples` call, in order. Cleared at the start of each
  /// non-empty push and on `reset()`.
  recent_frames: Vec<FrameResult>,
  finished: bool,
}

impl Vad {
  // ── Construction ─────────────────────────────────────────────────────

  /// Construct from the bundled ONNX model + CMVN with default `VadOptions`.
  #[cfg(feature = "bundled")]
  #[cfg_attr(docsrs, doc(cfg(feature = "bundled")))]
  pub fn bundled() -> Result<Self> {
    Self::bundled_with(VadOptions::default())
  }

  /// Construct from the bundled artifacts with custom `VadOptions`.
  #[cfg(feature = "bundled")]
  #[cfg_attr(docsrs, doc(cfg(feature = "bundled")))]
  pub fn bundled_with(options: VadOptions) -> Result<Self> {
    Self::from_memory_with_cmvn(BUNDLED_MODEL, BUNDLED_CMVN, options)
  }

  /// Construct from in-memory model bytes + bundled CMVN with default options.
  #[cfg(feature = "bundled")]
  #[cfg_attr(docsrs, doc(cfg(feature = "bundled")))]
  pub fn from_memory(model: &[u8]) -> Result<Self> {
    Self::from_memory_with(model, VadOptions::default())
  }

  /// Construct from in-memory model bytes + bundled CMVN with custom options.
  #[cfg(feature = "bundled")]
  #[cfg_attr(docsrs, doc(cfg(feature = "bundled")))]
  pub fn from_memory_with(model: &[u8], options: VadOptions) -> Result<Self> {
    Self::from_memory_with_cmvn(model, BUNDLED_CMVN, options)
  }

  /// Construct from an ONNX file on disk + bundled CMVN with default options.
  #[cfg(feature = "bundled")]
  #[cfg_attr(docsrs, doc(cfg(feature = "bundled")))]
  pub fn from_file(model: impl AsRef<Path>) -> Result<Self> {
    Self::from_file_with(model, VadOptions::default())
  }

  /// Construct from an ONNX file + bundled CMVN with custom options.
  #[cfg(feature = "bundled")]
  #[cfg_attr(docsrs, doc(cfg(feature = "bundled")))]
  pub fn from_file_with(model: impl AsRef<Path>, options: VadOptions) -> Result<Self> {
    let runner = OrtRunner::from_file(model, options.session_options())?;
    Self::wrap(runner, BUNDLED_CMVN, options)
  }

  /// Construct with explicit model + CMVN bytes.
  pub fn from_memory_with_cmvn(model: &[u8], cmvn: &[u8], options: VadOptions) -> Result<Self> {
    let runner = OrtRunner::from_memory(model, options.session_options())?;
    Self::wrap(runner, cmvn, options)
  }

  /// Construct with explicit model file + CMVN file paths.
  pub fn from_file_with_cmvn(
    model: impl AsRef<Path>,
    cmvn: impl AsRef<Path>,
    options: VadOptions,
  ) -> Result<Self> {
    let runner = OrtRunner::from_file(model, options.session_options())?;
    let cmvn_bytes =
      std::fs::read(cmvn.as_ref()).map_err(|source| crate::error::Error::LoadCmvn {
        path: cmvn.as_ref().to_path_buf(),
        source,
      })?;
    Self::wrap(runner, &cmvn_bytes, options)
  }

  /// Wrap an externally built `ort::Session`. The session must implement
  /// the FireRedVAD streaming model contract.
  pub fn from_ort_session(
    session: ort::session::Session,
    cmvn: &[u8],
    options: VadOptions,
  ) -> Result<Self> {
    let runner = OrtRunner::from_ort_session(session);
    Self::wrap(runner, cmvn, options)
  }

  fn wrap(runner: OrtRunner, cmvn: &[u8], options: VadOptions) -> Result<Self> {
    let features = FeatureExtractor::new(cmvn)?;
    let detector = Postprocessor::new(options.clone());
    Ok(Self {
      runner,
      features,
      detector,
      pending_segments: VecDeque::new(),
      feature_scratch: vec![0.0; NUM_MEL_BINS],
      recent_frames: Vec::new(),
      finished: false,
    })
  }

  // ── Sans-I/O surface ─────────────────────────────────────────────────

  /// Feed 16 kHz f32 PCM and return the next available closed segment.
  ///
  /// Returns `Ok(Some(segment))` when a segment is ready, `Ok(None)` when
  /// none is available yet. Pass an empty slice (`&[]`) to drain buffered
  /// segments without processing new PCM — useful when a single push
  /// closes more than one segment (rare but possible at force-split).
  pub fn push_samples(&mut self, pcm: &[f32]) -> Result<Option<SpeechSegment>> {
    // Destructure self into disjoint mutable borrows of each field so the
    // borrow checker can see the inference output (`&[f32]` borrowed from
    // `runner`) and the per-frame mutations on `detector`/`recent_frames`/
    // `pending_segments` as non-overlapping. Without this split we'd need
    // to `to_vec()` the prob slice every push, which allocated `T*4` bytes
    // per call on the hot path.
    let Self {
      runner,
      features,
      detector,
      pending_segments,
      feature_scratch,
      recent_frames,
      ..
    } = self;

    if !pcm.is_empty() {
      recent_frames.clear();
      features.push_pcm(pcm);
      while features.has_full_window() {
        features.extract_one(feature_scratch);
        runner.push_feature(feature_scratch);
      }
      if runner.pending_feature_frames() > 0 {
        // `probs` borrows `runner` immutably for the duration of the loop;
        // `detector`, `recent_frames`, `pending_segments` are disjoint
        // field borrows so we can mutate them without conflict.
        let probs = runner.infer()?;
        for &prob in probs {
          let (frame_result, segment) = detector.push_probability(prob);
          recent_frames.push(frame_result);
          if let Some(s) = segment {
            pending_segments.push_back(s);
          }
        }
      }
    }
    Ok(pending_segments.pop_front())
  }

  /// Mark end-of-stream. Returns the trailing segment if one was open, or
  /// `None` when the stream ended in silence.
  ///
  /// Call `push_samples(&[])` after `finish` to drain any additionally
  /// buffered segments in the rare multi-segment case.
  pub fn finish(&mut self) -> Result<Option<SpeechSegment>> {
    self.finished = true;
    if let Some(segment) = self.detector.finish_active() {
      self.pending_segments.push_back(segment);
    }
    Ok(self.pending_segments.pop_front())
  }

  /// Reset all per-stream state (caches, smoothing, state machine, queue,
  /// frame counters). Re-uses the underlying `ort::Session`.
  pub fn reset(&mut self) {
    self.runner.reset();
    self.features.reset();
    self.detector.reset();
    self.pending_segments.clear();
    self.recent_frames.clear();
    self.finished = false;
  }

  // ── Inspection ───────────────────────────────────────────────────────

  /// Currently active options.
  pub const fn options(&self) -> &VadOptions {
    self.detector_options()
  }

  // Tiny helper so `options()` stays `const fn` — VecDeque::is_empty is
  // const since 1.71 but we need a `&VadOptions` borrow that the borrow
  // checker can prove without surfacing the private detector field.
  const fn detector_options(&self) -> &VadOptions {
    self.detector.options_const()
  }

  /// Replace the options at runtime. In-flight detector state is preserved.
  pub fn set_options(&mut self, options: VadOptions) {
    self.detector.set_options(options);
  }

  /// Total number of 10 ms frames consumed since the last reset.
  pub const fn frame_count(&self) -> u64 {
    self.detector.frame_count()
  }

  /// Number of int16-range PCM samples buffered awaiting the next frame.
  pub fn pending_samples(&self) -> usize {
    self.features.pending_samples()
  }

  /// Whether the postprocessor is currently inside a SPEECH or POSSIBLE_SILENCE state.
  pub fn is_active(&self) -> bool {
    self.detector.is_active()
  }

  /// Whether [`Self::finish`] has been called.
  pub const fn is_finished(&self) -> bool {
    self.finished
  }

  /// Number of buffered segments awaiting drain via `push_samples(&[])`.
  pub fn pending_segments(&self) -> usize {
    self.pending_segments.len()
  }

  /// Per-frame snapshots produced by the most recent non-empty
  /// `push_samples` call, in order.
  ///
  /// One [`FrameResult`] per 10 ms frame consumed by that push. Each
  /// carries `raw_prob`, `smoothed_prob`, `is_speech`, and the
  /// boundary flags / latest-segment-frame indices the postprocessor
  /// computed for that frame. Empty after `reset()`, after `finish()`
  /// (which produces no new frames), and after any `push_samples(&[])`
  /// drain call.
  ///
  /// This is inspection-only and never required for the segment-emit
  /// happy path. Useful for UI/diagnostics, parity testing against the
  /// upstream Python reference, and any custom postprocessing that
  /// needs the per-frame probability stream.
  pub fn recent_frames(&self) -> &[FrameResult] {
    &self.recent_frames
  }

  /// Debug-only: extract Mel-fbank features for a one-shot PCM input
  /// without running ONNX inference.
  ///
  /// Returns a flat `Vec<f32>` of `T * 80` values where `T` is the
  /// number of 10 ms frames produced by upstream Kaldi's `snip_edges=true`
  /// rule. Only the feature stage runs; the engine's per-stream caches,
  /// smoothing window, and state machine are untouched. Calling this
  /// after `reset()` is the way to start with a clean feature buffer.
  ///
  /// Behind the unstable `_debug-features` feature flag — exists solely
  /// so the parity harness can diff mel features against
  /// `kaldi_native_fbank`. Never use in production.
  #[cfg(feature = "_debug-features")]
  #[doc(hidden)]
  pub fn _debug_extract_mel_features(&mut self, pcm: &[f32]) -> Vec<f32> {
    self.features.reset();
    self.features.push_pcm(pcm);
    let mut out = Vec::new();
    while self.features.has_full_window() {
      let mut frame = vec![0.0; NUM_MEL_BINS];
      self.features.extract_one(&mut frame);
      out.extend_from_slice(&frame);
    }
    out
  }
}

#[cfg(test)]
mod tests {
  #[allow(unused_imports)]
  use super::*;

  #[cfg(feature = "bundled")]
  #[test]
  fn bundled_constructs_with_defaults() {
    let _ = Vad::bundled().expect("bundled constructs");
  }

  #[cfg(feature = "bundled")]
  #[test]
  fn one_second_of_silence_emits_no_segment() {
    let mut vad = Vad::bundled().expect("bundled constructs");
    vad.push_samples(&vec![0.0; 16_000]).expect("push silence");
    let mut segments = 0usize;
    while vad.push_samples(&[]).expect("drain").is_some() {
      segments += 1;
    }
    assert_eq!(segments, 0);
    assert!(!vad.is_active());
  }

  #[cfg(feature = "bundled")]
  #[test]
  fn reset_clears_segment_queue_and_frame_counter() {
    let mut vad = Vad::bundled().expect("bundled");
    vad.push_samples(&vec![0.0; 1_600]).expect("push 100ms");
    vad.reset();
    assert_eq!(vad.frame_count(), 0);
    assert_eq!(vad.pending_segments(), 0);
    assert_eq!(vad.pending_samples(), 0);
    assert!(!vad.is_finished());
  }

  #[cfg(feature = "bundled")]
  #[test]
  fn finish_marks_finished_and_flushes_no_segment_when_idle() {
    let mut vad = Vad::bundled().expect("bundled");
    let result = vad.finish().expect("finish");
    assert!(vad.is_finished());
    assert!(result.is_none());
    let mut segments = 0usize;
    while vad.push_samples(&[]).expect("drain").is_some() {
      segments += 1;
    }
    assert_eq!(segments, 0);
  }

  #[cfg(feature = "bundled")]
  #[test]
  fn recent_frames_captures_one_frameresult_per_10ms_frame() {
    let mut vad = Vad::bundled().expect("bundled");
    // 25 ms (400 samples) for the FIRST frame; +10 ms (160) per subsequent.
    // 5*160 + 240 = 1040 samples → 5 frames.
    vad.push_samples(&vec![0.0; 1040]).expect("push samples");
    let frames = vad.recent_frames();
    assert_eq!(frames.len(), 5);
    for (i, frame) in frames.iter().enumerate() {
      assert_eq!(frame.frame_index(), i as u64);
      assert!(frame.raw_prob() >= 0.0 && frame.raw_prob() <= 1.0);
      assert!(frame.smoothed_prob() >= 0.0 && frame.smoothed_prob() <= 1.0);
    }
  }

  #[cfg(feature = "bundled")]
  #[test]
  fn recent_frames_is_cleared_at_each_non_empty_push() {
    let mut vad = Vad::bundled().expect("bundled");
    vad.push_samples(&vec![0.0; 1040]).expect("push 1");
    let count_after_first = vad.recent_frames().len();
    assert!(count_after_first > 0);
    vad.push_samples(&vec![0.0; 320]).expect("push 2"); // 2 more frames
    assert_eq!(vad.recent_frames().len(), 2);
  }

  #[cfg(feature = "bundled")]
  #[test]
  fn recent_frames_is_empty_after_reset_and_after_drain_calls() {
    let mut vad = Vad::bundled().expect("bundled");
    vad.push_samples(&vec![0.0; 1040]).expect("push");
    assert!(!vad.recent_frames().is_empty());
    vad.reset();
    assert!(vad.recent_frames().is_empty());

    vad.push_samples(&vec![0.0; 1040]).expect("push");
    assert!(!vad.recent_frames().is_empty());
    let _ = vad.push_samples(&[]).expect("drain"); // empty push should NOT clear
    assert!(
      !vad.recent_frames().is_empty(),
      "drain calls preserve recent_frames"
    );
  }
}