CUGL 1.3
Cornell University Game Library
Public Types | Public Member Functions | Static Public Member Functions | List of all members
cugl::AudioChannels Class Reference

#include <CUAudioChannels.h>

Public Types

enum  State { State::INACTIVE, State::PLAYING, State::PAUSED }
 

Public Member Functions

void playMusic (const std::shared_ptr< Sound > &music, bool loop=false, float volume=-1.0f, float fade=0.0f)
 
const SoundcurrentMusic () const
 
State getMusicState () const
 
bool isMusicLoop () const
 
void setMusicLoop (bool loop)
 
float getMusicVolume () const
 
void setMusicVolume (float volume)
 
float getMusicPan () const
 
void setMusicPan (float pan)
 
float getMusicDuration () const
 
float getMusicElapsed () const
 
float getMusicRemaining () const
 
void setMusicElapsed (float time)
 
void setMusicRemaining (float time)
 
void stopMusic (float fade=DEFAULT_FADE)
 
void pauseMusic (float fade=DEFAULT_FADE)
 
void resumeMusic ()
 
void setMusicListener (std::function< void(Sound *, bool)> callback)
 
std::function< void(Sound *, bool)> getMusicListener () const
 
void queueMusic (const std::shared_ptr< Sound > &music, bool loop=false, float volume=-1.0f, float fade=0.0f)
 
const std::vector< Sound * > getMusicQueue () const
 
size_t getMusicPending () const
 
float getOverlap () const
 
void setOverlap (double time)
 
void advanceMusicQueue (float fade=DEFAULT_FADE, unsigned int steps=0)
 
void clearMusicQueue ()
 
bool playEffect (const std::string &key, const std::shared_ptr< Sound > &sound, bool loop=false, float volume=-1.0f, bool force=false)
 
bool playEffect (const char *key, const std::shared_ptr< Sound > &sound, bool loop=false, float volume=1.0f, bool force=false)
 
size_t getAvailableChannels () const
 
State getEffectState (const std::string &key) const
 
State getEffectState (const char *key) const
 
bool isActiveEffect (const std::string &key) const
 
bool isActiveEffect (const char *key) const
 
const SoundcurrentEffect (const std::string &key) const
 
const SoundcurrentEffect (const char *key) const
 
bool isEffectLoop (const std::string &key) const
 
bool isEffectLoop (const char *key) const
 
void setEffectLoop (const std::string &key, bool loop)
 
void setEffectLoop (const char *key, bool loop)
 
float getEffectVolume (const std::string &key) const
 
float getEffectVolume (const char *key) const
 
void setEffectVolume (const std::string &key, float volume)
 
void setEffectVolume (const char *key, float volume)
 
float getEffectPan (const std::string &key) const
 
float getEffectPan (const char *key) const
 
void setEffectPan (const std::string &key, float pan)
 
void setEffectPan (const char *key, float pan)
 
float getEffectDuration (const std::string &key) const
 
float getEffectDuration (const char *key) const
 
float getEffectElapsed (const std::string &key) const
 
float getEffectElapsed (const char *key) const
 
void setEffectElapsed (const std::string &key, float time)
 
void setEffectElapsed (const char *key, float time)
 
float getEffectRemaining (const std::string &key) const
 
float getEffectRemaining (const char *key) const
 
void setEffectRemaining (const std::string &key, float time)
 
void setEffectRemaining (const char *key, float time)
 
void stopEffect (const std::string &key, float fade=DEFAULT_FADE)
 
void stopEffect (const char *key, float fade=DEFAULT_FADE)
 
void pauseEffect (const std::string &key, float fade=DEFAULT_FADE)
 
void pauseEffect (const char *key, float fade=DEFAULT_FADE)
 
void resumeEffect (std::string key)
 
void resumeEffect (const char *key)
 
void stopAllEffects (float fade=DEFAULT_FADE)
 
void pauseAllEffects (float fade=DEFAULT_FADE)
 
void resumeAllEffects ()
 
void setEffectListener (std::function< void(const std::string &key, bool)> callback)
 
std::function< void(const std::string &key, bool)> getEffectListener () const
 
void stopAll (float fade=DEFAULT_FADE)
 
void pauseAll (float fade=DEFAULT_FADE)
 
void resumeAll ()
 

Static Public Member Functions

static AudioChannelsget ()
 
static void start (Uint32 slots)
 
static void start (Uint32 channels, Uint32 buffer)
 
static void stop ()
 

Detailed Description

Class provides a singleton audio engine

This class is a simple (e.g. 2000-era) audio engine. It exposes a flat channel structure and there is no (publicly available) mixer graph for advanced effects such as 3D audio. If you need these features, use AudioManager instead. In fact, you should only use one of either this class or AudioManager, not both (since this class built on top of AudioManager).

