Switchboard SDK
Loading...
Searching...
No Matches
switchboard::AudioPlayer Class Reference

Plays the loaded audio file. More...

#include <AudioPlayer.hpp>

Public Member Functions

 AudioPlayer ()
 AudioPlayer constructor.
 
virtual ~AudioPlayer ()=default
 AudioPlayer destructor.
 
bool load (const std::string &path, Codec format)
 Loads an entire audio file at the given path. Stops playback.
 
bool load (const std::vector< uint8 > &inputData, Codec format)
 Loads encoded audio data. Stops playback.
 
bool load (AudioBuffer< float > &audioBuffer, bool copyData)
 Loads audio data from an AudioBuffer instance.
 
bool stream (const std::string &path, Codec format)
 Streams the encoded audio file from the given path. Stops playback.
 
void setSource (AudioPlayerSource &audioPlayerSource)
 Sets the source of the audio player.
 
void play ()
 Starts the audio playback.
 
void pause ()
 Pauses audio playback.
 
void stop ()
 Stops audio playback.
 
bool isPlaying () const
 Indicates whether the audio player is currently playing.
 
bool isLoopingEnabled () const
 Indicates whether looping of the audio file is enabled.
 
void setLoopingEnabled (bool enabled)
 Enables or disables looping of the loaded audio file.
 
double getDuration () const
 
double getPosition () const
 
void setPosition (double position)
 
double getStartPosition () const
 
void setStartPosition (double startPosition)
 
double getEndPosition () const
 
void setEndPosition (double endPosition)
 
uint getSourceSampleRate () const
 Gets the sample rate of the current audio source. SAMPLE_RATE_UNDEFINED is returned if there is none.
 
uint getSourceNumberOfChannels () const
 Gets the number of channels of the current audio source. NUM_CHANNELS_UNDEFINED is returned if there is none.
 
int getNumberOfBufferedFrames () const
 Retrieves the number of buffered frames currently in the AudioPlayer.
 
virtual void process (float *outputBuffer, const uint numberOfFrames, const uint numberOfChannels, const uint sampleRate)
 Renders the loaded audio data into the given audio buffer.
 

Protected Attributes

std::atomic< bool > playing
 
bool loopingEnabled
 
std::unique_ptr< switchboard::Resamplerresampler
 
std::unique_ptr< RingBuffer< float > > ringBuffer
 
AudioData< float > audioChunk
 
AudioData< float > resampledAudioChunk
 
uint currentSampleRate
 
std::unique_ptr< AudioPlayerSourceinternalSource
 
AudioPlayerSourcecurrentSource
 

Detailed Description

Plays the loaded audio file.

AudioPlayer class.

Member Function Documentation

◆ getDuration()

double switchboard::AudioPlayer::getDuration ( ) const

The duration of the currently playing media.

Returns
The duration of the media in seconds.

◆ getEndPosition()

double switchboard::AudioPlayer::getEndPosition ( ) const

Indicates the current end position of the playback.

Returns
Returns the current end position in seconds.

◆ getNumberOfBufferedFrames()

int switchboard::AudioPlayer::getNumberOfBufferedFrames ( ) const

Retrieves the number of buffered frames currently in the AudioPlayer.

The count returned by this method is indicative of the internal state of the AudioPlayer's buffering system and can be used for diagnostics or managing the flow of data through the AudioPlayer.

Returns
The number of buffered frames in the AudioPlayer.

◆ getPosition()

double switchboard::AudioPlayer::getPosition ( ) const

Indicates the current position of the playback head.

Returns
Returns the elapsed time in seconds.

◆ getSourceNumberOfChannels()

uint switchboard::AudioPlayer::getSourceNumberOfChannels ( ) const

Gets the number of channels of the current audio source. NUM_CHANNELS_UNDEFINED is returned if there is none.

Returns
The current audio source's number of channels.

◆ getSourceSampleRate()

uint switchboard::AudioPlayer::getSourceSampleRate ( ) const

Gets the sample rate of the current audio source. SAMPLE_RATE_UNDEFINED is returned if there is none.

Returns
The current audio source's sample rate.

◆ getStartPosition()

double switchboard::AudioPlayer::getStartPosition ( ) const

Indicates the current start position of the playback.

Returns
Returns the current start position in seconds.

◆ isLoopingEnabled()

bool switchboard::AudioPlayer::isLoopingEnabled ( ) const

Indicates whether looping of the audio file is enabled.

Returns
True when it is enabled, false when it is not.

◆ isPlaying()

bool switchboard::AudioPlayer::isPlaying ( ) const

Indicates whether the audio player is currently playing.

Returns
True when it is playing, false when it is not.

◆ load() [1/3]

bool switchboard::AudioPlayer::load ( AudioBuffer< float > & audioBuffer,
bool copyData )

Loads audio data from an AudioBuffer instance.

Parameters
audioBufferThe audio buffer containing the audio data.
copyDataWhen set to true, the audio data from the audio buffer will be copied and stored in the AudioPlayer instance.
Returns
True if loading was successful, false otherwise.

◆ load() [2/3]

bool switchboard::AudioPlayer::load ( const std::string & path,
Codec format )

Loads an entire audio file at the given path. Stops playback.

Parameters
pathThe path of the encoded audio file to load.
formatThe audio file format.
Returns
True if loading of the audio file was successful, false otherwise.

◆ load() [3/3]

bool switchboard::AudioPlayer::load ( const std::vector< uint8 > & inputData,
Codec format )

Loads encoded audio data. Stops playback.

Parameters
inputDataVector containing the encoded audio data.
formatThe audio format.
Returns
True if loading was successful, false otherwise.

◆ process()

void switchboard::AudioPlayer::process ( float * outputBuffer,
const uint numberOfFrames,
const uint numberOfChannels,
const uint sampleRate )
virtual

Renders the loaded audio data into the given audio buffer.

Parameters
outputBufferThe buffer that will contain the output samples.
numberOfFramesThe number of frames to put in the output buffer.
numberOfChannelsThe number of channels to put in the output channels.
sampleRateThe sample rate in which to play the recording.

◆ setEndPosition()

void switchboard::AudioPlayer::setEndPosition ( double endPosition)

Sets the ending position of the playback. Only takes effect after audio is loaded.

Parameters
endPositionThe desired position in seconds.

◆ setLoopingEnabled()

void switchboard::AudioPlayer::setLoopingEnabled ( bool enabled)

Enables or disables looping of the loaded audio file.

Parameters
enabledTrue when it is enabled, false when it is not.

◆ setPosition()

void switchboard::AudioPlayer::setPosition ( double position)

Sets the playback head to the desired position.

Parameters
positionThe desired position in seconds.

◆ setSource()

void switchboard::AudioPlayer::setSource ( AudioPlayerSource & audioPlayerSource)

Sets the source of the audio player.

Parameters
audioPlayerSourceThe source of the audio player.

◆ setStartPosition()

void switchboard::AudioPlayer::setStartPosition ( double startPosition)

Sets the starting position of the playback. Only takes effect after audio is loaded.

Parameters
startPositionThe desired position in seconds.

◆ stream()

bool switchboard::AudioPlayer::stream ( const std::string & path,
Codec format )

Streams the encoded audio file from the given path. Stops playback.

Parameters
pathThe path to the encoded audio file to stream.
formatThe audio file format.
Returns
True if loading of the audio file was successful, false otherwise.