Update example to allow you to change where the sound is coming from

This commit is contained in:
Gwilym Kuiper 2021-06-25 21:53:42 +01:00
parent 80ce5173e1
commit 773729de98
2 changed files with 25 additions and 2 deletions

View file

@ -5,6 +5,8 @@ extern crate agb;
use agb::sound::mixer::SoundChannel; use agb::sound::mixer::SoundChannel;
use agb::Gba; 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 // 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"); 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] #[no_mangle]
pub fn main() -> ! { pub fn main() -> ! {
let mut gba = Gba::new(); let mut gba = Gba::new();
let mut input = ButtonController::new();
let vblank_provider = gba.display.vblank.get(); let vblank_provider = gba.display.vblank.get();
let mut mixer = gba.mixer.mixer(); let mut mixer = gba.mixer.mixer();
mixer.enable(); mixer.enable();
let channel = SoundChannel::new(I_WILL_NOT_LET_YOU_LET_ME_DOWN); 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 { loop {
input.update();
{
let channel = mixer.get_channel(&channel_id).unwrap();
let half: Num<i16, 4> = Num::new(1) / 2;
let half_usize: Num<usize, 8> = 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(); vblank_provider.wait_for_VBlank();
mixer.vblank(); mixer.vblank();
} }

View file

@ -63,7 +63,7 @@ impl Mixer {
panic!("Cannot play more than 16 sounds at once"); 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 let Some(channel) = &mut self.channels[id.0] {
if self.indices[id.0] == id.1 { if self.indices[id.0] == id.1 {
return Some(channel); return Some(channel);