oximedia-audio 0.1.4

Audio codec implementations for OxiMedia
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! Voice Activity Detection (VAD).
//!
//! Provides an energy-based VAD with spectral flatness measure,
//! hangover logic, and minimum speech duration filtering.

/// Decision returned by the VAD for each processed frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VadDecision {
    /// Active speech detected.
    Speech,
    /// Silence (no speech activity).
    Silence,
    /// Transitioning between silence and speech (ramp-up or hangover).
    Transition,
}

/// Configuration for the voice activity detector.
#[derive(Debug, Clone)]
pub struct VadConfig {
    /// Energy threshold in dBFS (e.g. -40.0).
    ///
    /// Frames with energy above this threshold are candidates for speech.
    pub threshold_db: f32,
    /// Minimum consecutive speech duration in milliseconds.
    ///
    /// Short bursts shorter than this remain `Transition` rather than `Speech`.
    pub min_speech_ms: u32,
    /// Hangover duration in milliseconds.
    ///
    /// After energy drops below threshold the VAD keeps reporting `Speech`
    /// (or `Transition`) for this many milliseconds.
    pub hangover_ms: u32,
}

impl VadConfig {
    /// Creates a default EBU / telephony-suitable VAD configuration.
    #[must_use]
    pub fn default_telephony() -> Self {
        Self {
            threshold_db: -40.0,
            min_speech_ms: 30,
            hangover_ms: 100,
        }
    }
}

impl Default for VadConfig {
    fn default() -> Self {
        Self::default_telephony()
    }
}

// ---------------------------------------------------------------------------
// Internal state machine
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum VadState {
    /// Currently in silence.
    Silence,
    /// Energy above threshold but not long enough for confirmed speech.
    RampUp,
    /// Confirmed speech.
    Speech,
    /// Energy dropped; holding speech decision for hangover period.
    Hangover,
}

/// Voice activity detector.
pub struct VadDetector {
    config: VadConfig,
    sample_rate: u32,
    /// Current internal FSM state.
    state: VadState,
    /// Energy of the most recently processed frame in dBFS.
    current_energy_db: f32,
    /// Spectral flatness of the most recently processed frame.
    current_sfm: f32,
    /// Number of consecutive samples with energy above threshold.
    above_threshold_samples: u64,
    /// Number of consecutive samples spent in hangover.
    hangover_samples_remaining: u64,
    /// Minimum samples required for confirmed speech.
    min_speech_samples: u64,
    /// Total hangover samples per configuration.
    hangover_total_samples: u64,
}

impl VadDetector {
    /// Create a new `VadDetector`.
    ///
    /// # Panics
    ///
    /// Never panics; if `sample_rate` is 0 the detector uses 0-sample
    /// thresholds (effectively always transitions immediately).
    #[must_use]
    pub fn new(config: VadConfig, sample_rate: u32) -> Self {
        let sr = sample_rate as u64;
        let min_speech_samples = if sr > 0 {
            sr * u64::from(config.min_speech_ms) / 1_000
        } else {
            0
        };
        let hangover_total_samples = if sr > 0 {
            sr * u64::from(config.hangover_ms) / 1_000
        } else {
            0
        };

        Self {
            config,
            sample_rate,
            state: VadState::Silence,
            current_energy_db: f32::NEG_INFINITY,
            current_sfm: 1.0,
            above_threshold_samples: 0,
            hangover_samples_remaining: 0,
            min_speech_samples,
            hangover_total_samples,
        }
    }

