CUGL 2.1
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
cugl::audio::AudioPlayer Class Reference

#include <CUAudioPlayer.h>

Inheritance diagram for cugl::audio::AudioPlayer:
cugl::audio::AudioNode

Public Member Functions

 AudioPlayer ()
 
 ~AudioPlayer ()
 
bool init (const std::shared_ptr< AudioSample > &source)
 
virtual void dispose () override
 
std::shared_ptr< AudioSamplegetSource ()
 
virtual Uint32 read (float *buffer, Uint32 frames) override
 
virtual bool completed () override
 
virtual bool mark () override
 
virtual bool unmark () override
 
virtual bool reset () override
 
virtual Sint64 advance (Uint32 frames) override
 
virtual Sint64 getPosition () const override
 
virtual Sint64 setPosition (Uint32 position) override
 
virtual double getElapsed () const override
 
virtual double setElapsed (double time) override
 
virtual double getRemaining () const override
 
virtual double setRemaining (double time) override
 
- Public Member Functions inherited from cugl::audio::AudioNode
 AudioNode ()
 
virtual ~AudioNode ()
 
virtual bool init ()
 
virtual bool init (Uint8 channels, Uint32 rate)
 
Uint8 getChannels () const
 
Uint32 getRate () const
 
float getGain ()
 
virtual void setGain (float gain)
 
const std::string & getClassName () const
 
const std::string & getName () const
 
void setName (const std::string name)
 
Sint32 getTag () const
 
void setTag (Sint32 tag)
 
virtual std::string toString (bool verbose=false) const
 
 operator std::string () const
 
Callback getCallback ()
 
void setCallback (Callback callback)
 
virtual bool isPaused ()
 
virtual bool pause ()
 
virtual bool resume ()
 

Static Public Member Functions

static std::shared_ptr< AudioPlayeralloc (const std::shared_ptr< AudioSample > &sample)
 

Protected Attributes

std::shared_ptr< AudioSample_source
 
std::shared_ptr< AudioDecoder_decoder
 
std::atomic< Uint64 > _offset
 
std::atomic< Uint64 > _marked
 
float * _buffer
 
float * _chunker
 
Uint32 _chksize
 
Uint32 _chklimt
 
Uint32 _chklast
 
std::atomic< bool > _dirty
 
- Protected Attributes inherited from cugl::audio::AudioNode
Uint8 _channels
 
Uint32 _sampling
 
bool _booted
 
std::atomic< float > _ndgain
 
std::atomic< bool > _paused
 
std::atomic< bool > _polling
 
Callback _callback
 
std::atomic< bool > _calling
 
Sint32 _tag
 
std::string _localname
 
std::string _classname
 
size_t _hashOfName
 

Additional Inherited Members

- Public Types inherited from cugl::audio::AudioNode
enum  Action : int {
  COMPLETE = 0, INTERRUPT = 1, FADE_OUT = 2, FADE_IN = 3,
  FADE_DIP = 4, LOOPBACK = 5
}
 
typedef std::function< void(const std::shared_ptr< AudioNode > &node, Action type)> Callback
 
- Static Public Attributes inherited from cugl::audio::AudioNode
const static Uint32 DEFAULT_CHANNELS
 
const static Uint32 DEFAULT_SAMPLING
 
- Protected Member Functions inherited from cugl::audio::AudioNode
void notify (const std::shared_ptr< AudioNode > &node, Action action)
 

Detailed Description

This class represents a playback instance for AudioPlayback.

A single sound asset may have multiple instances playing simultaneously, particularly in the case of sound effects. This node allows us to keep the playback distinct for each instance.

A player can be reset and can jump to anywhere in the sounds. However, it cannot be set to loop or sequence two sound assets together. To do that you should combine this node with AudioScheduler.

This class is medium-weight, and has a lot of buffers to support stream decoding (when appropriate). In practice, it may be best to create a memory pool of preallocated players (which are reinitialized) than to construct them on the fly.

