Struct Audio

Source
pub struct Audio { /* private fields */ }
Expand description

A simple 4-track audio system to load/decode audio files from disk to play later. Supported formats are: MP3, WAV, Vorbis and Flac.

Implementations§

Source§

impl Audio

Source

pub fn new() -> Self

Create a new sound subsystem. You only need one of these – you can use it to load and play any number of audio clips.

Examples found in repository?
examples/play.rs (line 5)
3fn main() {
4    // Create a 4-channel audio subsystem.
5    let mut audio = Audio::new();
6    // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7    // but at least doesn't crash.  Helpful for CI.
8    if audio.disabled() {
9        println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10    }
11    audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12    audio.play("startup"); // Execution continues while playback occurs in another thread.
13    audio.wait(); // Block until sounds finish playing
14}
Source

pub fn disabled(&self) -> bool

If no sound device was detected, the audio subsystem will run in a disabled mode that doesn’t actually do anything. This method indicates whether audio is disabled.

Examples found in repository?
examples/play.rs (line 8)
3fn main() {
4    // Create a 4-channel audio subsystem.
5    let mut audio = Audio::new();
6    // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7    // but at least doesn't crash.  Helpful for CI.
8    if audio.disabled() {
9        println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10    }
11    audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12    audio.play("startup"); // Execution continues while playback occurs in another thread.
13    audio.wait(); // Block until sounds finish playing
14}
Source

pub fn add<S: AsRef<str>, P: AsRef<Path>>(&mut self, name: S, path: P)

Add an audio clip to play. Audio clips will be decoded and buffered during this call so the first call to .play() is not staticky if you compile in debug mode. name is what you will refer to this clip as when you need to play it. Files known to be supported by the underlying library (rodio) at the time of this writing are MP3, WAV, Vorbis and Flac.

Examples found in repository?
examples/play.rs (line 11)
3fn main() {
4    // Create a 4-channel audio subsystem.
5    let mut audio = Audio::new();
6    // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7    // but at least doesn't crash.  Helpful for CI.
8    if audio.disabled() {
9        println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10    }
11    audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12    audio.play("startup"); // Execution continues while playback occurs in another thread.
13    audio.wait(); // Block until sounds finish playing
14}
Source

pub fn play<S: AsRef<str>>(&mut self, name: S)

Play an audio clip that has already been loaded. name is the name you chose when you added the clip to the Audio system. If you forgot to load the clip first, this will crash.

Examples found in repository?
examples/play.rs (line 12)
3fn main() {
4    // Create a 4-channel audio subsystem.
5    let mut audio = Audio::new();
6    // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7    // but at least doesn't crash.  Helpful for CI.
8    if audio.disabled() {
9        println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10    }
11    audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12    audio.play("startup"); // Execution continues while playback occurs in another thread.
13    audio.wait(); // Block until sounds finish playing
14}
Source

pub fn wait(&self)

Block until no sounds are playing. Convenient for keeping a thread alive until all sounds have played.

Examples found in repository?
examples/play.rs (line 13)
3fn main() {
4    // Create a 4-channel audio subsystem.
5    let mut audio = Audio::new();
6    // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7    // but at least doesn't crash.  Helpful for CI.
8    if audio.disabled() {
9        println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10    }
11    audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12    audio.play("startup"); // Execution continues while playback occurs in another thread.
13    audio.wait(); // Block until sounds finish playing
14}

Trait Implementations§

Source§

impl Default for Audio

Source§

fn default() -> Audio

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Audio

§

impl !RefUnwindSafe for Audio

§

impl !Send for Audio

§

impl !Sync for Audio

§

impl Unpin for Audio

§

impl !UnwindSafe for Audio

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<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,