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

#include <CUAudioMixer.h>

Inheritance diagram for cugl::audio::AudioMixer:
cugl::audio::AudioNode

Public Member Functions

 AudioMixer ()
 
 ~AudioMixer ()
 
virtual bool init () override
 
bool init (Uint8 width)
 
virtual bool init (Uint8 channels, Uint32 rate) override
 
bool init (Uint8 width, Uint8 channels, Uint32 rate)
 
virtual void dispose () override
 
std::shared_ptr< AudioNodeattach (Uint8 slot, const std::shared_ptr< AudioNode > &input)
 
std::shared_ptr< AudioNodedetach (Uint8 slot)
 
virtual Uint32 read (float *buffer, Uint32 frames) override
 
float getKnee () const
 
void setKnee (float knee)
 
- Public Member Functions inherited from cugl::audio::AudioNode
 AudioNode ()
 
virtual ~AudioNode ()
 
Uint8 getChannels () const
 
Uint32 getRate () const
 
float getGain ()
 
virtual void setGain (float gain)
 
const std::string & getName () const
 
void setName (const std::string &name)
 
void setName (const char *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 ()
 
virtual bool completed ()
 
virtual bool mark ()
 
virtual bool unmark ()
 
virtual bool reset ()
 
virtual Sint64 advance (Uint32 frames)
 
virtual Sint64 getPosition () const
 
virtual Sint64 setPosition (Uint32 position)
 
virtual double getElapsed () const
 
virtual double setElapsed (double time)
 
virtual double getRemaining () const
 
virtual double setRemaining (double time)
 

Static Public Member Functions

static std::shared_ptr< AudioMixeralloc (Uint8 width=8)
 
static std::shared_ptr< AudioMixeralloc (Uint8 width, Uint8 channels, Uint32 rate)
 

Static Public Attributes

static const Uint8 DEFAULT_WIDTH
 
static const float DEFAULT_KNEE
 
- Static Public Attributes inherited from cugl::audio::AudioNode
static const Uint32 DEFAULT_CHANNELS
 
static const Uint32 DEFAULT_SAMPLING
 

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
 
- Public Attributes inherited from cugl::audio::AudioNode
std::string _classname
 
- Protected Member Functions inherited from cugl::audio::AudioNode
void notify (const std::shared_ptr< AudioNode > &node, Action action)
 
- 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
 
size_t _hashOfName
 

Detailed Description

This class represents an audio mixer.

This mixer can take (a fixed number of) input streams and combine them together into a single output stream. The input streams must all have the same number of channels and sample rate as this node.

Mixing works by adding together all of the streams. This means that the results may exceed the range [-1,1], causing clipping. The mixer provides a "soft-knee" option (disabled by default) for confining the results to the range [-1,1]. When a knee k is specified, all values [-k,k] will not be effected, but values outside of this range will asymptotically bend to the range [-1,1].

The audio graph should only be accessed in the main thread. In addition, no methods marked as AUDIO THREAD ONLY should ever be accessed by the user.

This class does not support any actions for the AudioNode#setCallback.

Constructor & Destructor Documentation

cugl::audio::AudioMixer::AudioMixer ( )

Creates a degenerate mizer that takes no inputs

The mixer has no width and therefore cannot accept any inputs. The mixer must be initialized to be used.

cugl::audio::AudioMixer::~AudioMixer ( )
inline

Deletes this mixer, disposing of all resources.

Member Function Documentation

static std::shared_ptr<AudioMixer> cugl::audio::AudioMixer::alloc ( Uint8  width = 8)
inlinestatic

Returns a newly allocated mixer with default stereo settings

The number of channels is two, for stereo output. The sample rate is CD standard 44100 HZ.

These values determine the buffer the structure for all read operations. In addition, they also detemine exactly which audio nodes are supported by this mixer. A mixer can only mix nodes that agree on both sample rate and frequency.

Parameters
widthThe number of audio nodes that may be attached to this mixer
Returns
a newly allocated mixer with default stereo settings
static std::shared_ptr<AudioMixer> cugl::audio::AudioMixer::alloc ( Uint8  width,
Uint8  channels,
Uint32  rate 
)
inlinestatic

Returns a newly allocated mixer with the given number of channels and sample rate

These values determine the buffer the structure for all read operations. In addition, they also detemine exactly which audio nodes are supported by this mixer. A mixer can only mix nodes that agree on both sample rate and frequency.

Parameters
widthThe number of audio nodes that may be attached to this mixer
channelsThe number of audio channels
rateThe sample rate (frequency) in HZ
Returns
a newly allocated mixer with the given number of channels and sample rate
std::shared_ptr<AudioNode> cugl::audio::AudioMixer::attach ( Uint8  slot,
const std::shared_ptr< AudioNode > &  input 
)

Attaches an input node to this mixer.

The input is attached at the given slot. Any input node previously at that slot is removed (and returned by this method).

Parameters
slotThe slot for the input node
inputThe input node to attach
Returns
the input node previously at the given slot
std::shared_ptr<AudioNode> cugl::audio::AudioMixer::detach ( Uint8  slot)

Detaches the input node at the given slot.

The input node detached is returned by this method.

Parameters
slotThe slot for the input node
Returns
the input node detached from the slot
virtual void cugl::audio::AudioMixer::dispose ( )
overridevirtual

Disposes any resources allocated for this mixer

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.

float cugl::audio::AudioMixer::getKnee ( ) const

Returns the "soft knee" of this mixer, or -1 if not set

The soft knee is used to ensure that the results fit in the range [-1,1]. If the knee is k, then values in the range [-k,k] are unaffected, but values outside of this range are asymptotically clamped to the range [-1,1], using the formula (x-k+k*k)/x.

If the value is 0, then this mixer will hard clamp to [-1,1]. If it is negative, all inputs will be mixed exactly with no distortion.

Returns
the "soft knee" of this mixer, or -1 if not set
virtual bool cugl::audio::AudioMixer::init ( )
overridevirtual

Initializes the mixer with default stereo settings

The number of channels is two, for stereo output. The sample rate is CD standard 44100 HZ.

These values determine the buffer the structure for all read operations. In addition, they also detemine exactly which audio nodes are supported by this mixer. A mixer can only mix nodes that agree on both sample rate and frequency.

Parameters
widthThe number of audio nodes that may be attached to this mixer
Returns
true if initialization was successful

Reimplemented from cugl::audio::AudioNode.

bool cugl::audio::AudioMixer::init ( Uint8  width)

Initializes the mixer with default stereo settings

The number of channels is two, for stereo output. The sample rate is CD standard 44100 HZ.

These values determine the buffer the structure for all read operations. In addition, they also detemine exactly which audio nodes are supported by this mixer. A mixer can only mix nodes that agree on both sample rate and frequency.

Parameters
widthThe number of audio nodes that may be attached to this mixer
Returns
true if initialization was successful
virtual bool cugl::audio::AudioMixer::init ( Uint8  channels,
Uint32  rate 
)
overridevirtual

Initializes the mixer with the given number of channels and sample rate

These values determine the buffer the structure for all read operations. In addition, they also detemine exactly which audio nodes are supported by this mixer. A mixer can only mix nodes that agree on both sample rate and frequency.

Parameters
widthThe number of audio nodes that may be attached to this mixer
channelsThe number of audio channels
rateThe sample rate (frequency) in HZ
Returns
true if initialization was successful

Reimplemented from cugl::audio::AudioNode.

bool cugl::audio::AudioMixer::init ( Uint8  width,
Uint8  channels,
Uint32  rate 
)

Initializes the mixer with the given number of channels and sample rate

These values determine the buffer the structure for all read operations. In addition, they also detemine exactly which audio nodes are supported by this mixer. A mixer can only mix nodes that agree on both sample rate and frequency.

Parameters
widthThe number of audio nodes that may be attached to this mixer
channelsThe number of audio channels
rateThe sample rate (frequency) in HZ
Returns
true if initialization was successful
virtual Uint32 cugl::audio::AudioMixer::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, unless part of a custom audio graph node.

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

Reading the buffer has no affect on the read position. You must manually move the frame position forward. This is to allow for a frame window to be reread if necessary.

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.

void cugl::audio::AudioMixer::setKnee ( float  knee)

Sets the "soft knee" of this mixer.

The soft knee is used to ensure that the results fit in the range [-1,1]. If the knee is k, then values in the range [-k,k] are unaffected, but values outside of this range are asymptotically clamped to the range [-1,1], using the formula (x-k+k*k)/x

If the value is 0, then this mixer will hard clamp to [-1,1]. If it is negative, all inputs will be mixed exactly with no distortion.

Parameters
kneethe "soft knee" of this mixer

Member Data Documentation

const float cugl::audio::AudioMixer::DEFAULT_KNEE
static

The standard knee value for preventing clipping

const Uint8 cugl::audio::AudioMixer::DEFAULT_WIDTH
static

The default number of inputs supported (typically 8)


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