Extract methods

This commit is contained in:
Gwilym Inzani 2023-07-16 20:55:28 +01:00
parent 938f05f8d1
commit d929a1689a

View file

@ -1,5 +1,6 @@
use core::cell::RefCell;
use core::marker::PhantomData;
use core::ops::ControlFlow;
use core::pin::Pin;
use alloc::boxed::Box;
@ -417,6 +418,25 @@ impl MixerBuffer {
for channel in channels.filter(|channel| !channel.is_done) {
if channel.volume != 0.into() {
if channel.is_stereo {
self.write_stereo(channel, working_buffer);
} else {
self.write_mono(channel, working_buffer);
}
}
}
let write_buffer = free(|cs| self.state.borrow(cs).borrow_mut().active_advanced());
unsafe {
agb_rs__mixer_collapse(
write_buffer,
working_buffer.as_ptr(),
self.frequency.buffer_size(),
);
}
}
fn write_stereo(&self, channel: &mut SoundChannel, working_buffer: &mut [Num<i16, 4>]) {
if (channel.pos + 2 * self.frequency.buffer_size() as u32).floor()
>= channel.data.len() as u32
{
@ -424,10 +444,9 @@ impl MixerBuffer {
channel.pos = 0.into();
} else {
channel.is_done = true;
continue;
return;
}
}
unsafe {
agb_rs__mixer_add_stereo(
channel.data.as_ptr().add(channel.pos.floor() as usize),
@ -438,7 +457,9 @@ impl MixerBuffer {
}
channel.pos += 2 * self.frequency.buffer_size() as u32;
} else {
}
fn write_mono(&self, channel: &mut SoundChannel, working_buffer: &mut [Num<i16, 4>]) {
let right_amount = ((channel.panning + 1) / 2) * channel.volume;
let left_amount = ((-channel.panning + 1) / 2) * channel.volume;
@ -457,8 +478,8 @@ impl MixerBuffer {
)
};
let mul_amount = ((left_amount.to_raw() as i32) << 16)
| (right_amount.to_raw() as i32 & 0x0000ffff);
let mul_amount =
((left_amount.to_raw() as i32) << 16) | (right_amount.to_raw() as i32 & 0x0000ffff);
'outer: for i in 0..self.frequency.buffer_size() {
if channel.pos >= channel_len {
@ -472,27 +493,13 @@ impl MixerBuffer {
// SAFETY: channel.pos < channel_len by the above if statement and the fact we reduce the playback speed
let value =
unsafe { *channel.data.get_unchecked(channel.pos.floor() as usize) }
as i8 as i32;
unsafe { *channel.data.get_unchecked(channel.pos.floor() as usize) } as i8 as i32;
// SAFETY: working buffer length = self.frequency.buffer_size()
unsafe { *working_buffer_i32.get_unchecked_mut(i) += value * mul_amount };
channel.pos += playback_speed;
}
}
}
}
let write_buffer = free(|cs| self.state.borrow(cs).borrow_mut().active_advanced());
unsafe {
agb_rs__mixer_collapse(
write_buffer,
working_buffer.as_ptr(),
self.frequency.buffer_size(),
);
}
}
}
#[cfg(test)]