From 38868cb269129bcd99e04ea4e1bafb72f7f50b84 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Tue, 27 Jun 2023 21:50:46 +0100 Subject: [PATCH] Extract even more from the loop body --- agb/src/sound/mixer/sw_mixer.rs | 40 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index a147fd21..5895a08f 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -422,25 +422,31 @@ impl MixerBuffer { ) { working_buffer.fill(0.into()); - for channel in channels.filter(|channel| !channel.is_done) { - let playback_speed = if channel.is_stereo { - 2.into() - } else { - channel.playback_speed - }; + for (channel, playback_speed) in + channels + .filter(|channel| !channel.is_done) + .filter_map(|channel| { + let playback_speed = if channel.is_stereo { + 2.into() + } else { + channel.playback_speed + }; - if (channel.pos + playback_speed * self.frequency.buffer_size() as u32).floor() - >= channel.data.len() as u32 - { - // TODO: This should probably play what's left rather than skip the last bit - if channel.should_loop { - channel.pos = 0.into(); - } else { - channel.is_done = true; - continue; - } - } + if (channel.pos + playback_speed * self.frequency.buffer_size() as u32).floor() + >= channel.data.len() as u32 + { + // TODO: This should probably play what's left rather than skip the last bit + if channel.should_loop { + channel.pos = 0.into(); + } else { + channel.is_done = true; + return None; + } + } + Some((channel, playback_speed)) + }) + { if channel.volume != 0.into() { if channel.is_stereo { unsafe {