Skip to main content

speech_prep/
monitoring.rs

1//! Lightweight counters and VAD statistics.
2
3use std::sync::atomic::{AtomicU64, Ordering};
4
5/// Thread-safe counter for tracking processing metrics.
6#[derive(Debug, Default)]
7pub(crate) struct AtomicCounter(AtomicU64);
8
9impl AtomicCounter {
10    pub(crate) fn new(initial: u64) -> Self {
11        Self(AtomicU64::new(initial))
12    }
13
14    pub(crate) fn get(&self) -> u64 {
15        self.0.load(Ordering::Relaxed)
16    }
17
18    pub(crate) fn reset(&self) {
19        self.0.store(0, Ordering::Relaxed);
20    }
21
22    pub(crate) fn fetch_add(&self, val: u64) -> u64 {
23        self.0.fetch_add(val, Ordering::Relaxed)
24    }
25
26    pub(crate) fn fetch_sub(&self, val: u64) -> u64 {
27        self.0.fetch_sub(val, Ordering::Relaxed)
28    }
29}
30
31/// VAD statistics snapshot.
32#[derive(Debug, Clone, Default)]
33pub struct VADStats {
34    pub frames_processed: u64,
35    pub speech_frames: u64,
36    pub silence_frames: u64,
37}
38
39impl VADStats {
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    pub fn speech_ratio(&self) -> f64 {
45        if self.frames_processed == 0 {
46            return 0.0;
47        }
48        self.speech_frames as f64 / self.frames_processed as f64
49    }
50}