    /// Process a single frame of audio samples and return the VAD decision.
    ///
    /// The frame length should match the expected block size (e.g. 10–30 ms
    /// at the configured sample rate). Shorter frames are accepted but
    /// internal timing counters may be less accurate.
    ///
    /// Returns `VadDecision::Silence` on an empty frame.
    pub fn process_frame(&mut self, samples: &[f32]) -> VadDecision {
        if samples.is_empty() {
            return VadDecision::Silence;
        }

        self.current_energy_db = compute_energy_db(samples);
        self.current_sfm = self.spectral_flatness_measure(samples);

        let n = samples.len() as u64;
        let is_active = self.current_energy_db >= self.config.threshold_db;

        self.state = match self.state {
            VadState::Silence => {
                if is_active {
                    self.above_threshold_samples = n;
                    VadState::RampUp
                } else {
                    self.above_threshold_samples = 0;
                    VadState::Silence
                }
            }
            VadState::RampUp => {
                if is_active {
                    self.above_threshold_samples += n;
                    if self.above_threshold_samples >= self.min_speech_samples {
                        VadState::Speech
                    } else {
                        VadState::RampUp
                    }
                } else {
                    // Too short – drop back to silence
                    self.above_threshold_samples = 0;
                    VadState::Silence
                }
            }
            VadState::Speech => {
                if is_active {
                    self.above_threshold_samples += n;
                    VadState::Speech
                } else {
                    // Begin hangover
                    self.hangover_samples_remaining = self.hangover_total_samples;
                    VadState::Hangover
                }
            }
            VadState::Hangover => {
                if is_active {
                    // Speech resumed
                    self.hangover_samples_remaining = 0;
                    VadState::Speech
                } else if self.hangover_samples_remaining > n {
                    self.hangover_samples_remaining -= n;
                    VadState::Hangover
                } else {
                    self.hangover_samples_remaining = 0;
                    self.above_threshold_samples = 0;
                    VadState::Silence
                }
            }
        };

        self.decision_for_state()
    }

    /// Returns `true` if the current state is speech or hangover.
    #[must_use]
    pub fn is_speech(&self) -> bool {
        matches!(self.state, VadState::Speech | VadState::Hangover)
    }

    /// Returns the energy of the last processed frame in dBFS.
    #[must_use]
    pub fn frame_energy_db(&self) -> f32 {
        self.current_energy_db
    }

    /// Returns the spectral flatness measure of the last processed frame.
    ///
    /// Values close to 1.0 indicate noise-like signal; close to 0.0 indicate
    /// tonal / speech-like signal.
    #[must_use]
    pub fn last_sfm(&self) -> f32 {
        self.current_sfm
    }

    /// Compute a spectral flatness proxy for the given samples.
    ///
    /// Uses the variance of local short-term energy (computed in 8 non-overlapping
    /// sub-frames) relative to the mean energy. Tonal signals have highly periodic
    /// energy envelopes resulting in larger variance; noise-like signals have lower
    /// variance.  The result is inverted so that:
    ///
    /// - Values close to 0.0 indicate tonal (periodic, speech-like) content.
    /// - Values close to 1.0 indicate noise-like (flat-spectrum) content.
    ///
    /// Returns a value in [0.0, 1.0].
    pub fn spectral_flatness_measure(&self, samples: &[f32]) -> f32 {
        const NUM_BINS: usize = 8;
        if samples.len() < NUM_BINS * 2 {
            return 1.0; // not enough data
        }

        let bin_len = samples.len() / NUM_BINS;
        let mut energies = [0.0f64; NUM_BINS];
        for (b, chunk) in samples.chunks(bin_len).take(NUM_BINS).enumerate() {
            energies[b] =
                chunk.iter().map(|&s| (s as f64) * (s as f64)).sum::<f64>() / chunk.len() as f64;
        }

        let mean = energies.iter().sum::<f64>() / NUM_BINS as f64;
        if mean < 1e-30 {
            return 1.0; // silence
        }

        let variance = energies
            .iter()
            .map(|&e| {
                let d = e - mean;
                d * d
            })
            .sum::<f64>()
            / NUM_BINS as f64;

        // Coefficient of variation (std/mean) normalised to [0,1]
        let cv = (variance.sqrt() / mean) as f32;

        // Tonal → high CV (large swings) → low flatness
        // Noise  → low CV (even energy) → high flatness
        // We cap CV at 2.0 and invert.
        let flatness = 1.0 - (cv / 2.0).clamp(0.0, 1.0);
        flatness.clamp(0.0, 1.0)
    }

    /// Reset all internal state.
    pub fn reset(&mut self) {
        self.state = VadState::Silence;
        self.current_energy_db = f32::NEG_INFINITY;
        self.current_sfm = 1.0;
        self.above_threshold_samples = 0;
        self.hangover_samples_remaining = 0;
    }

