From 93d0fe570029bf02c341470226e57cb1fc3eae91 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sat, 10 Dec 2022 00:19:05 +0000 Subject: [PATCH] Start refactoring towards needing &self in write_channels --- agb/src/sound/mixer/sw_mixer.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index af2d68ef..285e086b 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -89,6 +89,8 @@ pub struct Mixer { indices: [i32; 8], frequency: Frequency, + working_buffer: Box<[Num], InternalAllocator>, + fifo_timer: Timer, } @@ -142,6 +144,10 @@ impl Mixer { set_asm_buffer_size(frequency); + let mut working_buffer = + Vec::with_capacity_in(frequency.buffer_size() * 2, InternalAllocator); + working_buffer.resize(frequency.buffer_size() * 2, 0.into()); + Self { frequency, buffer, @@ -151,6 +157,7 @@ impl Mixer { interrupt_timer, _interrupt_handler: interrupt_handler, + working_buffer: working_buffer.into_boxed_slice(), fifo_timer, } } @@ -198,7 +205,7 @@ impl Mixer { } self.buffer - .write_channels(self.channels.iter_mut().flatten()); + .write_channels(&mut self.working_buffer, self.channels.iter_mut().flatten()); } /// Start playing a given [`SoundChannel`]. @@ -315,7 +322,6 @@ impl SoundBuffer { struct MixerBuffer { buffers: [SoundBuffer; 3], - working_buffer: Box<[Num], InternalAllocator>, frequency: Frequency, state: Mutex>, @@ -355,10 +361,6 @@ impl MixerBufferState { impl MixerBuffer { fn new(frequency: Frequency) -> Self { - let mut working_buffer = - Vec::with_capacity_in(frequency.buffer_size() * 2, InternalAllocator); - working_buffer.resize(frequency.buffer_size() * 2, 0.into()); - MixerBuffer { buffers: [ SoundBuffer::new(frequency), @@ -366,8 +368,6 @@ impl MixerBuffer { SoundBuffer::new(frequency), ], - working_buffer: working_buffer.into_boxed_slice(), - state: Mutex::new(RefCell::new(MixerBufferState { active_buffer: 0, playing_buffer: 0, @@ -392,8 +392,12 @@ impl MixerBuffer { hw::enable_dma_for_sound(right_buffer, LeftOrRight::Right); } - fn write_channels<'a>(&mut self, channels: impl Iterator) { - self.working_buffer.fill(0.into()); + fn write_channels<'a>( + &mut self, + working_buffer: &mut [Num], + channels: impl Iterator, + ) { + working_buffer.fill(0.into()); for channel in channels { if channel.is_done { @@ -422,7 +426,7 @@ impl MixerBuffer { unsafe { agb_rs__mixer_add_stereo( channel.data.as_ptr().add(channel.pos.floor()), - self.working_buffer.as_mut_ptr(), + working_buffer.as_mut_ptr(), channel.volume, ); } @@ -433,7 +437,7 @@ impl MixerBuffer { unsafe { agb_rs__mixer_add( channel.data.as_ptr().add(channel.pos.floor()), - self.working_buffer.as_mut_ptr(), + working_buffer.as_mut_ptr(), playback_speed, left_amount, right_amount, @@ -449,7 +453,7 @@ impl MixerBuffer { let write_buffer = &mut self.buffers[write_buffer_index].0; unsafe { - agb_rs__mixer_collapse(write_buffer.as_mut_ptr(), self.working_buffer.as_ptr()); + agb_rs__mixer_collapse(write_buffer.as_mut_ptr(), working_buffer.as_ptr()); } } }