namespace ytLive.Services.Audio; /// /// Computes the smoothed mic level (0..1) from captured samples. Pure — /// unit-tested; the only feeds it and forwards the /// result. RMS-based so it tracks perceived loudness, smoothed so the meter /// does not flicker. /// public sealed class AudioLevelMeter { private const float Smoothing = 0.2f; private float _level; public float Level => _level; public float Push(AudioSample sample) { if (sample.Samples.Length == 0) return _level; double sumSquares = 0; var count = 0; foreach (var value in sample.Samples) { sumSquares += value * value; count++; } var rms = (float)Math.Sqrt(sumSquares / count); _level = _level * (1 - Smoothing) + rms * Smoothing; return _level; } public void Reset() => _level = 0; }