Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
rtp::client::AudioSystem Class Reference

System responsible for handling audio playback (music, SFX, ambient) More...

#include <AudioSystem.hpp>

Inheritance diagram for rtp::client::AudioSystem:
Collaboration diagram for rtp::client::AudioSystem:

Public Member Functions

 AudioSystem (ecs::Registry &registry)
 
 ~AudioSystem () override
 
void update (float dt) override
 Update audio system (called every frame)
 
void setMasterVolume (float volume)
 Set master volume for all audio.
 
float getMasterVolume () const
 Get current master volume.
 
void setMusicVolume (float volume)
 Set music volume (affects AudioSource components)
 
void setSfxVolume (float volume)
 Set SFX volume (affects SoundEvent components)
 
void stopAllSounds ()
 Stop all currently playing sounds.
 
- Public Member Functions inherited from rtp::ecs::ISystem
virtual ~ISystem () noexcept=default
 

Private Member Functions

sf::SoundBuffer * loadSoundBuffer (const std::string &path)
 Load or get cached sound buffer.
 
void updateAudioSources (float dt)
 Update all AudioSource components.
 
void updateSoundEvents (float dt)
 Update all SoundEvent components.
 
void playAudioSource (ecs::components::audio::AudioSource &audioSource)
 Play an AudioSource.
 
void playSoundEffect (ecs::components::audio::SoundEvent &soundEvent)
 Play a sound effect (one-time)
 
void cleanupFinishedSounds ()
 Remove finished non-looping sounds from tracking.
 

Private Attributes

ecs::Registry_registry
 
float _masterVolume
 
float _musicVolume
 
float _sfxVolume
 
std::unordered_map< uint32_t, std::unique_ptr< sf::Sound > > _activeSounds
 
std::unordered_map< std::string, std::unique_ptr< sf::SoundBuffer > > _soundBuffers
 
std::unordered_map< uint32_t, bool > _loopingSounds
 Track which sounds should loop.
 
std::unordered_map< uint32_t, float > _soundBaseVolumes
 Track base volume (0.0-1.0) for each sound.
 
std::unordered_map< uint32_t, bool > _soundIsMusic
 Track if sound is music (true) or SFX (false)
 
uint32_t _nextSoundId
 

Detailed Description

System responsible for handling audio playback (music, SFX, ambient)

Manages two types of audio:

  • AudioSource: Persistent audio (music, loops, ambient sounds)
  • SoundEvent: One-time sound effects (gunshots, explosions, impacts)

Definition at line 30 of file AudioSystem.hpp.

Constructor & Destructor Documentation

◆ AudioSystem()

rtp::client::AudioSystem::AudioSystem ( ecs::Registry registry)
explicit

Definition at line 13 of file AudioSystem.cpp.

◆ ~AudioSystem()

rtp::client::AudioSystem::~AudioSystem ( )
override

Definition at line 16 of file AudioSystem.cpp.

References stopAllSounds().

Member Function Documentation

◆ cleanupFinishedSounds()

void rtp::client::AudioSystem::cleanupFinishedSounds ( )
private

Remove finished non-looping sounds from tracking.

Definition at line 162 of file AudioSystem.cpp.

References _activeSounds, _loopingSounds, _soundBaseVolumes, and _soundIsMusic.

Referenced by updateAudioSources().

◆ getMasterVolume()

float rtp::client::AudioSystem::getMasterVolume ( ) const

Get current master volume.

Returns
Current master volume

Definition at line 37 of file AudioSystem.cpp.

References _masterVolume.

◆ loadSoundBuffer()

sf::SoundBuffer * rtp::client::AudioSystem::loadSoundBuffer ( const std::string &  path)
private

Load or get cached sound buffer.

Parameters
pathPath to the audio file
Returns
Pointer to the sound buffer, or nullptr if loading failed

Definition at line 75 of file AudioSystem.cpp.

References _soundBuffers, and rtp::log::error().

Referenced by playAudioSource(), and playSoundEffect().

◆ playAudioSource()

◆ playSoundEffect()

void rtp::client::AudioSystem::playSoundEffect ( ecs::components::audio::SoundEvent soundEvent)
private

◆ setMasterVolume()

void rtp::client::AudioSystem::setMasterVolume ( float  volume)

Set master volume for all audio.

Parameters
volumeVolume level (0.0 - 100.0)

Definition at line 25 of file AudioSystem.cpp.

References _activeSounds, _masterVolume, _musicVolume, _sfxVolume, _soundBaseVolumes, and _soundIsMusic.

◆ setMusicVolume()

void rtp::client::AudioSystem::setMusicVolume ( float  volume)

