:)
Loading...
Searching...
No Matches
RustyAudioEngine Class Reference

Handles all run-time execution of audio code. More...

#include <RustyAudioEngine.hpp>

Public Member Functions

 RustyAudioEngine ()
 Audio Engine constructor - Handles startup.
 
 ~RustyAudioEngine ()
 Audio Engine destructor - Handles proper shutdown.
 
int audioCallback (const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
 Non-static audio callback.
 
bool init ()
 Initialisation function for RustyAudioEngine.
 
template<typename DSPType >
void addProcessor (DSPType *dspUnit)
 Adds a RustyProcessor to signal chain.
 
void addSettings (SettingsIO settings)
 Passes user settings into engine.
 

Static Public Member Functions

static int audioCallbackWrapper (const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
 Static audio callback.
 

Private Member Functions

bool initInputDevice (std::string inputDevice)
 Initialises audio stream to chosen input device.
 
bool initOutputDevice (std::string outputDevice)
 Initialises audio stream to chosen output device.
 
bool terminate ()
 Begins audio engine shutdown process.
 
bool setupStream ()
 Starts streams when all IO preparation is successful.
 

Private Attributes

PaError _err
 PortAudio object to catch PA errors.
 
PaStream * _stream
 PortAudio stream.
 
PaStreamParameters _inputParameters
 PortAudio input parameters.
 
PaStreamParameters _outputParameters
 PortAudio output parameters.
 
bool _streaming
 Keeps track of audio engine streaming state.
 
RustyUserData * _data = nullptr
 Pointer to instance of engine for audioCallback workaround.
 
SettingsIO _settings
 User IO settings object.
 
std::vector< RustyProcessor * > _processingUnits
 A map of all instantiated RustyProcessors in the audio chain.
 

Detailed Description

Handles all run-time execution of audio code.

From managing the signal chain to initialising the IO with device drivers. This class handles all things audio.

Makes use of PortAudio to open IO streams to devices and to create an audioCallback() on a high priority interrupt thread.

RustyAudioEngine cannot be instantiated more than once due to the audioCallback from PortAudio's IO stream being static.

Instantiated inside RustyAudioLibAPI as a private member.

See also
RustyAudioLibAPI
PortAudio

Definition at line 57 of file RustyAudioEngine.hpp.

Constructor & Destructor Documentation

◆ RustyAudioEngine()

RustyAudioEngine::RustyAudioEngine ( )

Audio Engine constructor - Handles startup.

Definition at line 3 of file RustyAudioEngine.cpp.

◆ ~RustyAudioEngine()

RustyAudioEngine::~RustyAudioEngine ( )

Audio Engine destructor - Handles proper shutdown.

Definition at line 8 of file RustyAudioEngine.cpp.

Member Function Documentation

◆ audioCallback()

int RustyAudioEngine::audioCallback ( const void *  inputBuffer,
void *  outputBuffer,
unsigned long  framesPerBuffer,
const PaStreamCallbackTimeInfo *  timeInfo,
PaStreamCallbackFlags  statusFlags,
void *  userData 
)

Non-static audio callback.

Non-static audio callback used to perform any manipulation of the audio signal. DSP units are passed audio samples from this function.

Called by static audioCallbackWrapper().

Parameters
inputBufferPointer to buffer of input audio samples from PaStream.
ouputBufferPointer to buffer of output audio samples to be sent to PaStream.
framesPerBufferFrames refers to number of samples without multiplying by the number of interweaved samples.
timeInfoPointer to information about ADC and DAC capture times - Callback is required to meet it's time deadlines.
statusFlagsPaStatus flags.
userDataRaw pointer to user data.
Returns
Int of PaStreamCallbackResult {continue, complete, abort}
See also
audioCallbackWrapper()

Definition at line 22 of file RustyAudioEngine.cpp.

◆ audioCallbackWrapper()

int RustyAudioEngine::audioCallbackWrapper ( const void *  inputBuffer,
void *  outputBuffer,
unsigned long  framesPerBuffer,
const PaStreamCallbackTimeInfo *  timeInfo,
PaStreamCallbackFlags  statusFlags,
void *  userData 
)
static

Static audio callback.

Static audio callback used as workaround. In current implementation (and seemingly all IO device libraries) the callback function is written in C meaning it is static. This meant I was unable to call members from my non-static RustyAudioEngine class. This wrapper layer allowed for a workaround, by passing a reference to the static function I could essentially trick it into calling non-static member functions. I then use that to call the other audioCallback() function and process audio there.

Parameters
inputBufferPointer to buffer of input audio samples from PaStream.
ouputBufferPointer to buffer of output audio samples to be sent to PaStream.
framesPerBufferFrames refers to number of samples without multiplying by the number of interweaved samples.
timeInfoPointer to information about ADC and DAC capture times - Callback is required to meet it's time deadlines.
statusFlagsPaStatus flags.
userDataRaw pointer to user data.
Returns
Int of PaStreamCallbackResult {continue, complete, abort}
See also
audioCallback()

Definition at line 82 of file RustyAudioEngine.cpp.

◆ init()

bool RustyAudioEngine::init ( )

Initialisation function for RustyAudioEngine.

Init() function used to initialise audio to chosen IO devices. Opening streams to user selected drivers.

Returns
Returns true if init successful.
Exceptions
PortAudioErrorsPortAudio exceptions related to IO streaming and initialisation.

Definition at line 96 of file RustyAudioEngine.cpp.

◆ addProcessor()

template<typename DSPType >
void RustyAudioEngine::addProcessor ( DSPType *  dspUnit)
inline

Adds a RustyProcessor to signal chain.

Allows user to pass in a pointer to any object that inherits from RustyProcessor. Processor will then be added to the signal chain via the _processingUnit vector.

Realised later on that the template wasn't necessary and I could just use RustyProcessor as the argument but I like static_assert macro to check the type so kept this function as is.

Template Parameters
DSPTypeMust be derived from RustyProcessor.
Exceptions
INVALID_DSPObject was not derived from RustyProcessor
See also
RustyProcessor()
Parameters
dspUnitAdds processor to audio engine signal chain

Definition at line 158 of file RustyAudioEngine.hpp.

◆ addSettings()

void RustyAudioEngine::addSettings ( SettingsIO  settings)
inline

Passes user settings into engine.


User decides their IO devices via the setIO() function, that function eventually results in an object of user settings being handed to the engine.

Parameters
settingsSettingsIO object of user IO parameters

Definition at line 176 of file RustyAudioEngine.hpp.

◆ initInputDevice()

bool RustyAudioEngine::initInputDevice ( std::string  inputDevice)
private

Initialises audio stream to chosen input device.


Creates necessary PortAudio objects and opens a stream to the device with name chosen by user.

Parameters
inputDeviceString of users input device
Returns
Returns true if successful

Definition at line 122 of file RustyAudioEngine.cpp.

◆ initOutputDevice()

bool RustyAudioEngine::initOutputDevice ( std::string  outputDevice)
private

Initialises audio stream to chosen output device.


Creates necessary PortAudio objects and opens a stream to the device with name chosen by user.

Parameters
outputDeviceString of users output device
Returns
Returns true if successful

Definition at line 153 of file RustyAudioEngine.cpp.

◆ terminate()

bool RustyAudioEngine::terminate ( )
private

Begins audio engine shutdown process.


Shutdowns down PortAudio streams and deletes any allocated data that needs to be removed.

Returns
Returns true if successful

Definition at line 183 of file RustyAudioEngine.cpp.

◆ setupStream()

bool RustyAudioEngine::setupStream ( )
private

Starts streams when all IO preparation is successful.


Opens stream between users IO devices and audio engine.

Returns
Returns true if successful

Definition at line 205 of file RustyAudioEngine.cpp.

Member Data Documentation

◆ _err

PaError RustyAudioEngine::_err
private

PortAudio object to catch PA errors.

Definition at line 225 of file RustyAudioEngine.hpp.

◆ _stream

PaStream* RustyAudioEngine::_stream
private

PortAudio stream.

Definition at line 226 of file RustyAudioEngine.hpp.

◆ _inputParameters

PaStreamParameters RustyAudioEngine::_inputParameters
private

PortAudio input parameters.

Definition at line 227 of file RustyAudioEngine.hpp.

◆ _outputParameters

PaStreamParameters RustyAudioEngine::_outputParameters
private

PortAudio output parameters.

Definition at line 228 of file RustyAudioEngine.hpp.

◆ _streaming

bool RustyAudioEngine::_streaming
private

Keeps track of audio engine streaming state.

Definition at line 229 of file RustyAudioEngine.hpp.

◆ _data

RustyUserData* RustyAudioEngine::_data = nullptr
private

Pointer to instance of engine for audioCallback workaround.

Definition at line 231 of file RustyAudioEngine.hpp.

◆ _settings

SettingsIO RustyAudioEngine::_settings
private

User IO settings object.

Definition at line 232 of file RustyAudioEngine.hpp.

◆ _processingUnits

std::vector<RustyProcessor*> RustyAudioEngine::_processingUnits
private

A map of all instantiated RustyProcessors in the audio chain.

Definition at line 234 of file RustyAudioEngine.hpp.


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