The default audio output and the sounds it plays.
More...
#include <aegis2d/audio/audio.hpp>
|
| static constexpr std::size_t | max_voices = 32 |
| | Most sounds played at the same time; playing one more stops the oldest effect.
|
| |
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.
...
if (actions.pressed(fire, window.keyboard())) {
}
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.
◆ AudioDevice() [1/3]
| aegis2d::audio::AudioDevice::AudioDevice |
( |
| ) |
|
Opens the default audio output of the system.
- Exceptions
-
| AudioError | if 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.
◆ load()
| SoundId aegis2d::audio::AudioDevice::load |
( |
const std::filesystem::path & |
path | ) |
|
Loads a sound from a WAV file.
- Parameters
-
| path | WAV 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
-
| AudioError | if 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 |
◆ operator=() [1/2]
Closes this device and takes over the device of other.
- Returns
- This device.
◆ operator=() [2/2]
◆ 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
-
| sound | Identifier returned by load(). |
| channel | Channel the sound belongs to. |
| pan | Stereo position: -1 (full left), 0 (center), 1 (full right); clamped. |
| gain_scale | Distance attenuation: 0 (silent) to 1 (normal); clamped. |
- Exceptions
-
| std::out_of_range | if sound was not loaded. |
| AudioError | if SDL cannot convert or start the sound. |
◆ play() [2/2]
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
-
| sound | Identifier returned by load(). |
| channel | Channel the sound belongs to. |
- Exceptions
-
| std::out_of_range | if sound was not loaded. |
| AudioError | if 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
-
| sound | Identifier returned by load(). |
| loop | true to loop until stop_music(), false to play it once. |
- Exceptions
-
| std::out_of_range | if sound was not loaded. |
| AudioError | if 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
-
| channel | Channel 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
-
| volumes | Slider 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
-
| channel | Channel 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.
◆ 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: