From 26620e850e4e76acd69713b70dd2fb6289b99c73 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Fri, 29 Oct 2021 15:44:43 +0100 Subject: [PATCH 1/2] Have the constants only in one place --- agb/crt0.s | 2 +- agb/src/sound/mixer/mixer.s | 25 ++++++++++++++++--------- agb/src/sound/mixer/sw_mixer.rs | 12 ++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/agb/crt0.s b/agb/crt0.s index b2c327d0..9a20af8a 100644 --- a/agb/crt0.s +++ b/agb/crt0.s @@ -64,5 +64,5 @@ b .Initialise_mb b 1b .pool -.include "src/sound/mixer/mixer.s" .include "interrupt_handler.s" +.include "src/sound/mixer/mixer.s" diff --git a/agb/src/sound/mixer/mixer.s b/agb/src/sound/mixer/mixer.s index c6abf36e..4ea2f3f4 100644 --- a/agb/src/sound/mixer/mixer.s +++ b/agb/src/sound/mixer/mixer.s @@ -1,4 +1,8 @@ -.equ SOUND_BUFFER_SIZE, 176 +.section .iwram + .global agb_rs__buffer_size + .balign 4 +agb_rs__buffer_size: + .word agb_arm_func agb_rs__mixer_add @ Arguments @@ -19,8 +23,8 @@ agb_arm_func agb_rs__mixer_add modifications_fallback: 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 - mov r8, #SOUND_BUFFER_SIZE @ the number of steps left + mov r5, #0 @ current index we're reading from + ldr r8, agb_rs__buffer_size @ the number of steps left 1: @@ -62,8 +66,8 @@ same_modification: lsrs r7, r7, #1 bne 1b - mov r5, #0 @ current index we're reading from - mov r8, #SOUND_BUFFER_SIZE @ the number of steps left + mov r5, #0 @ current index we're reading from + ldr r8, agb_rs__buffer_size @ the number of steps left .macro mixer_add_loop_simple add r4, r0, r5, asr #8 @ calculate the address of the next read from the sound buffer @@ -132,7 +136,7 @@ agb_arm_func agb_rs__mixer_add_stereo str r4, [r1], #4 @ store the new value, and increment the pointer .endm - mov r8, #SOUND_BUFFER_SIZE + ldr r8, agb_rs__buffer_size 1: mixer_add_loop_simple_stereo mixer_add_loop_simple_stereo @@ -159,8 +163,10 @@ agb_arm_func agb_rs__mixer_collapse @ Arguments: @ r0 = target buffer (i8) @ r1 = input buffer (i16) of fixnums with 4 bits of precision (read in sets of i16 in an i32) + push {r4} - mov r2, #SOUND_BUFFER_SIZE @ loop counter + ldr r2, agb_rs__buffer_size @ loop counter + mov r4, r2 1: @ r12 = *r1; r1++ @@ -173,11 +179,12 @@ agb_arm_func agb_rs__mixer_collapse clamp_s8 r12 @ clamp the audio to 8 bit values clamp_s8 r3 - strb r3, [r0, #SOUND_BUFFER_SIZE] @ *(r0 + SOUND_BUFFER_SIZE) = r3 + strb r3, [r0, r4] @ *(r0 + r4 = SOUND_BUFFER_SIZE) = r3 strb r12, [r0], #1 @ *r0 = r12; r0++ subs r2, r2, #1 @ r2 -= 1 bne 1b @ loop if not 0 + pop {r4} bx lr -agb_arm_end agb_rs__mixer_collapse \ No newline at end of file +agb_arm_end agb_rs__mixer_collapse diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index 5b7e9406..aac9d0bc 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -94,6 +94,16 @@ impl Mixer { const SOUND_FREQUENCY: i32 = 10512; const SOUND_BUFFER_SIZE: usize = 176; +fn set_asm_buffer_size() { + extern "C" { + static mut agb_rs__buffer_size: usize; + } + + unsafe { + agb_rs__buffer_size = SOUND_BUFFER_SIZE; + } +} + #[repr(C, align(4))] struct SoundBuffer([i8; SOUND_BUFFER_SIZE * 2]); @@ -106,6 +116,8 @@ struct MixerBuffer { impl MixerBuffer { fn new() -> Self { + set_asm_buffer_size(); + MixerBuffer { buffer1: SoundBuffer([0; SOUND_BUFFER_SIZE * 2]), buffer2: SoundBuffer([0; SOUND_BUFFER_SIZE * 2]), From c6e7827e343e1b6ec03258d973af6a8640c98a48 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Fri, 29 Oct 2021 15:50:48 +0100 Subject: [PATCH 2/2] Add support for 18157Hz --- agb-sound-converter/Cargo.toml | 3 +++ agb-sound-converter/src/lib.rs | 10 ++++++++-- agb/Cargo.toml | 1 + agb/src/sound/mixer/sw_mixer.rs | 7 +++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/agb-sound-converter/Cargo.toml b/agb-sound-converter/Cargo.toml index 78b5ee6e..a442133b 100644 --- a/agb-sound-converter/Cargo.toml +++ b/agb-sound-converter/Cargo.toml @@ -17,6 +17,9 @@ debug = true [lib] proc-macro = true +[features] +freq18157 = [] + [dependencies] hound = "3.4.0" syn = "1.0.73" diff --git a/agb-sound-converter/src/lib.rs b/agb-sound-converter/src/lib.rs index c38efb79..d373945c 100644 --- a/agb-sound-converter/src/lib.rs +++ b/agb-sound-converter/src/lib.rs @@ -12,6 +12,11 @@ use std::{ }; use syn::parse_macro_input; +#[cfg(not(feature = "freq18157"))] +const FREQUENCY: u32 = 10512; +#[cfg(feature = "freq18157")] +const FREQUENCY: u32 = 18157; + #[proc_macro] pub fn include_wav(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as syn::LitStr); @@ -43,8 +48,9 @@ pub fn include_wav(input: TokenStream) -> TokenStream { assert_eq!( wav_reader.spec().sample_rate, - 10512, - "agb currently only supports sample rate of 10512Hz" + FREQUENCY, + "agb currently only supports sample rate of {}Hz", + FREQUENCY ); let samples = samples_from_reader(wav_reader); diff --git a/agb/Cargo.toml b/agb/Cargo.toml index 19f4862f..8da26911 100644 --- a/agb/Cargo.toml +++ b/agb/Cargo.toml @@ -17,6 +17,7 @@ debug = true [features] default = ["alloc"] alloc = [] +freq18157 = ["agb_sound_converter/freq18157"] [dependencies] bitflags = "1.2" diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index aac9d0bc..a65c7ce4 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -91,9 +91,16 @@ impl Mixer { // I've picked one frequency that works nicely. But there are others that work nicely // which we may want to consider in the future: http://deku.gbadev.org/program/sound1.html +#[cfg(not(feature = "freq18157"))] const SOUND_FREQUENCY: i32 = 10512; +#[cfg(not(feature = "freq18157"))] const SOUND_BUFFER_SIZE: usize = 176; +#[cfg(feature = "freq18157")] +const SOUND_FREQUENCY: i32 = 18157; +#[cfg(feature = "freq18157")] +const SOUND_BUFFER_SIZE: usize = 304; + fn set_asm_buffer_size() { extern "C" { static mut agb_rs__buffer_size: usize;