From 773729de98955370c28dbb00ebf4a1cea08341db Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Fri, 25 Jun 2021 21:53:42 +0100 Subject: [PATCH] Update example to allow you to change where the sound is coming from --- agb/examples/mixer_basic.rs | 25 ++++++++++++++++++++++++- agb/src/sound/mixer/sw_mixer.rs | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/agb/examples/mixer_basic.rs b/agb/examples/mixer_basic.rs index 4d479687..fd47bf2c 100644 --- a/agb/examples/mixer_basic.rs +++ b/agb/examples/mixer_basic.rs @@ -5,6 +5,8 @@ extern crate agb; use agb::sound::mixer::SoundChannel; use agb::Gba; +use agb::input::{ButtonController, Tri}; +use agb::number::Num; // Music - "I will not let you let me down" by Josh Woodward, free download at http://joshwoodward.com const I_WILL_NOT_LET_YOU_LET_ME_DOWN: &[u8] = include_bytes!("i-will-not-let-you-let-me-down.raw"); @@ -12,15 +14,36 @@ const I_WILL_NOT_LET_YOU_LET_ME_DOWN: &[u8] = include_bytes!("i-will-not-let-you #[no_mangle] pub fn main() -> ! { let mut gba = Gba::new(); + let mut input = ButtonController::new(); let vblank_provider = gba.display.vblank.get(); let mut mixer = gba.mixer.mixer(); mixer.enable(); let channel = SoundChannel::new(I_WILL_NOT_LET_YOU_LET_ME_DOWN); - mixer.play_sound(channel); + let channel_id = mixer.play_sound(channel).unwrap(); loop { + input.update(); + + { + let channel = mixer.get_channel(&channel_id).unwrap(); + + let half: Num = Num::new(1) / 2; + let half_usize: Num = Num::new(1) / 2; + match input.x_tri() { + Tri::Negative => channel.panning(-half), + Tri::Zero => channel.panning(0.into()), + Tri::Positive => channel.panning(half), + }; + + match input.y_tri() { + Tri::Negative => channel.playback(half_usize.change_base() + 1), + Tri::Zero => channel.playback(1.into()), + Tri::Positive => channel.playback(half_usize), + }; + } + vblank_provider.wait_for_VBlank(); mixer.vblank(); } diff --git a/agb/src/sound/mixer/sw_mixer.rs b/agb/src/sound/mixer/sw_mixer.rs index 755025b0..d7ac10ec 100644 --- a/agb/src/sound/mixer/sw_mixer.rs +++ b/agb/src/sound/mixer/sw_mixer.rs @@ -63,7 +63,7 @@ impl Mixer { panic!("Cannot play more than 16 sounds at once"); } - pub fn get_channel(&mut self, id: ChannelId) -> Option<&'_ mut SoundChannel> { + pub fn get_channel(&mut self, id: &ChannelId) -> Option<&'_ mut SoundChannel> { if let Some(channel) = &mut self.channels[id.0] { if self.indices[id.0] == id.1 { return Some(channel);