Compile in release and make actually safe

This commit is contained in:
Gwilym Inzani 2023-07-16 20:52:29 +01:00
parent d4b2a2bc1a
commit 938f05f8d1
2 changed files with 36 additions and 21 deletions

View file

@ -414,10 +414,9 @@ impl MixerBuffer {
) { ) {
working_buffer.fill(0.into()); working_buffer.fill(0.into());
let channels = channels for channel in channels.filter(|channel| !channel.is_done) {
.filter(|channel| !channel.is_done) if channel.volume != 0.into() {
.filter_map(|channel| { if channel.is_stereo {
let playback_speed = if channel.is_stereo {
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
{ {
@ -425,21 +424,10 @@ impl MixerBuffer {
channel.pos = 0.into(); channel.pos = 0.into();
} else { } else {
channel.is_done = true; channel.is_done = true;
return None; continue;
} }
} }
2.into()
} else {
channel.playback_speed
};
Some((channel, playback_speed))
});
for (channel, playback_speed) in channels {
if channel.volume != 0.into() {
if channel.is_stereo {
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),
@ -455,6 +443,22 @@ impl MixerBuffer {
let left_amount = ((-channel.panning + 1) / 2) * channel.volume; let left_amount = ((-channel.panning + 1) / 2) * channel.volume;
let channel_len = Num::<u32, 8>::new(channel.data.len() as u32); let channel_len = Num::<u32, 8>::new(channel.data.len() as u32);
let mut playback_speed = channel.playback_speed;
while playback_speed >= channel_len {
playback_speed -= channel_len;
}
// SAFETY: always aligned correctly by construction
let working_buffer_i32: &mut [i32] = unsafe {
core::slice::from_raw_parts_mut(
working_buffer.as_mut_ptr().cast(),
working_buffer.len() / 2,
)
};
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() { 'outer: for i in 0..self.frequency.buffer_size() {
if channel.pos >= channel_len { if channel.pos >= channel_len {
@ -466,11 +470,13 @@ impl MixerBuffer {
} }
} }
let value = channel.data[channel.pos.floor() as usize] as i8 as i16; // SAFETY: channel.pos < channel_len by the above if statement and the fact we reduce the playback speed
let value =
working_buffer[2 * i] += right_amount * value; unsafe { *channel.data.get_unchecked(channel.pos.floor() as usize) }
working_buffer[2 * i + 1] += left_amount * value; 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; channel.pos += playback_speed;
} }
} }

View file

@ -14,3 +14,12 @@ xm = ["dep:agb_xm"]
agb_xm = { version = "0.15.0", path = "../agb-xm", optional = true } agb_xm = { version = "0.15.0", path = "../agb-xm", optional = true }
agb = { version = "0.15.0", path = "../../agb" } agb = { version = "0.15.0", path = "../../agb" }
agb_tracker_interop = { version = "0.15.0", path = "../agb-tracker-interop", default-features = false } agb_tracker_interop = { version = "0.15.0", path = "../agb-tracker-interop", default-features = false }
[profile.dev]
opt-level = 3
debug = true
[profile.release]
opt-level = 3
lto = "fat"
debug = true