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 playplaying: Whether the audio is currently playinglooping: Whether the audio should loop when it finishesvolume: 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 mixingauto_play: Whether to start playing automatically when spawnedspatial: Whether to apply spatial audio (requires Transform)max_distance: Maximum distance for spatial audio attenuationattenuation: Distance-based volume falloff modelsink_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: boolWhether the audio is currently playing
looping: boolWhether the audio should loop when it finishes
volume: f32Volume multiplier (0.0 = silent, 1.0 = full volume)
pitch: f32Pitch multiplier (0.5 = half speed, 2.0 = double speed)
channel: AudioChannelAudio channel for grouping and mixing
auto_play: boolWhether to start playing automatically when spawned
spatial: boolWhether to apply spatial audio (requires Transform)
max_distance: f32Maximum distance for spatial audio attenuation
attenuation: AttenuationModelDistance-based volume falloff model
Implementations§
Source§impl AudioSource
impl AudioSource
Sourcepub fn new(audio: AssetHandle<AudioAsset>) -> AudioSource
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);Sourcepub fn with_volume(self, volume: f32) -> AudioSource
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);Sourcepub fn with_pitch(self, pitch: f32) -> AudioSource
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);Sourcepub fn with_looping(self, looping: bool) -> AudioSource
pub fn with_looping(self, looping: bool) -> AudioSource
Sets whether the audio should loop.
Sourcepub fn with_channel(self, channel: AudioChannel) -> AudioSource
pub fn with_channel(self, channel: AudioChannel) -> AudioSource
Sets the audio channel.
Sourcepub fn with_auto_play(self, auto_play: bool) -> AudioSource
pub fn with_auto_play(self, auto_play: bool) -> AudioSource
Sets whether to start playing automatically when spawned.
Sourcepub fn with_audio_path(self, path: impl Into<String>) -> AudioSource
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.
Sourcepub fn with_spatial(self, spatial: bool) -> AudioSource
pub fn with_spatial(self, spatial: bool) -> AudioSource
Sets whether to apply spatial audio (requires Transform component).
Sourcepub fn with_max_distance(self, max_distance: f32) -> AudioSource
pub fn with_max_distance(self, max_distance: f32) -> AudioSource
Sets the maximum distance for spatial audio attenuation.
Sourcepub fn with_attenuation(self, attenuation: AttenuationModel) -> AudioSource
pub fn with_attenuation(self, attenuation: AttenuationModel) -> AudioSource
Sets the attenuation model for spatial audio.
Sourcepub fn is_playing(&self) -> bool
pub fn is_playing(&self) -> bool
Returns whether the audio is currently playing.
Sourcepub fn is_spatial(&self) -> bool
pub fn is_spatial(&self) -> bool
Returns whether the audio is spatial.
Trait Implementations§
Source§impl Clone for AudioSource
impl Clone for AudioSource
Source§fn clone(&self) -> AudioSource
fn clone(&self) -> AudioSource
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AudioSource
impl Debug for AudioSource
Source§impl Default for AudioSource
impl Default for AudioSource
Source§fn default() -> AudioSource
fn default() -> AudioSource
Source§impl<'de> Deserialize<'de> for AudioSource
impl<'de> Deserialize<'de> for AudioSource
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<AudioSource, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<AudioSource, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for AudioSource
impl Display for AudioSource
Source§impl Serialize for AudioSource
impl Serialize for AudioSource
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Component for AudioSource
Auto Trait Implementations§
impl Freeze for AudioSource
impl RefUnwindSafe for AudioSource
impl Send for AudioSource
impl Sync for AudioSource
impl Unpin for AudioSource
impl UnsafeUnpin for AudioSource
impl UnwindSafe for AudioSource
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().