Struct AudioEngine

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

The main struct of the crate.

This hold all existing SoundSources and cpal::platform::Stream.

Each sound is associated with a group, which is purely used by set_group_volume, to allow mixing multiple sounds together.

Implementations§

Source§

impl<G> AudioEngine<G>
where G: Default + Eq + Hash + Send,

Source

pub fn new_sound<T>(&self, source: T) -> Result<Sound<G>, &'static str>
where T: SoundSource + Send + 'static,

Add a new Sound in the default Group.

Same as calling new_sound_with_group(G::default(), source).

See Self::new_sound_with_group, for more information.

Source§

impl AudioEngine

Source

pub fn new() -> Result<AudioEngine, &'static str>

Tries to create a new AudioEngine.

cpal will spawn a new thread where the sound samples will be sampled, mixed, and outputed to the output stream.

Source

pub fn with_groups<G>() -> Result<AudioEngine<G>, &'static str>
where G: Eq + Hash + Send,

Tries to create a new AudioEngine, with the given type to represent sound groups.

cpal will spawn a new thread where the sound samples will be sampled, mixed, and outputed to the output stream.

§Example
use audio_engine::{AudioEngine, WavDecoder};

#[derive(Eq, Hash, PartialEq)]
enum Group {
    Effect,
    Music,
}

let audio_engine = AudioEngine::with_groups::<Group>()?;
let mut fx = audio_engine.new_sound_with_group(Group::Effect, my_fx)?;
let mut music = audio_engine.new_sound_with_group(Group::Music, my_music)?;

fx.play();
music.play();

// decrease music volume, for example
audio_engine.set_group_volume(Group::Music, 0.1);
Source§

impl<G> AudioEngine<G>
where G: Eq + Hash + Send,

Source

pub fn sample_rate(&self) -> u32

The sample rate that is currently being outputed to the device.

Source

pub fn channels(&self) -> u16

The sample rate of the current output device.

May change when the device changes.

Source

pub fn new_sound_with_group<T>( &self, group: G, source: T, ) -> Result<Sound<G>, &'static str>
where T: SoundSource + Send + 'static,

Add a new Sound with the given Group.

The added sound starts in the stopped state, and play must be called to start playing it.

If the number of channels of source mismatch the output number of channel, source will be wrapped in a ChannelConverter.

If the sample rate of source mismatch the output sample rate, source will be wrapped in a SampleRateConverter.

Source

pub fn set_group_volume(&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.

Trait Implementations§

Source§

impl NewSoundEffect for AudioEngine

Source§

fn load_from_bytes( &self, bytes: &'static [u8], duration: f64, ) -> Result<SoundEffect, GameUtilError>

Auto Trait Implementations§

§

impl<G> Freeze for AudioEngine<G>

§

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

§

impl<G> Send for AudioEngine<G>

§

impl<G> Sync for AudioEngine<G>

§

impl<G> Unpin for AudioEngine<G>

§

impl<G = ()> !UnwindSafe for AudioEngine<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> AnyToAny for T
where T: 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSync for T
where T: Sync,