This class separates sound into two categories: music and effects. In theory, streaming assets should be used as music and non-streaming assets should be used for effects. However, this difference is not enforced and any Sound asset may be used either as a music asset or an effect asset freely at any time.

Music is treated separately because we assume that only one music asset is played at a time. Music can be queued up and there a methods to manage this queue, to allow you to play a continuous, uninterrupted stream of music that reacts to the player's actions.

Effects on the other hand are intended for short sound effects that are often happening in parallel. The engine has a fixed number of "channels" or slots for these sounds (historically 24) and it can only play as many sounds simultaneously as it has slots. Slots are assigned automatically by the engine. However, when you play an effect, you must assign it a unique key so that you can access it later (for volume changes, panning, early termination, etc.). This key eliminates any need for tracking the channel assigned to an effect.

Sound assets may either by mono or stereo, but must be encoded at 48000 samples per second. The output of the audio engine is always stereo, regardless of the sounds asset. Mono assets are played half volume to each (left/right) speaker.

You cannot create new instances of this class. Instead, you should access the singleton through the three static methods: start(), stop(), and get().

IMPORTANT: Like the OpenGL context, this class is not thread-safe. It is only safe to access this class in the main application thread. This means it should never be called in a call-back function as those are typically executed in the host thread. If you need to access the AudioEngine in a callback function, you should use the Application#schedule method to delay until the main thread is next available.

Member Enumeration Documentation

◆ State

This enumeration provides a way to determine the state of a channel

Enumerator
INACTIVE 

This sound channel is not actually active

PLAYING 

This sound is active and currently playing

PAUSED 

This sound is active but is currently paused

Member Function Documentation

◆ advanceMusicQueue()

void cugl::AudioChannels::advanceMusicQueue ( float  fade = DEFAULT_FADE,
unsigned int  steps = 0 
)

Advances ahead in the music queue.

The value fade is the number of seconds to fade out the currently playing music assets (if any). This is to ensure a smooth transition to the next song. If the music ends naturally, before this time, the fadeout will not carry over to later entries in the queue.

The value step is the number of songs to skip over. A value of 0 will simply skip over the active music to the next element of the queue. Each value above 0 will skip over one more element in the queue. If this skipping empties the queue, no music will play.

Parameters
fadeThe number of seconds to fade out the current asset
stepsThe number of elements to skip in the queue

◆ clearMusicQueue()

void cugl::AudioChannels::clearMusicQueue ( )

Clears the music queue, but does not release any other resources.

This method does not stop the current background music from playing. It only clears pending music assets from the queue.

◆ currentEffect() [1/2]

const Sound* cugl::AudioChannels::currentEffect ( const char *  key) const
inline

Returns the sound asset attached to the given key.

If there is no active sound effect for the given key, this method returns nullptr.

Parameters
keythe reference key for the sound effect
Returns
the sound asset attached to the given key.

◆ currentEffect() [2/2]

const Sound* cugl::AudioChannels::currentEffect ( const std::string &  key) const

Returns the sound asset attached to the given key.

If there is no active sound effect for the given key, this method returns nullptr.

Parameters
keythe reference key for the sound effect
Returns
the sound asset attached to the given key.

◆ currentMusic()

const Sound* cugl::AudioChannels::currentMusic ( ) const

Returns the music asset currently playing

If there is no active background music, this method returns nullptr.

Returns
the music asset currently playing

◆ get()

static AudioChannels* cugl::AudioChannels::get ( )
inlinestatic

Returns the singleton instance of the audio engine.

If the audio engine has not been started, then this method will return nullptr.

Returns
the singleton instance of the audio engine.

◆ getAvailableChannels()

size_t cugl::AudioChannels::getAvailableChannels ( ) const
inline

Returns the number of slots or "channels" available for sound effects.

There are a limited number of channels available for sound effects. If all channels are in use, this method will return 0. If you go over the number available, you cannot play another sound unless you force it. In that case, it will grab the channel from the longest playing sound effect.

Returns
the number of channels available for sound effects.

◆ getEffectDuration() [1/2]

float cugl::AudioChannels::getEffectDuration ( const char *  key) const
inline

Returns the duration of the sound effect, in seconds.

Because the asset is fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method returns -1.

Parameters
keythe reference key for the sound effect
Returns
the duration of the sound effect, in seconds.

◆ getEffectDuration() [2/2]

float cugl::AudioChannels::getEffectDuration ( const std::string &  key) const

Returns the duration of the sound effect, in seconds.

Because the asset is fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method returns -1.

Parameters
keythe reference key for the sound effect
Returns
the duration of the sound effect, in seconds.

◆ getEffectElapsed() [1/2]

float cugl::AudioChannels::getEffectElapsed ( const char *  key) const
inline

Returns the elapsed time of the sound effect, in seconds

The elapsed time is the current position of the sound from the beginning. It does not include any time spent on a continuous loop. Because most sound effects are fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method returns -1.

