|
Game Audio Module
A C++ audio system using miniaudio with Python bindings
|
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< Sound > | Create (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. | |
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.
|
private |
Private constructor - use Create() instead.
| engine | Pointer to the miniaudio engine |
| filepath | Path to the audio file |
| group | Optional audio group this sound belongs to |
| audio::Sound::~Sound | ( | ) |
Destructor.
Stops all playing instances and releases resources
|
private |
Removes and deletes finished instances.
Cleans up instances that have finished playing to free resources.
|
static |
Factory method for creating Sound instances.
This static factory method ensures proper initialization and encapsulation of Sound objects.
| engine | Pointer to the miniaudio engine |
| filepath | Path to the audio file |
| group | Optional audio group this sound belongs to |
| FileLoadException | if the file cannot be accessed |
|
inlineprivate |
Get the maximum distance.
|
inlineprivate |
Get the minimum distance.
|
inlineprivate |
Get the 3D position of the sound.
|
inlineprivate |
Get the rolloff factor.
|
inlineprivate |
Gets the current volume level.
|
inlineprivate |
Checks if this sound is set to loop.
|
private |
Checks if any instance of this sound is playing.
|
inlineprivate |
Check if spatialization is enabled.
|
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().
| FileLoadException | If the sound file cannot be loaded or initialized for playback |
|
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.
| position | 3D position for this instance |
| FileLoadException | If the sound file cannot be loaded or initialized for playback |
|
private |
Sets whether new instances should loop.
| should_loop | Whether new instances should loop continuously |
|
private |
Set the maximum distance for distance attenuation.
At distances beyond maxDistance, the sound will be at minimum gain.
| maxDistance | Maximum distance (must be > 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.
| minDistance | Minimum distance (must be > 0) |
|
private |
Sets pitch for the next instance to be played.
| pitch | Pitch multiplier (1.0 = normal pitch, 0.5 = half speed, 2.0 = double speed) |
|
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.
| position | 3D position of the sound |
|
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)
| rolloff | Rolloff factor (typically 1.0 to 2.0) |
|
private |
Enable or disable spatialization for this sound.
| enabled | Whether to enable spatialization |
|
private |
Sets volume for all instances.
| volume | Volume level (0.0 to 1.0) |
|
private |
Stops all instances of this sound.
Stops all currently playing instances of this sound.
|
friend |
Allow AudioManager to access private members.
|
friend |
Allow AudioSystem to create sounds.
|
friend |
Allow AudioTrack to control playback.
|
private |
Pointer to miniaudio engine.
|
private |
Path to the audio file.
|
private |
Pointer to parent sound group (if any)
|
private |
Whether new instances should loop.
|
private |
Maximum distance for attenuation.
|
private |
Minimum distance for attenuation.
|
private |
Pitch for next instance (1.0 = normal)
|
private |
3D position of the sound
|
private |
Rolloff factor for distance attenuation.
|
private |
Collection of playing instances.
|
private |
Whether spatialization is enabled.
|
private |
Current volume level.