Skip to main content

AudioManager

Struct AudioManager 

Source
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

Source

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);
Source

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);
Source

pub fn set_sink_volume(&self, sink_id: u64, volume: f32) -> bool

Sets the volume for a specific sink.

§Arguments
  • sink_id - ID returned from play()
  • volume - Volume level (0.0-1.0, will be clamped)
§Returns

true if the sink was found and volume set, false otherwise.

Source

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 from play()
  • speed - Speed multiplier (0.1-10.0, will be clamped)
§Returns

true if the sink was found and speed set, false otherwise.

Source

pub fn is_finished(&self, sink_id: u64) -> bool

Returns whether a sink has finished playing.

§Arguments
  • sink_id - ID returned from play()
§Returns

true if the sink exists but has no more audio to play, false if the sink does not exist or is still playing.

Source§

impl AudioManager

Source

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.

Source

pub fn play_looped(&mut self, asset: &AudioAsset) -> GoudResult<u64>

Plays an audio asset with looping enabled.

Same as play() but the audio will repeat indefinitely until stopped.

§Arguments
  • asset - Reference to the audio asset to play
§Returns

A unique ID for this audio playback instance, or an error if playback fails.

Source

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 play
  • volume - 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.

Source

pub fn pause(&self, sink_id: u64) -> bool

Pauses audio playback for the given sink ID.

§Arguments
  • sink_id - ID returned from play()
§Returns

true if the sink was found and paused, false otherwise.

Source

pub fn resume(&self, sink_id: u64) -> bool

Resumes audio playback for the given sink ID.

§Arguments
  • sink_id - ID returned from play()
§Returns

true if the sink was found and resumed, false otherwise.

Source

pub fn stop(&mut self, sink_id: u64) -> bool

Stops audio playback for the given sink ID and removes it.

§Arguments
  • sink_id - ID returned from play()
§Returns

true if the sink was found and stopped, false otherwise.

Source

pub fn is_playing(&self, sink_id: u64) -> bool

Checks if audio is currently playing for the given sink ID.

§Arguments
  • sink_id - ID returned from play()
§Returns

true if the sink exists and is not paused, false otherwise.

Source

pub fn active_count(&self) -> usize

Returns the number of active audio players.

Source

pub fn stop_all(&mut self)

Stops all currently playing audio.

Source

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

Source

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 play
  • source_position - Position of the audio source in world space
  • listener_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();
Source

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 from play_spatial()
  • source_position - Current position of the audio source
  • listener_position - Current position of the listener
  • max_distance - Maximum distance for audio
  • rolloff - 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

Source

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

Source§

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

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

impl Send for AudioManager

Source§

impl Sync for AudioManager

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> 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<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>,

Source§

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

Source§

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