Parameters
keythe reference key for the sound effect
Returns
the elapsed time of the sound effect, in seconds

◆ getEffectElapsed() [2/2]

float cugl::AudioChannels::getEffectElapsed ( const std::string &  key) const

Returns the elapsed time of the sound effect, in seconds

The elapsed time is the current position of the sound from the beginning. It does not include any time spent on a continuous loop. Because most sound effects are fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method returns -1.

Parameters
keythe reference key for the sound effect
Returns
the elapsed time of the sound effect, in seconds

◆ getEffectListener()

std::function<void(const std::string& key,bool)> cugl::AudioChannels::getEffectListener ( ) const
inline

Returns the callback for sound effects

This callback function is called whenever a sound effect completes. It is called whether or not the sound completed normally or if it was terminated manually. However, the second parameter can be used to distinguish the two cases.

Returns
the callback for sound effects

◆ getEffectPan() [1/2]

float cugl::AudioChannels::getEffectPan ( const char *  key) const
inline

Returns the stereo pan of the sound effect.

This audio engine provides limited (e.g. not full 3D) stereo panning for simple effects. The pan value is a float from -1 to 1. A value of 0 (default) plays to both channels (regardless of whether the current effect is mono or stereo). A value of -1 will play to the left channel only, while the right will play to the right channel only.

In the case of stereo assets, panning to the left or right will mix the audio feed; this process will never lose audio.

If the key does not correspond to a channel, this method returns 0.

Parameters
keythe reference key for the sound effect
Returns
the stereo pan of the sound effect

◆ getEffectPan() [2/2]

float cugl::AudioChannels::getEffectPan ( const std::string &  key) const

Returns the stereo pan of the sound effect.

This audio engine provides limited (e.g. not full 3D) stereo panning for simple effects. The pan value is a float from -1 to 1. A value of 0 (default) plays to both channels (regardless of whether the current effect is mono or stereo). A value of -1 will play to the left channel only, while the right will play to the right channel only.

In the case of stereo assets, panning to the left or right will mix the audio feed; this process will never lose audio.

If the key does not correspond to a channel, this method returns 0.

Parameters
keythe reference key for the sound effect
Returns
the stereo pan of the sound effect

◆ getEffectRemaining() [1/2]

float cugl::AudioChannels::getEffectRemaining ( const char *  key) const
inline

Returns the time remaining for the sound effect, in seconds

The time remaining is just duration-elapsed. This method does not take into account whether the sound is on a loop. Because most sound effects are fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method returns -1.

Parameters
keythe reference key for the sound effect
Returns
the time remaining for the sound effect, in seconds

◆ getEffectRemaining() [2/2]

float cugl::AudioChannels::getEffectRemaining ( const std::string &  key) const

Returns the time remaining for the sound effect, in seconds

The time remaining is just duration-elapsed. This method does not take into account whether the sound is on a loop. Because most sound effects are fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method returns -1.

Parameters
keythe reference key for the sound effect
Returns
the time remaining for the sound effect, in seconds

◆ getEffectState() [1/2]

State cugl::AudioChannels::getEffectState ( const char *  key) const
inline

Returns the current state of the sound effect for the given key.

If there is no sound effect for the given key, it returns State::INACTIVE.

Parameters
keythe reference key for the sound effect
Returns
the current state of the sound effect for the given key.

◆ getEffectState() [2/2]

State cugl::AudioChannels::getEffectState ( const std::string &  key) const

Returns the current state of the sound effect for the given key.

If there is no sound effect for the given key, it returns State::INACTIVE.

Parameters
keythe reference key for the sound effect
Returns
the current state of the sound effect for the given key.

◆ getEffectVolume() [1/2]

float cugl::AudioChannels::getEffectVolume ( const char *  key) const
inline

Returns the current volume of the sound effect.

The volume is a value 0 to 1, where 1 is maximum volume and 0 is complete silence. If the key does not correspond to a channel, this method returns 0.

Parameters
keythe reference key for the sound effect
Returns
the current volume of the sound effect

◆ getEffectVolume() [2/2]

float cugl::AudioChannels::getEffectVolume ( const std::string &  key) const

Returns the current volume of the sound effect.

The volume is a value 0 to 1, where 1 is maximum volume and 0 is complete silence. If the key does not correspond to a channel, this method returns 0.

Parameters
keythe reference key for the sound effect
Returns
the current volume of the sound effect

◆ getMusicDuration()

float cugl::AudioChannels::getMusicDuration ( ) const

Returns the length of background music, in seconds.

This is only the duration of the active background music. All other music in the queue is ignored. If there is no active background music, this method will return 0.

This information is retrieved from the decoder. As the file is completely decoded at load time, the result of this method is reasonably accurate.

Returns
the length of background music, in seconds.

◆ getMusicElapsed()

float cugl::AudioChannels::getMusicElapsed ( ) const

Returns the elapsed time of the background music, in seconds

