Skip to content

Commit

Permalink
Improve ChannelVolume's Mono conversion algorithm to use the average …
Browse files Browse the repository at this point in the history
…of samples rather than just the sum
  • Loading branch information
will3942 committed Jan 17, 2025
1 parent b0f5e27 commit 541a169
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/source/channel_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
}
}
Expand Down Expand Up @@ -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)),
);
}
}
Expand Down

0 comments on commit 541a169

Please sign in to comment.