agb/agb/examples/mixer_basic.rs

58 lines
1.7 KiB
Rust
Raw Normal View History

#![no_std]
#![no_main]
extern crate agb;
use agb::input::{Button, ButtonController, Tri};
use agb::number::Num;
2021-06-06 21:44:32 +10:00
use agb::sound::mixer::SoundChannel;
2021-10-18 08:38:19 +11:00
use agb::{include_wav, Gba};
2021-10-18 08:38:19 +11:00
// Music - "Dead Code" by Josh Woodward, free download at http://joshwoodward.com
const DEAD_CODE: &[u8] = include_wav!("examples/JoshWoodward-DeadCode.wav");
#[agb::entry]
fn main() -> ! {
2021-06-06 21:44:32 +10:00
let mut gba = Gba::new();
let mut input = ButtonController::new();
2021-07-04 03:57:17 +10:00
let vblank_provider = agb::interrupt::VBlank::get();
2021-06-06 21:44:32 +10:00
let mut timers = gba.timers.timers();
let mut mixer = gba.mixer.mixer(&mut timers.timer0);
mixer.enable();
2021-10-18 08:38:19 +11:00
let channel = SoundChannel::new(DEAD_CODE);
let channel_id = mixer.play_sound(channel).unwrap();
2021-06-06 21:44:32 +10:00
loop {
input.update();
{
2021-06-26 06:54:12 +10:00
if let Some(channel) = mixer.get_channel(&channel_id) {
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),
};
2021-06-26 06:54:12 +10:00
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),
};
2021-06-26 07:01:50 +10:00
if input.is_pressed(Button::L) {
channel.volume(half);
} else {
channel.volume(1.into());
}
2021-06-26 06:54:12 +10:00
}
}
vblank_provider.wait_for_vblank();
2021-06-06 21:44:32 +10:00
mixer.vblank();
}
}