diff --git a/CHANGELOG.md b/CHANGELOG.md index b7971029..a09c5466 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - You can now use include_aseprite and include_background_gfx to include files from the out directory using the `$OUT_DIR` token. +- Added `.pause()` and `.resume()` methods to `SoundChannels` to let you pause and resume from where you left off. ## [0.18.0] - 2023/10/31 diff --git a/agb/examples/mixer_basic.rs b/agb/examples/mixer_basic.rs index ad29463c..f94257fb 100644 --- a/agb/examples/mixer_basic.rs +++ b/agb/examples/mixer_basic.rs @@ -46,6 +46,14 @@ fn main(mut gba: Gba) -> ! { } else { channel.volume(1); } + + if input.is_pressed(Button::A) { + channel.resume(); + } + + if input.is_pressed(Button::B) { + channel.pause(); + } } } diff --git a/agb/src/sound/mixer/mod.rs b/agb/src/sound/mixer/mod.rs index f3dc224c..dbef2c9d 100644 --- a/agb/src/sound/mixer/mod.rs +++ b/agb/src/sound/mixer/mod.rs @@ -228,6 +228,7 @@ pub struct SoundChannel { should_loop: bool, restart_point: Num, + is_playing: bool, playback_speed: Num, volume: Num, // between 0 and 1 @@ -272,6 +273,7 @@ impl SoundChannel { pos: 0.into(), should_loop: false, playback_speed: 1.into(), + is_playing: true, panning: 0.into(), is_done: false, priority: SoundPriority::Low, @@ -316,6 +318,7 @@ impl SoundChannel { pos: 0.into(), should_loop: false, playback_speed: 1.into(), + is_playing: true, panning: 0.into(), is_done: false, priority: SoundPriority::High, @@ -419,4 +422,18 @@ impl SoundChannel { self.pos = pos.into(); self } + + /// Pause this channel. You can resume later by using [`.resume()`](SoundChannel::resume()) + #[inline] + pub fn pause(&mut self) -> &mut Self { + self.is_playing = false; + self + } + + /// Resume a paused channel paused by [`.pause()`](SoundChannel::pause()) + #[inline] + pub fn resume(&mut self) -> &mut Self { + self.is_playing = true; + self + } } diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index 917b6657..88c0a871 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -414,8 +414,8 @@ impl MixerBuffer { working_buffer: &mut [Num], channels: impl Iterator, ) { - let mut channels = - channels.filter(|channel| !channel.is_done && channel.volume != 0.into()); + let mut channels = channels + .filter(|channel| !channel.is_done && channel.volume != 0.into() && channel.is_playing); if let Some(channel) = channels.next() { if channel.is_stereo {