ofDocsdocumentation sound ofSoundPlayer

ofSoundPlayer

The ofSoundPlayer class wraps one of several underlying audio utility libraries, depending on your OS and your configuration that can be Fmod, Quicktime, or OpenAL. The ofSoundPlayer is primarily to allow you to load sound files and control and manipulate their playback and properties, if you want more low level access to sound data and to your sound card then you should take a look at the ofSoundStream class.


getPan( )

float getPan()

Gets stereo pan. \return stereo pan in the range -1 to 1.

Returns the pan position of the sound. -1.0 - 1.0 range. 0.0 is center pan, -1.0 is full left pan and 1.0 is full right pan. Default is 0.0

Example:

    
    ofSoundPlayer mySound;    
    mySound.load("beat.mp3");    
    mySound.play();    
    mySound.getPan();//Returns 0.0     
    mySound.setPan(1.0f);     
    mySound.getPan();//Returns 1.0

getPosition( )

float getPosition()

Gets position of the playhead. \return playhead position as a float between 0 and 1.

Returns the current position of the playhead as a float between 0.0 and 1.0. 0.0 is the beginning of the sound file and 1.0 is the end.

Example:

    ofSoundPlayer mySound;  
    mySound.load("beat.mp3");   
    mySound.play();   
    mySound.getPosition(); //Returns the current position as a percent 0.0-1.0

getPositionMS( )

int getPositionMS()

Gets position of the playhead. \return playhead position in milliseconds.

This returns the position of the playhead in terms of milliseconds (i.e. 5000 for 5 seconds into the sound).


getSpeed( )

float getSpeed()

Gets playback speed. \return playback speed (see ofSoundPlayer::setSpeed()).

Returns the speed of the sound playback in relation to its normal speed. So 2.0 would mean the sound is playing twice as fast. 0.5 would mean half as fast.

Example:

    
    ofSoundPlayer mySound;    
    mySound.load("beat.mp3");   
    mySound.getSpeed(); //Returns 1.0   
    mySound.setSpeed(2.0f);   
    mySound.getSpeed(); //Returns 2.0f

getVolume( )

float getVolume()

Gets current volume. \return current volume in the range 0 to 1.

Returns the current volume of the sound player, as set by setVolume(). 0.0 is silent and 1.0 is full volume.


isLoaded( )

bool isLoaded()

Queries the player to see if its file was loaded successfully. \return whether or not the player is ready to begin playback.

Returns whether or not a sound has been successfully loaded into the sound player.


isPlaying( )

bool isPlaying()

Gets current playback state. \return true if the player is currently playing a file.


load( ... )

bool load(const filesystem::path &fileName, bool stream=false)

Tells the sound player which file to play.

Codec support varies by platform but wav, aif, and mp3 are safe.

Parameters:

fileName Path to the sound file, relative to your app's data folder.

stream set "true" to enable streaming from disk (for large files).


ofSoundPlayer( )

ofSoundPlayer()

Creates the ofSoundPlayer instance and initializes the underlying properties in the underlying engine.


play( )

void play()

Starts playback.

Plays the sound. If setMultiPlay() has been set to true each play() command will spawn a new copy of the sound on a new channel, letting the existing sounds continue until they are finished. If setMultiPlay() is set to false it will restart the playback of the song.

Examples:

Normal Playback:

ofSoundPlayer mySound;
mySound.load("beat.mp3");
mySound.play(); //Plays sound
mySound.play(); //Restarts and plays sound

Multiplay:

ofSoundPlayer mySound;
mySound.setMultiPlay(true);
mySound.load("beat.mp3");
mySound.play(); //Plays sound
mySound.play(); //Adds new copy of sound to channel and plays over currently playing sound

setLoop( ... )

void setLoop(bool loop)

Sets whether to loop once the end of the file is reached.

Parameters:

loop "true" to loop, default is false.

Loops the sound if set to true. Does not loop the sound if set to false. Default is false.

Example:

ofSoundPlayer mySound;
mySound.load("beat.mp3");
mySound.setLoop(true); //Sound will loop
mySound.play();

setMultiPlay( ... )

void setMultiPlay(bool multiplay)

Enables playing multiple simultaneous copies of the sound.

Parameters:

multiplay "true" to enable, default is false.

Allows a sound to be played multiple times at once. When set to true the play() function will start playing the sound on a new channel, letting the old channels continue until they are done playing. When set to false the play() function will stop the channel before playing the sound.

Example:

ofSoundPlayer mySound;
mySound.setMultiPlay(true);
mySound.load("beat.mp3");
mySound.play(); //Plays sound
mySound.play(); //Adds new copy of sound to channel and plays over currently playing sound

setPan( ... )

void setPan(float pan)

Sets stereo pan.

Parameters:

pan range is -1 to 1 (-1 is full left, 1 is full right).

Sets the pan position (pct) of the sound. -1.0 - 1.0 range. 0.0 is center pan, -1.0 is full left pan and 1.0 is full right pan.

Example:

ofSoundPlayer mySound;
mySound.load("beat.mp3");
mySound.play();
mySound.setPan(-1.0f); //Pans to the left
mySound.setPan(1.0f); //Pans to the right
mySound.setPan(0.0f); //Back to center

setPaused( ... )

void setPaused(bool paused)

Enables pause / resume.

Parameters:

paused "true" to pause, "false" to resume.

Pauses and un-pauses the playback of the sound.

Example

ofSoundPlayer mySound;
mySound.load("beat.mp3");
mySound.play();
mySound.setPaused(true); //Sound is paused
mySound.setPaused(false); //Sound is unpaused, playback continues

setPosition( ... )

void setPosition(float percent)

Sets position of the playhead within the file (aka "seeking").

Parameters:

percent range is 0 (beginning of file) to 1 (end of file).

Sets the playback-head to the position (pct) specified. 0.0 - 1.0 range. 0.0 is the beginning of the sound file and 1.0 is the end.

Example:

ofSoundPlayer mySound;
mySound.load("beat.mp3");
mySound.play();
mySound.setPosition(0.5f); //Moves the playhead to halfway through the sound
mySound.setPosition(0.0f); //Moves the playhead back to the beginning of the sound

setPositionMS( ... )

void setPositionMS(int ms)

Sets position of the playhead within the file (aka "seeking").

Parameters:

ms number of milliseconds from the start of the file.

This sets the position of the playhead in milliseconds.


setSpeed( ... )

void setSpeed(float speed)

Sets the playback speed of the sound. 1.0 is normal speed. 2.0 is double the normal speed etc.

Example:

ofSoundPlayer mySound;
mySound.loadSound("beat.mp3");
mySound.play();
mySound.setSpeed(2.0f); //Chipmunk Voice
mySound.setSpeed(0.2f); //Isaac Hayes on Muscle Relaxers
mySound.setSpeed(1.0f); //Normal again

setVolume( ... )

void setVolume(float vol)

Sets playback volume.

Parameters:

vol range is 0 to 1.

Sets the volume of the sound. 0.0 - 1.0 range. 0.0 is silent and 1.0 is full volume.

Example:

ofSoundPlayer mySound;
mySound.load("beat.mp3");
mySound.play();
mySound.setVolume(0.1f); //Sets volume at 10% of maximum

stop( )

void stop()

Stops playback.

Stops the sound currently playing.

Example:

ofSoundPlayer mySound;
mySound.load("beat.mp3");
mySound.play(); //Begins playback of sound
mySound.stop(); //Ends playback, stops audio

unload( )

void unload()

Stops and unloads the current sound.