The elapsed time is the current position of the music from the beginning. It does not include any time spent on a continuous loop. If there is no active background music, this method will return 0.

This information is not guaranteed to be accurate. Attempting to time the playback of streaming data (as opposed to a fully in-memory PCM buffer) is very difficult and not cross-platform. We have tried to be reasonably accurate, but from our tests we can only guarantee accuracy within a 10th of a second.

Returns
the elapsed time of the background music, in seconds

◆ getMusicListener()

std::function<void(Sound*,bool)> cugl::AudioChannels::getMusicListener ( ) const
inline

Returns the callback for background music

This callback function is called whenever a background music track completes. It is called whether or not the music completed normally or if it was terminated manually. However, the second parameter can be used to distinguish the two cases.

Returns
the callback for background music

◆ getMusicPan()

float cugl::AudioChannels::getMusicPan ( ) const

Returns the stereo pan of the background music

This audio engine provides limited (e.g. not full 3D) stereo panning for simple effects. The pan value is a float from -1 to 1. A value of 0 (default) plays to both channels (regardless of whether the current music is mono or stereo). A value of -1 will play to the left channel only, while the right will play to the right channel only.

In the case of stereo assets, panning to the left or right will mix the audio feed; this process will never lose audio.

Returns
the stereo pan of the background music

◆ getMusicPending()

size_t cugl::AudioChannels::getMusicPending ( ) const

Returns the size of the music queue

Returns
the size of the music queue

◆ getMusicQueue()

const std::vector<Sound*> cugl::AudioChannels::getMusicQueue ( ) const

Returns the list of assets for the music queue

Returns
the list of assets for the music queue

◆ getMusicRemaining()

float cugl::AudioChannels::getMusicRemaining ( ) const

Returns the time remaining for the background music, in seconds

The time remaining is just duration-elapsed. This method does not take into account whether the music is on a loop. It also does not include the duration of any music waiting in the queue. If there is no active background music, this method will return 0.

This information is not guaranteed to be accurate. Attempting to time the playback of streaming data (as opposed to a fully in-memory PCM buffer) is very difficult and not cross-platform. We have tried to be reasonably accurate, but from our tests we can only guarantee accuracy within a 10th of a second.

Returns
the time remaining for the background music, in seconds

◆ getMusicState()

State cugl::AudioChannels::getMusicState ( ) const

Returns the current state of the background music

Returns
the current state of the background music

◆ getMusicVolume()

float cugl::AudioChannels::getMusicVolume ( ) const

Returns the volume of the background music

The volume is a value 0 to 1, where 1 is maximum volume and 0 is complete silence. If there is no active background music, this method will return 0.

Returns
the volume of the background music

◆ getOverlap()

float cugl::AudioChannels::getOverlap ( ) const

Returns the overlap time in seconds.

The overlap time is the amount of time to cross-fade between a music asset and the next. It does not apply to looped music; music assets can never cross-fade with themselves.

By default, this value is 0. Assets play sequentially but do not overlap. However, you may get smoother transitions between musical segments if you adjust this value. The overlap should be chosen with care. If the play length of an asset is less than the overlap, the results are undefined.

Returns
the overlap time in seconds.

◆ isActiveEffect() [1/2]

bool cugl::AudioChannels::isActiveEffect ( const char *  key) const
inline

Returns true if the key is associated with an active channel.

Parameters
keythe reference key for the sound effect
Returns
true if the key is associated with an active channel.

◆ isActiveEffect() [2/2]

bool cugl::AudioChannels::isActiveEffect ( const std::string &  key) const
inline

Returns true if the key is associated with an active channel.

Parameters
keythe reference key for the sound effect
Returns
true if the key is associated with an active channel.

◆ isEffectLoop() [1/2]

bool cugl::AudioChannels::isEffectLoop ( const char *  key) const
inline

Returns true if the sound effect is in a continuous loop.

If the key does not correspond to a channel, this method returns false.

Parameters
keythe reference key for the sound effect
Returns
true if the sound effect is in a continuous loop.

◆ isEffectLoop() [2/2]

bool cugl::AudioChannels::isEffectLoop ( const std::string &  key) const

Returns true if the sound effect is in a continuous loop.

If the key does not correspond to a channel, this method returns false.

Parameters
keythe reference key for the sound effect
Returns
true if the sound effect is in a continuous loop.

◆ isMusicLoop()

bool cugl::AudioChannels::isMusicLoop ( ) const

Returns true if the background music is in a continuous loop.

If there is no active background music, this method will return false.

Returns
true if the background music is in a continuous loop.

◆ pauseAll()

void cugl::AudioChannels::pauseAll ( float  fade = DEFAULT_FADE)

Pauses all sounds, both music and sound effects.

Before the sounds are paused, this method gives the user an option to fade out everything. If the argument is 0, it will pause the sounds immediately. Otherwise it will fade everythign to completion over the given number of seconds (or until the end of each sound). Only by fading can you guarantee no audible clicks.

