Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Sound::Resource::SoundManager Class Reference

#include <SoundManager.hpp>

Classes

struct  Sound
struct  TransparentHash
struct  TransparentEqual

Public Member Functions

 SoundManager ()=default
 ~SoundManager ()
 SoundManager (const SoundManager &)=delete
SoundManageroperator= (const SoundManager &)=delete
 SoundManager (SoundManager &&other) noexcept
SoundManageroperator= (SoundManager &&other) noexcept
void Init (Engine::Core &core)
 Initialize the sound system.
void CheckCallbackErrors ()
 Check and log any deferred errors from the audio callback.
void RegisterSound (const std::string &soundName, const std::string &filePath, bool loop=false)
 Register the provided sound file.
void UnregisterSound (const std::string &soundName)
 Unregister the target sound file from the sound vector.
void Play (const std::string &soundName)
 Play the target sound.
void Stop (const std::string &soundName)
 Stop the target sound.
void Pause (const std::string &soundName)
 Pause the target sound.
bool IsPlaying (const std::string &soundName)
 Check if the target sound is playing.
void SetVolume (const std::string &soundName, float volume)
 Set the volume of a specific sound.
void SetPitch (const std::string &soundName, float pitch)
 Set the playback pitch (speed) for a sound.
void SetLoop (const std::string &soundName, bool shouldLoop)
 Set whether the sound should loop.
void SetLoopPoints (const std::string &soundName, float startSeconds, float endSeconds=0)
 Set the loop start and end points for a sound.
double GetPlayPosition (const std::string &soundName)
 Get the current playback position of a sound in seconds.
FunctionUtils::FunctionID AddCustomCallback (const CustomDataCallback &callback)
bool RemoveCustomCallback (FunctionUtils::FunctionID id)
 Remove a custom audio callback by its ID.
bool HasCustomCallback (FunctionUtils::FunctionID id) const
 Check if a custom callback exists by its ID.
void ClearCustomCallbacks ()
 Clear all custom callbacks.

Static Public Member Functions

static void data_callback (ma_device *pDevice, void *pOutput, const void *, ma_uint32 frameCount)
 Audio data callback used by the playback device to fill the output buffer.

Private Attributes

ma_result _result
ma_device_config _deviceConfig
ma_device _device
bool _deviceInit = false
ma_engine _engine
bool _engineInit = false
std::atomic< uint32_t > _callbackErrorFlags {0}
std::unordered_map< std::string, Sound, TransparentHash, TransparentEqual_soundsToPlay
FunctionUtils::FunctionContainer< void, ma_device *, void *, ma_uint32 > _customCallbacks

Static Private Attributes

static constexpr uint32_t ERROR_FRAME_TOO_LARGE = 0x1
static constexpr uint32_t ERROR_DECODER_READ = 0x2
static constexpr uint32_t ERROR_UNKNOWN_FORMAT = 0x4

Constructor & Destructor Documentation

◆ SoundManager() [1/3]

Sound::Resource::SoundManager::SoundManager ( )
default

Constructor.

◆ ~SoundManager()

Sound::Resource::SoundManager::~SoundManager ( )

Destructor.

◆ SoundManager() [2/3]

Sound::Resource::SoundManager::SoundManager ( const SoundManager & )
delete

Deleted copy constructor (contains non-copyable FunctionContainer).

◆ SoundManager() [3/3]

Sound::Resource::SoundManager::SoundManager ( SoundManager && other)
inlinenoexcept

Move constructor.

Note
std::atomic is not movable, so we manually transfer its value.

Member Function Documentation

◆ AddCustomCallback()

FunctionUtils::FunctionID Sound::Resource::SoundManager::AddCustomCallback ( const CustomDataCallback & callback)
inline

◆ CheckCallbackErrors()

void Sound::Resource::SoundManager::CheckCallbackErrors ( )
inline

Check and log any deferred errors from the audio callback.

Call this method periodically from the main thread (e.g., in an Update system) to report errors that occurred in the real-time audio callback without blocking it.

◆ ClearCustomCallbacks()

void Sound::Resource::SoundManager::ClearCustomCallbacks ( )
inline

Clear all custom callbacks.

◆ data_callback()

void Sound::Resource::SoundManager::data_callback ( ma_device * pDevice,
void * pOutput,
const void * ,
ma_uint32 frameCount )
inlinestatic

Audio data callback used by the playback device to fill the output buffer.

Parameters
pDevicePointer to the audio playback device.
pOutputPointer to the output buffer that will be filled with audio samples.
pInputUnused input buffer (typically nullptr for playback-only).
frameCountNumber of audio frames that must be written to the output buffer.
Note
Handles looping with configurable start/end points, checking loop bounds before reading.

Custom callbacks are invoked first, then all playing sounds are mixed. The mix buffer is cleared initially and all sounds are mixed with their respective volumes. Format conversion supports f32, s32, s16, and u8 with saturated addition to prevent overflow.

Returns
void

◆ GetPlayPosition()

double Sound::Resource::SoundManager::GetPlayPosition ( const std::string & soundName)
inline

Get the current playback position of a sound in seconds.

Return value is related to the absolute begin of the sound, not the start frame.

Parameters
soundNameThe name of the target sound to query.
Returns
double The playback position in seconds, or -1.0 on error.

