diff --git a/src-tauri/src/visualizer/tapped_source.rs b/src-tauri/src/visualizer/tapped_source.rs index d28af27cf..c5422d607 100644 --- a/src-tauri/src/visualizer/tapped_source.rs +++ b/src-tauri/src/visualizer/tapped_source.rs @@ -13,7 +13,7 @@ use super::RingBuffer; /// Wraps a Source and sends samples to a ring buffer for visualization pub struct TappedSource where - S: Source, + S: Source, { inner: S, ring_buffer: Arc, @@ -22,7 +22,7 @@ where impl TappedSource where - S: Source, + S: Source, { /// Create a new TappedSource wrapping the given source pub fn new( @@ -40,9 +40,9 @@ where impl Iterator for TappedSource where - S: Source, + S: Source, { - type Item = i16; + type Item = f32; #[inline] fn next(&mut self) -> Option { @@ -50,10 +50,8 @@ where // Only send to visualizer if enabled - this is a fast atomic check if self.enabled.load(std::sync::atomic::Ordering::Relaxed) { - // Convert i16 to f32 normalized to [-1, 1] - // This is the only computation we do in the hot path - let normalized = sample as f32 / i16::MAX as f32; - self.ring_buffer.push(normalized); + // f32 samples are already normalized to [-1.0, 1.0] + self.ring_buffer.push(sample); } Some(sample) @@ -67,7 +65,7 @@ where impl Source for TappedSource where - S: Source, + S: Source, { #[inline] fn current_frame_len(&self) -> Option { @@ -98,14 +96,14 @@ mod tests { #[test] fn test_tapped_source_passes_through() { - let samples: Vec = vec![1000, 2000, 3000, -1000, -2000]; + let samples: Vec = vec![0.5, 0.75, -0.5, -0.25, 0.0]; let source = SamplesBuffer::new(1, 44100, samples.clone()); let ring_buffer = Arc::new(RingBuffer::new(16)); let enabled = Arc::new(AtomicBool::new(true)); let tapped = TappedSource::new(source, ring_buffer, enabled); - let output: Vec = tapped.collect(); + let output: Vec = tapped.collect(); // Samples should pass through unchanged assert_eq!(output, samples); @@ -113,21 +111,21 @@ mod tests { #[test] fn test_tapped_source_fills_ring_buffer() { - let samples: Vec = vec![i16::MAX, 0, i16::MIN]; + let samples: Vec = vec![1.0, 0.0, -1.0]; let source = SamplesBuffer::new(1, 44100, samples); let ring_buffer = Arc::new(RingBuffer::new(16)); let enabled = Arc::new(AtomicBool::new(true)); let tapped = TappedSource::new(source, ring_buffer.clone(), enabled); - let _: Vec = tapped.collect(); + let _: Vec = tapped.collect(); - // Check ring buffer received normalized samples + // Check ring buffer received samples directly (f32 already normalized) let mut snapshot = [0.0f32; 3]; ring_buffer.snapshot(&mut snapshot); - assert!((snapshot[0] - 1.0).abs() < 0.001); // i16::MAX -> ~1.0 - assert!((snapshot[1] - 0.0).abs() < 0.001); // 0 -> 0.0 - assert!((snapshot[2] - (-1.0)).abs() < 0.01); // i16::MIN -> ~-1.0 + assert!((snapshot[0] - 1.0).abs() < 0.001); + assert!((snapshot[1] - 0.0).abs() < 0.001); + assert!((snapshot[2] - (-1.0)).abs() < 0.001); } }