From a4df095031884b6bd3f3cbf1e484c5c8ad6873df Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Sun, 23 Jul 2023 22:15:30 +0100 Subject: [PATCH] Improve accuracy of the panning and volume until the last second --- agb/src/sound/mixer/mod.rs | 8 ++++---- agb/src/sound/mixer/sw_mixer.rs | 7 +++++-- tracker/agb-tracker/src/lib.rs | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/agb/src/sound/mixer/mod.rs b/agb/src/sound/mixer/mod.rs index 7cb4c055..f3dc224c 100644 --- a/agb/src/sound/mixer/mod.rs +++ b/agb/src/sound/mixer/mod.rs @@ -229,9 +229,9 @@ pub struct SoundChannel { restart_point: Num, playback_speed: Num, - volume: Num, // between 0 and 1 + volume: Num, // between 0 and 1 - panning: Num, // between -1 and 1 + panning: Num, // between -1 and 1 is_done: bool, is_stereo: bool, @@ -366,7 +366,7 @@ impl SoundChannel { /// Defaults to 0 (meaning equal on left and right) and doesn't affect stereo /// sounds. #[inline(always)] - pub fn panning(&mut self, panning: impl Into>) -> &mut Self { + pub fn panning(&mut self, panning: impl Into>) -> &mut Self { let panning = panning.into(); debug_assert!(panning >= Num::new(-1), "panning value must be >= -1"); @@ -381,7 +381,7 @@ impl SoundChannel { /// /// Must be a value >= 0 and defaults to 1. #[inline(always)] - pub fn volume(&mut self, volume: impl Into>) -> &mut Self { + pub fn volume(&mut self, volume: impl Into>) -> &mut Self { let volume = volume.into(); assert!(volume >= Num::new(0), "volume must be >= 0"); diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index 26556f97..2fa951a1 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -449,14 +449,14 @@ impl MixerBuffer { agb_rs__mixer_add_stereo_first( channel.data.as_ptr().add(channel.pos.floor() as usize), working_buffer.as_mut_ptr(), - channel.volume, + channel.volume.change_base(), self.frequency.buffer_size(), ); } else { agb_rs__mixer_add_stereo( channel.data.as_ptr().add(channel.pos.floor() as usize), working_buffer.as_mut_ptr(), - channel.volume, + channel.volume.change_base(), self.frequency.buffer_size(), ); } @@ -475,6 +475,9 @@ impl MixerBuffer { let right_amount = ((channel.panning + 1) / 2) * channel.volume; let left_amount = ((-channel.panning + 1) / 2) * channel.volume; + let right_amount: Num = right_amount.change_base(); + let left_amount: Num = left_amount.change_base(); + let channel_len = Num::::new(channel.data.len() as u32); let mut playback_speed = channel.playback_speed; diff --git a/tracker/agb-tracker/src/lib.rs b/tracker/agb-tracker/src/lib.rs index 297b40bc..6c100e6a 100644 --- a/tracker/agb-tracker/src/lib.rs +++ b/tracker/agb-tracker/src/lib.rs @@ -256,7 +256,7 @@ impl TrackerChannel { }; } PatternEffect::Panning(panning) => { - channel.panning(*panning); + channel.panning(panning.change_base()); } PatternEffect::Volume(volume) => { channel.volume(volume.change_base());