This method allows them to be resumed later. You should generally call this method just before the app pages to the background.

Parameters
fadethe number of seconds to fade out

◆ pauseAllEffects()

void cugl::AudioChannels::pauseAllEffects ( float  fade = DEFAULT_FADE)

Pauses all sound effects, allowing them to be resumed later.

Before the effects are paused, this method gives the user an option to fade out the effect. If the argument is 0, it will pause all effects immediately. Otherwise it will fade the to completion over the given number of seconds (or until the end of the effect). Only by fading can you guarantee no audible clicks.

Sound effects already paused will remain paused.

Parameters
fadethe number of seconds to fade out

◆ pauseEffect() [1/2]

void cugl::AudioChannels::pauseEffect ( const char *  key,
float  fade = DEFAULT_FADE 
)
inline

Pauses the sound effect for the given key.

Before the effect is paused, this method gives the user an option to fade out the effect. If the argument is 0, it will pause the sound immediately. Otherwise it will fade to completion over the given number of seconds (or until the end of the effect). Only by fading can you guarantee no audible clicks.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
fadethe number of seconds to fade out

◆ pauseEffect() [2/2]

void cugl::AudioChannels::pauseEffect ( const std::string &  key,
float  fade = DEFAULT_FADE 
)

Pauses the sound effect for the given key.

Before the effect is paused, this method gives the user an option to fade out the effect. If the argument is 0, it will pause the sound immediately. Otherwise it will fade to completion over the given number of seconds (or until the end of the effect). Only by fading can you guarantee no audible clicks.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
fadethe number of seconds to fade out

◆ pauseMusic()

void cugl::AudioChannels::pauseMusic ( float  fade = DEFAULT_FADE)

Pauses the background music, allowing it to be resumed later.

Before the music is stopped, this method gives the user an option to fade out the music. If the argument is 0, it will pause the music immediately. Otherwise it will fade to completion over the given number of seconds (or until the end of the song). Only by fading can you guarantee no audible clicks.

This method has no effect on the music queue.

Parameters
fadeThe number of seconds to fade out

◆ playEffect() [1/2]

bool cugl::AudioChannels::playEffect ( const char *  key,
const std::shared_ptr< Sound > &  sound,
bool  loop = false,
float  volume = 1.0f,
bool  force = false 
)
inline

Plays the given sound effect, and associates it with the specified key.

Sound effects are associated with a reference key. This allows the application to easily reference the sound state without having to internally manage pointers to the audio channel.

If the key is already associated with an active sound effect, this method will stop the existing sound and replace it with this one. It is the responsibility of the application layer to manage key usage.

There are a limited number of slots ("channels") available for sound effects. If you go over the number available, the sound will not play unless force is true. In that case, it will grab the channel from the longest playing sound effect.

Parameters
keyThe reference key for the sound effect
soundThe sound effect to play
loopWhether to loop the sound effect continuously
volumeThe sound effect (< 0 to use asset default volume)
forceWhether to force another sound to stop.
Returns
true if there was an available channel for the sound

◆ playEffect() [2/2]

bool cugl::AudioChannels::playEffect ( const std::string &  key,
const std::shared_ptr< Sound > &  sound,
bool  loop = false,
float  volume = -1.0f,
bool  force = false 
)

Plays the given sound effect, and associates it with the specified key.

Sound effects are associated with a reference key. This allows the application to easily reference the sound state without having to internally manage pointers to the audio channel.

If the key is already associated with an active sound effect, this method will stop the existing sound and replace it with this one. It is the responsibility of the application layer to manage key usage.

There are a limited number of slots ("channels") available for sound effects. If you go over the number available, the sound will not play unless force is true. In that case, it will grab the channel from the longest playing sound effect.

Parameters
keyThe reference key for the sound effect
soundThe sound effect to play
loopWhether to loop the sound effect continuously
volumeThe sound effect (< 0 to use asset default volume)
forceWhether to force another sound to stop.
Returns
true if there was an available channel for the sound

◆ playMusic()

void cugl::AudioChannels::playMusic ( const std::shared_ptr< Sound > &  music,
bool  loop = false,
float  volume = -1.0f,
float  fade = 0.0f 
)

Plays given music asset as a background track.

Music is handled differently from sound effects. You can only play one music asset at a time. However, it is possible to queue music assets for immediate playback once the active asset is finished. Proper queue management is the keep for smooth, uninterrupted playback that responds to the user's actions.

This method immediately plays the provided asset. Hence it overrides and clears the music queue. To safely play an asset without affecting the music queue, use the method queueMusic instead.

When it begins playing, the music will start at full volume unless you provide a number of seconds to fade in. Note that looping a song will cause it to block the queue indefinitely until you turn off looping for that asset {

See also
setLoop}. This can be desired behavior, as it gives you a way to control the speed of the queue processing.
Parameters
musicThe music asset to play
loopWhether to loop the music continuously
volumeThe music volume (< 0 to use asset default volume)
fadeThe number of seconds to fade in

