Skip to content

Latest commit

 

History

History
293 lines (181 loc) · 6.01 KB

File metadata and controls

293 lines (181 loc) · 6.01 KB

📦 Audio

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.

Behaviour

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.

✳️ AudioChannel

Fields

  • 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 parent Audio instance.

Methods

Add

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.

Delete

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.

Play

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.

Pause

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.

Stop

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.

SetAttenuation

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.

SetLoop

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.

SetPitch

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.

SetRelativeToListener

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.

GetSound

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.

IsPlaying / IsPaused / IsStopped

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).

GetAttenuation / GetPitch / IsLooping / IsRelativeToListener

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.

GetVolume / SetVolume

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.

IsPlaying

bool IsPlaying()

Checks if any sound in the channel is currently playing.

Returns: True if at least one sound is playing.

Clear

void Clear()

Removes all sounds from the channel.

✳️ Audio

Fields

  • float masterVolume: Global volume factor (0–100) applied to every channel managed by the pipeline.

Methods

AddChannel

void AddChannel(String name)

Adds a new audio channel to the system.

Parameters:

  • name: Unique channel identifier.

DeleteChannel

void DeleteChannel(String name)

Removes a channel.

Parameters:

  • name: Name of the channel to remove.

GetChannel

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.

GetMasterVolume

float GetMasterVolume()

Gets the global master volume.

Returns: Current master volume (0–100).

SetMasterVolume

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.

StopAll

void StopAll()

Stops every sound across every channel managed by the pipeline.