Game Audio Module
A C++ audio system using miniaudio with Python bindings
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
audio::Sound Class Reference

Represents an audio file that can be played multiple times simultaneously. More...

#include <sound.h>

Public Member Functions

 ~Sound ()
 Destructor.
 

Static Public Member Functions

static std::unique_ptr< SoundCreate (ma_engine *engine, const std::string &filepath, AudioGroup *group=nullptr)
 Factory method for creating Sound instances.
 

Private Member Functions

 Sound (ma_engine *engine, const std::string &filepath, AudioGroup *group)
 Private constructor - use Create() instead.
 
void CleanupFinishedInstances ()
 Removes and deletes finished instances.
 
Playback Control
void Play ()
 Starts a new instance of the sound.
 
void Play (const Vec3 &position)
 Starts a new instance of the sound at a specific position.
 
void Stop ()
 Stops all instances of this sound.
 
void SetLooping (bool should_loop)
 Sets whether new instances should loop.
 
void SetVolume (float volume)
 Sets volume for all instances.
 
void SetPitch (float pitch)
 Sets pitch for the next instance to be played.
 
Spatial Audio (3D Positioning)
void SetPosition (const Vec3 &position)
 Set the 3D position of the sound.
 
Vec3 GetPosition () const
 Get the 3D position of the sound.
 
void SetMinDistance (float minDistance)
 Set the minimum distance for distance attenuation.
 
float GetMinDistance () const
 Get the minimum distance.
 
void SetMaxDistance (float maxDistance)
 Set the maximum distance for distance attenuation.
 
float GetMaxDistance () const
 Get the maximum distance.
 
void SetRolloff (float rolloff)
 Set the rolloff factor for distance attenuation.
 
float GetRolloff () const
 Get the rolloff factor.
 
void SetSpatializationEnabled (bool enabled)
 Enable or disable spatialization for this sound.
 
bool IsSpatializationEnabled () const
 Check if spatialization is enabled.
 
State Queries
float GetVolume () const
 Gets the current volume level.
 
bool IsLooping () const
 Checks if this sound is set to loop.
 
bool IsPlaying () const
 Checks if any instance of this sound is playing.
 

Private Attributes

ma_engine * engine_
 Pointer to miniaudio engine.
 
std::string filepath_
 Path to the audio file.
 
ma_sound_group * group_
 Pointer to parent sound group (if any)
 
bool looping_
 Whether new instances should loop.
 
float pitch_
 Pitch for next instance (1.0 = normal)
 
std::vector< std::unique_ptr< SoundInstance > > sound_instances_
 Collection of playing instances.
 
float volume_
 Current volume level.
 
Spatial Audio State
Vec3 position_
 3D position of the sound
 
float min_distance_
 Minimum distance for attenuation.
 
float max_distance_
 Maximum distance for attenuation.
 
float rolloff_
 Rolloff factor for distance attenuation.
 
bool spatialization_enabled_
 Whether spatialization is enabled.
 

Friends

class AudioManager
 Allow AudioManager to access private members.
 
class AudioSystem
 Allow AudioSystem to create sounds.
 
class AudioTrack
 Allow AudioTrack to control playback.
 

Detailed Description

Represents an audio file that can be played multiple times simultaneously.

The Sound class wraps a single audio file but allows it to be played multiple times concurrently (e.g., multiple footsteps from the same sound file). Each playback instance is tracked separately and can be controlled individually.

Constructor & Destructor Documentation

◆ Sound()

audio::Sound::Sound ( ma_engine *  engine,
const std::string &  filepath,
AudioGroup group 
)
private

Private constructor - use Create() instead.

Parameters
enginePointer to the miniaudio engine
filepathPath to the audio file
groupOptional audio group this sound belongs to

◆ ~Sound()

audio::Sound::~Sound ( )

Destructor.

Stops all playing instances and releases resources

Member Function Documentation

◆ CleanupFinishedInstances()

void audio::Sound::CleanupFinishedInstances ( )
private

Removes and deletes finished instances.

Cleans up instances that have finished playing to free resources.

◆ Create()

std::unique_ptr< Sound > audio::Sound::Create ( ma_engine *  engine,
const std::string &  filepath,
AudioGroup group = nullptr 
)
static

Factory method for creating Sound instances.

This static factory method ensures proper initialization and encapsulation of Sound objects.

Parameters
enginePointer to the miniaudio engine
filepathPath to the audio file
groupOptional audio group this sound belongs to
Returns
std::unique_ptr<Sound> Unique pointer to a new Sound
Exceptions
FileLoadExceptionif the file cannot be accessed

◆ GetMaxDistance()

float audio::Sound::GetMaxDistance ( ) const
inlineprivate

Get the maximum distance.

Returns
float Current maximum distance

◆ GetMinDistance()

float audio::Sound::GetMinDistance ( ) const
inlineprivate

Get the minimum distance.

Returns
float Current minimum distance

◆ GetPosition()

Vec3 audio::Sound::GetPosition ( ) const
inlineprivate

Get the 3D position of the sound.

Returns
Vec3 Current 3D position

◆ GetRolloff()

float audio::Sound::GetRolloff ( ) const
inlineprivate

Get the rolloff factor.

