Realtime audio i/o C++ classes. More...
#include <RtAudio.h>
Classes | |
struct | DeviceInfo |
The public device information structure for returning queried values. More... | |
struct | StreamOptions |
The structure for specifying stream options. More... | |
struct | StreamParameters |
The structure for specifying input or output stream parameters. More... | |
Public Types | |
enum | Api { UNSPECIFIED , MACOSX_CORE , LINUX_ALSA , UNIX_JACK , LINUX_PULSE , LINUX_OSS , WINDOWS_ASIO , WINDOWS_WASAPI , WINDOWS_DS , RTAUDIO_DUMMY , NUM_APIS } |
Audio API specifier arguments. More... | |
Public Member Functions | |
RtAudio (RtAudio::Api api=UNSPECIFIED, RtAudioErrorCallback &&errorCallback=0) | |
The class constructor. | |
~RtAudio () | |
The destructor. | |
RtAudio::Api | getCurrentApi (void) |
Returns the audio API specifier for the current instance of RtAudio. | |
unsigned int | getDeviceCount (void) |
A public function that queries for the number of audio devices available. | |
std::vector< unsigned int > | getDeviceIds (void) |
A public function that returns a vector of audio device IDs. | |
std::vector< std::string > | getDeviceNames (void) |
A public function that returns a vector of audio device names. | |
RtAudio::DeviceInfo | getDeviceInfo (unsigned int deviceId) |
Return an RtAudio::DeviceInfo structure for a specified device ID. | |
unsigned int | getDefaultOutputDevice (void) |
A function that returns the ID of the default output device. | |
unsigned int | getDefaultInputDevice (void) |
A function that returns the ID of the default input device. | |
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. | |
void | closeStream (void) |
A function that closes a stream and frees any associated stream memory. | |
RtAudioErrorType | startStream (void) |
A function that starts a stream. | |
RtAudioErrorType | stopStream (void) |
Stop a stream, allowing any samples remaining in the output queue to be played. | |
RtAudioErrorType | abortStream (void) |
Stop a stream, discarding any samples remaining in the input/output queue. | |
const std::string | getErrorText (void) |
Retrieve the error message corresponding to the last error or warning condition. | |
bool | isStreamOpen (void) const |
Returns true if a stream is open and false if not. | |
bool | isStreamRunning (void) const |
Returns true if the stream is running and false if it is stopped or not open. | |
double | getStreamTime (void) |
Returns the number of seconds of processed data since the stream was started. | |
void | setStreamTime (double time) |
Set the stream time to a time in seconds greater than or equal to 0.0. | |
long | getStreamLatency (void) |
Returns the internal stream latency in sample frames. | |
unsigned int | getStreamSampleRate (void) |
Returns actual sample rate in use by the (open) stream. | |
void | setErrorCallback (RtAudioErrorCallback errorCallback) |
Set a client-defined function that will be invoked when an error or warning occurs. | |
void | showWarnings (bool value=true) |
Specify whether warning messages should be output or not. | |
Static Public Member Functions | |
static std::string | getVersion (void) |
A static function to determine the current RtAudio version. | |
static void | getCompiledApi (std::vector< RtAudio::Api > &apis) |
A static function to determine the available compiled audio APIs. | |
static std::string | getApiName (RtAudio::Api api) |
Return the name of a specified compiled audio API. | |
static std::string | getApiDisplayName (RtAudio::Api api) |
Return the display name of a specified compiled audio API. | |
static RtAudio::Api | getCompiledApiByName (const std::string &name) |
Return the compiled audio API having the given name. | |
static RtAudio::Api | getCompiledApiByDisplayName (const std::string &name) |
Return the compiled audio API having the given display name. | |
Realtime audio i/o C++ classes.
RtAudio provides a common API (Application Programming Interface) for realtime audio input/output across Linux (native ALSA, Jack, and OSS), Macintosh OS X (CoreAudio and Jack), and Windows (DirectSound, ASIO and WASAPI) operating systems.
RtAudio GitHub site: https://github.com/thestk/rtaudio RtAudio WWW site: http://www.music.mcgill.ca/~gary/rtaudio/
RtAudio: realtime audio i/o C++ classes Copyright (c) 2001-2023 Gary P. Scavone
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Any person wishing to distribute modifications to the Software is asked to send the modifications to the original developer so that they can be incorporated into the canonical version. This is, however, not a binding provision of this license.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
struct RtAudio::DeviceInfo |
The public device information structure for returning queried values.
Class Members | ||
---|---|---|
unsigned int | ID {} |
Device ID used to specify a device to RtAudio. |
string | name |
Character string device name. |
unsigned int | outputChannels {} |
Maximum output channels supported by device. |
unsigned int | inputChannels {} |
Maximum input channels supported by device. |
unsigned int | duplexChannels {} |
Maximum simultaneous input/output channels supported by device. |
bool | isDefaultOutput {false} |
true if this is the default output device. |
bool | isDefaultInput {false} |
true if this is the default input device. |
vector< unsigned int > | sampleRates |
Supported sample rates (queried from list of standard rates). |
unsigned int | currentSampleRate {} |
Current sample rate, system sample rate as currently configured. |
unsigned int | preferredSampleRate {} |
Preferred sample rate, e.g. for WASAPI the system sample rate. |
RtAudioFormat | nativeFormats {} |
Bit mask of supported data formats. |
struct RtAudio::StreamOptions |
The structure for specifying stream options.
The following flags can be OR'ed together to allow a client to make changes to the default stream behavior:
By default, RtAudio streams pass and receive audio data from the client in an interleaved format. By passing the RTAUDIO_NONINTERLEAVED flag to the openStream() function, audio data will instead be presented in non-interleaved buffers. In this case, each buffer argument in the RtAudioCallback function will point to a single array of data, with nFrames
samples for each channel concatenated back-to-back. For example, the first sample of data for the second channel would be located at index nFrames
(assuming the buffer
pointer was recast to the correct data type for the stream).
Certain audio APIs offer a number of parameters that influence the I/O latency of a stream. By default, RtAudio will attempt to set these parameters internally for robust (glitch-free) performance (though some APIs, like Windows DirectSound, make this difficult). By passing the RTAUDIO_MINIMIZE_LATENCY flag to the openStream() function, internal stream settings will be influenced in an attempt to minimize stream latency, though possibly at the expense of stream performance.
If the RTAUDIO_HOG_DEVICE flag is set, RtAudio will attempt to open the input and/or output stream device(s) for exclusive use. Note that this is not possible with all supported audio APIs.
If the RTAUDIO_SCHEDULE_REALTIME flag is set, RtAudio will attempt to select realtime scheduling (round-robin) for the callback thread. The priority
parameter will only be used if the RTAUDIO_SCHEDULE_REALTIME flag is set. It defines the thread's realtime priority.
If the RTAUDIO_ALSA_USE_DEFAULT flag is set, RtAudio will attempt to open the "default" PCM device when using the ALSA API. Note that this will override any specified input or output device id.
The numberOfBuffers
parameter can be used to control stream latency in the Windows DirectSound, Linux OSS, and Linux Alsa APIs only. A value of two is usually the smallest allowed. Larger numbers can potentially result in more robust stream performance, though likely at the cost of stream latency. The value set by the user is replaced during execution of the RtAudio::openStream() function by the value actually used by the system.
The streamName
parameter can be used to set the client name when using the Jack API or the application name when using the Pulse API. By default, the Jack client name is set to RtApiJack. However, if you wish to create multiple instances of RtAudio with Jack, each instance must have a unique client name. The default Pulse application name is set to "RtAudio."
Class Members | ||
---|---|---|
RtAudioStreamFlags | flags {} |
A bit-mask of stream flags (RTAUDIO_NONINTERLEAVED, RTAUDIO_MINIMIZE_LATENCY, RTAUDIO_HOG_DEVICE, RTAUDIO_ALSA_USE_DEFAULT). |
unsigned int | numberOfBuffers {} |
Number of stream buffers. |
string | streamName |
A stream name (currently used only in Jack). |
int | priority {} |
Scheduling priority of callback thread (only used with flag RTAUDIO_SCHEDULE_REALTIME). |
struct RtAudio::StreamParameters |
The structure for specifying input or output stream parameters.
Class Members | ||
---|---|---|
unsigned int | deviceId {} |
Device id as provided by getDeviceIds(). |
unsigned int | nChannels {} |
Number of channels. |
unsigned int | firstChannel {} |
First channel index on device (default = 0). |
enum RtAudio::Api |
Audio API specifier arguments.
RtAudio::RtAudio | ( | RtAudio::Api | api = UNSPECIFIED , |
RtAudioErrorCallback && | errorCallback = 0 |
||
) |
The class constructor.
The constructor attempts to create an RtApi instance.
If an API argument is specified but that API has not been compiled, a warning is issued and an instance of an available API is created. If no compiled API is found, the routine will abort (though this should be impossible because RtDummy is the default if no API-specific preprocessor definition is provided to the compiler). If no API argument is specified and multiple API support has been compiled, the default order of use is JACK, ALSA, OSS (Linux systems) and ASIO, DS (Windows systems).
An optional errorCallback function can be specified to subsequently receive warning and error messages.
RtAudio::~RtAudio | ( | ) |
The destructor.
If a stream is running or open, it will be stopped and closed automatically.
|
static |
A static function to determine the available compiled audio APIs.
The values returned in the std::vector can be compared against the enumerated list values. Note that there can be more than one API compiled for certain operating systems.
|
static |
Return the name of a specified compiled audio API.
This obtains a short lower-case name used for identification purposes. This value is guaranteed to remain identical across library versions. If the API is unknown, this function will return the empty string.
|
static |
Return the display name of a specified compiled audio API.
This obtains a long name used for display purposes. If the API is unknown, this function will return the empty string.
|
static |
Return the compiled audio API having the given name.
A case insensitive comparison will check the specified name against the list of compiled APIs, and return the one that matches. On failure, the function returns UNSPECIFIED.
|
static |
Return the compiled audio API having the given display name.
A case sensitive comparison will check the specified display name against the list of compiled APIs, and return the one that matches. On failure, the function returns UNSPECIFIED.
|
inline |
A public function that queries for the number of audio devices available.
This function performs a system query of available devices each time it is called, thus supporting devices (dis)connected after instantiation. If a system error occurs during processing, a warning will be issued.
|
inline |
A public function that returns a vector of audio device IDs.
The ID values returned by this function are used internally by RtAudio to identify a given device. The values themselves are arbitrary and do not correspond to device IDs used by the underlying API (nor are they index values). This function performs a system query of available devices each time it is called, thus supporting devices (dis)connected after instantiation. If no devices are available, the vector size will be zero. If a system error occurs during processing, a warning will be issued.
|
inline |
A public function that returns a vector of audio device names.
This function performs a system query of available devices each time it is called, thus supporting devices (dis)connected after instantiation. If no devices are available, the vector size will be zero. If a system error occurs during processing, a warning will be issued.
|
inline |
Return an RtAudio::DeviceInfo structure for a specified device ID.
Any device ID returned by getDeviceIds() is valid, unless it has been removed between the call to getDevceIds() and this function. If an invalid argument is provided, an RTAUDIO_INVALID_USE will be passed to the user-provided errorCallback function (or otherwise printed to stderr) and all members of the returned RtAudio::DeviceInfo structure will be initialized to default, invalid values (ID = 0, empty name, ...). If the specified device is the current default input or output device, the corresponding "isDefault" member will have a value of "true".
|
inline |
A function that returns the ID of the default output device.
If the underlying audio API does not provide a "default device", the first probed output device ID will be returned. If no devices are available, the return value will be 0 (which is an invalid device identifier).
|
inline |
A function that returns the ID of the default input device.
If the underlying audio API does not provide a "default device", the first probed input device ID will be returned. If no devices are available, the return value will be 0 (which is an invalid device identifier).
RtAudioErrorType RtAudio::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.
An RTAUDIO_SYSTEM_ERROR is returned if a stream cannot be opened with the specified parameters or an error occurs during processing. An RTAUDIO_INVALID_USE is returned if a stream is already open or any invalid stream parameters are specified.
outputParameters | Specifies output stream parameters to use when opening a stream, including a device ID, number of channels, and starting channel number. For input-only streams, this argument should be NULL. The device ID is a value returned by getDeviceIds(). |
inputParameters | Specifies input stream parameters to use when opening a stream, including a device ID, number of channels, and starting channel number. For output-only streams, this argument should be NULL. The device ID is a value returned by getDeviceIds(). |
format | An RtAudioFormat specifying the desired sample data format. |
sampleRate | The desired sample rate (sample frames per second). |
bufferFrames | A pointer to a value indicating the desired internal buffer size in sample frames. The actual value used by the device is returned via the same pointer. A value of zero can be specified, in which case the lowest allowable value is determined. |
callback | A client-defined function that will be invoked when input data is available and/or output data is needed. |
userData | An optional pointer to data that can be accessed from within the callback function. |
options | An optional pointer to a structure containing various global stream options, including a list of OR'ed RtAudioStreamFlags and a suggested number of stream buffers that can be used to control stream latency. More buffers typically result in more robust performance, though at a cost of greater latency. If a value of zero is specified, a system-specific median value is chosen. If the RTAUDIO_MINIMIZE_LATENCY flag bit is set, the lowest allowable value is used. The actual value used is returned via the structure argument. The parameter is API dependent. |
|
inline |
A function that closes a stream and frees any associated stream memory.
If a stream is not open, an RTAUDIO_WARNING will be passed to the user-provided errorCallback function (or otherwise printed to stderr).
|
inline |
A function that starts a stream.
An RTAUDIO_SYSTEM_ERROR is returned if an error occurs during processing. An RTAUDIO_WARNING is returned if a stream is not open or is already running.
|
inline |
Stop a stream, allowing any samples remaining in the output queue to be played.
An RTAUDIO_SYSTEM_ERROR is returned if an error occurs during processing. An RTAUDIO_WARNING is returned if a stream is not open or is already stopped.
|
inline |
Stop a stream, discarding any samples remaining in the input/output queue.
An RTAUDIO_SYSTEM_ERROR is returned if an error occurs during processing. An RTAUDIO_WARNING is returned if a stream is not open or is already stopped.
|
inline |
Retrieve the error message corresponding to the last error or warning condition.
This function can be used to get a detailed error message when a non-zero RtAudioErrorType is returned by a function. This is the same message sent to the user-provided errorCallback function.
|
inline |
Returns the number of seconds of processed data since the stream was started.
The stream time is calculated from the number of sample frames processed by the underlying audio system, which will increment by units of the audio buffer size. It is not an absolute running time. If a stream is not open, the returned value may not be valid.
|
inline |
Returns the internal stream latency in sample frames.
The stream latency refers to delay in audio input and/or output caused by internal buffering by the audio system and/or hardware. For duplex streams, the returned value will represent the sum of the input and output latencies. If a stream is not open, the returned value will be invalid. If the API does not report latency, the return value will be zero.
|
inline |
Returns actual sample rate in use by the (open) stream.
On some systems, the sample rate used may be slightly different than that specified in the stream parameters. If a stream is not open, a value of zero is returned.
|
inline |
Specify whether warning messages should be output or not.
The default behaviour is for warning messages to be output, either to a client-defined error callback function (if specified) or to stderr.
![]() |
©2001-2023 Gary P. Scavone, McGill University. All Rights Reserved. Maintained by Gary P. Scavone. |