◆ queueMusic()

void cugl::AudioChannels::queueMusic ( const std::shared_ptr< Sound > &  music,
bool  loop = false,
float  volume = -1.0f,
float  fade = 0.0f 
)

Adds the given music asset to the background music queue

Music is handled differently from sound effects. You can only play one music asset at a time. However, it is possible to queue music assets for immediate playback once the active asset is finished. Proper queue management is the keep for smooth, uninterrupted playback that responds to the user's actions.

If the queue is empty and there is no active music, this method will play the music immediately. Otherwise, it will add the music to the queue, and it will play as soon as it is removed from the queue.

When it begins playing, the music will start at full volume unless you provide a number of seconds to fade in. Note that looping a song will cause it to block the queue indefinitely until you turn off looping for that asset {

See also
setLoop}. This can be desired behavior, as it gives you a way to control the speed of the queue processing.
Parameters
musicThe music asset to queue
loopWhether to loop the music continuously
volumeThe music volume (< 0 to use asset default volume)
fadeThe number of seconds to fade in

◆ resumeAll()

void cugl::AudioChannels::resumeAll ( )

Resumes all paused sounds, both music and sound effects.

You should generally call this method right after the app returns from the background.

◆ resumeAllEffects()

void cugl::AudioChannels::resumeAllEffects ( )

Resumes all paused sound effects.

◆ resumeEffect() [1/2]

void cugl::AudioChannels::resumeEffect ( const char *  key)
inline

Resumes the sound effect for the given key.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect

◆ resumeEffect() [2/2]

void cugl::AudioChannels::resumeEffect ( std::string  key)

Resumes the sound effect for the given key.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect

◆ resumeMusic()

void cugl::AudioChannels::resumeMusic ( )

Resumes the background music assuming that it was paused previously.

This method has no effect on the music queue.

◆ setEffectElapsed() [1/2]

void cugl::AudioChannels::setEffectElapsed ( const char *  key,
float  time 
)
inline

Sets the elapsed time of the sound effect, in seconds

The elapsed time is the current position of the sound from the beginning. It does not include any time spent on a continuous loop. Because most sound effects are fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
timethe new position of the sound effect

◆ setEffectElapsed() [2/2]

void cugl::AudioChannels::setEffectElapsed ( const std::string &  key,
float  time 
)

Sets the elapsed time of the sound effect, in seconds

The elapsed time is the current position of the sound from the beginning. It does not include any time spent on a continuous loop. Because most sound effects are fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
timethe new position of the sound effect

◆ setEffectListener()

void cugl::AudioChannels::setEffectListener ( std::function< void(const std::string &key, bool)>  callback)
inline

Sets the callback for sound effects

This callback function is called whenever a sound effect completes. It is called whether or not the sound completed normally or if it was terminated manually. However, the second parameter can be used to distinguish the two cases.

Parameters
callbackThe callback for sound effects

◆ setEffectLoop() [1/2]

void cugl::AudioChannels::setEffectLoop ( const char *  key,
bool  loop 
)
inline

Sets whether the sound effect is in a continuous loop.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
loopwhether the sound effect is in a continuous loop

◆ setEffectLoop() [2/2]

void cugl::AudioChannels::setEffectLoop ( const std::string &  key,
bool  loop 
)

Sets whether the sound effect is in a continuous loop.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
loopwhether the sound effect is in a continuous loop

◆ setEffectPan() [1/2]

void cugl::AudioChannels::setEffectPan ( const char *  key,
float  pan 
)
inline

Sets the stereo pan of the sound effect.

This audio engine provides limited (e.g. not full 3D) stereo panning for simple effects. The pan value is a float from -1 to 1. A value of 0 (default) plays to both channels (regardless of whether the current effect is mono or stereo). A value of -1 will play to the left channel only, while the right will play to the right channel only.

In the case of stereo assets, panning to the left or right will mix the audio feed; this process will never lose audio.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
panthe stereo pan of the sound effect

◆ setEffectPan() [2/2]

void cugl::AudioChannels::setEffectPan ( const std::string &  key,
float  pan 
)

Sets the stereo pan of the sound effect.

This audio engine provides limited (e.g. not full 3D) stereo panning for simple effects. The pan value is a float from -1 to 1. A value of 0 (default) plays to both channels (regardless of whether the current effect is mono or stereo). A value of -1 will play to the left channel only, while the right will play to the right channel only.

In the case of stereo assets, panning to the left or right will mix the audio feed; this process will never lose audio.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
panthe stereo pan of the sound effect

◆ setEffectRemaining() [1/2]

void cugl::AudioChannels::setEffectRemaining ( const char *  key,
float  time 
)
inline