    /// Return the current VAD decision without processing a new frame.
    #[must_use]
    pub fn current_decision(&self) -> VadDecision {
        self.decision_for_state()
    }

    /// Return a reference to the configuration.
    #[must_use]
    pub fn config(&self) -> &VadConfig {
        &self.config
    }

    /// Return the sample rate.
    #[must_use]
    pub fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    // -----------------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------------

    fn decision_for_state(&self) -> VadDecision {
        match self.state {
            VadState::Silence => VadDecision::Silence,
            VadState::RampUp | VadState::Hangover => VadDecision::Transition,
            VadState::Speech => VadDecision::Speech,
        }
    }
}

// ---------------------------------------------------------------------------
// Free helper: compute RMS energy in dBFS
// ---------------------------------------------------------------------------

/// Compute the RMS energy of `samples` in dBFS.
///
/// Returns `f32::NEG_INFINITY` for silence (all-zero input).
fn compute_energy_db(samples: &[f32]) -> f32 {
    if samples.is_empty() {
        return f32::NEG_INFINITY;
    }
    let mean_sq: f32 = samples.iter().map(|&s| s * s).sum::<f32>() / samples.len() as f32;
    if mean_sq < f32::EPSILON {
        f32::NEG_INFINITY
    } else {
        10.0 * mean_sq.log10()
    }
}

