zk-audio 0.1.0

Audio processing library for voice recording and enhancement
Documentation
use crate::core::{AudioFrame, CaptureDiagnostics};

#[derive(Debug, Default)]
pub struct LevelMetrics {
    sum_squares: f64,
    sample_count: u64,
    peak: f32,
    clipping_events: u64,
    frames_processed: u64,
}

impl LevelMetrics {
    pub fn observe(&mut self, frame: &AudioFrame) {
        self.frames_processed += 1;
        for sample in &frame.samples {
            let value = sample.abs();
            self.sum_squares += (*sample as f64) * (*sample as f64);
            self.sample_count += 1;
            if value > self.peak {
                self.peak = value;
            }
            if value >= 0.999 {
                self.clipping_events += 1;
            }
        }
    }

    pub fn apply(&self, diagnostics: &mut CaptureDiagnostics) {
        diagnostics.frames_processed = self.frames_processed;
        diagnostics.peak_level = self.peak;
        diagnostics.clipping_events = self.clipping_events;
        diagnostics.rms_level = if self.sample_count == 0 {
            0.0
        } else {
            ((self.sum_squares / self.sample_count as f64).sqrt()) as f32
        };
    }
}