Struct agb::sound::mixer::Mixer

source ·
pub struct Mixer<'gba> { /* private fields */ }
Expand description

The main software mixer struct.

Tracks which sound channels are currently playing and handles actually playing them. You should not create this struct directly, instead creating it through the Gba struct as follows:

let mut mixer = gba.mixer.mixer(Frequency::Hz10512);

Example

// Outside your main function in global scope:
const MY_CRAZY_SOUND: &[u8] = include_wav!("examples/sfx/jump.wav");

// in your main function:
let mut mixer = gba.mixer.mixer(Frequency::Hz10512);
let mut channel = SoundChannel::new(MY_CRAZY_SOUND);
channel.stereo();
let _ = mixer.play_sound(channel);

loop {
   mixer.frame();
   vblank.wait_for_vblank();
}

Implementations§

source§

impl Mixer<'_>

source

pub fn enable(&mut self)

Enable sound output

You must call this method in order to start playing sound. You can do as much set up before this as you like, but you will not get any sound out of the console until this method is called.

source

pub fn frame(&mut self)

Do the CPU intensive mixing for the next frame’s worth of data.

This is where almost all of the CPU time for the mixer is done, and must be done every frame or you will get crackling sounds.

Normally you would run this during vdraw, just before the vblank interrupt.

Example
loop {
    mixer.frame();
    vblank.wait_for_vblank();
}
source

pub fn play_sound(&mut self, new_channel: SoundChannel) -> Option<ChannelId>

Start playing a given SoundChannel.

Returns a ChannelId which you can later use to modify the playing sound.

Will first try to play the sound in an unused channel (of the 8 possible channels) followed by overriding a low priority sound (if the sound channel being passed in is high priority).

Returns Some if the channel is now playing (which is guaranteed if the channel is high priority) or None if it failed to find a slot.

Panics if you try to play a high priority sound and there are no free channels.

Example
let mut channel = SoundChannel::new_high_priority(MY_BGM);
let bgm_channel_id = mixer.play_sound(channel).unwrap(); // will always be Some if high priority
source

pub fn channel(&mut self, id: &ChannelId) -> Option<&mut SoundChannel>

Lets you modify an already playing channel.

Allows you to change the volume, panning or stop an already playing channel. Will return Some if the channel is still playing, or None if it has already finished.

Example
let mut channel = SoundChannel::new_high_priority(MY_BGM);
let bgm_channel_id = mixer.play_sound(channel).unwrap(); // will always be Some if high priority

// Later, stop that particular channel
mixer.channel(&bgm_channel_id).expect("Expected still to be playing").stop();

Auto Trait Implementations§

§

impl<'gba> !RefUnwindSafe for Mixer<'gba>

§

impl<'gba> !Send for Mixer<'gba>

§

impl<'gba> !Sync for Mixer<'gba>

§

impl<'gba> Unpin for Mixer<'gba>

§

impl<'gba> !UnwindSafe for Mixer<'gba>

Blanket Implementations§

§

impl<T> Any for Twhere T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for Twhere T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for Twhere U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.