Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::audio::AudioDevice Class Reference

The default audio output and the sounds it plays. More...

#include <aegis2d/audio/audio.hpp>

Public Member Functions

 AudioDevice ()
 Opens the default audio output of the system.
 
 AudioDevice (const AudioDevice &)=delete
 
AudioDevice & operator= (const AudioDevice &)=delete
 
 AudioDevice (AudioDevice &&other) noexcept
 Takes over the device of other, which must not be used afterwards.
 
AudioDevice & operator= (AudioDevice &&other) noexcept
 Closes this device and takes over the device of other.
 
 ~AudioDevice ()
 Stops every sound and closes the device.
 
SoundId load (const std::filesystem::path &path)
 Loads a sound from a WAV file.
 
std::size_t sounds () const noexcept
 Number of sounds loaded.
 
void play (SoundId sound, Channel channel=Channel::Effects)
 Starts playing a sound, mixed with the sounds already playing.
 
void play (SoundId sound, Channel channel, float pan, float gain_scale=1.0F)
 Plays a sound with stereo panning and distance attenuation.
 
void play_music (SoundId sound, bool loop=true)
 Plays a sound as the background music, on Channel::Music.
 
void stop_music () noexcept
 Stops the background music.
 
bool music_playing () const noexcept
 Tells whether background music plays.
 
void stop (Channel channel) noexcept
 Stops every sound of a channel at once.
 
void stop_all () noexcept
 Stops every sound at once.
 
void set_volumes (const Volumes &volumes) noexcept
 Changes the volumes, at once for the sounds already playing too.
 
Volumes volumes () const noexcept
 Current volumes.
 
void update () noexcept
 Frees the sounds that have finished playing; call it once per frame.
 
std::size_t playing () const noexcept
 Number of sounds still playing.
 
std::size_t playing (Channel channel) const noexcept
 Number of sounds of a channel still playing.
 

Static Public Attributes

static constexpr std::size_t max_voices = 32
 Most sounds played at the same time; playing one more stops the oldest effect.
 

Detailed Description

The default audio output and the sounds it plays.

Sounds are loaded once from WAV files, then played by identifier, as many at a time as needed: SDL mixes them on its own thread and converts each sound to the device format. Call update() once per frame to free the sounds that have finished. The device must be created and used from one thread.

const audio::SoundId shot = audio.load(core::asset_path("sounds/shot.wav"));
...
if (actions.pressed(fire, window.keyboard())) {
audio.play(shot);
}
audio.update();
The default audio output and the sounds it plays.
Definition audio.hpp:54
void play(SoundId sound, Channel channel=Channel::Effects)
Starts playing a sound, mixed with the sounds already playing.
void update() noexcept
Frees the sounds that have finished playing; call it once per frame.
SoundId load(const std::filesystem::path &path)
Loads a sound from a WAV file.
std::size_t SoundId
Identifier of a sound loaded by an AudioDevice, given by AudioDevice::load().
Definition audio.hpp:36
std::filesystem::path asset_path(const std::filesystem::path &relative)
Path of one asset.

Constructor & Destructor Documentation

◆ AudioDevice() [1/3]

aegis2d::audio::AudioDevice::AudioDevice ( )

Opens the default audio output of the system.

Exceptions
AudioErrorif SDL cannot initialize audio or open the device (no sound card, no audio server).

◆ AudioDevice() [2/3]

aegis2d::audio::AudioDevice::AudioDevice ( const AudioDevice &  )
delete

◆ AudioDevice() [3/3]

aegis2d::audio::AudioDevice::AudioDevice ( AudioDevice &&  other)
noexcept

Takes over the device of other, which must not be used afterwards.

◆ ~AudioDevice()

aegis2d::audio::AudioDevice::~AudioDevice ( )

Stops every sound and closes the device.

Member Function Documentation

◆ load()

SoundId aegis2d::audio::AudioDevice::load ( const std::filesystem::path &  path)

Loads a sound from a WAV file.

Parameters
pathWAV file: PCM, IEEE float or ADPCM, any rate and channel count.
Returns
The identifier to play it with: 0 for the first sound, then 1, 2...
Exceptions
AudioErrorif the file cannot be read or is not a supported WAV file; the message starts with the file path.

◆ music_playing()

bool aegis2d::audio::AudioDevice::music_playing ( ) const
noexcept

Tells whether background music plays.

