From 13f5fe01d7d5daae877afcf3c50d682c4889c9c7 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Tue, 27 Jun 2023 22:34:45 +0100 Subject: [PATCH] Actually use the new first methods --- agb/src/sound/mixer/mixer.s | 4 ++-- agb/src/sound/mixer/sw_mixer.rs | 37 ++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/agb/src/sound/mixer/mixer.s b/agb/src/sound/mixer/mixer.s index 254699c6..1f9db61b 100644 --- a/agb/src/sound/mixer/mixer.s +++ b/agb/src/sound/mixer/mixer.s @@ -35,7 +35,7 @@ agb_arm_func \fn_name ldrsb r6, [r4] @ load the current sound sample to r6 add r5, r5, r2 @ calculate the position to read the next sample from -.ifc is_first,true +.ifc \is_first,true mul r4, r6, r7 @ r4 = r6 * r7 (calculating both the left and right samples together) .else ldr r4, [r1] @ read the current value @@ -82,7 +82,7 @@ agb_arm_func \fn_name lsl r6, r6, #16 orr r6, r6, lsr #16 -.ifc is_first,true +.ifc \is_first,true mov r4, r6, lsl r3 @ r4 = r6 << r3 .else ldr r4, [r1] @ read the current value diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index 2cd3257b..9ae8e3bc 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -420,9 +420,7 @@ impl MixerBuffer { working_buffer: &mut [Num], channels: impl Iterator, ) { - working_buffer.fill(0.into()); - - let channels = channels + let mut channels = channels .filter(|channel| !channel.is_done) .filter_map(|channel| { let playback_speed = if channel.is_stereo { @@ -446,6 +444,39 @@ impl MixerBuffer { Some((channel, playback_speed)) }); + if let Some((channel, playback_speed)) = channels.next() { + if channel.volume != 0.into() { + if channel.is_stereo { + unsafe { + agb_rs__mixer_add_stereo_first( + channel.data.as_ptr().add(channel.pos.floor() as usize), + working_buffer.as_mut_ptr(), + channel.volume, + ); + } + } else { + let right_amount = ((channel.panning + 1) / 2) * channel.volume; + let left_amount = ((-channel.panning + 1) / 2) * channel.volume; + + unsafe { + agb_rs__mixer_add_first( + channel.data.as_ptr().add(channel.pos.floor() as usize), + working_buffer.as_mut_ptr(), + playback_speed, + left_amount, + right_amount, + ); + } + } + } else { + working_buffer.fill(0.into()); + } + + channel.pos += playback_speed * self.frequency.buffer_size() as u32; + } else { + working_buffer.fill(0.into()); + } + for (channel, playback_speed) in channels { if channel.volume != 0.into() { if channel.is_stereo {