From 057467ecf8a0a5140fc09a3b3ab830df4e201e34 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Fri, 18 Jun 2021 22:56:49 +0100 Subject: [PATCH] Only do the if statement once per channel rather than once per index --- agb/src/sound/mixer/mixer.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/agb/src/sound/mixer/mixer.rs b/agb/src/sound/mixer/mixer.rs index 45e5f8fd..69484d4b 100644 --- a/agb/src/sound/mixer/mixer.rs +++ b/agb/src/sound/mixer/mixer.rs @@ -91,22 +91,23 @@ impl MixerBuffer { let right_amount = (channel.panning + 1) / 2; let left_amount = -right_amount + 1; - for i in 0..SOUND_BUFFER_SIZE { - if channel.pos.floor() >= channel.data.len() { - if channel.should_loop { - channel.pos -= channel.data.len(); - } else { - channel.is_done = true; - continue; - } + if channel.pos + channel.playback_speed * SOUND_BUFFER_SIZE >= channel.data.len().into() + { + // TODO: This should probably play what's left rather than skip the last bit + if channel.should_loop { + channel.pos -= channel.data.len(); + } else { + channel.is_done = true; + continue; } + } + for i in 0..SOUND_BUFFER_SIZE { let v = (channel.data[channel.pos.floor()] as i8) as i16; - let v: Num = v.into(); channel.pos += channel.playback_speed; - buffer[i] += v * left_amount; - buffer[i + SOUND_BUFFER_SIZE] += v * right_amount; + buffer[i] += left_amount * v; + buffer[i + SOUND_BUFFER_SIZE] += right_amount * v; } }