#include <iostream>
#include <cstdlib>
int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
{
unsigned int i, j;
double *buffer = (double *) outputBuffer;
double *lastValues = (double *) userData;
if ( status )
std::cout << "Stream underflow detected!" << std::endl;
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()
{
if ( deviceIds.size() < 1 ) {
std::cout << "\nNo audio devices found!\n";
exit( 0 );
}
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256;
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 );
}
goto cleanup;
}
char input;
std::cout << "\nPlaying ... press <enter> to quit.\n";
std::cin.get( input );
cleanup:
return 0;
}
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition RtAudio.h:178
Realtime audio i/o C++ classes.
Definition RtAudio.h:268
unsigned int getDefaultOutputDevice(void)
A function that returns the ID of the default output device.
Definition RtAudio.h:915
bool isStreamRunning(void) const
Returns true if the stream is running and false if it is stopped or not open.
Definition RtAudio.h:922
RtAudioErrorType stopStream(void)
Stop a stream, allowing any samples remaining in the output queue to be played.
Definition RtAudio.h:918
const std::string getErrorText(void)
Retrieve the error message corresponding to the last error or warning condition.
Definition RtAudio.h:920
unsigned int nChannels
Definition RtAudio.h:305
void closeStream(void)
A function that closes a stream and frees any associated stream memory.
Definition RtAudio.h:916
RtAudioErrorType openStream(RtAudio::StreamParameters *outputParameters, RtAudio::StreamParameters *inputParameters, RtAudioFormat format, unsigned int sampleRate, unsigned int *bufferFrames, RtAudioCallback callback, void *userData=NULL, RtAudio::StreamOptions *options=NULL)
A public function for opening a stream with the specified parameters.
RtAudioErrorType startStream(void)
A function that starts a stream.
Definition RtAudio.h:917
bool isStreamOpen(void) const
Returns true if a stream is open and false if not.
Definition RtAudio.h:921
std::vector< unsigned int > getDeviceIds(void)
A public function that returns a vector of audio device IDs.
Definition RtAudio.h:912
unsigned int deviceId
Definition RtAudio.h:304
unsigned int firstChannel
Definition RtAudio.h:306
The structure for specifying input or output stream parameters.
Definition RtAudio.h:302
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.