diff --git a/agb/src/sound/mixer/mixer.s b/agb/src/sound/mixer/mixer.s index 66bec925..2299a22b 100644 --- a/agb/src/sound/mixer/mixer.s +++ b/agb/src/sound/mixer/mixer.s @@ -157,12 +157,12 @@ agb_arm_func agb_rs__mixer_collapse push {{r4-r11,lr}} CONST_0 .req r7 -CONST_127 .req r8 +CONST_128 .req r8 TEMP .req r10 SWAP_SIGN .req r11 ldr CONST_0, =0 - ldr CONST_127, =127 + ldr CONST_128, =128 ldr SWAP_SIGN, =0x80808080 mov r4, r2 @@ -192,8 +192,8 @@ SWAP_SIGN .req r11 .macro load_sample left_reg:req right_reg:req mov \right_reg, \left_reg, lsl #16 @ push the sample 16 bits first - add \right_reg, CONST_127, \right_reg, asr #20 @ move right sample back to being the correct value - add \left_reg, CONST_127, \left_reg, asr #20 @ now we only have the left sample + add \right_reg, CONST_128, \right_reg, asr #20 @ move right sample back to being the correct value + add \left_reg, CONST_128, \left_reg, asr #20 @ now we only have the left sample clamp_s8 \left_reg @ clamp the audio to 8 bit values clamp_s8 \right_reg diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index 5c01181b..2b75734e 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -470,3 +470,64 @@ impl MixerBuffer { } } } + +#[cfg(test)] +mod test { + use crate::fixnum::num; + use alloc::vec; + + use super::*; + + #[test_case] + fn collapse_should_correctly_reduce_size_of_input(_: &mut crate::Gba) { + let input: &[Num] = &[ + num!(10.0), + num!(10.0), + num!(5.0), + num!(5.0), + num!(-10.0), + num!(-10.5), + num!(-5.9), + num!(-5.2), + num!(0.0), + num!(1.1), + num!(2.2), + num!(3.3), + num!(155.4), + num!(-230.5), + num!(400.6), + num!(-700.7), + num!(10.0), + num!(10.0), + num!(5.0), + num!(5.0), + num!(-10.0), + num!(-10.5), + num!(-5.9), + num!(-5.2), + num!(0.0), + num!(1.1), + num!(2.2), + num!(3.3), + num!(155.4), + num!(-230.5), + num!(400.6), + num!(-700.7), + ]; + + let mut output_buffer = vec![0i8; input.len()]; + + unsafe { + agb_rs__mixer_collapse(output_buffer.as_mut_ptr(), input.as_ptr(), input.len() / 2); + } + + // output will be unzipped, so input is LRLRLRLRLRLRLR... and output is LLLLLLRRRRRR + assert_eq!( + output_buffer, + &[ + 10, 5, -10, -6, 0, 2, 127, 127, 10, 5, -10, -6, 0, 2, 127, 127, 10, 5, -11, -6, 1, + 3, -128, -128, 10, 5, -11, -6, 1, 3, -128, -128 + ] + ); + } +}