From 3c0d881a45dbfd1bb049e786059bb7154b8b1f26 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Fri, 25 Jun 2021 21:57:24 +0100 Subject: [PATCH] Allow for volume control --- agb/src/sound/mixer/mod.rs | 11 +++++++++++ agb/src/sound/mixer/sw_mixer.rs | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) 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() {