A player is always associated with a node in the audio graph. As such, it should only be accessed in the main thread. In addition, no methods marked as AUDIO THREAD ONLY should ever be accessed by the user. The only exception to this rule is by another (custom) audio graph node in its audio thread methods.

This class does not support any actions for the AudioNode#setCallback. Fade in/out and scheduling have been refactored into other nodes to provide proper audio patch support.

Constructor & Destructor Documentation

◆ AudioPlayer()

cugl::audio::AudioPlayer::AudioPlayer ( )

Creates a degenerate audio player with no associated source.

The player has no channels or source file, so read options will do nothing. The player must be initialized to be used.

◆ ~AudioPlayer()

cugl::audio::AudioPlayer::~AudioPlayer ( )
inline

Deletes this audio player, disposing of all resources.

Member Function Documentation

◆ advance()

virtual Sint64 cugl::audio::AudioPlayer::advance ( Uint32  frames)
overridevirtual

Advances the stream by the given number of frames.

This method only advances the read position, it does not actually read data into a buffer.

Parameters
framesThe number of frames to advace
Returns
the actual number of frames advanced; -1 if not supported

Reimplemented from cugl::audio::AudioNode.

◆ alloc()

static std::shared_ptr<AudioPlayer> cugl::audio::AudioPlayer::alloc ( const std::shared_ptr< AudioSample > &  sample)
inlinestatic

Returns a newly allocated player for the given audio sample.

The player will either be streamed or buffered, depending on the type of audio sample. We do not require separate players for each type.

Parameters
samplethe audio sample to be played.
Returns
a newly allocated player for the given audio sample.

◆ completed()

virtual bool cugl::audio::AudioPlayer::completed ( )
overridevirtual

Returns true if this audio node has no more data.

A completed audio node is one that will return 0 (no frames read) on subsequent threads read.

Returns
true if this audio node has no more data.

Reimplemented from cugl::audio::AudioNode.

◆ dispose()

virtual void cugl::audio::AudioPlayer::dispose ( )
overridevirtual

Disposes any resources allocated for this player

The state of the node is reset to that of an uninitialized constructor. Unlike the destructor, this method allows the node to be reinitialized.

Reimplemented from cugl::audio::AudioNode.

◆ getElapsed()

virtual double cugl::audio::AudioPlayer::getElapsed ( ) const
overridevirtual

Returns the elapsed time in seconds.

The value returned is always measured from the start of the steam, regardless of the presence of any marks.

Returns
the elapsed time in seconds.

Reimplemented from cugl::audio::AudioNode.

◆ getPosition()

virtual Sint64 cugl::audio::AudioPlayer::getPosition ( ) const
overridevirtual

Returns the current frame position of this audio node

The value returned will always be the absolute frame position regardless of the presence of any marks.

Returns
the current frame position of this audio node.

Reimplemented from cugl::audio::AudioNode.

◆ getRemaining()

virtual double cugl::audio::AudioPlayer::getRemaining ( ) const
overridevirtual

Returns the remaining time in seconds.

The remaining time is duration from the current read position to the end of the sample. It is not effected by any fade-out.

Returns
the remaining time in seconds.

Reimplemented from cugl::audio::AudioNode.

◆ getSource()

std::shared_ptr<AudioSample> cugl::audio::AudioPlayer::getSource ( )
inline

Returns the source for this instance

Returns
the source for this instance

◆ init()

bool cugl::audio::AudioPlayer::init ( const std::shared_ptr< AudioSample > &  source)

Initializes a player for the given audio sample.

The player will be set for a single playthrough of this given sample. However the player may be reset or reinitialized.

Parameters
sourceThe audio sample to be played.
Returns
true if initialization was successful

◆ mark()

virtual bool cugl::audio::AudioPlayer::mark ( )
overridevirtual

Marks the current read position in the audio steam.

This method is used by reset() to determine where to restore the read position.