Set music volume (affects AudioSource components)

Parameters
volumeVolume level (0.0 - 100.0)

Definition at line 41 of file AudioSystem.cpp.

References _activeSounds, _masterVolume, _musicVolume, _soundBaseVolumes, and _soundIsMusic.

◆ setSfxVolume()

void rtp::client::AudioSystem::setSfxVolume ( float  volume)

Set SFX volume (affects SoundEvent components)

Parameters
volumeVolume level (0.0 - 100.0)

Definition at line 53 of file AudioSystem.cpp.

References _activeSounds, _masterVolume, _sfxVolume, _soundBaseVolumes, and _soundIsMusic.

◆ stopAllSounds()

void rtp::client::AudioSystem::stopAllSounds ( )

Stop all currently playing sounds.

Definition at line 65 of file AudioSystem.cpp.

References _activeSounds, _loopingSounds, _soundBaseVolumes, and _soundIsMusic.

Referenced by ~AudioSystem(), and rtp::client::Application::changeState().

◆ update()

void rtp::client::AudioSystem::update ( float  dt)
overridevirtual

Update audio system (called every frame)

Parameters
dtDelta time in seconds

Implements rtp::ecs::ISystem.

Definition at line 20 of file AudioSystem.cpp.

References updateAudioSources(), and updateSoundEvents().

◆ updateAudioSources()

void rtp::client::AudioSystem::updateAudioSources ( float  dt)
private

Update all AudioSource components.

Parameters
dtDelta time in seconds

Definition at line 91 of file AudioSystem.cpp.

References _activeSounds, _registry, cleanupFinishedSounds(), rtp::log::error(), playAudioSource(), and rtp::ecs::Registry::zipView().

Referenced by update().

◆ updateSoundEvents()

void rtp::client::AudioSystem::updateSoundEvents ( float  dt)
private

Update all SoundEvent components.

Parameters
dtDelta time in seconds

Definition at line 116 of file AudioSystem.cpp.

References _registry, rtp::log::error(), rtp::log::info(), playSoundEffect(), and rtp::ecs::Registry::zipView().

Referenced by update().

Member Data Documentation

◆ _activeSounds

std::unordered_map<uint32_t, std::unique_ptr<sf::Sound> > rtp::client::AudioSystem::_activeSounds
private

◆ _loopingSounds

std::unordered_map<uint32_t, bool> rtp::client::AudioSystem::_loopingSounds
private

Track which sounds should loop.

Definition at line 77 of file AudioSystem.hpp.

Referenced by cleanupFinishedSounds(), playAudioSource(), and stopAllSounds().

◆ _masterVolume

float rtp::client::AudioSystem::_masterVolume
private

◆ _musicVolume

float rtp::client::AudioSystem::_musicVolume
private

Definition at line 73 of file AudioSystem.hpp.

Referenced by playAudioSource(), setMasterVolume(), and setMusicVolume().

◆ _nextSoundId

uint32_t rtp::client::AudioSystem::_nextSoundId
private

Definition at line 80 of file AudioSystem.hpp.

Referenced by playAudioSource(), and playSoundEffect().

◆ _registry

ecs::Registry& rtp::client::AudioSystem::_registry
private

Definition at line 71 of file AudioSystem.hpp.

Referenced by updateAudioSources(), and updateSoundEvents().

◆ _sfxVolume

float rtp::client::AudioSystem::_sfxVolume
private

Definition at line 74 of file AudioSystem.hpp.

Referenced by playSoundEffect(), setMasterVolume(), and setSfxVolume().

◆ _soundBaseVolumes

std::unordered_map<uint32_t, float> rtp::client::AudioSystem::_soundBaseVolumes
private

Track base volume (0.0-1.0) for each sound.

Definition at line 78 of file AudioSystem.hpp.

Referenced by cleanupFinishedSounds(), playAudioSource(), playSoundEffect(), setMasterVolume(), setMusicVolume(), setSfxVolume(), and stopAllSounds().

◆ _soundBuffers

std::unordered_map<std::string, std::unique_ptr<sf::SoundBuffer> > rtp::client::AudioSystem::_soundBuffers
private

Definition at line 76 of file AudioSystem.hpp.

Referenced by loadSoundBuffer().

◆ _soundIsMusic

std::unordered_map<uint32_t, bool> rtp::client::AudioSystem::_soundIsMusic
private

Track if sound is music (true) or SFX (false)

Definition at line 79 of file AudioSystem.hpp.

Referenced by cleanupFinishedSounds(), playAudioSource(), playSoundEffect(), setMasterVolume(), setMusicVolume(), setSfxVolume(), and stopAllSounds().


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