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::cell::RefCell;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::ops::ControlFlow;
use core::pin::Pin; use core::pin::Pin;
use alloc::boxed::Box; use alloc::boxed::Box;
@ -417,6 +418,25 @@ impl MixerBuffer {
for channel in channels.filter(|channel| !channel.is_done) { for channel in channels.filter(|channel| !channel.is_done) {
if channel.volume != 0.into() { if channel.volume != 0.into() {
if channel.is_stereo { 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() if (channel.pos + 2 * self.frequency.buffer_size() as u32).floor()
>= channel.data.len() as u32 >= channel.data.len() as u32
{ {
@ -424,10 +444,9 @@ impl MixerBuffer {
channel.pos = 0.into(); channel.pos = 0.into();
} else { } else {
channel.is_done = true; channel.is_done = true;
continue; return;
} }
} }
unsafe { unsafe {
agb_rs__mixer_add_stereo( agb_rs__mixer_add_stereo(
channel.data.as_ptr().add(channel.pos.floor() as usize), 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; 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 right_amount = ((channel.panning + 1) / 2) * channel.volume;
let left_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) let mul_amount =
| (right_amount.to_raw() as i32 & 0x0000ffff); ((left_amount.to_raw() as i32) << 16) | (right_amount.to_raw() as i32 & 0x0000ffff);
'outer: for i in 0..self.frequency.buffer_size() { 'outer: for i in 0..self.frequency.buffer_size() {
if channel.pos >= channel_len { 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 // SAFETY: channel.pos < channel_len by the above if statement and the fact we reduce the playback speed
let value = let value =
unsafe { *channel.data.get_unchecked(channel.pos.floor() as usize) } unsafe { *channel.data.get_unchecked(channel.pos.floor() as usize) } as i8 as i32;
as i8 as i32;
// SAFETY: working buffer length = self.frequency.buffer_size() // SAFETY: working buffer length = self.frequency.buffer_size()
unsafe { *working_buffer_i32.get_unchecked_mut(i) += value * mul_amount }; unsafe { *working_buffer_i32.get_unchecked_mut(i) += value * mul_amount };
channel.pos += playback_speed; 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)] #[cfg(test)]