Returns
true if the read position was marked.

Reimplemented from cugl::audio::AudioNode.

◆ read()

virtual Uint32 cugl::audio::AudioPlayer::read ( float *  buffer,
Uint32  frames 
)
overridevirtual

Reads up to the specified number of frames into the given buffer

AUDIO THREAD ONLY: Users should never access this method directly. The only exception is when the user needs to create a custom subclass of this AudioNode.

The buffer should have enough room to store frames * channels elements. The channels are interleaved into the output buffer.

This method will always forward the read position after reading. Reading again may return different data.

Parameters
bufferThe read buffer to store the results
framesThe maximum number of frames to read
Returns
the actual number of frames read

Reimplemented from cugl::audio::AudioNode.

◆ reset()

virtual bool cugl::audio::AudioPlayer::reset ( )
overridevirtual

Resets the read position to the marked position of the audio stream.

If no mark is set, this will reset to the player to the beginning of the audio sample.

Returns
true if the read position was moved.

Reimplemented from cugl::audio::AudioNode.

◆ setElapsed()

virtual double cugl::audio::AudioPlayer::setElapsed ( double  time)
overridevirtual

Sets the read position to the elapsed time in seconds.

The value returned is always measured from the start of the steam, regardless of the presence of any marks.

Parameters
timeThe elapsed time in seconds.
Returns
the new elapsed time in seconds.

Reimplemented from cugl::audio::AudioNode.

◆ setPosition()

virtual Sint64 cugl::audio::AudioPlayer::setPosition ( Uint32  position)
overridevirtual

Sets the current frame position of this audio node.

The value set will always be the absolute frame position regardless of the presence of any marks.

Parameters
positionthe current frame position of this audio node.
Returns
the new frame position of this audio node.

Reimplemented from cugl::audio::AudioNode.

◆ setRemaining()

virtual double cugl::audio::AudioPlayer::setRemaining ( double  time)
overridevirtual

Sets the remaining time in seconds.

This method will move the read position so that the distance between it and the end of the same is the given number of seconds.

Parameters
timeThe remaining time in seconds.
Returns
the new remaining time in seconds.

Reimplemented from cugl::audio::AudioNode.

◆ unmark()

virtual bool cugl::audio::AudioPlayer::unmark ( )
overridevirtual

Clears the current marked position.

Clearing the mark in a player is equivelent to setting the mark at the beginning of the audio asset. Future calls to reset() will return to the start of the audio stream.

Returns
true if the read position was cleared.

Reimplemented from cugl::audio::AudioNode.

Member Data Documentation

◆ _buffer

float* cugl::audio::AudioPlayer::_buffer
protected

A reference to the underlying data buffer (IN-MEMORY ACCESS)

◆ _chklast

Uint32 cugl::audio::AudioPlayer::_chklast
protected

The number of the last read frame in the chunk

◆ _chklimt

Uint32 cugl::audio::AudioPlayer::_chklimt
protected

The pointer to the last available frame in the chunk

◆ _chksize

Uint32 cugl::audio::AudioPlayer::_chksize
protected

The size of a single chunk in frames

◆ _chunker

float* cugl::audio::AudioPlayer::_chunker
protected

A buffer for storing each chunk as we need it

◆ _decoder

std::shared_ptr<AudioDecoder> cugl::audio::AudioPlayer::_decoder
protected

The decoder for the current asset

◆ _dirty

std::atomic<bool> cugl::audio::AudioPlayer::_dirty
protected

Whether or not we need to reposition (STREAMING ACCESS)

◆ _marked

std::atomic<Uint64> cugl::audio::AudioPlayer::_marked
protected

The last marked position (starts at 0)

◆ _offset

std::atomic<Uint64> cugl::audio::AudioPlayer::_offset
protected

The current read position

◆ _source

std::shared_ptr<AudioSample> cugl::audio::AudioPlayer::_source
protected

The original source for this instance


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