Add collapse test and fix bug uncovered by it

This commit is contained in:
Gwilym Inzani 2023-06-18 16:28:32 +01:00
parent b2dcd8c854
commit 5a374ba4f0
2 changed files with 65 additions and 4 deletions

View file

@ -157,12 +157,12 @@ agb_arm_func agb_rs__mixer_collapse
push {{r4-r11,lr}} push {{r4-r11,lr}}
CONST_0 .req r7 CONST_0 .req r7
CONST_127 .req r8 CONST_128 .req r8
TEMP .req r10 TEMP .req r10
SWAP_SIGN .req r11 SWAP_SIGN .req r11
ldr CONST_0, =0 ldr CONST_0, =0
ldr CONST_127, =127 ldr CONST_128, =128
ldr SWAP_SIGN, =0x80808080 ldr SWAP_SIGN, =0x80808080
mov r4, r2 mov r4, r2
@ -192,8 +192,8 @@ SWAP_SIGN .req r11
.macro load_sample left_reg:req right_reg:req .macro load_sample left_reg:req right_reg:req
mov \right_reg, \left_reg, lsl #16 @ push the sample 16 bits first 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 \right_reg, CONST_128, \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 \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 \left_reg @ clamp the audio to 8 bit values
clamp_s8 \right_reg clamp_s8 \right_reg

View file

@ -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<i16, 4>] = &[
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
]
);
}
}