Returns
true from play_music() until stop_music() or, without loop, its end.

◆ operator=() [1/2]

AudioDevice & aegis2d::audio::AudioDevice::operator= ( AudioDevice &&  other)
noexcept

Closes this device and takes over the device of other.

Returns
This device.

◆ operator=() [2/2]

AudioDevice & aegis2d::audio::AudioDevice::operator= ( const AudioDevice &  )
delete

◆ play() [1/2]

void aegis2d::audio::AudioDevice::play ( SoundId  sound,
Channel  channel,
float  pan,
float  gain_scale = 1.0F 
)

Plays a sound with stereo panning and distance attenuation.

Like play(), but the source is converted to stereo and the left and right channels are scaled independently: left = clamp(1 - pan, 0, 1) and right = clamp(1 + pan, 0, 1), both then scaled by gain_scale. Use positional() to get pan and gain_scale from a world position and a camera view.

Parameters
soundIdentifier returned by load().
channelChannel the sound belongs to.
panStereo position: -1 (full left), 0 (center), 1 (full right); clamped.
gain_scaleDistance attenuation: 0 (silent) to 1 (normal); clamped.
Exceptions
std::out_of_rangeif sound was not loaded.
AudioErrorif SDL cannot convert or start the sound.

◆ play() [2/2]

void aegis2d::audio::AudioDevice::play ( SoundId  sound,
Channel  channel = Channel::Effects 
)

Starts playing a sound, mixed with the sounds already playing.

The sound gets the gain of its channel (volumes()). When max_voices sounds already play, the oldest one that is not the background music is stopped.

Parameters
soundIdentifier returned by load().
channelChannel the sound belongs to.
Exceptions
std::out_of_rangeif sound was not loaded.
AudioErrorif SDL cannot start the sound.

◆ play_music()

void aegis2d::audio::AudioDevice::play_music ( SoundId  sound,
bool  loop = true 
)

Plays a sound as the background music, on Channel::Music.

The music replaces the one already playing. A looping music starts again without a gap: SDL's audio thread asks for the next samples as it needs them, so the loop does not depend on the game's frame rate. The music volume applies to it, and it does not count against max_voices, nor is it stopped to make room for effects.

Parameters
soundIdentifier returned by load().
looptrue to loop until stop_music(), false to play it once.
Exceptions
std::out_of_rangeif sound was not loaded.
AudioErrorif SDL cannot start the music.

◆ playing() [1/2]

std::size_t aegis2d::audio::AudioDevice::playing ( ) const
noexcept

Number of sounds still playing.

Returns
Sounds whose end has not been played yet.

◆ playing() [2/2]

std::size_t aegis2d::audio::AudioDevice::playing ( Channel  channel) const
noexcept

Number of sounds of a channel still playing.

Parameters
channelChannel to count, such as Channel::Music to restart a finished tune.
Returns
Sounds of channel whose end has not been played yet.

◆ set_volumes()

void aegis2d::audio::AudioDevice::set_volumes ( const Volumes &  volumes)
noexcept

Changes the volumes, at once for the sounds already playing too.

Each sound gets Volumes::gain() of its channel, so the music slider changes only the music and the effects slider only the effects.

Parameters
volumesSlider positions; values outside [0, 1] are clamped.

◆ sounds()

std::size_t aegis2d::audio::AudioDevice::sounds ( ) const
noexcept

Number of sounds loaded.

Returns
Every SoundId below it is valid.

◆ stop()

void aegis2d::audio::AudioDevice::stop ( Channel  channel)
noexcept

Stops every sound of a channel at once.

Parameters
channelChannel to silence.

◆ stop_all()

void aegis2d::audio::AudioDevice::stop_all ( )
noexcept

Stops every sound at once.

◆ stop_music()

void aegis2d::audio::AudioDevice::stop_music ( )
noexcept

Stops the background music.

◆ update()

void aegis2d::audio::AudioDevice::update ( )
noexcept

Frees the sounds that have finished playing; call it once per frame.

◆ volumes()

Volumes aegis2d::audio::AudioDevice::volumes ( ) const
noexcept

Current volumes.

Returns
The volumes given to set_volumes(), clamped; all at 1 at first.

Member Data Documentation

◆ max_voices

constexpr std::size_t aegis2d::audio::AudioDevice::max_voices = 32
staticconstexpr

Most sounds played at the same time; playing one more stops the oldest effect.


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