Skip to main content

speech_prep/
monitoring.rs

1//! Lightweight monitoring primitives.
2
3use std::sync::atomic::{AtomicU64, Ordering};
4
5/// Thread-safe counter for tracking processing metrics.
6#[derive(Debug, Default)]
7pub struct AtomicCounter(AtomicU64);
8
9impl AtomicCounter {
10    pub fn new(initial: u64) -> Self {
11        Self(AtomicU64::new(initial))
12    }
13
14    pub fn increment(&self) -> u64 {
15        self.0.fetch_add(1, Ordering::Relaxed)
16    }
17
18    pub fn add(&self, n: u64) -> u64 {
19        self.0.fetch_add(n, Ordering::Relaxed)
20    }
21
22    pub fn get(&self) -> u64 {
23        self.0.load(Ordering::Relaxed)
24    }
25
26    pub fn reset(&self) {
27        self.0.store(0, Ordering::Relaxed);
28    }
29
30    pub fn fetch_add(&self, val: u64) -> u64 {
31        self.0.fetch_add(val, Ordering::Relaxed)
32    }
33
34    pub fn fetch_sub(&self, val: u64) -> u64 {
35        self.0.fetch_sub(val, Ordering::Relaxed)
36    }
37}
38
39/// VAD statistics snapshot.
40#[derive(Debug, Clone, Default)]
41pub struct VADStats {
42    pub frames_processed: u64,
43    pub speech_frames: u64,
44    pub silence_frames: u64,
45}
46
47impl VADStats {
48    pub fn new() -> Self {
49        Self::default()
50    }
51
52    pub fn speech_ratio(&self) -> f64 {
53        if self.frames_processed == 0 {
54            return 0.0;
55        }
56        self.speech_frames as f64 / self.frames_processed as f64
57    }
58}
59
60/// Trait for collecting audio processing metrics.
61pub trait AudioMetrics: Send + Sync {
62    fn record_latency(&self, _stage: &str, _duration_ms: f64) {}
63    fn increment_counter(&self, _name: &str) {}
64}
65
66/// No-op metrics implementation.
67#[derive(Debug, Default, Clone)]
68pub struct NoopMetrics;
69
70impl AudioMetrics for NoopMetrics {}