cangine
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
cangine::AudioManager Class Reference

Singleton class that manages audio playback. More...

#include <audiomanager.hpp>

Collaboration diagram for cangine::AudioManager:
Collaboration graph
[legend]

Public Member Functions

 AudioManager ()
 
 ~AudioManager ()
 
void add (const Sound &sound)
 Adds a Sound object to the manager.
 
void add (const std::string &path)
 Adds a sound from a file path, using the path as its name.
 
void add (const std::string &path, const std::string &name)
 Adds a sound from a file path and assigns a name to it.
 
void play (const std::string &name, bool loop=false)
 Plays a sound by name.
 
void play (const Sound &sound, bool loop=false)
 Plays a Sound object directly.
 
void stop (const std::string &name)
 Stops a sound by name.
 
void stopAll ()
 Stops all currently playing sounds.
 
void pause (const std::string &name)
 Pauses a sound by name.
 
void pauseAll ()
 Pauses all currently playing sounds.
 
void resume (const std::string &name)
 Resumes a paused sound by name.
 
void resumeAll ()
 Resumes all paused sounds.
 
void setVolume (const std::string &name, float volume)
 Sets the volume of a sound by name.
 
void setVolumeAll (float volume)
 Sets the volume of all sounds.
 
bool isPlaying (const std::string &name)
 Checks whether a sound is currently playing.
 
bool isPaused (const std::string &name)
 Checks whether a sound is currently paused.
 
float getDuration (const std::string &name)
 Returns the total duration of a sound.
 
float getPosition (const std::string &name)
 Returns the current playback position of a sound.
 
void seek (const std::string &name, float seconds)
 Seeks to a specific position in a sound.
 

Static Public Member Functions

static AudioManager * getInstance ()
 Returns the singleton instance of AudioManager.
 

Private Member Functions

void cleanupFinishedSounds ()
 Removes sounds that have finished playing from memory.
 

Private Attributes

std::vector< Sound > sounds
 Loaded sound objects.
 
std::vector< std::pair< ma_sound *, std::string > > activeSounds
 Currently active sounds (handle, name)
 
ma_engine engine {}
 miniaudio engine instance
 
bool initialized = false
 Whether the engine has been initialized.
 

Static Private Attributes

static AudioManager * instance = nullptr
 Singleton instance.
 

Detailed Description

Singleton class that manages audio playback.

Provides loading, playing, stopping, pausing and volume control of audio sources via the miniaudio engine.

Constructor & Destructor Documentation

◆ AudioManager()

cangine::AudioManager::AudioManager ( )

◆ ~AudioManager()

cangine::AudioManager::~AudioManager ( )

Member Function Documentation

◆ add() [1/3]

void cangine::AudioManager::add ( const Sound &  sound)

Adds a Sound object to the manager.

Parameters
soundSound object to add

◆ add() [2/3]

void cangine::AudioManager::add ( const std::string &  path)

Adds a sound from a file path, using the path as its name.

Parameters
pathPath to the audio file

◆ add() [3/3]

void cangine::AudioManager::add ( const std::string &  path,
const std::string &  name 
)

Adds a sound from a file path and assigns a name to it.

Parameters
pathPath to the audio file
nameName to assign to the sound

◆ cleanupFinishedSounds()

void cangine::AudioManager::cleanupFinishedSounds ( )
private

Removes sounds that have finished playing from memory.

◆ getDuration()

float cangine::AudioManager::getDuration ( const std::string &  name)

Returns the total duration of a sound.

Parameters
nameName of the sound
Returns
Duration in seconds

◆ getInstance()

AudioManager * cangine::AudioManager::getInstance ( )
static

Returns the singleton instance of AudioManager.

Returns
Pointer to the AudioManager instance

◆ getPosition()

float cangine::AudioManager::getPosition ( const std::string &  name)

Returns the current playback position of a sound.

Parameters
nameName of the sound
Returns
Position in seconds

◆ isPaused()

bool cangine::AudioManager::isPaused ( const std::string &  name)

Checks whether a sound is currently paused.

Parameters
nameName of the sound
Returns
true if paused, false otherwise

◆ isPlaying()

bool cangine::AudioManager::isPlaying ( const std::string &  name)

Checks whether a sound is currently playing.

Parameters
nameName of the sound
Returns
true if playing, false otherwise

◆ pause()

void cangine::AudioManager::pause ( const std::string &  name)

Pauses a sound by name.

Parameters
nameName of the sound to pause

◆ pauseAll()

void cangine::AudioManager::pauseAll ( )

Pauses all currently playing sounds.

◆ play() [1/2]

void cangine::AudioManager::play ( const Sound &  sound,
bool  loop = false 
)

Plays a Sound object directly.

Parameters
soundSound object to play
loopWhether to loop the sound

◆ play() [2/2]

void cangine::AudioManager::play ( const std::string &  name,
bool  loop = false 
)

Plays a sound by name.

Parameters
nameName of the sound to play
loopWhether to loop the sound

◆ resume()

void cangine::AudioManager::resume ( const std::string &  name)

Resumes a paused sound by name.

Parameters
nameName of the sound to resume

◆ resumeAll()

void cangine::AudioManager::resumeAll ( )

Resumes all paused sounds.

◆ seek()

void cangine::AudioManager::seek ( const std::string &  name,
float  seconds 
)

Seeks to a specific position in a sound.

Parameters
nameName of the sound
secondsTarget position in seconds

◆ setVolume()

void cangine::AudioManager::setVolume ( const std::string &  name,
float  volume 
)

Sets the volume of a sound by name.

Parameters
nameName of the sound
volumeVolume level (0.0 = silent, 1.0 = full)

◆ setVolumeAll()

void cangine::AudioManager::setVolumeAll ( float  volume)

Sets the volume of all sounds.

Parameters
volumeVolume level (0.0 = silent, 1.0 = full)

◆ stop()

void cangine::AudioManager::stop ( const std::string &  name)

Stops a sound by name.

Parameters
nameName of the sound to stop

◆ stopAll()

void cangine::AudioManager::stopAll ( )

Stops all currently playing sounds.

Member Data Documentation

◆ activeSounds

std::vector<std::pair<ma_sound*, std::string> > cangine::AudioManager::activeSounds
private

Currently active sounds (handle, name)

◆ engine

ma_engine cangine::AudioManager::engine {}
private

miniaudio engine instance

◆ initialized

bool cangine::AudioManager::initialized = false
private

Whether the engine has been initialized.

◆ instance

AudioManager * cangine::AudioManager::instance = nullptr
staticprivate

Singleton instance.

◆ sounds

std::vector<Sound> cangine::AudioManager::sounds
private

Loaded sound objects.


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