Start refactoring towards needing &self in write_channels

This commit is contained in:
Gwilym Kuiper 2022-12-10 00:19:05 +00:00
parent a870a1deda
commit 93d0fe5700

View file

@ -89,6 +89,8 @@ pub struct Mixer {
indices: [i32; 8], indices: [i32; 8],
frequency: Frequency, frequency: Frequency,
working_buffer: Box<[Num<i16, 4>], InternalAllocator>,
fifo_timer: Timer, fifo_timer: Timer,
} }
@ -142,6 +144,10 @@ impl Mixer {
set_asm_buffer_size(frequency); 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 { Self {
frequency, frequency,
buffer, buffer,
@ -151,6 +157,7 @@ impl Mixer {
interrupt_timer, interrupt_timer,
_interrupt_handler: interrupt_handler, _interrupt_handler: interrupt_handler,
working_buffer: working_buffer.into_boxed_slice(),
fifo_timer, fifo_timer,
} }
} }
@ -198,7 +205,7 @@ impl Mixer {
} }
self.buffer 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`]. /// Start playing a given [`SoundChannel`].
@ -315,7 +322,6 @@ impl SoundBuffer {
struct MixerBuffer { struct MixerBuffer {
buffers: [SoundBuffer; 3], buffers: [SoundBuffer; 3],
working_buffer: Box<[Num<i16, 4>], InternalAllocator>,
frequency: Frequency, frequency: Frequency,
state: Mutex<RefCell<MixerBufferState>>, state: Mutex<RefCell<MixerBufferState>>,
@ -355,10 +361,6 @@ impl MixerBufferState {
impl MixerBuffer { impl MixerBuffer {
fn new(frequency: Frequency) -> Self { 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 { MixerBuffer {
buffers: [ buffers: [
SoundBuffer::new(frequency), SoundBuffer::new(frequency),
@ -366,8 +368,6 @@ impl MixerBuffer {
SoundBuffer::new(frequency), SoundBuffer::new(frequency),
], ],
working_buffer: working_buffer.into_boxed_slice(),
state: Mutex::new(RefCell::new(MixerBufferState { state: Mutex::new(RefCell::new(MixerBufferState {
active_buffer: 0, active_buffer: 0,
playing_buffer: 0, playing_buffer: 0,
@ -392,8 +392,12 @@ impl MixerBuffer {
hw::enable_dma_for_sound(right_buffer, LeftOrRight::Right); hw::enable_dma_for_sound(right_buffer, LeftOrRight::Right);
} }
fn write_channels<'a>(&mut self, channels: impl Iterator<Item = &'a mut SoundChannel>) { fn write_channels<'a>(
self.working_buffer.fill(0.into()); &mut self,
working_buffer: &mut [Num<i16, 4>],
channels: impl Iterator<Item = &'a mut SoundChannel>,
) {
working_buffer.fill(0.into());
for channel in channels { for channel in channels {
if channel.is_done { if channel.is_done {
@ -422,7 +426,7 @@ impl MixerBuffer {
unsafe { unsafe {
agb_rs__mixer_add_stereo( agb_rs__mixer_add_stereo(
channel.data.as_ptr().add(channel.pos.floor()), channel.data.as_ptr().add(channel.pos.floor()),
self.working_buffer.as_mut_ptr(), working_buffer.as_mut_ptr(),
channel.volume, channel.volume,
); );
} }
@ -433,7 +437,7 @@ impl MixerBuffer {
unsafe { unsafe {
agb_rs__mixer_add( agb_rs__mixer_add(
channel.data.as_ptr().add(channel.pos.floor()), channel.data.as_ptr().add(channel.pos.floor()),
self.working_buffer.as_mut_ptr(), working_buffer.as_mut_ptr(),
playback_speed, playback_speed,
left_amount, left_amount,
right_amount, right_amount,
@ -449,7 +453,7 @@ impl MixerBuffer {
let write_buffer = &mut self.buffers[write_buffer_index].0; let write_buffer = &mut self.buffers[write_buffer_index].0;
unsafe { 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());
} }
} }
} }