◆ HasCustomCallback()

bool Sound::Resource::SoundManager::HasCustomCallback ( FunctionUtils::FunctionID id) const
inline

Check if a custom callback exists by its ID.

Parameters
idID of the callback to check.
Returns
bool True if the callback exists, false otherwise.

◆ Init()

void Sound::Resource::SoundManager::Init ( Engine::Core & core)
inline

Initialize the sound system.

Returns
void

◆ IsPlaying()

bool Sound::Resource::SoundManager::IsPlaying ( const std::string & soundName)
inline

Check if the target sound is playing.

Parameters
soundNameName of the target sound
Returns
bool

◆ operator=() [1/2]

SoundManager & Sound::Resource::SoundManager::operator= ( const SoundManager & )
delete

Deleted copy assignment operator (contains non-copyable FunctionContainer).

◆ operator=() [2/2]

SoundManager & Sound::Resource::SoundManager::operator= ( SoundManager && other)
inlinenoexcept

Move assignment operator.

Note
std::atomic is not movable, so we manually transfer its value.

◆ Pause()

void Sound::Resource::SoundManager::Pause ( const std::string & soundName)
inline

Pause the target sound.

Parameters
soundNameName of the target sound to pause
Returns
bool

◆ Play()

void Sound::Resource::SoundManager::Play ( const std::string & soundName)
inline

Play the target sound.

Parameters
soundNameName of the target sound to play
Returns
void

◆ RegisterSound()

void Sound::Resource::SoundManager::RegisterSound ( const std::string & soundName,
const std::string & filePath,
bool loop = false )
inline

Register the provided sound file.

Parameters
soundNameName to assign to the registered sound
filePathPath of the sound file to register
loopSet the sound param to loop, default: False
Returns
void

◆ RemoveCustomCallback()

bool Sound::Resource::SoundManager::RemoveCustomCallback ( FunctionUtils::FunctionID id)
inline

Remove a custom audio callback by its ID.

Parameters
idID of the callback to remove.
Returns
bool True if the callback was removed, false if it didn't exist.

◆ SetLoop()

void Sound::Resource::SoundManager::SetLoop ( const std::string & soundName,
bool shouldLoop )
inline

Set whether the sound should loop.

Parameters
soundNameName of the sound.
shouldLoopWhether the sound should loop.

◆ SetLoopPoints()

void Sound::Resource::SoundManager::SetLoopPoints ( const std::string & soundName,
float startSeconds,
float endSeconds = 0 )
inline

Set the loop start and end points for a sound.

Parameters
soundNameName of the sound.
startSecondsTime in seconds where the loop starts.
endSecondsTime in seconds where the loop ends (0 = end of file).

◆ SetPitch()

void Sound::Resource::SoundManager::SetPitch ( const std::string & soundName,
float pitch )
inline

Set the playback pitch (speed) for a sound.

Parameters
soundNameName of the sound.
pitchPitch factor (> 0), 1.0 = normal.

◆ SetVolume()

void Sound::Resource::SoundManager::SetVolume ( const std::string & soundName,
float volume )
inline

Set the volume of a specific sound.

Parameters
soundNameName of the sound.
volumeVolume level (0.0 = silent, 1.0 = full volume).

◆ Stop()

void Sound::Resource::SoundManager::Stop ( const std::string & soundName)
inline

Stop the target sound.

Parameters
soundNameName of the target sound to stop
Returns
void

◆ UnregisterSound()

void Sound::Resource::SoundManager::UnregisterSound ( const std::string & soundName)
inline

Unregister the target sound file from the sound vector.

Parameters
soundNameName of the target sound to remove
Returns
void

Member Data Documentation

◆ _callbackErrorFlags

std::atomic<uint32_t> Sound::Resource::SoundManager::_callbackErrorFlags {0}
private

◆ _customCallbacks

FunctionUtils::FunctionContainer<void, ma_device *, void *, ma_uint32> Sound::Resource::SoundManager::_customCallbacks
private

◆ _device

ma_device Sound::Resource::SoundManager::_device
private

◆ _deviceConfig

ma_device_config Sound::Resource::SoundManager::_deviceConfig
private

◆ _deviceInit

bool Sound::Resource::SoundManager::_deviceInit = false
private

◆ _engine

ma_engine Sound::Resource::SoundManager::_engine
private

◆ _engineInit

bool Sound::Resource::SoundManager::_engineInit = false
private

◆ _result

ma_result Sound::Resource::SoundManager::_result
private

◆ _soundsToPlay

std::unordered_map<std::string, Sound, TransparentHash, TransparentEqual> Sound::Resource::SoundManager::_soundsToPlay
private

◆ ERROR_DECODER_READ

uint32_t Sound::Resource::SoundManager::ERROR_DECODER_READ = 0x2
staticconstexprprivate

◆ ERROR_FRAME_TOO_LARGE

uint32_t Sound::Resource::SoundManager::ERROR_FRAME_TOO_LARGE = 0x1
staticconstexprprivate

◆ ERROR_UNKNOWN_FORMAT

uint32_t Sound::Resource::SoundManager::ERROR_UNKNOWN_FORMAT = 0x4
staticconstexprprivate

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