Public Member Functions | Protected Member Functions | List of all members
ime::audio::SoundEffect Class Referencefinal

Play sound effects (Short audio clips) More...

#include <SoundEffect.h>

Inheritance diagram for ime::audio::SoundEffect:
ime::audio::Audio ime::EventEmitter

Public Member Functions

 SoundEffect ()
 Default constructor. More...
 
 SoundEffect (const SoundEffect &)
 Copy constructor. More...
 
SoundEffectoperator= (const SoundEffect &)
 Copy assignment operator. More...
 
 SoundEffect (SoundEffect &&) noexcept
 Move constructor. More...
 
SoundEffectoperator= (SoundEffect &&) noexcept
 Move assignment operator. More...
 
void setSource (const std::string &source) override
 Set the sound effect file to be played. More...
 
const std::string & getSource () const override
 Get sound effect source. More...
 
void setVolume (float volume) override
 Set the volume of the sound effect. More...
 
float getVolume () const override
 Get the volume of the sound effect. More...
 
void setPitch (float pitch) override
 Set the pitch of the sound effect. More...
 
float getPitch () const override
 Get the pitch of the sound effect. More...
 
void setLoop (bool isLooped) override
 Set whether or not the sound effect should loop after reaching the end. More...
 
bool isLooped () const override
 Check if sound effect is looped or not. More...
 
void seek (Time position) override
 Change the current playing position of the sound effect. More...
 
Time getPlayingPosition () const override
 Get the current playing position of the sound effect. More...
 
void play () override
 Play sound effect. More...
 
void pause () override
 Pause sound effect. More...
 
void stop () override
 Stop playing the sound effect. More...
 
Time getDuration () const override
 Get the total duration of the sound effect. More...
 
Status getStatus () const override
 Get the current status of the sound effect. More...
 
std::string getType () override
 Get the type of the audio. More...
 
 ~SoundEffect () override
 Destructor. More...
 
void adjustVolume (float offset)
 Turn the volume up/down by a given offset. More...
 
void setMute (bool mute)
 Set whether or not the audio should be audible. More...
 
bool isMuted () const
 Check if audio is muted or not. More...
 
void restart ()
 Reset the playing position to the beginning. More...
 
template<typename... Args>
void emit (const std::string &event, Args...args)
 Fire an event. More...
 
template<typename... Args>
int addEventListener (const std::string &event, Callback< Args... > callback)
 Add an event listener (callback) to an event. More...
 
template<typename... Args>
int on (const std::string &event, Callback< Args... > callback)
 Add an event listener to an event. More...
 
template<typename ... Args>
int addOnceEventListener (const std::string &event, Callback< Args... > callback)
 Add an event listener to an event. More...
 
bool removeEventListener (const std::string &event, int id)
 Remove an event listener from an event. More...
 
bool removeAllEventListeners (const std::string &event)
 Remove all event listeners of an event. More...
 
bool hasEvent (const std::string &event) const
 Check if an event exists or not. More...
 
std::size_t getEventListenerCount (const std::string &event) const
 Get the number of event listeners currently registered to an event. More...
 
std::size_t getEventsCount () const
 Get the current number of created events. More...
 
bool hasEventListener (const std::string &event, int id) const
 Check if an event has a certain event listener. More...
 

Protected Member Functions

template<typename... Args>
void emit (const std::string &event, Args...args)
 Prevent external publishing of events. More...
 

Detailed Description

Play sound effects (Short audio clips)

This class loads the audio file to be played in memory. Therefore, it is advised to use it with short audio files that can be quickly loaded form the disk and into the program. Example, gun shots and explosions. If long audio files are used with this class you might experience audio latency and other issues. It is advised to use Audio::Music for long sounds such as background music.

Definition at line 45 of file SoundEffect.h.

Constructor & Destructor Documentation

◆ SoundEffect() [1/3]

ime::audio::SoundEffect::SoundEffect ( )

Default constructor.

◆ SoundEffect() [2/3]

ime::audio::SoundEffect::SoundEffect ( const SoundEffect )

Copy constructor.

◆ SoundEffect() [3/3]

ime::audio::SoundEffect::SoundEffect ( SoundEffect &&  )
noexcept

Move constructor.

◆ ~SoundEffect()

ime::audio::SoundEffect::~SoundEffect ( )
override

Destructor.

Member Function Documentation

◆ addEventListener()

template<typename... Args>
int ime::EventEmitter::addEventListener ( const std::string &  event,
Callback< Args... >  callback 
)
inherited

