Skip to main content

AudioSource

Struct AudioSource 

Source
pub struct AudioSource {
    pub audio: AssetHandle<AudioAsset>,
    pub audio_path: Option<String>,
    pub playing: bool,
    pub looping: bool,
    pub volume: f32,
    pub pitch: f32,
    pub channel: AudioChannel,
    pub auto_play: bool,
    pub spatial: bool,
    pub max_distance: f32,
    pub attenuation: AttenuationModel,
    /* private fields */
}
Expand description

AudioSource component for spatial audio playback.

Attach this component to an entity to enable audio playback. The audio system will automatically handle playback, looping, volume, pitch, and spatial audio based on the component’s configuration.

§Fields

  • audio: Reference to the audio asset to play
  • playing: Whether the audio is currently playing
  • looping: Whether the audio should loop when it finishes
  • volume: Volume multiplier (0.0 = silent, 1.0 = full volume)
  • pitch: Pitch multiplier (0.5 = half speed, 2.0 = double speed)
  • channel: Audio channel for grouping and mixing
  • auto_play: Whether to start playing automatically when spawned
  • spatial: Whether to apply spatial audio (requires Transform)
  • max_distance: Maximum distance for spatial audio attenuation
  • attenuation: Distance-based volume falloff model
  • sink_id: Internal audio sink ID (managed by audio system)

§Examples

See module-level documentation for usage examples.

Fields§

§audio: AssetHandle<AudioAsset>

Reference to the audio asset to play

§audio_path: Option<String>

Optional path to the audio asset for serialization.

The handle cannot survive serialization, but this path string can. A higher-level system resolves it back to an AssetHandle after deserialization.

§playing: bool

Whether the audio is currently playing

§looping: bool

Whether the audio should loop when it finishes

§volume: f32

Volume multiplier (0.0 = silent, 1.0 = full volume)

§pitch: f32

Pitch multiplier (0.5 = half speed, 2.0 = double speed)

§channel: AudioChannel

Audio channel for grouping and mixing

§auto_play: bool

Whether to start playing automatically when spawned

§spatial: bool

Whether to apply spatial audio (requires Transform)

§max_distance: f32

Maximum distance for spatial audio attenuation

§attenuation: AttenuationModel

Distance-based volume falloff model

Implementations§

Source§

impl AudioSource

Source

pub fn new(audio: AssetHandle<AudioAsset>) -> AudioSource

Creates a new AudioSource with default settings.

§Arguments
  • audio: Reference to the audio asset to play
§Default Values
  • playing: false (stopped)
  • looping: false (one-shot)
  • volume: 1.0 (full volume)
  • pitch: 1.0 (normal speed)
  • channel: SFX
  • auto_play: false
  • spatial: false (non-spatial)
  • max_distance: 100.0
  • attenuation: InverseDistance
  • sink_id: None
§Examples
use goud_engine::ecs::components::AudioSource;
use goud_engine::assets::AssetHandle;
use goud_engine::assets::loaders::audio::AudioAsset;

let audio_handle: AssetHandle<AudioAsset> = AssetHandle::default();
let source = AudioSource::new(audio_handle);

assert_eq!(source.playing, false);
assert_eq!(source.volume, 1.0);
assert_eq!(source.pitch, 1.0);
Source

pub fn with_volume(self, volume: f32) -> AudioSource

Sets the volume (0.0-1.0, clamped).

§Examples
use goud_engine::ecs::components::AudioSource;
use goud_engine::assets::AssetHandle;
use goud_engine::assets::loaders::audio::AudioAsset;

let audio_handle: AssetHandle<AudioAsset> = AssetHandle::default();
let source = AudioSource::new(audio_handle).with_volume(0.5);
assert_eq!(source.volume, 0.5);
Source

pub fn with_pitch(self, pitch: f32) -> AudioSource

Sets the pitch (0.5-2.0, clamped).

§Examples
use goud_engine::ecs::components::AudioSource;
use goud_engine::assets::AssetHandle;
use goud_engine::assets::loaders::audio::AudioAsset;

let audio_handle: AssetHandle<AudioAsset> = AssetHandle::default();
let source = AudioSource::new(audio_handle).with_pitch(1.5);
assert_eq!(source.pitch, 1.5);
Source

pub fn with_looping(self, looping: bool) -> AudioSource

Sets whether the audio should loop.

Source

pub fn with_channel(self, channel: AudioChannel) -> AudioSource

Sets the audio channel.

Source

pub fn with_auto_play(self, auto_play: bool) -> AudioSource

Sets whether to start playing automatically when spawned.

Source

pub fn with_audio_path(self, path: impl Into<String>) -> AudioSource

Sets the audio asset path for serialization.

The path is stored alongside the source so it survives serialization. A higher-level system resolves it back to an AssetHandle after deserialization.

Source

pub fn with_spatial(self, spatial: bool) -> AudioSource

Sets whether to apply spatial audio (requires Transform component).

Source

pub fn with_max_distance(self, max_distance: f32) -> AudioSource

Sets the maximum distance for spatial audio attenuation.

Source

pub fn with_attenuation(self, attenuation: AttenuationModel) -> AudioSource

Sets the attenuation model for spatial audio.

Source

pub fn play(&mut self)

Starts playing the audio.

Source

pub fn pause(&mut self)

Pauses the audio (retains playback position).

Source

pub fn stop(&mut self)

Stops the audio (resets playback position).

Source

pub fn is_playing(&self) -> bool

Returns whether the audio is currently playing.

Source

pub fn is_spatial(&self) -> bool

Returns whether the audio is spatial.

Source

pub fn has_sink(&self) -> bool

Returns whether the audio has an active sink.

Trait Implementations§

Source§

impl Clone for AudioSource

Source§

fn clone(&self) -> AudioSource

Returns a duplicate 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 AudioSource

Source§

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

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

impl Default for AudioSource

Source§

fn default() -> AudioSource

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

impl<'de> Deserialize<'de> for AudioSource

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<AudioSource, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for AudioSource

Source§

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

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

impl Serialize for AudioSource

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Component for AudioSource

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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
Source§

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

Source§

fn to_sample_(self) -> U

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

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

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> QueryState for T
where T: Send + Sync + Clone + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,