//! Analyzer tap — captures audio samples for loudness analysis. //! //! Sits in the audio pipeline as a transparent `Source` wrapper. //! Batches samples and sends them to the loudness analyzer thread via a bounded //! channel. Uses `try_send` so it never blocks the audio thread — if the channel //! is full, the batch is silently dropped (graceful degradation). use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::mpsc::SyncSender; use std::sync::Arc; use std::time::Duration; use rodio::Source; /// Messages sent from the audio pipeline to the loudness analyzer thread. pub enum AnalyzerMessage { /// A batch of interleaved f32 samples. Samples(Vec), /// A new track has started — reset analyzer state. NewTrack { track_id: u64, sample_rate: u32, channels: u16, target_lufs: f32, /// Shared gain atomic — analyzer writes, DynamicAmplify reads. gain_atomic: Arc, }, /// Seek occurred — reset accumulated samples but keep current gain. Reset, /// Shut down the analyzer thread. Shutdown, } const BATCH_SIZE: usize = 4096; pub struct AnalyzerTap where S: Source, { inner: S, sender: SyncSender, enabled: Arc, buffer: Vec, } impl AnalyzerTap where S: Source, { pub fn new(source: S, sender: SyncSender, enabled: Arc) -> Self { Self { inner: source, sender, enabled, buffer: Vec::with_capacity(BATCH_SIZE), } } #[inline] fn flush_if_full(&mut self) { if self.buffer.len() >= BATCH_SIZE { let batch = std::mem::replace(&mut self.buffer, Vec::with_capacity(BATCH_SIZE)); // Non-blocking send — drop batch if channel is full let _ = self.sender.try_send(AnalyzerMessage::Samples(batch)); } } } impl Iterator for AnalyzerTap where S: Source, { type Item = f32; #[inline] fn next(&mut self) -> Option { let sample = self.inner.next()?; if self.enabled.load(Ordering::Relaxed) { self.buffer.push(sample); self.flush_if_full(); } Some(sample) } #[inline] fn size_hint(&self) -> (usize, Option) { self.inner.size_hint() } } impl Source for AnalyzerTap where S: Source, { #[inline] fn current_span_len(&self) -> Option { self.inner.current_span_len() } #[inline] fn channels(&self) -> std::num::NonZero { self.inner.channels() } #[inline] fn sample_rate(&self) -> std::num::NonZero { self.inner.sample_rate() } #[inline] fn total_duration(&self) -> Option { self.inner.total_duration() } }