Struct tetra::audio::Sound

source ·
pub struct Sound { /* private fields */ }
Expand description

Sound data that can be played back.

All of the playback methods on this type return a SoundInstance that can be used to control the sound after it has started. If you just want to ‘fire and forget’ a sound, you can discard it - the sound will continue playing regardless.

Supported File Formats

Audio can be decoded from various common file formats via the new and from_encoded constructors. Individual decoders can be enabled or disabled via Cargo feature flags.

FormatCargo featureEnabled by default?
WAVaudio_wavYes
OGG Vorbisaudio_vorbisYes
MP3audio_mp3Yes
FLACaudio_flacNo

Performance

When you create an instance of Sound, the audio data is loaded into memory. It is not decoded until playback begins.

You can clone a sound cheaply, as it is reference-counted internally. The underlying data will be shared by all of the clones (and, by extension, all of the SoundInstances created from them).

Examples

The audio example demonstrates how to play several different kinds of sound.

Implementations§

source§

impl Sound

source

pub fn new<P>(path: P) -> Result<Sound>where P: AsRef<Path>,

Creates a new sound from the given file.

Note that the data is not decoded until playback begins, so this function will not validate that the data being read is formatted correctly.

Errors
source

pub fn from_encoded(data: &[u8]) -> Sound

Creates a new sound from a slice of binary data, encoded in one of Tetra’s supported file formats.

This is useful in combination with include_bytes, as it allows you to include your audio data directly in the binary.

Note that the data is not decoded until playback begins, so this function will not validate that the data being read is formatted correctly.

source

pub fn play(&self, ctx: &Context) -> Result<SoundInstance>

Plays the sound.

Errors
source

pub fn repeat(&self, ctx: &Context) -> Result<SoundInstance>

Plays the sound repeatedly.

Errors
source

pub fn spawn(&self, ctx: &Context) -> Result<SoundInstance>

Spawns a new instance of the sound that is not playing yet.

Errors
source

pub fn play_with( &self, ctx: &Context, volume: f32, speed: f32 ) -> Result<SoundInstance>

Plays the sound, with the provided settings.

Errors
source

pub fn repeat_with( &self, ctx: &Context, volume: f32, speed: f32 ) -> Result<SoundInstance>

Plays the sound repeatedly, with the provided settings.

Errors
source

pub fn spawn_with( &self, ctx: &Context, volume: f32, speed: f32 ) -> Result<SoundInstance>

Spawns a new instance of the sound that is not playing yet, with the provided settings.

Errors

Trait Implementations§

source§

impl Clone for Sound

source§

fn clone(&self) -> Sound

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Sound

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<Sound> for Sound

source§

fn eq(&self, other: &Sound) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Sound

source§

impl StructuralEq for Sound

source§

impl StructuralPartialEq for Sound

Auto Trait Implementations§

§

impl RefUnwindSafe for Sound

§

impl Send for Sound

§

impl Sync for Sound

§

impl Unpin for Sound

§

impl UnwindSafe for Sound

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

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

const: unstable · 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.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

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

§

fn to_sample_(self) -> U

source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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.
const: unstable · source§

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

Performs the conversion.
§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,