Skip to main content

speech_prep/vad/
metrics.rs

1//! VAD metrics collection and monitoring.
2
3use crate::monitoring::VADStats;
4use crate::time::AudioDuration;
5
6/// Metrics sink for VAD instrumentation.
7pub trait VadMetricsCollector: Send + Sync {
8    /// Record a snapshot of VAD metrics.
9    fn record_vad_metrics(&self, snapshot: &VadMetricsSnapshot);
10}
11
12/// Structured metrics emitted after each detection call.
13#[derive(Debug, Clone)]
14pub struct VadMetricsSnapshot {
15    /// Frame-level statistics collected during detection.
16    pub stats: VADStats,
17    /// End-to-end latency for the detection call.
18    pub latency: AudioDuration,
19    /// Adaptive baseline state captured alongside the stats.
20    pub adaptive: AdaptiveThresholdSnapshot,
21}
22
23/// Snapshot of the adaptive baseline and threshold state.
24#[derive(Debug, Clone, Copy, Default)]
25pub struct AdaptiveThresholdSnapshot {
26    /// Current exponentially weighted baseline for energy.
27    pub energy_baseline: f32,
28    /// Current exponentially weighted baseline for spectral flux.
29    pub flux_baseline: f32,
30    /// Current dynamic decision threshold used by the detector.
31    pub dynamic_threshold: f32,
32}
33
34impl VadMetricsSnapshot {
35    /// Create a new snapshot from aggregated stats and observed latency.
36    #[must_use]
37    pub fn new(
38        stats: VADStats,
39        latency: AudioDuration,
40        adaptive: AdaptiveThresholdSnapshot,
41    ) -> Self {
42        Self {
43            stats,
44            latency,
45            adaptive,
46        }
47    }
48
49    /// Convenience accessor for the speech ratio.
50    #[must_use]
51    pub fn speech_ratio(&self) -> f64 {
52        self.stats.speech_ratio()
53    }
54}
55
56/// No-op metrics collector for components that do not require instrumentation.
57#[derive(Debug, Default, Clone, Copy)]
58pub struct NoopVadMetricsCollector;
59
60impl VadMetricsCollector for NoopVadMetricsCollector {
61    fn record_vad_metrics(&self, _snapshot: &VadMetricsSnapshot) {}
62}