Home Class/Enum List File List Compound Members C interface

Playback

In this example, we provide a complete program that demonstrates the use of RtAudio for audio playback. Our program produces a two-channel sawtooth waveform for output.

#include "RtAudio.h"
#include <iostream>
#include <cstdlib>
// Two-channel sawtooth wave generator.
int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData )
{
unsigned int i, j;
double *buffer = (double *) outputBuffer;
double *lastValues = (double *) userData;
if ( status )
std::cout << "Stream underflow detected!" << std::endl;
// Write interleaved audio data.
for ( i=0; i<nBufferFrames; i++ ) {
for ( j=0; j<2; j++ ) {
*buffer++ = lastValues[j];
lastValues[j] += 0.005 * (j+1+(j*0.1));
if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
}
}
return 0;
}
int main()
{
RtAudio dac;
if ( dac.getDeviceCount() < 1 ) {
std::cout << "\nNo audio devices found!\n";
exit( 0 );
}
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
parameters.firstChannel = 0;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256; // 256 sample frames
double data[2] = {0, 0};
try {
dac.openStream( &parameters, NULL, RTAUDIO_FLOAT64,
sampleRate, &bufferFrames, &saw, (void *)&data );
dac.startStream();
}
catch ( RtAudioError& e ) {
e.printMessage();
exit( 0 );
}
char input;
std::cout << "\nPlaying ... press <enter> to quit.\n";
std::cin.get( input );
try {
// Stop the stream
dac.stopStream();
}
catch (RtAudioError& e) {
e.printMessage();
}
if ( dac.isStreamOpen() ) dac.closeStream();
return 0;
}
RtAudio.h
RtAudioStreamStatus
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition RtAudio.h:159
RtAudioError
Exception handling class for RtAudio.
Definition RtAudio.h:220
RtAudioError::printMessage
virtual void printMessage(void) const
Prints thrown error message to stderr.
Definition RtAudio.h:243
RtAudio
Realtime audio i/o C++ classes.
Definition RtAudio.h:280
RtAudio::getDefaultOutputDevice
unsigned int getDefaultOutputDevice(void)
A function that returns the index of the default output device.
Definition RtAudio.h:860
RtAudio::openStream
void openStream(RtAudio::StreamParameters *outputParameters, RtAudio::StreamParameters *inputParameters, RtAudioFormat format, unsigned int sampleRate, unsigned int *bufferFrames, RtAudioCallback callback, void *userData=NULL, RtAudio::StreamOptions *options=NULL, RtAudioErrorCallback errorCallback=NULL)
A public function for opening a stream with the specified parameters.
RtAudio::getDeviceCount
unsigned int getDeviceCount(void)
A public function that queries for the number of audio devices available.
Definition RtAudio.h:857
RtAudio::StreamParameters::nChannels
unsigned int nChannels
Definition RtAudio.h:315
RtAudio::closeStream
void closeStream(void)
A function that closes a stream and frees any associated stream memory.
Definition RtAudio.h:861
RtAudio::isStreamOpen
bool isStreamOpen(void) const
Returns true if a stream is open and false if not.
Definition RtAudio.h:865
RtAudio::StreamParameters::deviceId
unsigned int deviceId
Definition RtAudio.h:314
RtAudio::StreamParameters::firstChannel
unsigned int firstChannel
Definition RtAudio.h:316
RtAudio::startStream
void startStream(void)
A function that starts a stream.
Definition RtAudio.h:862
RtAudio::stopStream
void stopStream(void)
Stop a stream, allowing any samples remaining in the output queue to be played.
Definition RtAudio.h:863
RtAudio::StreamParameters
The structure for specifying input or output stream parameters.
Definition RtAudio.h:313

We open the stream in exactly the same way as the previous example (except with a data format change) and specify the address of our callback function "saw()". The callback function will automatically be invoked when the underlying audio system needs data for output. Note that the callback function is called only when the stream is "running" (between calls to the RtAudio::startStream() and RtAudio::stopStream() functions). We can also pass a pointer value to the RtAudio::openStream() function that is made available in the callback function. In this way, it is possible to gain access to arbitrary data created in our main() function from within the globally defined callback function.

In this example, we stop the stream with an explicit call to RtAudio::stopStream(). It is also possible to stop a stream by returning a non-zero value from the callback function. A return value of 1 will cause the stream to finish draining its internal buffers and then halt (equivalent to calling the RtAudio::stopStream() function). A return value of 2 will cause the stream to stop immediately (equivalent to calling the RtAudio::abortStream() function).

| ©2001-2021 Gary P. Scavone, McGill University. All Rights Reserved. Maintained by Gary P. Scavone.