Struct Mixer

Source
pub struct Mixer<G: Eq + Hash + Send + 'static = ()> { /* private fields */ }
Expand description

Keep track of each Sound, and mix they output together.

Implementations§

Source§

impl<G: Eq + Hash + Send + 'static> Mixer<G>

Source

pub fn new(channels: u16, sample_rate: SampleRate) -> Self

Create a new Mixer.

The created Mixer output samples with given sample rate and number of channels. This configuration can be changed by calling set_config.

Source

pub fn set_config(&mut self, channels: u16, sample_rate: SampleRate)

Change the number of channels and the sample rate.

This keep also keep all currently playing sounds, and convert them to the new config, if necessary.

Source

pub fn add_sound(&mut self, group: G, sound: Box<dyn SoundSource + Send>) -> u64

Add new sound to the Mixer.

Return the SoundId associated with that sound. This Id is globally unique.

The added sound is started in stopped state, and play must be called to start playing it. mark_to_remove is true by default.

Source

pub fn play(&mut self, id: u64)

Start playing the sound associated with the given id.

If the sound was paused or stop, it will start playing again. Otherwise, does nothing.

Source

pub fn pause(&mut self, id: u64)

Pause the sound associated with the given id.

If the sound is playing, it will pause. If play is called, this sound will continue from where it was when paused. If the sound is not playing, does nothing.

Source

pub fn stop(&mut self, id: u64)

Stop the sound associated with the given id.

If the sound is playing, it will pause and reset the song. When play is called, this sound will start from the begging.

Even if the sound is not playing, it will reset the sound to the start. If the sound is marked to be removed, this sound will be removed from the Mixer.

Source

pub fn reset(&mut self, id: u64)

Reset the sound associated with the given id.

This reset the sound to the start, the sound being playing or not.

Source

pub fn set_loop(&mut self, id: u64, looping: bool)

Set if the sound associated with the given id will loop.

If true, ever time the sound reachs its end, it will reset, and continue to play in a loop.

This also set mark_to_remove to false.

Source

pub fn set_volume(&mut self, id: u64, volume: f32)

Set the volume of the sound associated with the given id.

The output samples of the SoundSource assicociated with the given id will be multiplied by this volume.

Source

pub fn set_group_volume(&mut self, group: G, volume: f32)

Set the volume of the given group.

The volume of all sounds associated with this group is multiplied by this volume.

Source

pub fn mark_to_remove(&mut self, id: u64, drop: bool)

Mark if the sound will be removed after it reachs its end.

If false, it will be possible to reset the sound and play it again after it has already reached its end. Otherwise, the sound will be removed when it reachs its end, even if it is marked to loop.

Source

pub fn sound_count(&self) -> usize

The number of sounds in the mixer.

This include the sounds that are currently stopped.

Source

pub fn playing_count(&self) -> usize

The number of sounds being played currently.

Does not include the sounds that are currently stopped.

Trait Implementations§

Source§

impl<G: Eq + Hash + Send + 'static> SoundSource for Mixer<G>

Source§

fn channels(&self) -> u16

Return the number of channels.
Source§

fn sample_rate(&self) -> u32

Return the sample rate.
Source§

fn reset(&mut self)

Start the sound from the begining.
Source§

fn write_samples(&mut self, buffer: &mut [i16]) -> usize

Write the samples to buffer. Read more

Auto Trait Implementations§

§

impl<G> Freeze for Mixer<G>

§

impl<G = ()> !RefUnwindSafe for Mixer<G>

§

impl<G> Send for Mixer<G>

§

impl<G = ()> !Sync for Mixer<G>

§

impl<G> Unpin for Mixer<G>
where G: Unpin,

§

impl<G = ()> !UnwindSafe for Mixer<G>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.