The Audio module provides a simple system to manage and play sounds within an application. It allows you to organize sounds into channels, control playback, volume, pitch, looping, and other properties, all in a structured and easy-to-use manner.
The module is built around two main classes: Audio and AudioChannel.
````Audiois a singleton that manages multipleAudioChannel``` instances. Each ```AudioChannel``` holds a collection of sounds, represented by ```sf::Sound``` objects from the SFML library, stored in a dictionary keyed by their name. The module handles loading, playing, pausing, stopping, and adjusting sound properties such as attenuation, pitch, volume, and whether the sound is relative to the listener.
-
float volume: Current volume of the channel (0–100), independent of the master volume. -
float masterVolume: Master volume factor applied on top of the channel volume (0–100), set internally by the parentAudioinstance.
bool Add(String name, String file)Adds a new sound to the channel.
Parameters:
-
name: Unique identifier for the sound. -
file: Path to the sound file to load.
Returns: True if the sound was added successfully, false if a sound with that name already exists or the file could not be loaded.
bool Delete(String name)Removes a sound from the channel.
Parameters:
name: Name of the sound to remove.
Returns: True if the sound was removed, false if it does not exist.
bool Play(String name)Plays a sound.
Parameters:
name: Name of the sound to play.
Returns: True if the sound was found and played, false otherwise.
bool Pause(String name)Pauses a sound.
Parameters:
name: Name of the sound to pause.
Returns: True if the sound was found and paused, false otherwise.
bool Stop(String name)Stops a sound completely, resetting the sound's position to the start.
Parameters:
name: Name of the sound to stop.
Returns: True if the sound was found and stopped, false otherwise.
bool SetAttenuation(String name, float attenuation)Sets how sound diminishes over distance.
Parameters:
-
name: Name of the sound. -
attenuation: Attenuation factor (SFML standard).
Returns: True if the sound was found and updated, false otherwise.
bool SetLoop(String name, bool flag = true)Enables or disables looping of a sound.
Parameters:
-
name: Name of the sound. -
flag: True to loop, false to play once.
Returns: True if the sound was found and updated, false otherwise.
bool SetPitch(String name, float pitch)Adjusts the pitch of the sound.
Parameters:
-
name: Name of the sound. -
pitch: Pitch multiplier (1.0 is normal).
Returns: True if the sound was found and updated, false otherwise.
bool SetRelativeToListener(String name, bool flag = true)Determines if the sound's position is relative to the listener.
Parameters:
-
name: Name of the sound. -
flag: True if relative to listener, false otherwise.
Returns: True if the sound was found and updated, false otherwise.
SoundPtr GetSound(String name)Retrieves the sound object.
Parameters:
name: Name of the sound.
Returns: Shared pointer to the sf::Sound object, or nullptr if not found.
bool IsPlaying(String name)
bool IsPaused(String name)
bool IsStopped(String name)Checks the status of a sound.
Parameters:
name: Name of the sound.
Returns: True if the sound is in the corresponding state, false otherwise (including if the sound does not exist).
float GetAttenuation(String name)
float GetPitch(String name)
bool IsLooping(String name)
bool IsRelativeToListener(String name)Retrieves sound properties.
Parameters:
name: Name of the sound.
Returns: Corresponding value, or default (0.0f / false) if the sound does not exist.
float GetVolume()
bool SetVolume(float volume)Gets or sets the volume for all sounds in the channel.
Parameters (SetVolume):
volume: Desired volume level (0–100).
Returns (SetVolume): True if the volume was set successfully, false if out of range.
bool IsPlaying()Checks if any sound in the channel is currently playing.
Returns: True if at least one sound is playing.
void Clear()Removes all sounds from the channel.
float masterVolume: Global volume factor (0–100) applied to every channel managed by the pipeline.
void AddChannel(String name)Adds a new audio channel to the system.
Parameters:
name: Unique channel identifier.
void DeleteChannel(String name)Removes a channel.
Parameters:
name: Name of the channel to remove.
AudioChannelPtr GetChannel(String name)Retrieves a channel object.
Parameters:
name: Name of the channel.
Returns: Shared pointer to the AudioChannel, or nullptr if not found.
float GetMasterVolume()Gets the global master volume.
Returns: Current master volume (0–100).
bool SetMasterVolume(float masterVolume)Sets the global master volume, applying it to every existing channel.
Parameters:
masterVolume: Desired master volume level (0–100).
Returns: True if the volume was set successfully, false if out of range.
void StopAll()Stops every sound across every channel managed by the pipeline.