Sets the time remaining for the sound effect, in seconds

The time remaining is just duration-elapsed. This method does not take into account whether the sound is on a loop. Because most sound effects are fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
timethe new time remaining for the sound effect

◆ setEffectRemaining() [2/2]

void cugl::AudioChannels::setEffectRemaining ( const std::string &  key,
float  time 
)

Sets the time remaining for the sound effect, in seconds

The time remaining is just duration-elapsed. This method does not take into account whether the sound is on a loop. Because most sound effects are fully decompressed at load time, the result of this method is reasonably accurate.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
timethe new time remaining for the sound effect

◆ setEffectVolume() [1/2]

void cugl::AudioChannels::setEffectVolume ( const char *  key,
float  volume 
)
inline

Sets the current volume of the sound effect.

The volume is a value 0 to 1, where 1 is maximum volume and 0 is complete silence. If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
volumethe current volume of the sound effect

◆ setEffectVolume() [2/2]

void cugl::AudioChannels::setEffectVolume ( const std::string &  key,
float  volume 
)

Sets the current volume of the sound effect.

The volume is a value 0 to 1, where 1 is maximum volume and 0 is complete silence. If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
volumethe current volume of the sound effect

◆ setMusicElapsed()

void cugl::AudioChannels::setMusicElapsed ( float  time)

Sets the elapsed time of the background music, in seconds

The elapsed time is the current position of the music from the beginning. It does not include any time spent on a continuous loop.

This adjustment is not guaranteed to be accurate. Attempting to time the playback of streaming data (as opposed to a fully in-memory PCM buffer) is very difficult and not cross-platform. We have tried to be reasonably accurate, but from our tests we can only guarantee accuracy within a 10th of a second.

If there is no active background music, this method will have no effect.

Parameters
timethe new position of the background music

◆ setMusicListener()

void cugl::AudioChannels::setMusicListener ( std::function< void(Sound *, bool)>  callback)
inline

Sets the callback for background music

This callback function is called whenever a background music track completes. It is called whether or not the music completed normally or if it was terminated manually. However, the second parameter can be used to distinguish the two cases.

Parameters
callbackThe callback for background music

◆ setMusicLoop()

void cugl::AudioChannels::setMusicLoop ( bool  loop)

Sets whether the background music is on a continuous loop.

If loop is true, this will block the queue until it is set to false again. This can be desired behavior, as it gives you a way to control the speed of the queue processing.

If there is no active background music, this method will do nothing.

Parameters
loopwhether the background music should be on a continuous loop

◆ setMusicPan()

void cugl::AudioChannels::setMusicPan ( float  pan)

Sets the stereo pan of the background music

This audio engine provides limited (e.g. not full 3D) stereo panning for simple effects. The pan value is a float from -1 to 1. A value of 0 (default) plays to both channels (regardless of whether the current music is mono or stereo). A value of -1 will play to the left channel only, while the right will play to the right channel only.

In the case of stereo assets, panning to the left or right will mix the audio feed; this process will never lose audio.

Parameters
panThe stereo pan of the background music

◆ setMusicRemaining()

void cugl::AudioChannels::setMusicRemaining ( float  time)

Sets the time remaining for the background music, in seconds

The time remaining is just duration-elapsed. It does not take into account whether the music is on a loop. It also does not include the duration of any music waiting in the queue.

This adjustment is not guaranteed to be accurate. Attempting to time the playback of streaming data (as opposed to a fully in-memory PCM buffer) is very difficult and not cross-platform. We have tried to be reasonably accurate, but from our tests we can only guarantee accuracy within a 10th of a second.

If there is no active background music, this method will have no effect.

Parameters
timethe new time remaining of the background music

◆ setMusicVolume()

void cugl::AudioChannels::setMusicVolume ( float  volume)

Sets the volume of the background music

The volume is a value 0 to 1, where 1 is maximum volume and 0 is complete silence. If there is no active background music, this method will have no effect.

Parameters
volumethe volume of the background music

◆ setOverlap()

void cugl::AudioChannels::setOverlap ( double  time)

Sets the overlap time in seconds.

The overlap time is the amount of time to cross-fade between a music asset and the next. It does not apply to looped music; music assets can never cross-fade with themselves.

By default, this value is 0. Assets play sequentially but do not overlap. However, you may get smoother transitions between musical segments if you adjust this value. The overlap should be chosen with care. If the play length of an asset is less than the overlap, the results are undefined.

Parameters
timeThe overlap time in seconds.

◆ start() [1/2]

static void cugl::AudioChannels::start ( Uint32  channels,
Uint32  buffer 
)
static

Starts the singleton audio engine.

Once this method is called, the method get() will no longer return nullptr. Calling the method multiple times (without calling stop) will have no effect.

The parameter slots indicates the number of simultaneously supported sounds. Attempting to play more than this number of sounds may fail, it may eject a previously playing sound, depending on the settings.