Returns
float Current rolloff factor

◆ GetVolume()

float audio::Sound::GetVolume ( ) const
inlineprivate

Gets the current volume level.

Returns
float Current volume (0.0 to 1.0)

◆ IsLooping()

bool audio::Sound::IsLooping ( ) const
inlineprivate

Checks if this sound is set to loop.

Returns
bool True if set to loop, false otherwise

◆ IsPlaying()

bool audio::Sound::IsPlaying ( ) const
private

Checks if any instance of this sound is playing.

Returns
bool True if any instance is playing, false otherwise

◆ IsSpatializationEnabled()

bool audio::Sound::IsSpatializationEnabled ( ) const
inlineprivate

Check if spatialization is enabled.

Returns
bool True if spatialization is enabled

◆ Play() [1/2]

void audio::Sound::Play ( )
private

Starts a new instance of the sound.

Creates and plays a new instance of this sound, allowing multiple overlapping playbacks of the same sound.

Uses the default position set via SetPosition().

Exceptions
FileLoadExceptionIf the sound file cannot be loaded or initialized for playback

◆ Play() [2/2]

void audio::Sound::Play ( const Vec3 position)
private

Starts a new instance of the sound at a specific position.

Creates and plays a new instance of this sound at the specified position. This allows multiple overlapping spatialized sounds from the same audio file to play at different positions simultaneously (e.g., multiple gunshots).

The position is only applied to this new instance. Existing instances keep their positions unchanged.

Parameters
position3D position for this instance
Exceptions
FileLoadExceptionIf the sound file cannot be loaded or initialized for playback

◆ SetLooping()

void audio::Sound::SetLooping ( bool  should_loop)
private

Sets whether new instances should loop.

Parameters
should_loopWhether new instances should loop continuously

◆ SetMaxDistance()

void audio::Sound::SetMaxDistance ( float  maxDistance)
private

Set the maximum distance for distance attenuation.

At distances beyond maxDistance, the sound will be at minimum gain.

Parameters
maxDistanceMaximum distance (must be > minDistance)

◆ SetMinDistance()

void audio::Sound::SetMinDistance ( float  minDistance)
private

Set the minimum distance for distance attenuation.

At distances less than minDistance, the sound will be at full volume. Beyond minDistance, volume will attenuate based on the attenuation model.

Parameters
minDistanceMinimum distance (must be > 0)

◆ SetPitch()

void audio::Sound::SetPitch ( float  pitch)
private

Sets pitch for the next instance to be played.

Parameters
pitchPitch multiplier (1.0 = normal pitch, 0.5 = half speed, 2.0 = double speed)

◆ SetPosition()

void audio::Sound::SetPosition ( const Vec3 position)
private

Set the 3D position of the sound.

Sets the position for all instances of this sound. The sound will be spatialized relative to the listener position.

Parameters
position3D position of the sound

◆ SetRolloff()

void audio::Sound::SetRolloff ( float  rolloff)
private

Set the rolloff factor for distance attenuation.

Higher values mean faster volume dropoff with distance. Typical values: 1.0 (linear), 2.0 (inverse square)

Parameters
rolloffRolloff factor (typically 1.0 to 2.0)

◆ SetSpatializationEnabled()

void audio::Sound::SetSpatializationEnabled ( bool  enabled)
private

Enable or disable spatialization for this sound.

Parameters
enabledWhether to enable spatialization

◆ SetVolume()

void audio::Sound::SetVolume ( float  volume)
private

Sets volume for all instances.

Parameters
volumeVolume level (0.0 to 1.0)

◆ Stop()

void audio::Sound::Stop ( )
private

Stops all instances of this sound.

Stops all currently playing instances of this sound.

Friends And Related Symbol Documentation

◆ AudioManager

friend class AudioManager
friend

Allow AudioManager to access private members.

◆ AudioSystem

friend class AudioSystem
friend

Allow AudioSystem to create sounds.

◆ AudioTrack

friend class AudioTrack
friend

Allow AudioTrack to control playback.

Member Data Documentation

◆ engine_

ma_engine* audio::Sound::engine_
private

Pointer to miniaudio engine.

◆ filepath_

std::string audio::Sound::filepath_
private

Path to the audio file.

◆ group_

ma_sound_group* audio::Sound::group_
private

Pointer to parent sound group (if any)

◆ looping_

bool audio::Sound::looping_
private

Whether new instances should loop.

◆ max_distance_

float audio::Sound::max_distance_
private

Maximum distance for attenuation.

◆ min_distance_

float audio::Sound::min_distance_
private

Minimum distance for attenuation.

◆ pitch_

float audio::Sound::pitch_
private

Pitch for next instance (1.0 = normal)

◆ position_

Vec3 audio::Sound::position_
private

3D position of the sound

◆ rolloff_

float audio::Sound::rolloff_
private

Rolloff factor for distance attenuation.

◆ sound_instances_

std::vector<std::unique_ptr<SoundInstance> > audio::Sound::sound_instances_
private

Collection of playing instances.

◆ spatialization_enabled_

bool audio::Sound::spatialization_enabled_
private

Whether spatialization is enabled.

◆ volume_

float audio::Sound::volume_
private

Current volume level.


The documentation for this class was generated from the following files: