Pass the buffer size rather than using the global variable for it

This commit is contained in:
Gwilym Inzani 2023-07-14 22:50:11 +01:00
parent 86db9d15bf
commit d38fea7f7a
2 changed files with 14 additions and 26 deletions

View file

@ -1,9 +1,3 @@
.section .iwram.buffer_size
.global agb_rs__buffer_size
.balign 4
agb_rs__buffer_size:
.word 0
.macro mixer_add fn_name:req is_first:req
agb_arm_func \fn_name
@ Arguments
@ -12,8 +6,9 @@ agb_arm_func \fn_name
@ r2 - playback speed (usize fixnum with 8 bits)
@ r3 - amount to modify the left channel by (u16 fixnum with 4 bits)
@ stack position 1 - amount to modify the right channel by (u16 fixnum with 4 bits)
@ stack position 2 - the buffer_size (usize)
@
@ The sound buffer must be SOUND_BUFFER_SIZE * 2 in size = 176 * 2
@ The sound buffer must be buffer_size * 2 in size
push {{r4-r8}}
ldr r7, [sp, #20] @ load the right channel modification amount into r7
@ -25,9 +20,7 @@ agb_arm_func \fn_name
orr r7, r7, r3, lsl #16 @ r7 now is the left channel followed by the right channel modifications.
mov r5, #0 @ current index we're reading from
ldr r8, =agb_rs__buffer_size @ the number of steps left
ldr r8, [r8]
ldr r8, [sp, #24]
1:
.rept 4
@ -69,8 +62,7 @@ agb_arm_func \fn_name
sub r3, r3, #1
mov r5, #0 @ current index we're reading from
ldr r8, =agb_rs__buffer_size @ the number of steps left
ldr r8, [r8]
ldr r8, [sp, #24] @ the number of steps we have left
1:
.rept 4
@ -110,14 +102,14 @@ agb_arm_func \fn_name
@ r0 - pointer to the data to be copied (u8 array)
@ r1 - pointer to the sound buffer (i16 array which will alternate left and right channels, 32-bit aligned)
@ r2 - volume to play the sound at
@ r3 - the buffer size
@
@ The sound buffer must be SOUND_BUFFER_SIZE * 2 in size = 176 * 2
push {{r4-r11}}
ldr r5, =0x00000FFF
ldr r8, =agb_rs__buffer_size
ldr r8, [r8]
mov r8, r3
.macro add_stereo_sample sample_reg:req
ldrsh r6, [r0], #2 @ load the current sound sample to r6

View file

@ -27,6 +27,7 @@ extern "C" {
playback_speed: Num<u32, 8>,
left_amount: Num<i16, 4>,
right_amount: Num<i16, 4>,
buffer_size: usize,
);
fn agb_rs__mixer_add_first(
@ -35,18 +36,21 @@ extern "C" {
playback_speed: Num<u32, 8>,
left_amount: Num<i16, 4>,
right_amount: Num<i16, 4>,
buffer_size: usize,
);
fn agb_rs__mixer_add_stereo(
sound_data: *const u8,
sound_buffer: *mut Num<i16, 4>,
volume: Num<i16, 4>,
buffer_size: usize,
);
fn agb_rs__mixer_add_stereo_first(
sound_data: *const u8,
sound_buffer: *mut Num<i16, 4>,
volume: Num<i16, 4>,
buffer_size: usize,
);
fn agb_rs__mixer_collapse(
@ -164,8 +168,6 @@ impl Mixer<'_> {
})
};
set_asm_buffer_size(frequency);
let mut working_buffer =
Vec::with_capacity_in(frequency.buffer_size() * 2, InternalAllocator);
working_buffer.resize(frequency.buffer_size() * 2, 0.into());
@ -322,16 +324,6 @@ impl Mixer<'_> {
}
}
fn set_asm_buffer_size(frequency: Frequency) {
extern "C" {
static mut agb_rs__buffer_size: usize;
}
unsafe {
agb_rs__buffer_size = frequency.buffer_size();
}
}
struct SoundBuffer(Box<[i8], InternalAllocator>);
impl SoundBuffer {
@ -452,6 +444,7 @@ impl MixerBuffer {
channel.data.as_ptr().add(channel.pos.floor() as usize),
working_buffer.as_mut_ptr(),
channel.volume,
self.frequency.buffer_size(),
);
}
} else {
@ -465,6 +458,7 @@ impl MixerBuffer {
playback_speed,
left_amount,
right_amount,
self.frequency.buffer_size(),
);
}
}
@ -485,6 +479,7 @@ impl MixerBuffer {
channel.data.as_ptr().add(channel.pos.floor() as usize),
working_buffer.as_mut_ptr(),
channel.volume,
self.frequency.buffer_size(),
);
}
} else {
@ -498,6 +493,7 @@ impl MixerBuffer {
playback_speed,
left_amount,
right_amount,
self.frequency.buffer_size(),
);
}
}