diff --git a/agb/src/sound/mixer/mod.rs b/agb/src/sound/mixer/mod.rs index 2ceb6349..f9e0dc90 100644 --- a/agb/src/sound/mixer/mod.rs +++ b/agb/src/sound/mixer/mod.rs @@ -30,6 +30,7 @@ pub struct SoundChannel { should_loop: bool, playback_speed: Num, + volume: Num, // between 0 and 1 panning: Num, // between -1 and 1 is_done: bool, @@ -47,6 +48,7 @@ impl SoundChannel { panning: 0.into(), is_done: false, priority: SoundPriority::Low, + volume: 1.into(), } } @@ -59,6 +61,7 @@ impl SoundChannel { panning: 0.into(), is_done: false, priority: SoundPriority::High, + volume: 1.into(), } } @@ -79,4 +82,12 @@ impl SoundChannel { self.panning = panning; self } + + pub fn volume<'a>(&'a mut self, volume: Num) -> &'a mut Self { + assert!(volume <= Num::new(1), "volume must be <= 1"); + assert!(volume >= Num::new(0), "volume must be >= 0"); + + self.volume = volume; + self + } } diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index d7ac10ec..96b7dc1c 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -117,8 +117,8 @@ impl MixerBuffer { continue; } - let right_amount = (channel.panning + 1) / 2; - let left_amount = -right_amount + 1; + let right_amount = ((channel.panning + 1) / 2) * channel.volume; + let left_amount = (-right_amount + 1) * channel.volume; if channel.pos + channel.playback_speed * SOUND_BUFFER_SIZE >= channel.data.len().into() {