Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | 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::Object

Public Types

using Ptr = std::unique_ptr< Object >
 Unique object pointer. More...
 

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...
 
std::string getClassName () const override
 Get the name of this class. 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 (const 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...
 
 ~SoundEffect () override
 Destructor. More...
 
std::string getClassType () const override
 Get the name of this class. 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...
 
void setTag (const std::string &tag)
 Assign the object an alias. More...
 
const std::string & getTag () const
 Get the tag assigned to the object. More...
 
unsigned int getObjectId () const
 Get the unique id of the object. More...
 
int onPropertyChange (const std::string &property, const Callback< Property > &callback, bool oneTime=false)
 Add an event listener to a specific property change event. More...
 
int onPropertyChange (const Callback< Property > &callback, bool oneTime=false)
 Add an event listener to any property change event. More...
 
void suspendedEventListener (int id, bool suspend)
 Pause or resume execution of an event listener. More...
 
bool isEventListenerSuspended (int id) const
 Check if an event listener is suspended or not. More...
 
bool removeEventListener (const std::string &event, int id)
 Remove an event listener from an event. More...
 
bool removeEventListener (int id)
 Remove an event listener. More...
 
int onDestruction (const Callback<> &callback)
 Add a destruction listener. More...
 
bool isSameObjectAs (const Object &other) const
 Check if another object is the same instance as this object. More...
 

Protected Member Functions

void emitChange (const Property &property)
 Dispatch a property change event. More...
 
void emitDestruction ()
 Emit a destruction event. More...
 

Protected Attributes

EventEmitter eventEmitter_
 Event dispatcher. More...
 

Detailed Description

Play sound effects (Short audio clips)

Definition at line 37 of file SoundEffect.h.

Member Typedef Documentation

◆ Ptr

using ime::Object::Ptr = std::unique_ptr<Object>
inherited

Unique object pointer.

Definition at line 42 of file Object.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

◆ 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

◆ emitChange()

void ime::Object::emitChange ( const Property property)
protectedinherited

Dispatch a property change event.

Parameters
propertyThe property that changed

This function will invoke all the event listeners of the specified property

See also
emit

◆ emitDestruction()

void ime::Object::emitDestruction ( )
protectedinherited

Emit a destruction event.

Note
This function must be the first statement in the definition of a destructor to avoid undefined behavior. In addition, note that destruction listeners are invoked once. Therefore, multiple classes in a hierarchy may call this function but the class that makes the call first will be the one that invokes the destruction listeners

◆ getClassName()

std::string ime::audio::SoundEffect::getClassName ( ) const
overridevirtual

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

std::string ime::audio::Audio::getClassType ( ) const
overridevirtualinherited

Get the name of this class.

Returns
The name of this class

Note that this function is only implemented by child classes of Object which also serve as a base class for other classes

See also
getClassName

Reimplemented from ime::Object.

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

◆ getObjectId()

unsigned int ime::Object::getObjectId ( ) const
inherited

Get the unique id of the object.

Returns
The unique id of the object

Note that each instance of ime::Object has a unique id

See also
setTag

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

◆ getTag()

const std::string & ime::Object::getTag ( ) const
inherited

Get the tag assigned to the object.

Returns
The tag of the object
See also
setTag

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

◆ isEventListenerSuspended()

bool ime::Object::isEventListenerSuspended ( int  id) const
inherited

Check if an event listener is suspended or not.

Parameters
idThe identification number of the listener to be checked
Returns
True if suspended, otherwise false

This function also returns false if the specified event listener does not exist

See also
suspendedEventListener

◆ 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

◆ isSameObjectAs()

bool ime::Object::isSameObjectAs ( const Object other) const
inherited

Check if another object is the same instance as this object.

Parameters
otherThe object to compare against this object
Returns
True if other is the same instance as this object, otherwise false

◆ onDestruction()

int ime::Object::onDestruction ( const Callback<> &  callback)
inherited

Add a destruction listener.

Parameters
callbackFunction to be executed when the object is destroyed
Returns
The unique id of the destruction listener

The destruction listener is called when the object reaches the end of its lifetime. Note that an object may have multiple destruction listeners registered to it

Warning
It's not advised to call virtual functions in the destruction callback as some parts of the object may have already been destroyed by the time the callback is invoked. In such an event, the behavior is undefined
See also
removeEventListener

◆ onPropertyChange() [1/2]

int ime::Object::onPropertyChange ( const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to any property change event.

Parameters
callbackThe function to be executed when any property changes
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The unique id of the event listener

When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event

See also
onPropertyChange(const std::string&, const ime::Callback<ime::Property>&)

◆ onPropertyChange() [2/2]

int ime::Object::onPropertyChange ( const std::string &  property,
const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to add an event listener to
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, the setTag() function, modifies the tag property of the object, thus will generate a "tag" change event each time it is called

Note that multiple event listeners may be registered to the same property change event. In addition, when adding a property change event listener, the name of the property must be in lowercase.

// Prints the tag of the player object to the console everytime it changes
player.onPropertyChange("tag", [](const Property& property) {
cout << "New tag: " << property.getValue<std::string>() << endl;
});
...
//Sets tag = "player1" and invokes event listener(s)
player.setTag("player1");
See also
unsubscribe and onPropertyChange(const ime::Callback<ime::Property>&)

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

◆ removeEventListener() [1/2]

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

Remove an event listener from an event.

Parameters
eventThe name of the event to remove an event listener from
idThe unique id of the event listener to be removed
Returns
True if the event listener was removed or false if the event or the event listener is does not exist
// Display the tag of the object to console every time it changes
auto tagChangeId = object.onPropertyChange("tag", [](ime::Property tag) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the tag of the object when it changes
object.removeEventListener("tag", tagChangeId);
Class that can store a value of any type.
Definition: Property.h:38

◆ removeEventListener() [2/2]

bool ime::Object::removeEventListener ( int  id)
inherited

Remove an event listener.

Parameters
idThe id of the event listener to be removed
Returns
True if the event listener was removed or false if no such handler exists

◆ 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 ( const 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.

◆ setTag()

void ime::Object::setTag ( const std::string &  tag)
inherited

Assign the object an alias.

Parameters
tagThe alias of the object

This function is useful if you want to refer to the object by a tag instead of its object id. Unlike an object id, multiple objects may have the same tag

By default, the tag is an empty string

See also
getObjectId

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

◆ suspendedEventListener()

void ime::Object::suspendedEventListener ( int  id,
bool  suspend 
)
inherited

Pause or resume execution of an event listener.

Parameters
idThe event listeners unique identification number
suspendTrue to suspend/pause or false to unsuspend/resume
See also
isEventListenerSuspended

Member Data Documentation

◆ eventEmitter_

EventEmitter ime::Object::eventEmitter_
protectedinherited

Event dispatcher.

Definition at line 289 of file Object.h.


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