Add an event listener (callback) to an event.

Parameters
eventEvent to add event listener to
callbackFunction to execute when the event is fired
Returns
The event listener's identification number

Every event listener has a unique identification number. This number must be remembered in order to remove the event listener.

Note
If the same callback function is added multiple times, It will be treated as a unique event listener and hence given an identification number

◆ addOnceEventListener()

template<typename ... Args>
int ime::EventEmitter::addOnceEventListener ( const std::string &  event,
Callback< Args... >  callback 
)
inherited

Add an event listener to an event.

Parameters
eventEvent to add event listener to
callbackFunction to execute when the event is fired
Returns
The event listener's identification number

The event listener will be invoked once and subsequently removed from the event. This means that the callback will only execute when an event is fired for the first time. To execute a callback each time the an event is fired

See also
addEventListener

◆ adjustVolume()

void ime::audio::Audio::adjustVolume ( float  offset)
inherited

Turn the volume up/down by a given offset.

Parameters
offsetValue to increase/decrease the volume by

This function adds on to the current volume. A positive offset increases the volume while a negative offset decreases the volume. This function will unmmute the audio if its muted

See also
setMute

◆ emit() [1/2]

template<typename... Args>
void ime::EventEmitter::emit ( typename...  Args)
protectedinherited

Prevent external publishing of events.

Only the class knows the conditions under which an event may be fired. Therefore, events must not be raised externally as this may result in events being raised at the wong time, resulting in undesired and incorrect behavior

◆ emit() [2/2]

template<typename... Args>
void ime::EventEmitter::emit ( const std::string &  event,
Args...  args 
)
inherited

Fire an event.

Parameters
eventName of the event to fire
argsArguments to be passed to event listeners

◆ getDuration()

Time ime::audio::SoundEffect::getDuration ( ) const
overridevirtual

Get the total duration of the sound effect.

Returns
The total duration of the sound effect

Implements ime::audio::Audio.

◆ getEventListenerCount()

std::size_t ime::EventEmitter::getEventListenerCount ( const std::string &  event) const
inherited

Get the number of event listeners currently registered to an event.

Parameters
eventEvent to get number of event listeners for
Returns
The number of event listeners registered to an event or 0 if no such event exists

◆ getEventsCount()

std::size_t ime::EventEmitter::getEventsCount ( ) const
inherited

Get the current number of created events.

Returns
Current umber of events

◆ getPitch()

float ime::audio::SoundEffect::getPitch ( ) const
overridevirtual

Get the pitch of the sound effect.

Returns
The pitch of the sound effect

Implements ime::audio::Audio.

◆ getPlayingPosition()

Time ime::audio::SoundEffect::getPlayingPosition ( ) const
overridevirtual

Get the current playing position of the sound effect.

Returns
Current playing position of the sound effect

Implements ime::audio::Audio.

◆ getSource()

const std::string& ime::audio::SoundEffect::getSource ( ) const
overridevirtual

Get sound effect source.

Returns
Filename of the actual sound effect file

Implements ime::audio::Audio.

◆ getStatus()

Status ime::audio::SoundEffect::getStatus ( ) const
overridevirtual

Get the current status of the sound effect.

Returns
Current status of the sound effect

Implements ime::audio::Audio.

◆ getType()

std::string ime::audio::SoundEffect::getType ( )
overridevirtual

Get the type of the audio.

Returns
The type of the audio

Implements ime::audio::Audio.

◆ getVolume()

float ime::audio::SoundEffect::getVolume ( ) const
overridevirtual

Get the volume of the sound effect.

Returns
Volume of the sound effect, in the range [0, 100]

Implements ime::audio::Audio.

◆ hasEvent()

bool ime::EventEmitter::hasEvent ( const std::string &  event) const
inherited

Check if an event exists or not.

Parameters
eventName of the event to check
Returns
True if event exists or false if the event does not exist

◆ hasEventListener()

bool ime::EventEmitter::hasEventListener ( const std::string &  event,
int  id 
) const
inherited

Check if an event has a certain event listener.

Parameters
eventName of the event
idIdentification number of the listener to be checked
Returns
True if the specified event has an event listener with the specified id, otherwise false

◆ isLooped()

bool ime::audio::SoundEffect::isLooped ( ) const
overridevirtual

Check if sound effect is looped or not.

Returns
True if sound effect is looped, otherwise false

Implements ime::audio::Audio.

◆ isMuted()

bool ime::audio::Audio::isMuted ( ) const
inherited

Check if audio is muted or not.

