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],
frequency: Frequency,
working_buffer: Box<[Num<i16, 4>], 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<i16, 4>], InternalAllocator>,
frequency: Frequency,
state: Mutex<RefCell<MixerBufferState>>,
@ -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<Item = &'a mut SoundChannel>) {
self.working_buffer.fill(0.into());
fn write_channels<'a>(
&mut self,
working_buffer: &mut [Num<i16, 4>],
channels: impl Iterator<Item = &'a mut SoundChannel>,
) {
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());
}
}
}