Home Class/Enum List File List Compound Members

Device Settings

The next step in using RtAudio is to open a stream with particular device and parameter settings.

#include "RtAudio.h"
int main()
{
RtAudio dac;
if ( dac.getDeviceCount() == 0 ) exit( 0 );
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256; // 256 sample frames
RtAudio::StreamOptions options;
options.flags = RTAUDIO_NONINTERLEAVED;
try {
dac.openStream( &parameters, NULL, RTAUDIO_FLOAT32,
sampleRate, &bufferFrames, &myCallback, NULL, &options );
}
catch ( RtAudioError& e ) {
std::cout << '\n' << e.getMessage() << '\n' << std::endl;
exit( 0 );
}
return 0;
}

The RtAudio::openStream() function attempts to open a stream with a specified set of parameter values. In the above example, we attempt to open a two channel playback stream using the default output device, 32-bit floating point data, a sample rate of 44100 Hz, and a frame rate of 256 sample frames per output buffer. If the user specifies an invalid parameter value (such as a device id greater than or equal to the number of enumerated devices), an RtAudioError is thrown of type = INVALID_USE. If a system error occurs or the device does not support the specified parameter values, an RtAudioError of type = SYSTEM_ERROR is thrown. In either case, a descriptive error message is bundled with the exception and can be queried with the RtAudioError::getMessage() or RtAudioError::what() functions.

RtAudio provides four signed integer and two floating point data formats which can be specified using the RtAudioFormat parameter values mentioned earlier. If the opened device does not natively support the given format, RtAudio will automatically perform the necessary data format conversion.

The `bufferFrames` parameter specifies the desired number of sample frames that will be written to and/or read from a device per write/read operation. This parameter can be used to control stream latency though there is no guarantee that the passed value will be that used by a device. In general, a lower `bufferFrames` value will produce less latency but perhaps less robust performance. A value of zero can be specified, in which case the smallest allowable value will be used. The `bufferFrames` parameter is passed as a pointer and the actual value used by the stream is set during the device setup procedure. `bufferFrames` values should be a power of two. Optimal and allowable buffer values tend to vary between systems and devices. Stream latency can also be controlled via the optional RtAudio::StreamOptions member `numberOfBuffers` (not used in the example above), though this tends to be more system dependent. In particular, the `numberOfBuffers` parameter is ignored when using the OS-X Core Audio, Jack, and the Windows ASIO APIs.

As noted earlier, the device capabilities reported by a driver or underlying audio API are not always accurate and/or may be dependent on a combination of device settings. Because of this, RtAudio does not attempt to query a device's capabilities or use previously reported values when opening a device. Instead, RtAudio simply attempts to set the given parameters on a specified device and then checks whether the setup is successful or not.

The RtAudioCallback parameter above is a pointer to a user-defined function that will be called whenever the audio system is ready for new output data or has new input data to be read. Further details on the use of a callback function are provided in the next section.

Several stream options are available to fine-tune the behavior of an audio stream. In the example above, we specify that data will be written by the user in a non-interleaved format via the RtAudio::StreamOptions member `flags`. That is, all `bufferFrames` of the first channel should be written consecutively, followed by all `bufferFrames` of the second channel. By default (when no option is specified), RtAudio expects data to be written in an interleaved format.

RtAudioError::getMessage
virtual const std::string getMessage(void) const
Returns the thrown error message string.
Definition: RtAudio.h:234
RtAudio::getDeviceCount
unsigned int getDeviceCount(void)
A public function that queries for the number of audio devices available.
Definition: RtAudio.h:830
RtAudioError
Exception handling class for RtAudio.
Definition: RtAudio.h:203
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::getDefaultOutputDevice
unsigned int getDefaultOutputDevice(void)
A function that returns the index of the default output device.
Definition: RtAudio.h:833
RtAudio::StreamParameters::nChannels
unsigned int nChannels
Definition: RtAudio.h:303
RtAudio::StreamOptions::flags
RtAudioStreamFlags flags
Definition: RtAudio.h:369
RtAudio
Realtime audio i/o C++ classes.
Definition: RtAudio.h:263
RtAudio::StreamParameters
The structure for specifying input or ouput stream parameters.
Definition: RtAudio.h:301
RtAudio::StreamParameters::deviceId
unsigned int deviceId
Definition: RtAudio.h:302
RtAudio.h
RtAudio::StreamOptions
The structure for specifying stream options.
Definition: RtAudio.h:368

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