/*! \page playback 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. \code #include "RtAudio.h" #include #include // 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= 1.0 ) lastValues[j] -= 2.0; } } return 0; } int main() { RtAudio dac; std::vector deviceIds = dac.getDeviceIds(); if ( deviceIds.size() < 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}; if ( dac.openStream( ¶meters, NULL, RTAUDIO_FLOAT64, sampleRate, &bufferFrames, &saw, (void *)&data ) ) { std::cout << '\n' << dac.getErrorText() << '\n' << std::endl; exit( 0 ); // problem with device settings } // Stream is open ... now start it. if ( dac.startStream() ) { std::cout << dac.getErrorText() << std::endl; goto cleanup; } char input; std::cout << "\nPlaying ... press to quit.\n"; std::cin.get( input ); // Block released ... stop the stream if ( dac.isStreamRunning() ) dac.stopStream(); // or could call dac.abortStream(); cleanup: if ( dac.isStreamOpen() ) dac.closeStream(); return 0; } \endcode 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 \e "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 \e 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). */