This buffer size is the number of samples collected at each poll. Smaller buffers clearly tax the CPU, as the device is collecting data at a higher rate. Furthermore, if the value is too small, the time to collect the data may be larger than the time to play it. This will result in pops and crackles in the audio.

However, larger values increase the audio lag. For example, a buffer of 1024 for a sample rate of 48000 Hz corresponds to 21 milliseconds. This is the delay between when sound is gathered and it is played. But this gathering process is also buffered, so this means that any sound effect generated at the same time that the audio device executes must wait 42 milliseconds before it can play. A value of 512 is the perfered value for 60 fps framerate. With that said, many devices cannot handle this rate and need a buffer size of 1024 instead.

Parameters
channelsThe maximum number of sound effect channels to support
bufferThe number of samples collected at each audio poll

◆ start() [2/2]

static void cugl::AudioChannels::start ( Uint32  slots)
static

Starts the singleton audio engine.

Once this method is called, the method get() will no longer return nullptr. Calling the method multiple times (without calling stop) will have no effect.

The parameter slots indicates the number of simultaneously supported sounds. Attempting to play more than this number of sounds may fail, it may eject a previously playing sound, depending on the settings.

This method creates an audio buffer size of 512. This is the number of samples collected at each poll. Smaller buffers clearly tax the CPU, as the device is collecting data at a higher rate. Furthermore, if the value is too small, the time to collect the data may be larger than the time to play it. This will result in pops and crackles in the audio.

However, larger values increase the audio lag. For example, a buffer of 1024 for a sample rate of 48000 Hz corresponds to 21 milliseconds. This is the delay between when sound is gathered and it is played. But this gathering process is also buffered, so this means that any sound effect generated at the same time that the audio device executes must wait 42 milliseconds before it can play. A value of 512 is the perferred value for 60 fps framerate. With that said, many devices cannot handle this rate and need a buffer size of 1024 instead.

Parameters
slotsThe maximum number of sound effect channels to support

◆ stop()

static void cugl::AudioChannels::stop ( )
static

Stops the singleton audio engine, releasing all resources.

Once this method is called, the method get() will return nullptr. Calling the method multiple times (without calling stop) will have no effect.

◆ stopAll()

void cugl::AudioChannels::stopAll ( float  fade = DEFAULT_FADE)

Stops all sounds, both music and sound effects.

Before the sounds are stopped, this method gives the user an option to fade out everything. If the argument is 0, it will halt the sounds immediately. Otherwise it will fade everythign to completion over the given number of seconds (or until the end of each sound). Only by fading can you guarantee no audible clicks.

This method effectively clears the sound engine.

Parameters
fadethe number of seconds to fade out

◆ stopAllEffects()

void cugl::AudioChannels::stopAllEffects ( float  fade = DEFAULT_FADE)

Stops all sound effects, removing them from the engine.

Before the effects are stopped, this method gives the user an option to fade out the effect. If the argument is 0, it will halt all effects immediately. Otherwise it will fade the to completion over the given number of seconds (or until the end of the effect). Only by fading can you guarantee no audible clicks.

You will need to add the effects again if you wish to replay them.

Parameters
fadethe number of seconds to fade out

◆ stopEffect() [1/2]

void cugl::AudioChannels::stopEffect ( const char *  key,
float  fade = DEFAULT_FADE 
)
inline

Stops the sound effect for the given key, removing it.

The effect will be removed from the audio engine entirely. You will need to add it again if you wish to replay it.

Before the effect is stopped, this method gives the user an option to fade out the effect. If the argument is 0, it will halt the sound immediately. Otherwise it will fade to completion over the given number of seconds (or until the end of the effect). Only by fading can you guarantee no audible clicks.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
fadethe number of seconds to fade out

◆ stopEffect() [2/2]

void cugl::AudioChannels::stopEffect ( const std::string &  key,
float  fade = DEFAULT_FADE 
)

Stops the sound effect for the given key, removing it.

The effect will be removed from the audio engine entirely. You will need to add it again if you wish to replay it.

Before the effect is stopped, this method gives the user an option to fade out the effect. If the argument is 0, it will halt the sound immediately. Otherwise it will fade to completion over the given number of seconds (or until the end of the effect). Only by fading can you guarantee no audible clicks.

If the key does not correspond to a channel, this method does nothing.

Parameters
keythe reference key for the sound effect
fadethe number of seconds to fade out

◆ stopMusic()

void cugl::AudioChannels::stopMusic ( float  fade = DEFAULT_FADE)

Stops the background music and clears the entire queue.

Before the music is stopped, this method gives the user an option to fade out the music. If the argument is 0, it will halt the music immediately. Otherwise it will fade to completion over the given number of seconds (or until the end of the song). Only by fading can you guarantee no audible clicks.

This method also clears the queue of any further music.

Parameters
fadeThe number of seconds to fade out

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