pub struct AudioManager { /* private fields */ }Expand description
Central audio playback manager resource.
AudioManager is an ECS resource that manages audio playback using the
rodio audio library. It maintains the audio output stream and provides
methods for playing, pausing, stopping, and controlling audio.
§Thread Safety
The AudioManager is thread-safe (Send + Sync) and can be used in parallel
systems. Internal state is protected by a Mutex.
§Example
use goud_engine::assets::AudioManager;
// Create audio manager
let mut audio_manager = AudioManager::new().expect("Failed to initialize audio");
// Set global volume (0.0 to 1.0)
audio_manager.set_global_volume(0.5);
// Get global volume
assert_eq!(audio_manager.global_volume(), 0.5);Implementations§
Source§impl AudioManager
impl AudioManager
Sourcepub fn global_volume(&self) -> f32
pub fn global_volume(&self) -> f32
Returns the global volume (0.0 to 1.0).
§Example
use goud_engine::assets::AudioManager;
let audio_manager = AudioManager::new().unwrap();
assert_eq!(audio_manager.global_volume(), 1.0);Sourcepub fn set_global_volume(&mut self, volume: f32)
pub fn set_global_volume(&mut self, volume: f32)
Sets the global volume (0.0 to 1.0).
All currently playing and future audio will use this volume. Values outside the range are clamped.
§Arguments
volume- Volume level (0.0 = mute, 1.0 = full volume)
§Example
use goud_engine::assets::AudioManager;
let mut audio_manager = AudioManager::new().unwrap();
audio_manager.set_global_volume(0.5);
assert_eq!(audio_manager.global_volume(), 0.5);Sourcepub fn set_sink_volume(&self, sink_id: u64, volume: f32) -> bool
pub fn set_sink_volume(&self, sink_id: u64, volume: f32) -> bool
Sourcepub fn set_sink_speed(&self, sink_id: u64, speed: f32) -> bool
pub fn set_sink_speed(&self, sink_id: u64, speed: f32) -> bool
Sets the playback speed (and pitch) for a specific sink.
Note: This only works for sinks that haven’t started playing yet. For real-time speed adjustment, the sink needs to be recreated.
§Arguments
sink_id- ID returned fromplay()speed- Speed multiplier (0.1-10.0, will be clamped)
§Returns
true if the sink was found and speed set, false otherwise.
Source§impl AudioManager
impl AudioManager
Sourcepub fn play(&mut self, asset: &AudioAsset) -> GoudResult<u64>
pub fn play(&mut self, asset: &AudioAsset) -> GoudResult<u64>
Plays an audio asset.
Creates a new audio player and starts playback of the given audio asset. Returns a unique player ID that can be used to control playback (pause/resume/stop).
§Arguments
asset- Reference to the audio asset to play
§Returns
A unique ID for this audio playback instance, or an error if playback fails.
§Errors
Returns ResourceLoadFailed if audio data is empty or cannot be decoded.
Sourcepub fn play_looped(&mut self, asset: &AudioAsset) -> GoudResult<u64>
pub fn play_looped(&mut self, asset: &AudioAsset) -> GoudResult<u64>
Sourcepub fn play_with_settings(
&mut self,
asset: &AudioAsset,
volume: f32,
speed: f32,
looping: bool,
) -> GoudResult<u64>
pub fn play_with_settings( &mut self, asset: &AudioAsset, volume: f32, speed: f32, looping: bool, ) -> GoudResult<u64>
Plays an audio asset with custom volume and pitch.
§Arguments
asset- Reference to the audio asset to playvolume- Volume multiplier (0.0-1.0, will be clamped)speed- Playback speed multiplier (0.1-10.0, affects pitch)looping- Whether to loop indefinitely
§Returns
A unique ID for this audio playback instance, or an error if playback fails.
Sourcepub fn is_playing(&self, sink_id: u64) -> bool
pub fn is_playing(&self, sink_id: u64) -> bool
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Returns the number of active audio players.
Sourcepub fn cleanup_finished(&mut self)
pub fn cleanup_finished(&mut self)
Cleans up finished audio players.
Removes players that have finished playing from the internal collection. This should be called periodically to prevent memory leaks.
Source§impl AudioManager
impl AudioManager
Sourcepub fn play_spatial(
&mut self,
asset: &AudioAsset,
source_position: Vec2,
listener_position: Vec2,
max_distance: f32,
rolloff: f32,
) -> GoudResult<u64>
pub fn play_spatial( &mut self, asset: &AudioAsset, source_position: Vec2, listener_position: Vec2, max_distance: f32, rolloff: f32, ) -> GoudResult<u64>
Plays audio with spatial positioning (2D).
Applies distance-based attenuation using the given parameters. Volume is calculated as: base_volume * attenuation_factor
§Arguments
asset- Reference to the audio asset to playsource_position- Position of the audio source in world spacelistener_position- Position of the listener (typically camera or player)max_distance- Maximum distance for audio (0 volume beyond this)rolloff- Rolloff factor for attenuation (1.0 = linear, 2.0 = quadratic)
§Returns
A unique ID for this audio playback instance, or an error if playback fails.
§Example
use goud_engine::assets::AudioManager;
use goud_engine::core::math::Vec2;
let mut audio_manager = AudioManager::new().unwrap();
// let audio_asset = ...; // Loaded AudioAsset
// let sink_id = audio_manager.play_spatial(
// &audio_asset,
// Vec2::new(100.0, 50.0), // Sound source position
// Vec2::new(0.0, 0.0), // Listener position
// 200.0, // Max distance
// 1.0 // Linear rolloff
// ).unwrap();Sourcepub fn update_spatial_volume(
&self,
sink_id: u64,
source_position: Vec2,
listener_position: Vec2,
max_distance: f32,
rolloff: f32,
) -> bool
pub fn update_spatial_volume( &self, sink_id: u64, source_position: Vec2, listener_position: Vec2, max_distance: f32, rolloff: f32, ) -> bool
Updates spatial audio volume for an existing sink.
Recalculates attenuation based on new positions and updates the sink volume. This should be called when either the source or listener moves.
§Arguments
sink_id- ID returned fromplay_spatial()source_position- Current position of the audio sourcelistener_position- Current position of the listenermax_distance- Maximum distance for audiorolloff- Rolloff factor for attenuation
§Returns
true if the sink was found and volume updated, false otherwise.
§Example
use goud_engine::assets::AudioManager;
use goud_engine::core::math::Vec2;
let mut audio_manager = AudioManager::new().unwrap();
// let sink_id = ...; // From play_spatial()
// audio_manager.update_spatial_volume(
// sink_id,
// Vec2::new(150.0, 75.0), // New source position
// Vec2::new(50.0, 25.0), // New listener position
// 200.0,
// 1.0
// );Source§impl AudioManager
impl AudioManager
Sourcepub fn new() -> GoudResult<Self>
pub fn new() -> GoudResult<Self>
Creates a new AudioManager instance.
Initializes the audio output stream. Returns an error if audio output is not available (e.g., no audio device found).
§Errors
Returns AudioInitFailed if the audio system cannot be initialized.
§Example
use goud_engine::assets::AudioManager;
let audio_manager = AudioManager::new().expect("Failed to initialize audio");Trait Implementations§
Source§impl Debug for AudioManager
impl Debug for AudioManager
impl Send for AudioManager
impl Sync for AudioManager
Auto Trait Implementations§
impl Freeze for AudioManager
impl !RefUnwindSafe for AudioManager
impl Unpin for AudioManager
impl UnsafeUnpin for AudioManager
impl !UnwindSafe for AudioManager
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<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 more