// ============================================================
// Unit tests
// ============================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::f32::consts::PI;

    const SAMPLE_RATE: u32 = 48_000;

    /// Generate a 1 kHz sine wave frame of `num_samples` samples at `amplitude`.
    fn sine_frame(amplitude: f32, num_samples: usize) -> Vec<f32> {
        (0..num_samples)
            .map(|i| amplitude * (2.0 * PI * 1000.0 * i as f32 / SAMPLE_RATE as f32).sin())
            .collect()
    }

    /// Generate silence.
    fn silence_frame(num_samples: usize) -> Vec<f32> {
        vec![0.0f32; num_samples]
    }

    // ------------------------------------------------------------------
    // VadConfig
    // ------------------------------------------------------------------

    #[test]
    fn test_default_config() {
        let cfg = VadConfig::default();
        assert_eq!(cfg.threshold_db, -40.0);
        assert_eq!(cfg.min_speech_ms, 30);
        assert_eq!(cfg.hangover_ms, 100);
    }

    // ------------------------------------------------------------------
    // Silence detection
    // ------------------------------------------------------------------

    #[test]
    fn test_silence_frame_returns_silence() {
        let mut vad = VadDetector::new(VadConfig::default(), SAMPLE_RATE);
        let frame = silence_frame(480); // 10 ms at 48 kHz
        let decision = vad.process_frame(&frame);
        assert_eq!(decision, VadDecision::Silence);
    }

    #[test]
    fn test_empty_frame_returns_silence() {
        let mut vad = VadDetector::new(VadConfig::default(), SAMPLE_RATE);
        let decision = vad.process_frame(&[]);
        assert_eq!(decision, VadDecision::Silence);
    }

    // ------------------------------------------------------------------
    // Speech detection
    // ------------------------------------------------------------------

    #[test]
    fn test_loud_tone_eventually_becomes_speech() {
        let cfg = VadConfig {
            threshold_db: -40.0,
            min_speech_ms: 10, // 480 samples at 48 kHz
            hangover_ms: 50,
        };
        let mut vad = VadDetector::new(cfg, SAMPLE_RATE);
        let frame = sine_frame(0.5, 480); // -6 dBFS → well above -40 dB

        // Feed 5 frames (50 ms) to satisfy min_speech_ms=10 ms
        let mut decisions = Vec::new();
        for _ in 0..5 {
            decisions.push(vad.process_frame(&frame));
        }

        assert!(
            decisions.contains(&VadDecision::Speech),
            "Should reach Speech state: {decisions:?}"
        );
    }

    // ------------------------------------------------------------------
    // Short burst remains Transition
    // ------------------------------------------------------------------

    #[test]
    fn test_short_burst_stays_transition() {
        let cfg = VadConfig {
            threshold_db: -40.0,
            min_speech_ms: 100, // require 100 ms before confirming speech
            hangover_ms: 50,
        };
        let mut vad = VadDetector::new(cfg, SAMPLE_RATE);
        let frame = sine_frame(0.5, 480); // 10 ms frame

        // Only feed ONE frame (10 ms < 100 ms min_speech)
        let d = vad.process_frame(&frame);
        assert_eq!(
            d,
            VadDecision::Transition,
            "One 10ms frame should be Transition, not Speech"
        );
    }

    // ------------------------------------------------------------------
    // Hangover behaviour
    // ------------------------------------------------------------------

    #[test]
    fn test_hangover_maintains_non_silence_after_speech_ends() {
        let cfg = VadConfig {
            threshold_db: -40.0,
            min_speech_ms: 10,
            hangover_ms: 100, // 4800 samples at 48 kHz
        };
        let mut vad = VadDetector::new(cfg, SAMPLE_RATE);
        let speech_frame = sine_frame(0.5, 480);
        let silent_frame = silence_frame(480);

        // Establish speech (3 frames = 30 ms ≥ min_speech 10 ms)
        for _ in 0..3 {
            vad.process_frame(&speech_frame);
        }
        assert_eq!(vad.process_frame(&speech_frame), VadDecision::Speech);

        // Now feed silence; hangover should keep non-Silence for ~100 ms
        let mut after_speech = Vec::new();
        for _ in 0..5 {
            // 5 frames = 50 ms, still within hangover
            after_speech.push(vad.process_frame(&silent_frame));
        }

        assert!(
            after_speech.iter().any(|d| *d != VadDecision::Silence),
            "Hangover should prevent immediate Silence: {after_speech:?}"
        );
    }

    #[test]
    fn test_hangover_eventually_returns_to_silence() {
        let cfg = VadConfig {
            threshold_db: -40.0,
            min_speech_ms: 10,
            hangover_ms: 50, // 2400 samples = 5 × 480-sample frames
        };
        let mut vad = VadDetector::new(cfg, SAMPLE_RATE);
        let speech_frame = sine_frame(0.5, 480);
        let silent_frame = silence_frame(480);

        // Establish speech
        for _ in 0..3 {
            vad.process_frame(&speech_frame);
        }

        // Feed 20 silent frames (400 ms >> 50 ms hangover)
        let mut last_decision = VadDecision::Speech;
        for _ in 0..20 {
            last_decision = vad.process_frame(&silent_frame);
        }

        assert_eq!(
            last_decision,
            VadDecision::Silence,
            "After hangover expires, VAD must return Silence"
        );
    }

    // ------------------------------------------------------------------
    // Reset behaviour
    // ------------------------------------------------------------------

    #[test]
    fn test_reset_returns_to_silence() {
        let mut vad = VadDetector::new(VadConfig::default(), SAMPLE_RATE);
        let speech_frame = sine_frame(0.5, 1440); // 30 ms
        for _ in 0..5 {
            vad.process_frame(&speech_frame);
        }

        vad.reset();
        assert_eq!(vad.current_decision(), VadDecision::Silence);
        assert!(!vad.is_speech());
    }

    // ------------------------------------------------------------------
    // Energy measurement accuracy
    // ------------------------------------------------------------------

    #[test]
    fn test_energy_db_of_sine_is_reasonable() {
        let mut vad = VadDetector::new(VadConfig::default(), SAMPLE_RATE);
        let frame = sine_frame(1.0, 4800); // 0 dBFS peak sine → RMS = -3 dBFS
        vad.process_frame(&frame);
        let e = vad.frame_energy_db();
        // 0 dBFS sine: RMS ≈ -3.01 dBFS → in power terms ≈ -3 dB
        assert!(
            e > -10.0,
            "Energy should be > -10 dB for full-scale sine: {e}"
        );
        assert!(e < 1.0, "Energy should be < 1 dB for full-scale sine: {e}");
    }

    #[test]
    fn test_silence_energy_is_neg_infinity() {
        let mut vad = VadDetector::new(VadConfig::default(), SAMPLE_RATE);
        let frame = silence_frame(4800);
        vad.process_frame(&frame);
        assert!(
            vad.frame_energy_db().is_infinite() && vad.frame_energy_db() < 0.0,
            "Silence energy must be -∞"
        );
    }

    // ------------------------------------------------------------------
    // Spectral flatness
    // ------------------------------------------------------------------

    #[test]
    fn test_sfm_sine_returns_valid_range() {
        let vad = VadDetector::new(VadConfig::default(), SAMPLE_RATE);
        let sine = sine_frame(0.5, 4800);
        let sfm = vad.spectral_flatness_measure(&sine);
        // SFM must always be in [0.0, 1.0]
        assert!(
            (0.0..=1.0).contains(&sfm),
            "SFM must be in [0,1], got {sfm}"
        );
    }

    #[test]
    fn test_sfm_noise_returns_valid_range() {
        let vad = VadDetector::new(VadConfig::default(), SAMPLE_RATE);
        // Pseudo-random noise via deterministic multi-frequency sequence
        let noise: Vec<f32> = (0..4800)
            .map(|i| {
                let x = (i as f32 * 1234.567_f32).sin() * 0.5
                    + (i as f32 * 7654.321_f32).cos() * 0.3
                    + (i as f32 * 3141.592_f32).sin() * 0.2;
                x.clamp(-1.0, 1.0)
            })
            .collect();

        let sfm = vad.spectral_flatness_measure(&noise);
        assert!(
            (0.0..=1.0).contains(&sfm),
            "Noise SFM must be in [0,1], got {sfm}"
        );
    }

    // ------------------------------------------------------------------
    // Mixed silence / speech sequence
    // ------------------------------------------------------------------

    #[test]
    fn test_mixed_sequence_silence_speech_silence() {
        let cfg = VadConfig {
            threshold_db: -40.0,
            min_speech_ms: 10,
            hangover_ms: 20,
        };
        let mut vad = VadDetector::new(cfg, SAMPLE_RATE);
        let speech = sine_frame(0.5, 480);
        let silence = silence_frame(480);

        // Silence
        for _ in 0..5 {
            let d = vad.process_frame(&silence);
            assert_eq!(d, VadDecision::Silence);
        }
        // Speech onset
        let mut saw_speech = false;
        for _ in 0..10 {
            let d = vad.process_frame(&speech);
            if d == VadDecision::Speech {
                saw_speech = true;
            }
        }
        assert!(saw_speech, "Should detect speech in speech segment");

        // Hangover + silence
        let mut returned_to_silence = false;
        for _ in 0..30 {
            if vad.process_frame(&silence) == VadDecision::Silence {
                returned_to_silence = true;
                break;
            }
        }
        assert!(returned_to_silence, "Should eventually return to Silence");
    }

    // ------------------------------------------------------------------
    // Various threshold values
    // ------------------------------------------------------------------

    #[test]
    fn test_high_threshold_treats_quiet_as_silence() {
        let cfg = VadConfig {
            threshold_db: -10.0, // very high threshold
            min_speech_ms: 10,
            hangover_ms: 20,
        };
        let mut vad = VadDetector::new(cfg, SAMPLE_RATE);
        // Quiet sine at -20 dBFS (amplitude ≈ 0.1)
        let frame = sine_frame(0.1, 4800);
        let d = vad.process_frame(&frame);
        // -20 dBFS < -10 dBFS threshold → silence
        assert!(
            d != VadDecision::Speech,
            "Quiet signal below high threshold must not be Speech: {d:?}"
        );
    }

    #[test]
    fn test_is_speech_after_confirmed_speech() {
        let cfg = VadConfig {
            threshold_db: -40.0,
            min_speech_ms: 10,
            hangover_ms: 100,
        };
        let mut vad = VadDetector::new(cfg, SAMPLE_RATE);
        let speech = sine_frame(0.5, 1440); // 30 ms
        for _ in 0..5 {
            vad.process_frame(&speech);
        }
        assert!(
            vad.is_speech(),
            "is_speech() should be true after confirmed speech"
        );
    }
}