Add pausing and resuming for sound channels

This commit is contained in:
Gwilym Inzani 2024-02-03 21:07:12 +00:00
parent ac8e7d84f7
commit d8110243f4
2 changed files with 19 additions and 2 deletions

View file

@ -228,6 +228,7 @@ pub struct SoundChannel {
should_loop: bool,
restart_point: Num<u32, 8>,
is_playing: bool,
playback_speed: Num<u32, 8>,
volume: Num<i16, 8>, // 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
}
}

View file

@ -414,8 +414,8 @@ impl MixerBuffer {
working_buffer: &mut [Num<i16, 4>],
channels: impl Iterator<Item = &'a mut SoundChannel>,
) {
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 {