Returns
True if audio is muted or false if audio is not muted

◆ on()

template<typename... Args>
int ime::EventEmitter::on ( const std::string &  event,
Callback< Args... >  callback 
)
inherited

Add an event listener to an event.

Parameters
eventEvent to add event listener to
callbackFunction to execute when the event is fired
Returns
The event listener's identification number

This function does the same thing as the addEventListener() function. It just provides a slightly more readable syntax:

returnButton.on("click", showMainMenu); as opposed to
returnButton.addEventListener("click", showMainMenu);

◆ operator=() [1/2]

SoundEffect& ime::audio::SoundEffect::operator= ( const SoundEffect )

Copy assignment operator.

◆ operator=() [2/2]

SoundEffect& ime::audio::SoundEffect::operator= ( SoundEffect &&  )
noexcept

Move assignment operator.

◆ pause()

void ime::audio::SoundEffect::pause ( )
overridevirtual

Pause sound effect.

This function pauses the stream if it was playing, otherwise (sound effect already paused or stopped) it has no effect

Implements ime::audio::Audio.

◆ play()

void ime::audio::SoundEffect::play ( )
overridevirtual

Play sound effect.

This function starts the sound effect if it was stopped, resumes it if it was paused, and restarts it from the beginning if it was already playing. This function uses its own thread so that it doesn't block the rest of the program while the sound effect is played

Implements ime::audio::Audio.

◆ removeAllEventListeners()

bool ime::EventEmitter::removeAllEventListeners ( const std::string &  event)
inherited

Remove all event listeners of an event.

Parameters
eventEvent to remove all listeners from
Returns
True if all listeners were removed, false if no such event exists

◆ removeEventListener()

bool ime::EventEmitter::removeEventListener ( const std::string &  event,
int  id 
)
inherited

Remove an event listener from an event.

Parameters
eventEvent to remove listener from
idIdentification number of the event listener to be removed
Returns
True if the event listener was removed from athe specified event or false if the specified event does exist or it does not have a listener with the given id

◆ restart()

void ime::audio::Audio::restart ( )
inherited

Reset the playing position to the beginning.

This function will reset the playing position without stopping the audio (unlike stop function), which would require play to be called to get the audio to play again

◆ seek()

void ime::audio::SoundEffect::seek ( Time  position)
overridevirtual

Change the current playing position of the sound effect.

Parameters
positionNew playing position of the sound effect

The playing position can be changed when the sound effect is either paused or playing. Changing the playing position when the sound effect is stopped has no effect, since playing the sound effect would reset its position

Implements ime::audio::Audio.

◆ setLoop()

void ime::audio::SoundEffect::setLoop ( bool  isLooped)
overridevirtual

Set whether or not the sound effect should loop after reaching the end.

Parameters
isLoopedTrue to play in loop, false to play once

Implements ime::audio::Audio.

◆ setMute()

void ime::audio::Audio::setMute ( bool  mute)
inherited

Set whether or not the audio should be audible.

Parameters
muteTrue to make audio inaudible, or false to make audio audible

When set to true the audio will not be audible but will continue to play if currently playing

◆ setPitch()

void ime::audio::SoundEffect::setPitch ( float  pitch)
overridevirtual

Set the pitch of the sound effect.

Parameters
pitchThe new pitch of the sound effect

The pitch represents the perceived fundamental frequency of a sound; thus you can make a sound more acute or grave by changing its pitch. Changing the pitch also changes the playing speed of the sound effect.

The default value for the pitch is 1

Implements ime::audio::Audio.

◆ setSource()

void ime::audio::SoundEffect::setSource ( const std::string &  source)
overridevirtual

Set the sound effect file to be played.

Parameters
sourceFilename of an audio file

The source must refer to an actual audio file on the disk. This audio file is the one to be played, pitched and done all sorts of things. This function doesn't start the sound effect, it sets the sound effect that will be played when play is called

See also
play

Implements ime::audio::Audio.

◆ setVolume()

void ime::audio::SoundEffect::setVolume ( float  volume)
overridevirtual

Set the volume of the sound effect.

Parameters
volumeVolume to set

The volume is a value between 0 (mute) and 100 (full volume).

The default value for the volume is 100

Implements ime::audio::Audio.

◆ stop()

void ime::audio::SoundEffect::stop ( )
overridevirtual

Stop playing the sound effect.

This function stops the sound effect if it was playing or paused, and does nothing if it was already stopped. It also resets the playing position

Implements ime::audio::Audio.


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