 |
Cytopia
0.3
A city building simulation game
|
#include <AudioMixer.hxx>
Definition at line 43 of file AudioMixer.hxx.
◆ DEFAULT_CHANNELS
◆ ORIENTATION_INDEX
orientation of listener
Enumerator |
---|
FORWARD_X | |
FORWARD_Y | |
FORWARD_Z | |
UP_X | |
UP_Y | |
UP_Z | |
Definition at line 124 of file AudioMixer.hxx.
◆ POSITION_INDEX
◆ AudioMixer() [1/3]
AudioMixer::AudioMixer |
( |
| ) |
|
- Precondition
- GameClock must be initialized
- Exceptions
-
ConfigurationError | if there is a problem opening the audio config file |
AudioError | if OpenAL context cannot be initialized or an OpenAL error occurs |
Definition at line 21 of file AudioMixer.cxx.
24 json config_json = json::parse(jsonFileContent,
nullptr,
false);
27 if (config_json.is_discarded())
31 for (
auto &item : audioConfig.
Music)
32 for (
auto &trigger : item.second.triggers)
34 for (
auto &item : audioConfig.
Sound)
35 for (
auto &trigger : item.second.triggers)
43 LOG(
LOG_WARNING) <<
"Unable to initialize default audio device! " << error_msg;
59 if (err != AL_NO_ERROR)
66 alListener3f(AL_VELOCITY, 0.0f, 0.0f, 0.0f);
76 Array<float, 6> listener_orientation_vector{0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f};
77 alListenerfv(AL_ORIENTATION, listener_orientation_vector.data());
◆ ~AudioMixer()
AudioMixer::~AudioMixer |
( |
| ) |
|
◆ AudioMixer() [2/3]
◆ AudioMixer() [3/3]
◆ get_al_error_msg()
const char * AudioMixer::get_al_error_msg |
( |
ALenum |
error | ) |
|
|
private |
Get a descriptive error message from an error code.
Definition at line 396 of file AudioMixer.cxx.
400 case AL_INVALID_NAME:
401 return "a bad name (ID) was passed to an OpenAL function";
402 case AL_INVALID_ENUM:
403 return "an invalid enum value was passed to an OpenAL function";
404 case AL_INVALID_VALUE:
405 return "an invalid value was passed to an OpenAL function";
406 case AL_INVALID_OPERATION:
407 return "the requested operation is not valid";
408 case AL_OUT_OF_MEMORY:
409 return "the requested operation resulted in OpenAL running out of memory";
411 return "there is not currently an error";
413 return "Unknown error";
◆ getTrack() [1/2]
Definition at line 106 of file AudioMixer.cxx.
114 if (possibilities.empty())
116 LOG(
LOG_WARNING) <<
"No Soundtracks are triggered by " << trigger._to_string();
◆ getTrack() [2/2]
◆ onTrackFinished()
void AudioMixer::onTrackFinished |
( |
int |
channelID | ) |
|
|
private |
Called whenever a Channel has finished playing.
- Parameters
-
channelID | the channel that has finished playing |
Definition at line 378 of file AudioMixer.cxx.
381 [channelID](
const auto trackptr)
383 const auto &track = *trackptr;
384 return track && track->Channel.get() == channelID;
389 track->isPlaying =
false;
◆ onTrackFinishedFuncPtr()
static void AudioMixer::onTrackFinishedFuncPtr |
( |
int |
channelID | ) |
|
|
inlinestaticprivate |
Because of SDL_Mixer's poor design, we are forced to use a function pointer. This means no lambda, no std::function, no std::bind. Our workaround involves creating the onTrackFinishedFunc global function object which captures the AudioMixer object
Definition at line 201 of file AudioMixer.hxx.
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ play() [1/4]
void AudioMixer::play |
( |
const AudioTrigger & |
trigger, |
|
|
const Coordinate3D & |
position, |
|
|
int |
effect = AL_EFFECT_NULL |
|
) |
| |
Plays a 3D Soundtrack from a trigger and optionally applies an effect.
- Parameters
-
trigger | the AudioTrigger |
position | the Coordinate3D position of the sound |
effect | the effect to apply. Should be in the form of an AL_EFFECT macro such as AL_EFFECT_REVERB or AL_EFFECT_ECHO. |
Definition at line 196 of file AudioMixer.cxx.
203 alSource3f(track->source[0], AL_POSITION,
static_cast<ALfloat
>(position.
x),
static_cast<ALfloat
>(position.
y),
204 static_cast<ALfloat
>(position.
z));
205 alSource3f(track->source[1], AL_POSITION,
static_cast<ALfloat
>(position.
x),
static_cast<ALfloat
>(position.
y),
206 static_cast<ALfloat
>(position.
z));
207 play(trigger, effect);
◆ play() [2/4]
void AudioMixer::play |
( |
const AudioTrigger & |
trigger, |
|
|
int |
effect = AL_EFFECT_NULL |
|
) |
| |
Plays a random Soundtrack from a trigger and optionally applies an effect.
- Parameters
-
trigger | the AudioTrigger |
effect | the effect to apply. Should be in the form of an AL_EFFECT macro such as AL_EFFECT_REVERB or AL_EFFECT_ECHO. |
Definition at line 170 of file AudioMixer.cxx.
175 if (effect == AL_EFFECT_NULL)
◆ play() [3/4]
Plays a 3D Soundtrack given its ID and optionally applies an effect.
- Parameters
-
id | the SoundtrackID |
position | the Coordinate3D position of the sound |
effect | the effect to apply. Should be in the form of an AL_EFFECT macro such as AL_EFFECT_REVERB or AL_EFFECT_ECHO. |
Definition at line 182 of file AudioMixer.cxx.
188 alSource3f(track->source[0], AL_POSITION,
static_cast<ALfloat
>(position.
x),
static_cast<ALfloat
>(position.
y),
189 static_cast<ALfloat
>(position.
z));
190 alSource3f(track->source[1], AL_POSITION,
static_cast<ALfloat
>(position.
x),
static_cast<ALfloat
>(position.
y),
191 static_cast<ALfloat
>(position.
z));
◆ play() [4/4]
void AudioMixer::play |
( |
const SoundtrackID & |
id, |
|
|
int |
effect = AL_EFFECT_NULL |
|
) |
| |
Plays a Soundtrack given its ID and optionally applies an effect.
- Parameters
-
id | the SoundtrackID |
effect | the effect to apply. Should be in the form of an AL_EFFECT macro such as AL_EFFECT_REVERB or AL_EFFECT_ECHO. |
Definition at line 158 of file AudioMixer.cxx.
163 if (effect == AL_EFFECT_NULL)
◆ playSoundtrack()
Plays the Soundtrack.
- Parameters
-
- Exceptions
-
AudioError | if track is invalid or its source is uninitialized |
Definition at line 259 of file AudioMixer.cxx.
267 alSourcePlay(track->source[track->isMusic]);
270 track->isPlaying =
true;
◆ playSoundtrackWithEffect()
void AudioMixer::playSoundtrackWithEffect |
( |
SoundtrackUPtr & |
soundtrack, |
|
|
int |
ALEffect |
|
) |
| |
|
private |
Plays the Soundtrack with a certain effect applied.
Given the soundtrack, this function will apply the given AL effect to the audio with the default settings.
- Parameters
-
soundtrack | the Soundtrack |
ALEffect | the effect to apply. Should be in the form of an AL_EFFECT macro such as AL_EFFECT_REVERB or AL_EFFECT_ECHO. |
- Exceptions
-
AudioError | if track is invalid, its source is uninitialized, or reverb effect could not be applied |
Definition at line 299 of file AudioMixer.cxx.
308 #define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x))
340 alEffecti(effect, AL_EFFECT_TYPE, ALEffect);
345 ALenum err = alGetError();
346 if (err != AL_NO_ERROR)
352 fprintf(stderr,
"OpenAL error: %s\n", alGetString(err));
364 assert(alGetError() == AL_NO_ERROR &&
"Failed to set effect slot");
369 alSource3i(track->source[track->isMusic], AL_AUXILIARY_SEND_FILTER, (ALint)(track->effect_slot), 0, AL_FILTER_NULL);
370 assert(alGetError() == AL_NO_ERROR &&
"Failed to setup reverb for sound source send 0.");
373 alSourcePlay(track->source[track->isMusic]);
375 track->isPlaying =
true;
◆ prune()
void AudioMixer::prune |
( |
| ) |
|
Updates soundtracks that are no longer playing.
Definition at line 230 of file AudioMixer.cxx.
240 alGetSourcei((**it)->source[(**it)->isMusic], AL_SOURCE_STATE, &state);
241 if (state != AL_PLAYING)
243 (**it)->isPlaying =
false;
◆ setMusicVolume()
void AudioMixer::setMusicVolume |
( |
float |
volume | ) |
|
sets the music volume
- Parameters
-
- Precondition
- volume must be a float within [0, 1]
- Postcondition
- Settings::MusicVolume is changed
Definition at line 139 of file AudioMixer.cxx.
143 alSourcef((**it).source[1], AL_GAIN, volume);
◆ setMuted()
void AudioMixer::setMuted |
( |
bool |
isMuted | ) |
|
toggles the mute option for sounds
- Parameters
-
isMuted | true to mute all sounds, false to unmute |
- Exceptions
-
Definition at line 211 of file AudioMixer.cxx.
◆ setSoundEffectVolume()
void AudioMixer::setSoundEffectVolume |
( |
float |
volume | ) |
|
sets the sound effects volume
- Parameters
-
- Precondition
- volume must be a float within [0, 1]
- Postcondition
- Settings::SoundEffectsVolume is changed
Definition at line 149 of file AudioMixer.cxx.
153 alSourcef((**it).source[0], AL_GAIN, volume);
◆ stopAll()
void AudioMixer::stopAll |
( |
| ) |
|
Stops all soundtracks.
Definition at line 213 of file AudioMixer.cxx.
223 alSourceStop((**it)->source[0]);
224 alSourceStop((**it)->source[1]);
225 (**it)->isPlaying =
false;
◆ Game
◆ alContext
ALCcontext* AudioMixer::alContext |
|
private |
OpenAL Soft setup, context of where audio is played.
Definition at line 217 of file AudioMixer.hxx.
◆ gAudioDevice
ALCdevice* AudioMixer::gAudioDevice |
|
private |
OpenAL Soft setup, audio device to be used.
Definition at line 212 of file AudioMixer.hxx.
◆ m_GetSound
◆ m_Playing
All the currently playing Soundtracks.
Definition at line 161 of file AudioMixer.hxx.
◆ m_Soundtracks
◆ m_Triggers
A Mapping between triggers and SoundtrackID.
Definition at line 151 of file AudioMixer.hxx.
◆ onTrackFinishedFunc
std::function< void(int)> AudioMixer::onTrackFinishedFunc |
|
staticprivate |
The documentation for this class was generated from the following files:
static LPALGETEFFECTF alGetEffectf
static LPALEFFECTI alEffecti
SoundtrackUPtr & getTrack(const AudioTrigger &trigger)
float soundEffectsVolume
the volume of sound effects as float between [0, 1]
Iterator choose(Iterator begin, Iterator end)
Pick random item from container.
static std::function< void(int)> onTrackFinishedFunc
static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv
void playSoundtrack(SoundtrackUPtr &soundtrack)
Plays the Soundtrack.
static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti
static LPALISEFFECT alIsEffect
static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv
static LPALEFFECTIV alEffectiv
static LPALGENEFFECTS alGenEffects
static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti
static LPALDELETEEFFECTS alDeleteEffects
static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv
std::string readFileAsString(const std::string &fileName, bool binaryMode)
Read contents from a file as string.
void playSoundtrackWithEffect(SoundtrackUPtr &soundtrack, int ALEffect)
Plays the Soundtrack with a certain effect applied.
static SoundtrackUPtr noResoruce
static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv
List< SoundtrackUPtr * > m_Playing
All the currently playing Soundtracks.
static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf
static LPALEFFECTF alEffectf
An unimplemented function was called.
static LPALGETEFFECTI alGetEffecti
float musicVolume
the volume of music as float between [0, 1]
static LPALGETEFFECTFV alGetEffectfv
static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots
Mapping< string, SoundtrackConfiguration > Sound
ALCcontext * alContext
OpenAL Soft setup, context of where audio is played.
void play(const SoundtrackID &id, int effect=AL_EFFECT_NULL)
Plays a Soundtrack given its ID and optionally applies an effect.
static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf
static LPALEFFECTFV alEffectfv
static Settings & instance(void)
Get an instance of the singleton.
const char * get_al_error_msg(ALenum error)
Get a descriptive error message from an error code.
Array< Vector< SoundtrackID >, AudioTrigger::_size()> m_Triggers
A Mapping between triggers and SoundtrackID.
std::array< Type, N > Array
GameClock::ClockTaskHndl addRealTimeClockTask(ClockCbk cbk, DelayType delay, PeriodType period=TimePointZero)
Add new real time clock task.
static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots
resource_type< ResourceID >::type get(const ResourceID &)
Fetches and return a Soundtrack.
ALCdevice * gAudioDevice
OpenAL Soft setup, audio device to be used.
Mapping< string, SoundtrackConfiguration > Music
static LPALGETEFFECTIV alGetEffectiv
An audio-related error occured.
static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot