AudioChannel.cpp

This class manages the buffers and other data needed in audio storage, playback, and processing.  There are circular buffers for the input and output audio streams.  Data that is reused from frame to frame (e.g. filter coefficients) is stored in this class and is accessible in ALFPackage.cpp for use with the DSP functions contained in DSPFunctions.c.

Summary
AudioChannel.cppThis class manages the buffers and other data needed in audio storage, playback, and processing.
Constructor
AudioChannelCreates an AudioChannel object with the default parameters.
initChannel()This function is called with the allocation of every DATF (or ALF) object from ActionScript for a desired audio channel (left or right).
reInitChannel()This funciton is called when a channel (L or R) has already been instantiated and initialized and a new song is loaded that requires changes to the buffer sizes.
initChannelVocoderThis function initializes a vocoder object for the AudioChanenl when needed for the vocoding function
Utilities
checkRoom()This function checks if the parameters entered are the same as the ones stored in the audio channel.
clearFFTFrame()Clears the fftFrame buffer, which has the FFT data.
clearInAudioFrame()Clears the inAudioFrame buffer that is one of the buffers that can be read and written to by C++ and Actionscript.
clearCorrFrame()Clears the frames for performing a correlation.
resetChannelResets the buffers and flags in the channel.
Get Functions
getCentroidVal()Returns the centroid of the audio spectrum from the current frame of data
getChannelName()Retrieves the name given to the AudioChannel object.
getFFTSize()Returns the size of the FFT used on the current audio frame
getHopSize()Returns the user-specificed hopSize object
getSampleRate()Returns the sampling rate of the audio under analysis
getOutAudioFrameSamples()Returns the current number of samples contained in outAudioFrame
Set Functions
setCentroidVal()Sets the value of the spectral centroid for the current frame
setChannelName()Sets the name of the AudioChannel
setHopSize()Sets the value of the hopSize
setSampleRate()Sets the sampling rate of the audio under analysis
setAudioFrameSamples()Sets the number of samples currently contained inthe inAudioFrame buffer
setOutAudioFrameSamples()Sets the number of samples currently contained inthe OutAudioFrame buffer
setRoom()Sets the parameters to the room impulse response function.
Flag ManagementThis is a set of functions to handle toggling various flags that are checked in ALFPackage.cpp in order to avoid duplicate computation.
clearFlags()This function clears all of the flags that are used to specify whether a given value has been calculated on the current frame.
getCentroidFlag()Returns the centroidFlag to determine if the centroid has been calculated.
getCircularBufferFlag()Returns the status fo the circularBufferFlag to determine if audio output should be routed through the ouput circular buffer.
getFFTFlag()Returns the status off the fftFlag to determine if the FFT has been computed on the current frame.
getMagFlag()Returns the status off the magflag to determine if the magnitude has been calculated on the current frame
setCentroidFlag()Sets the status of the centroid flag
setCircularBufferFlag()Sets the status of the circularBuffer flag.
setFFTFlag()Sets the status of the FFT flag
setMagFlag()Sets the status of the magnitude flag
Public MembersThe AudioChannel class contains many buffers required for storing various aspects of the current audio frame as well as the input/output buffer access so that flash can read/write directly from/to the C namespace.
Private MembersIncludes variables and flags.

Constructor

AudioChannel

AudioChannel::AudioChannel()

Creates an AudioChannel object with the default parameters.

Default parameters

  • hopSize - the size, in samples, of the ‘inAudioFrame’ buffer.  Default: 4096.
  • fs - the sampling rate of the audio under analysis.  Set by ‘initChannel’ method.  Default: 44100.
  • fftFlag - Boolean indicating whether or not an FFT has been performed on the current frame of audio.  Default value: false.
  • magFlag - Boolean indicating if the magnitude spectrum has been computed on the fftFrame.  Default: false.
  • centroidFlag - Boolean indicating if the spectral centroid has been performed on the current frame of data.  Default: false.
  • centroidVal - Stores the value of the spectral centroid for feature computatons.  Default: 0.
  • filterLen - The length of the filter used in the filter command in ALFPackage.cpp
  • filterProcessing - Boolean value indicating if processing functionality (aka filter) is currently in use.  Default: 0;

initChannel()

void AudioChannel::initChannel(int _hopSize,
int _fftSize,
int _fs,
int _lookAheadFrames)

This function is called with the allocation of every DATF (or ALF) object from ActionScript for a desired audio channel (left or right).  This function initializes the circular buffers needed for input and output.  It also initializes memory required for the feature analysis functions.  Certain flags are initialized as well.  See Sections Private Members, Public Members

Parameters

hopSizeThe desired size of the analysis frame, as selected by the user from ALF or DATF.
fftSizethe size of the FFT to be computed on each frame when using spectral analysis fucntions.
fsThe audio sampling rate of the user-selected audio
lookAheadFramesThe number of frames to calculate features on before beginning playback.  For example, entering a value of twenty will cause zeros to be returned as the audio output for the first 20 frames, while the feature values of the first twenty.

reInitChannel()

void AudioChannel::reInitChannel(int _hopSize,
int _fftSize,
int _fs,
int numCh,
int _lookAheadFrames)

This funciton is called when a channel (L or R) has already been instantiated and initialized and a new song is loaded that requires changes to the buffer sizes.

Parameters

hopSizeThe desired size of the analysis frame, as selected by the user from ALF or DATF.
fftSizethe size of the FFT to be computed on each frame when using spectral analysis fucntions.
fsThe audio sampling rate of the user-selected audio
lookAheadFramesThe number of frames to calculate features on before beginning playback.

initChannelVocoder

void AudioChannel::initChannelVocoder(int frameOverlap)

This function initializes a vocoder object for the AudioChanenl when needed for the vocoding function

Parameters

*none: this function uses internal class parameters.

Utilities

checkRoom()

bool AudioChannel::checkRoom(float newRoomLength,
float newRoomWidth,
float newRoomHeight,
float newSourceLength,
float newSourceWidth,
float newSourceHeight,
float newMicLength,
float newMicWidth,
float newMicHeight,
double newEchoStrength)

This function checks if the parameters entered are the same as the ones stored in the audio channel.

Parameters

newRoomLengthA float that is the new room length.
newRoomWidthA float that is the new room width.
newRoomHeightA float that is the new room height.
newSourceLengthA float that is the new source x location.
newSourceWidthA float that is the new source y location.
newSourceHeightA float that is the new source z location.
newMicLengthA float that is the new mic x location.
newMicWidthA float that is the new mic y location.
newMicHeightA float that is the new mic y location.
newEchoStrengthA double indicating the new echo strength.

Returns

True if a new room impulse response must be calculated, false if the parameters entered are the same as those in the channel.

See Also

setRoom, <DATF.reverb>

clearFFTFrame()

void AudioChannel::clearFFTFrame()

Clears the fftFrame buffer, which has the FFT data.  This must be done every frame.  It is equivalent to zero padding to the next power of 2 greater than the frameSize.  This also clears the buffers used in the filter function in ALFPackage.cpp.

clearInAudioFrame()

void AudioChannel::clearInAudioFrame()

Clears the inAudioFrame buffer that is one of the buffers that can be read and written to by C++ and Actionscript.  This function should be called when not writing a full frame as samples from the previous frame may remain and introduce error into calculations that are performed.

clearCorrFrame()

void AudioChannel::clearCorrFrame()

Clears the frames for performing a correlation.

resetChannel

void AudioChannel::resetChannel()

Resets the buffers and flags in the channel.

See Also

DATF.endOfFile

Get Functions

getCentroidVal()

float AudioChannel::getCentroidVal()

Returns the centroid of the audio spectrum from the current frame of data

Returns

centroidValThe current frame’s spectral centroid

getChannelName()

char *AudioChannel::getChannelName()

Retrieves the name given to the AudioChannel object.

Returns

A pointer to the character array containing the channel name.

getFFTSize()

int AudioChannel::getFFTSize()

Returns the size of the FFT used on the current audio frame

Returns

The fftSize used in Fourier analysis.

getHopSize()

int AudioChannel::getHopSize()

Returns the user-specificed hopSize object

Returns

The user’s desired hopSize

getSampleRate()

int AudioChannel::getSampleRate()

Returns the sampling rate of the audio under analysis

Returns

The audio sampling rate

getOutAudioFrameSamples()

int AudioChannel::getOutAudioFrameSamples()

Returns the current number of samples contained in outAudioFrame

Returns

The current number of samples contained in outAudioFrame

Set Functions

setCentroidVal()

void AudioChannel::setCentroidVal(float val)

Sets the value of the spectral centroid for the current frame

Parameters

valA float specifying the centroid value of the current frame

setChannelName()

void AudioChannel::setChannelName(char name[])

Sets the name of the AudioChannel

Parameters

nameA pointer for the character array containing the desired name of the channel

setHopSize()

void AudioChannel::setHopSize(int hop)

Sets the value of the hopSize

Parameters

hopAn int which is the new hopSize

setSampleRate()

int AudioChannel::setSampleRate(int _fs)

Sets the sampling rate of the audio under analysis

Returns

_fsThe audio sample rate

setAudioFrameSamples()

void AudioChannel::setAudioFrameSamples(int numSamples)

Sets the number of samples currently contained inthe inAudioFrame buffer

Parameters

numSamplesan int specifying the number of samples in the buffer

setOutAudioFrameSamples()

void AudioChannel::setOutAudioFrameSamples(int numSamples)

Sets the number of samples currently contained inthe OutAudioFrame buffer

Parameters

numSamplesan int specifying the number of samples in the buffer

setRoom()

void AudioChannel::setRoom(float newRoomLength,
float newRoomWidth,
float newRoomHeight,
float newSourceLength,
float newSourceWidth,
float newSourceHeight,
float newMicLength,
float newMicWidth,
float newMicHeight,
double newEchoStrength)

Sets the parameters to the room impulse response function.  These are saved on each frame and compared to the last frame when reverb is in use.  If the parameters are the same then no RIR will be calculated.

Parameters

newRoomLengthA float that is the new room length.
newRoomWidthA float that is the new room width.
newRoomHeightA float that is the new room height.
newSourceLengthA float that is the new source x location.
newSourceWidthA float that is the new source y location.
newSourceHeightA float that is the new source z location.
newMicLengthA float that is the new mic x location.
newMicWidthA float that is the new mic y location.
newMicHeightA float that is the new mic y location.
newEchoStrengthA double indicating the new echo strength.

See Also

checkRoom, <DATF.reverb>

Flag Management

This is a set of functions to handle toggling various flags that are checked in ALFPackage.cpp in order to avoid duplicate computation.  See Public Members, and Private Members for descriptions of each flag.

clearFlags()

void AudioChannel::clearFlags()

This function clears all of the flags that are used to specify whether a given value has been calculated on the current frame.  See the <Flags> section at the bottom of this page.

getCentroidFlag()

bool AudioChannel::getCentroidFlag()

Returns the centroidFlag to determine if the centroid has been calculated.  True if it has been calcutated.

Returns

The centroid flag for the current frame

getCircularBufferFlag()

bool AudioChannel::getCircularBufferFlag()

Returns the status fo the circularBufferFlag to determine if audio output should be routed through the ouput circular buffer.

Returns

The flag indicating true if the circular buffer is active.

getFFTFlag()

bool AudioChannel::getFFTFlag()

Returns the status off the fftFlag to determine if the FFT has been computed on the current frame.

Returns

The FFT flag for the current frame

getMagFlag()

bool AudioChannel::getMagFlag()

Returns the status off the magflag to determine if the magnitude has been calculated on the current frame

Returns

True if the magnitude spectrum has been calculated for the current frame.

setCentroidFlag()

void AudioChannel::setCentroidFlag(bool flagVal)

Sets the status of the centroid flag

Parameters

flagValTrue if the spectral centroid for the current frame has been calculated.

setCircularBufferFlag()

void AudioChannel::setCircularBufferFlag(bool flagVal)

Sets the status of the circularBuffer flag.  This is necessary to know which buffer to read from.

Parameters

flagValTrue if there is processing in use that needs to use the output circular buffer.

setFFTFlag()

void AudioChannel::setFFTFlag(bool flagVal)

Sets the status of the FFT flag

Parameters

flagValA boolean indicating true if the FFT for the current frame has been computed.

setMagFlag()

void AudioChannel::setMagFlag(bool flagVal)

Sets the status of the magnitude flag

Parameters

flagValA boolean indicating true if the magnitude has been computed for the current frame’s spectrum

Public Members

The AudioChannel class contains many buffers required for storing various aspects of the current audio frame as well as the input/output buffer access so that flash can read/write directly from/to the C namespace.  The following description includes memory and flags.

Audio Buffers

inAudioFrameBefore a frame can be processed, The DATF class writes samples directly to this buffer via a pointer.  The buffer is then used to fill an input Circular Buffer for overlap processing.
inAudioFrameSamplesThis is the number of samples written to the inAudioFrame.  Each frame DATF->setFrame sets the number of samples written to the buffer.
outAudioFrameWhen the ALF requests audioplayback, outAudioFrame is the buffer that provides the output audio samples.  The ALF class reads directly from this buffer via a pointer.  The sample are written directly to this array in ALFPackage.cpp->checkOutputBuffer.
outAudioFrameSamplesALFPackage.cpp->checkOutputBuffer writes samples to the outAudioFrame even if there are not enough samples to play back in ActionScript.  This number keeps track of how many samples are in the frame so that the proper number of samples are written on the next frame.
inBufferAn instance of the CircularBuffer class.  After inAudioFrame has been initialized by DATF.  Methods in ALFPackage copy the data to inBuffer.  This is required since features require overlap, so past samples are needed for future frames.
outBufferAn instance of the CircularBuffer class.  When the circularBufferFlag is set, a method the checkOutputBuffer method in ALF package loads outAudioFrame with samples from this buffer.  This is necessary since filtering operations produce sequences that are larger than what can be stored in outAudioFrame (and played back at once by Flash).

Audio Framing

inAudioFrameSamplesThe number of smples currently contained in inAudioFrame buffer.  This value is set in DATF->setFrame.
outAudioFrameSamplesThe number of samples currently contained in outAudioFrame.  This value is set in ALFPackage.cpp->checkOutputBuffer

Spectral Data

hannCoefficientsAn array containing the the Hann Window coefficients required for a tapered window.  Theses values are multiplied by the samples when filling the fftFrame with the current frame’s samples.
fftFrameAfter inAudioFrame is written to from DATF->setFrame, sample are written to fftFrame from the inBuffer.  The number of samples written to fftFrame will be twice the hopSize.  This occurs in ALFPackage.cpp->setInputBuffer.  Use this as the input to <DSPFunctions.c->realFFT>
fftOutThis buffer is used as the output from <DSPFuncitons.c->realFFT>.
freqDataAn array containing the center frequencies of the bins of the Discrete Fourier Transform.  This is needed for certain spectral feature computation algorithms.  These values are calculated in ALFPackage.cpp->initAudioChannel
magSpectrumAn array that contains the magnitude spectrum computed from the complex spectrum.  This is required for certain feature computation algorithms.
spectrumPrevAn array containing the previous frame’s magnitude spectrum.  This is required for the spectral flux algorithm.

Filtering

filterAn array containing the filter coefficients for use with <ALFPackage.cpp->filter>.
filterLenThe number of coefficients in the filter array.
filterFFTSizeThe size of the FFT needed to perform a frequency domain multiplication of the filter and audio.
filterArrayThis array is used to compute the realFFT on the filter coefficients for frequency domain multiplication with dataArrayOut.
filterArrayOutThis is the output from the realFFT computed on filterArray.
dataArrayAn array that is used as the input to the realFFT computed in <ALFPackage.cpp->filter>.  This is necessary since it may be a different size than fftFrame.
dataArrayOutAn array that is used as the output of the realFFT computed in <ALFPackage.cpp->filter>.

Flags

firstFrameA boolean indicating if the frame being processed is the first frame.  This is necessary becuase we read in 2xhopSize samples on the first frame.  This is used extensively in ALFPackage.cpp
stereoA bool, true if the AudioChannel is one of of a stereo pair.
filterProcessingThis flag is true when filtering is in use.  This is essential since we must know to play a frame with a few samples and padded by zeros on the last frame.  Otherwise, Actionscript will not issue the AUDIO_COMPLETE event.

Other

channelNameA string indicating the specified name of the AudioChannel.
corrDataAn array used in calculating an autocorrelation in DSPFunctions.c->LPC and ALFPackage.cpp->getHarmonics
corrDataOutAn array that is the ouput of the realFFT in the computation of an autocorrelation.

Private Members

Includes variables and flags.

Audio Framing

fsThe sampling rate of the audio under analysis.
hopSizeThe size of the current audio frame set by the user’s desired sampling rate.  Note that spectral analysis is computed on twice the amount of data, that is a frame is twice the size of the hopSize.  Only hopSize unique samples are read in on each frame, except the first where twice as many are read in.
fftSizeThe size of the fft used for computation.  In general, this is not the same as hopSize because it requires powers of 2.  It is calculated as the next power of 2 greater or equal to 2*hopSize.

Flags

fftFlagThis flag is true when the spectrum has been calculated on the current frame.
centroidFlagThis flag is true when the centroid has been calculated on the current frame.
magFlagThis flag is true when the magnitude spectrum has been calculated on the current frame.
circularBufferFlagThis flag is true when the circular output buffer is being used (i.e. audio is being synthesized by reverb).

Other

centroidValHolds the value of the spectral centroid for the current frame;
Reverb ParametersroomLength, roomWidth, roomHeight, sourceLength, sourceWidth, sourceHeight, micLength, micWidth, micHeight, echoStrength.  These parameters are used in computing the RIR (DSPFunctions.c->rir).  The functions <checkRoom> and <setRoom> are used to tell if new room parameters are given and a new RIR should be calculated.
AudioChannel::AudioChannel()
Creates an AudioChannel object with the default parameters.
void AudioChannel::initChannel(int _hopSize,
int _fftSize,
int _fs,
int _lookAheadFrames)
This function is called with the allocation of every DATF (or ALF) object from ActionScript for a desired audio channel (left or right).
void AudioChannel::reInitChannel(int _hopSize,
int _fftSize,
int _fs,
int numCh,
int _lookAheadFrames)
This funciton is called when a channel (L or R) has already been instantiated and initialized and a new song is loaded that requires changes to the buffer sizes.
void AudioChannel::initChannelVocoder(int frameOverlap)
This function initializes a vocoder object for the AudioChanenl when needed for the vocoding function
bool AudioChannel::checkRoom(float newRoomLength,
float newRoomWidth,
float newRoomHeight,
float newSourceLength,
float newSourceWidth,
float newSourceHeight,
float newMicLength,
float newMicWidth,
float newMicHeight,
double newEchoStrength)
This function checks if the parameters entered are the same as the ones stored in the audio channel.
void AudioChannel::clearFFTFrame()
Clears the fftFrame buffer, which has the FFT data.
void AudioChannel::clearInAudioFrame()
Clears the inAudioFrame buffer that is one of the buffers that can be read and written to by C++ and Actionscript.
void AudioChannel::clearCorrFrame()
Clears the frames for performing a correlation.
void AudioChannel::resetChannel()
Resets the buffers and flags in the channel.
float AudioChannel::getCentroidVal()
Returns the centroid of the audio spectrum from the current frame of data
char *AudioChannel::getChannelName()
Retrieves the name given to the AudioChannel object.
int AudioChannel::getFFTSize()
Returns the size of the FFT used on the current audio frame
int AudioChannel::getHopSize()
Returns the user-specificed hopSize object
int AudioChannel::getSampleRate()
Returns the sampling rate of the audio under analysis
int AudioChannel::getOutAudioFrameSamples()
Returns the current number of samples contained in outAudioFrame
void AudioChannel::setCentroidVal(float val)
Sets the value of the spectral centroid for the current frame
void AudioChannel::setChannelName(char name[])
Sets the name of the AudioChannel
void AudioChannel::setHopSize(int hop)
Sets the value of the hopSize
int AudioChannel::setSampleRate(int _fs)
Sets the sampling rate of the audio under analysis
void AudioChannel::setAudioFrameSamples(int numSamples)
Sets the number of samples currently contained inthe inAudioFrame buffer
void AudioChannel::setOutAudioFrameSamples(int numSamples)
Sets the number of samples currently contained inthe OutAudioFrame buffer
void AudioChannel::setRoom(float newRoomLength,
float newRoomWidth,
float newRoomHeight,
float newSourceLength,
float newSourceWidth,
float newSourceHeight,
float newMicLength,
float newMicWidth,
float newMicHeight,
double newEchoStrength)
Sets the parameters to the room impulse response function.
This is the wrapper class that manages communication and interfacing between Actionscript and C++.
void AudioChannel::clearFlags()
This function clears all of the flags that are used to specify whether a given value has been calculated on the current frame.
bool AudioChannel::getCentroidFlag()
Returns the centroidFlag to determine if the centroid has been calculated.
bool AudioChannel::getCircularBufferFlag()
Returns the status fo the circularBufferFlag to determine if audio output should be routed through the ouput circular buffer.
bool AudioChannel::getFFTFlag()
Returns the status off the fftFlag to determine if the FFT has been computed on the current frame.
bool AudioChannel::getMagFlag()
Returns the status off the magflag to determine if the magnitude has been calculated on the current frame
void AudioChannel::setCentroidFlag(bool flagVal)
Sets the status of the centroid flag
void AudioChannel::setCircularBufferFlag(bool flagVal)
Sets the status of the circularBuffer flag.
void AudioChannel::setFFTFlag(bool flagVal)
Sets the status of the FFT flag
void AudioChannel::setMagFlag(bool flagVal)
Sets the status of the magnitude flag
Includes variables and flags.
The AudioChannel class contains many buffers required for storing various aspects of the current audio frame as well as the input/output buffer access so that flash can read/write directly from/to the C namespace.
public function endOfFile():void
This function should be called when a file is complete or after ALF.stopAudio has been called.
public function setFrame(audio:ByteArray,
type:String):int
This function copies the data from a ByteArray into the shared Actionscript/C memory.
AS3_Val checkOutputBuffer(void *self,
AS3_Val args)
This function is called prior to playback in ALF.as.
AS3_Val setInputBuffer(void *self,
AS3_Val args)
This function copies the data from inAudioFrame in AudioChannel.cpp to to the inputBuffer in AudioChannel.cpp.
AS3_Val initAudioChannel(void *self,
AS3_Val args)
This funciton initializes the AudioChannel (either left or right).
int LPC(float *corrData,
int audioLength,
int order,
float *lpCE)
Performs linear predictive analysis on an audio segment for a desired order.
AS3_Val getHarmonics(void *self,
AS3_Val args)
This function finds harmonics present in a frame by using linear prediction.
float* rir(int fs,
float refCo,
float mic[],
float room[],
float src[],
int rirLen[])
Generates a room impulse response for the specified room dimensions, speaker and microphone positions.
Close