From 541a169d66a1bf9a8ef61a44fc636603340ea4d5 Mon Sep 17 00:00:00 2001 From: Will Evans Date: Fri, 17 Jan 2025 10:58:17 +0000 Subject: [PATCH] Improve ChannelVolume's Mono conversion algorithm to use the average of samples rather than just the sum --- src/source/channel_volume.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/source/channel_volume.rs b/src/source/channel_volume.rs index 84862875..ffdc3f87 100644 --- a/src/source/channel_volume.rs +++ b/src/source/channel_volume.rs @@ -34,12 +34,14 @@ where I::Item: Sample, { let mut sample = None; - for _ in 0..input.channels() { + let num_channels = input.channels(); + + for _ in 0..num_channels { if let Some(s) = input.next() { sample = Some( sample .get_or_insert_with(I::Item::zero_value) - .saturating_add(s), + .saturating_add(s.amplify(1.0 / num_channels as f32)), ); } } @@ -92,12 +94,15 @@ where if self.current_channel >= self.channel_volumes.len() { self.current_channel = 0; self.current_sample = None; - for _ in 0..self.input.channels() { + + let num_channels = self.input.channels(); + + for _ in 0..num_channels { if let Some(s) = self.input.next() { self.current_sample = Some( self.current_sample .get_or_insert_with(I::Item::zero_value) - .saturating_add(s), + .saturating_add(s.amplify(1.0 / num_channels as f32)), ); } }