| 1 | /************************************************************************/ |
| 2 | /*! \class RtAudio |
| 3 | \brief Realtime audio i/o C++ classes. |
| 4 | |
| 5 | RtAudio provides a common API (Application Programming Interface) |
| 6 | for realtime audio input/output across Linux (native ALSA, Jack, |
| 7 | and OSS), Macintosh OS X (CoreAudio and Jack), and Windows |
| 8 | (DirectSound, ASIO and WASAPI) operating systems. |
| 9 | |
| 10 | RtAudio GitHub site: https://github.com/thestk/rtaudio |
| 11 | RtAudio WWW site: http://www.music.mcgill.ca/~gary/rtaudio/ |
| 12 | |
| 13 | RtAudio: realtime audio i/o C++ classes |
| 14 | Copyright (c) 2001-2021 Gary P. Scavone |
| 15 | |
| 16 | Permission is hereby granted, free of charge, to any person |
| 17 | obtaining a copy of this software and associated documentation files |
| 18 | (the "Software"), to deal in the Software without restriction, |
| 19 | including without limitation the rights to use, copy, modify, merge, |
| 20 | publish, distribute, sublicense, and/or sell copies of the Software, |
| 21 | and to permit persons to whom the Software is furnished to do so, |
| 22 | subject to the following conditions: |
| 23 | |
| 24 | The above copyright notice and this permission notice shall be |
| 25 | included in all copies or substantial portions of the Software. |
| 26 | |
| 27 | Any person wishing to distribute modifications to the Software is |
| 28 | asked to send the modifications to the original developer so that |
| 29 | they can be incorporated into the canonical version. This is, |
| 30 | however, not a binding provision of this license. |
| 31 | |
| 32 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 33 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 34 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 35 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 36 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 37 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 38 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 39 | */ |
| 40 | /************************************************************************/ |
| 41 | |
| 42 | /*! |
| 43 | \file RtAudio.h |
| 44 | */ |
| 45 | |
| 46 | #ifndef __RTAUDIO_H |
| 47 | #define __RTAUDIO_H |
| 48 | |
| 49 | #define RTAUDIO_VERSION "6.0.0beta1" |
| 50 | |
| 51 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 52 | #if defined(RTAUDIO_EXPORT) |
| 53 | #define RTAUDIO_DLL_PUBLIC __declspec(dllexport) |
| 54 | #else |
| 55 | #define RTAUDIO_DLL_PUBLIC |
| 56 | #endif |
| 57 | #else |
| 58 | #if __GNUC__ >= 4 |
| 59 | #define RTAUDIO_DLL_PUBLIC __attribute__( (visibility( "default" )) ) |
| 60 | #else |
| 61 | #define RTAUDIO_DLL_PUBLIC |
| 62 | #endif |
| 63 | #endif |
| 64 | |
| 65 | #include <string> |
| 66 | #include <vector> |
| 67 | #include <iostream> |
| 68 | #include <functional> |
| 69 | |
| 70 | /*! \typedef typedef unsigned long RtAudioFormat; |
| 71 | \brief RtAudio data format type. |
| 72 | |
| 73 | Support for signed integers and floats. Audio data fed to/from an |
| 74 | RtAudio stream is assumed to ALWAYS be in host byte order. The |
| 75 | internal routines will automatically take care of any necessary |
| 76 | byte-swapping between the host format and the soundcard. Thus, |
| 77 | endian-ness is not a concern in the following format definitions. |
| 78 | |
| 79 | - \e RTAUDIO_SINT8: 8-bit signed integer. |
| 80 | - \e RTAUDIO_SINT16: 16-bit signed integer. |
| 81 | - \e RTAUDIO_SINT24: 24-bit signed integer. |
| 82 | - \e RTAUDIO_SINT32: 32-bit signed integer. |
| 83 | - \e RTAUDIO_FLOAT32: Normalized between plus/minus 1.0. |
| 84 | - \e RTAUDIO_FLOAT64: Normalized between plus/minus 1.0. |
| 85 | */ |
| 86 | typedef unsigned long RtAudioFormat; |
| 87 | static const RtAudioFormat RTAUDIO_SINT8 = 0x1; // 8-bit signed integer. |
| 88 | static const RtAudioFormat RTAUDIO_SINT16 = 0x2; // 16-bit signed integer. |
| 89 | static const RtAudioFormat RTAUDIO_SINT24 = 0x4; // 24-bit signed integer. |
| 90 | static const RtAudioFormat RTAUDIO_SINT32 = 0x8; // 32-bit signed integer. |
| 91 | static const RtAudioFormat RTAUDIO_FLOAT32 = 0x10; // Normalized between plus/minus 1.0. |
| 92 | static const RtAudioFormat RTAUDIO_FLOAT64 = 0x20; // Normalized between plus/minus 1.0. |
| 93 | |
| 94 | /*! \typedef typedef unsigned long RtAudioStreamFlags; |
| 95 | \brief RtAudio stream option flags. |
| 96 | |
| 97 | The following flags can be OR'ed together to allow a client to |
| 98 | make changes to the default stream behavior: |
| 99 | |
| 100 | - \e RTAUDIO_NONINTERLEAVED: Use non-interleaved buffers (default = interleaved). |
| 101 | - \e RTAUDIO_MINIMIZE_LATENCY: Attempt to set stream parameters for lowest possible latency. |
| 102 | - \e RTAUDIO_HOG_DEVICE: Attempt grab device for exclusive use. |
| 103 | - \e RTAUDIO_ALSA_USE_DEFAULT: Use the "default" PCM device (ALSA only). |
| 104 | - \e RTAUDIO_JACK_DONT_CONNECT: Do not automatically connect ports (JACK only). |
| 105 | |
| 106 | By default, RtAudio streams pass and receive audio data from the |
| 107 | client in an interleaved format. By passing the |
| 108 | RTAUDIO_NONINTERLEAVED flag to the openStream() function, audio |
| 109 | data will instead be presented in non-interleaved buffers. In |
| 110 | this case, each buffer argument in the RtAudioCallback function |
| 111 | will point to a single array of data, with \c nFrames samples for |
| 112 | each channel concatenated back-to-back. For example, the first |
| 113 | sample of data for the second channel would be located at index \c |
| 114 | nFrames (assuming the \c buffer pointer was recast to the correct |
| 115 | data type for the stream). |
| 116 | |
| 117 | Certain audio APIs offer a number of parameters that influence the |
| 118 | I/O latency of a stream. By default, RtAudio will attempt to set |
| 119 | these parameters internally for robust (glitch-free) performance |
| 120 | (though some APIs, like Windows DirectSound, make this difficult). |
| 121 | By passing the RTAUDIO_MINIMIZE_LATENCY flag to the openStream() |
| 122 | function, internal stream settings will be influenced in an attempt |
| 123 | to minimize stream latency, though possibly at the expense of stream |
| 124 | performance. |
| 125 | |
| 126 | If the RTAUDIO_HOG_DEVICE flag is set, RtAudio will attempt to |
| 127 | open the input and/or output stream device(s) for exclusive use. |
| 128 | Note that this is not possible with all supported audio APIs. |
| 129 | |
| 130 | If the RTAUDIO_SCHEDULE_REALTIME flag is set, RtAudio will attempt |
| 131 | to select realtime scheduling (round-robin) for the callback thread. |
| 132 | |
| 133 | If the RTAUDIO_ALSA_USE_DEFAULT flag is set, RtAudio will attempt to |
| 134 | open the "default" PCM device when using the ALSA API. Note that this |
| 135 | will override any specified input or output device id. |
| 136 | |
| 137 | If the RTAUDIO_JACK_DONT_CONNECT flag is set, RtAudio will not attempt |
| 138 | to automatically connect the ports of the client to the audio device. |
| 139 | */ |
| 140 | typedef unsigned int RtAudioStreamFlags; |
| 141 | static const RtAudioStreamFlags RTAUDIO_NONINTERLEAVED = 0x1; // Use non-interleaved buffers (default = interleaved). |
| 142 | static const RtAudioStreamFlags RTAUDIO_MINIMIZE_LATENCY = 0x2; // Attempt to set stream parameters for lowest possible latency. |
| 143 | static const RtAudioStreamFlags RTAUDIO_HOG_DEVICE = 0x4; // Attempt grab device and prevent use by others. |
| 144 | static const RtAudioStreamFlags RTAUDIO_SCHEDULE_REALTIME = 0x8; // Try to select realtime scheduling for callback thread. |
| 145 | static const RtAudioStreamFlags RTAUDIO_ALSA_USE_DEFAULT = 0x10; // Use the "default" PCM device (ALSA only). |
| 146 | static const RtAudioStreamFlags RTAUDIO_JACK_DONT_CONNECT = 0x20; // Do not automatically connect ports (JACK only). |
| 147 | |
| 148 | /*! \typedef typedef unsigned long RtAudioStreamStatus; |
| 149 | \brief RtAudio stream status (over- or underflow) flags. |
| 150 | |
| 151 | Notification of a stream over- or underflow is indicated by a |
| 152 | non-zero stream \c status argument in the RtAudioCallback function. |
| 153 | The stream status can be one of the following two options, |
| 154 | depending on whether the stream is open for output and/or input: |
| 155 | |
| 156 | - \e RTAUDIO_INPUT_OVERFLOW: Input data was discarded because of an overflow condition at the driver. |
| 157 | - \e RTAUDIO_OUTPUT_UNDERFLOW: The output buffer ran low, likely producing a break in the output sound. |
| 158 | */ |
| 159 | typedef unsigned int RtAudioStreamStatus; |
| 160 | static const RtAudioStreamStatus RTAUDIO_INPUT_OVERFLOW = 0x1; // Input data was discarded because of an overflow condition at the driver. |
| 161 | static const RtAudioStreamStatus RTAUDIO_OUTPUT_UNDERFLOW = 0x2; // The output buffer ran low, likely causing a gap in the output sound. |
| 162 | |
| 163 | //! RtAudio callback function prototype. |
| 164 | /*! |
| 165 | All RtAudio clients must create a function of type RtAudioCallback |
| 166 | to read and/or write data from/to the audio stream. When the |
| 167 | underlying audio system is ready for new input or output data, this |
| 168 | function will be invoked. |
| 169 | |
| 170 | \param outputBuffer For output (or duplex) streams, the client |
| 171 | should write \c nFrames of audio sample frames into this |
| 172 | buffer. This argument should be recast to the datatype |
| 173 | specified when the stream was opened. For input-only |
| 174 | streams, this argument will be NULL. |
| 175 | |
| 176 | \param inputBuffer For input (or duplex) streams, this buffer will |
| 177 | hold \c nFrames of input audio sample frames. This |
| 178 | argument should be recast to the datatype specified when the |
| 179 | stream was opened. For output-only streams, this argument |
| 180 | will be NULL. |
| 181 | |
| 182 | \param nFrames The number of sample frames of input or output |
| 183 | data in the buffers. The actual buffer size in bytes is |
| 184 | dependent on the data type and number of channels in use. |
| 185 | |
| 186 | \param streamTime The number of seconds that have elapsed since the |
| 187 | stream was started. |
| 188 | |
| 189 | \param status If non-zero, this argument indicates a data overflow |
| 190 | or underflow condition for the stream. The particular |
| 191 | condition can be determined by comparison with the |
| 192 | RtAudioStreamStatus flags. |
| 193 | |
| 194 | \param userData A pointer to optional data provided by the client |
| 195 | when opening the stream (default = NULL). |
| 196 | |
| 197 | \return |
| 198 | To continue normal stream operation, the RtAudioCallback function |
| 199 | should return a value of zero. To stop the stream and drain the |
| 200 | output buffer, the function should return a value of one. To abort |
| 201 | the stream immediately, the client should return a value of two. |
| 202 | */ |
| 203 | typedef int (*RtAudioCallback)( void *outputBuffer, void *inputBuffer, |
| 204 | unsigned int nFrames, |
| 205 | double streamTime, |
| 206 | RtAudioStreamStatus status, |
| 207 | void *userData ); |
| 208 | |
| 209 | enum RtAudioErrorType { |
| 210 | RTAUDIO_NO_ERROR, /*!< No error. */ |
| 211 | RTAUDIO_WARNING, /*!< A non-critical error. */ |
| 212 | RTAUDIO_UNKNOWN_ERROR, /*!< An unspecified error type. */ |
| 213 | RTAUDIO_NO_DEVICES_FOUND, /*!< No devices found on system. */ |
| 214 | RTAUDIO_INVALID_DEVICE, /*!< An invalid device ID was specified. */ |
| 215 | RTAUDIO_DEVICE_DISCONNECT, /*!< A device in use was disconnected. */ |
| 216 | RTAUDIO_MEMORY_ERROR, /*!< An error occurred during memory allocation. */ |
| 217 | RTAUDIO_INVALID_PARAMETER, /*!< An invalid parameter was specified to a function. */ |
| 218 | RTAUDIO_INVALID_USE, /*!< The function was called incorrectly. */ |
| 219 | RTAUDIO_DRIVER_ERROR, /*!< A system driver error occurred. */ |
| 220 | RTAUDIO_SYSTEM_ERROR, /*!< A system error occurred. */ |
| 221 | RTAUDIO_THREAD_ERROR /*!< A thread error occurred. */ |
| 222 | }; |
| 223 | |
| 224 | //! RtAudio error callback function prototype. |
| 225 | /*! |
| 226 | \param type Type of error. |
| 227 | \param errorText Error description. |
| 228 | */ |
| 229 | |
| 230 | typedef void (* RtAudioErrorCallback)(RtAudioErrorType type, const std::string &errorText); |
| 231 | // typedef std::function<void(RtAudioErrorType type, const std::string &errorText )> RtAudioErrorCallback; |
| 232 | |
| 233 | // **************************************************************** // |
| 234 | // |
| 235 | // RtAudio class declaration. |
| 236 | // |
| 237 | // RtAudio is a "controller" used to select an available audio i/o |
| 238 | // interface. It presents a common API for the user to call but all |
| 239 | // functionality is implemented by the class RtApi and its |
| 240 | // subclasses. RtAudio creates an instance of an RtApi subclass |
| 241 | // based on the user's API choice. If no choice is made, RtAudio |
| 242 | // attempts to make a "logical" API selection. |
| 243 | // |
| 244 | // **************************************************************** // |
| 245 | |
| 246 | class RtApi; |
| 247 | |
| 248 | class RTAUDIO_DLL_PUBLIC RtAudio |
| 249 | { |
| 250 | public: |
| 251 | |
| 252 | //! Audio API specifier arguments. |
| 253 | enum Api { |
| 254 | UNSPECIFIED, /*!< Search for a working compiled API. */ |
| 255 | LINUX_ALSA, /*!< The Advanced Linux Sound Architecture API. */ |
| 256 | LINUX_PULSE, /*!< The Linux PulseAudio API. */ |
| 257 | LINUX_OSS, /*!< The Linux Open Sound System API. */ |
| 258 | UNIX_JACK, /*!< The Jack Low-Latency Audio Server API. */ |
| 259 | MACOSX_CORE, /*!< Macintosh OS-X Core Audio API. */ |
| 260 | WINDOWS_WASAPI, /*!< The Microsoft WASAPI API. */ |
| 261 | WINDOWS_ASIO, /*!< The Steinberg Audio Stream I/O API. */ |
| 262 | WINDOWS_DS, /*!< The Microsoft DirectSound API. */ |
| 263 | RTAUDIO_DUMMY, /*!< A compilable but non-functional API. */ |
| 264 | NUM_APIS /*!< Number of values in this enum. */ |
| 265 | }; |
| 266 | |
| 267 | //! The public device information structure for returning queried values. |
| 268 | struct DeviceInfo { |
| 269 | bool probed; /*!< true if the device capabilities were successfully probed. */ |
| 270 | std::string name; /*!< Character string device identifier. */ |
| 271 | unsigned int outputChannels; /*!< Maximum output channels supported by device. */ |
| 272 | unsigned int inputChannels; /*!< Maximum input channels supported by device. */ |
| 273 | unsigned int duplexChannels; /*!< Maximum simultaneous input/output channels supported by device. */ |
| 274 | bool isDefaultOutput; /*!< true if this is the default output device. */ |
| 275 | bool isDefaultInput; /*!< true if this is the default input device. */ |
| 276 | std::vector<unsigned int> sampleRates; /*!< Supported sample rates (queried from list of standard rates). */ |
| 277 | unsigned int currentSampleRate; /*!< Current sample rate, system sample rate as currently configured. */ |
| 278 | unsigned int preferredSampleRate; /*!< Preferred sample rate, e.g. for WASAPI the system sample rate. */ |
| 279 | RtAudioFormat nativeFormats; /*!< Bit mask of supported data formats. */ |
| 280 | |
| 281 | DeviceInfo() |
| 282 | : probed(false), name(""), outputChannels(0), inputChannels(0), |
| 283 | duplexChannels(0), isDefaultOutput(false), isDefaultInput(false), |
| 284 | currentSampleRate(0), preferredSampleRate(0) |
| 285 | { } |
| 286 | }; |
| 287 | |
| 288 | //! The structure for specifying input or output stream parameters. |
| 289 | struct StreamParameters { |
| 290 | unsigned int deviceId; /*!< Device index (0 to getDeviceCount() - 1). */ |
| 291 | unsigned int nChannels; /*!< Number of channels. */ |
| 292 | unsigned int firstChannel; /*!< First channel index on device (default = 0). */ |
| 293 | |
| 294 | StreamParameters() |
| 295 | : deviceId(0), nChannels(0), firstChannel(0) |
| 296 | { } |
| 297 | }; |
| 298 | |
| 299 | //! The structure for specifying stream options. |
| 300 | /*! |
| 301 | The following flags can be OR'ed together to allow a client to |
| 302 | make changes to the default stream behavior: |
| 303 | |
| 304 | - \e RTAUDIO_NONINTERLEAVED: Use non-interleaved buffers (default = interleaved). |
| 305 | - \e RTAUDIO_MINIMIZE_LATENCY: Attempt to set stream parameters for lowest possible latency. |
| 306 | - \e RTAUDIO_HOG_DEVICE: Attempt grab device for exclusive use. |
| 307 | - \e RTAUDIO_SCHEDULE_REALTIME: Attempt to select realtime scheduling for callback thread. |
| 308 | - \e RTAUDIO_ALSA_USE_DEFAULT: Use the "default" PCM device (ALSA only). |
| 309 | |
| 310 | By default, RtAudio streams pass and receive audio data from the |
| 311 | client in an interleaved format. By passing the |
| 312 | RTAUDIO_NONINTERLEAVED flag to the openStream() function, audio |
| 313 | data will instead be presented in non-interleaved buffers. In |
| 314 | this case, each buffer argument in the RtAudioCallback function |
| 315 | will point to a single array of data, with \c nFrames samples for |
| 316 | each channel concatenated back-to-back. For example, the first |
| 317 | sample of data for the second channel would be located at index \c |
| 318 | nFrames (assuming the \c buffer pointer was recast to the correct |
| 319 | data type for the stream). |
| 320 | |
| 321 | Certain audio APIs offer a number of parameters that influence the |
| 322 | I/O latency of a stream. By default, RtAudio will attempt to set |
| 323 | these parameters internally for robust (glitch-free) performance |
| 324 | (though some APIs, like Windows DirectSound, make this difficult). |
| 325 | By passing the RTAUDIO_MINIMIZE_LATENCY flag to the openStream() |
| 326 | function, internal stream settings will be influenced in an attempt |
| 327 | to minimize stream latency, though possibly at the expense of stream |
| 328 | performance. |
| 329 | |
| 330 | If the RTAUDIO_HOG_DEVICE flag is set, RtAudio will attempt to |
| 331 | open the input and/or output stream device(s) for exclusive use. |
| 332 | Note that this is not possible with all supported audio APIs. |
| 333 | |
| 334 | If the RTAUDIO_SCHEDULE_REALTIME flag is set, RtAudio will attempt |
| 335 | to select realtime scheduling (round-robin) for the callback thread. |
| 336 | The \c priority parameter will only be used if the RTAUDIO_SCHEDULE_REALTIME |
| 337 | flag is set. It defines the thread's realtime priority. |
| 338 | |
| 339 | If the RTAUDIO_ALSA_USE_DEFAULT flag is set, RtAudio will attempt to |
| 340 | open the "default" PCM device when using the ALSA API. Note that this |
| 341 | will override any specified input or output device id. |
| 342 | |
| 343 | The \c numberOfBuffers parameter can be used to control stream |
| 344 | latency in the Windows DirectSound, Linux OSS, and Linux Alsa APIs |
| 345 | only. A value of two is usually the smallest allowed. Larger |
| 346 | numbers can potentially result in more robust stream performance, |
| 347 | though likely at the cost of stream latency. The value set by the |
| 348 | user is replaced during execution of the RtAudio::openStream() |
| 349 | function by the value actually used by the system. |
| 350 | |
| 351 | The \c streamName parameter can be used to set the client name |
| 352 | when using the Jack API. By default, the client name is set to |
| 353 | RtApiJack. However, if you wish to create multiple instances of |
| 354 | RtAudio with Jack, each instance must have a unique client name. |
| 355 | */ |
| 356 | struct StreamOptions { |
| 357 | RtAudioStreamFlags flags; /*!< A bit-mask of stream flags (RTAUDIO_NONINTERLEAVED, RTAUDIO_MINIMIZE_LATENCY, RTAUDIO_HOG_DEVICE, RTAUDIO_ALSA_USE_DEFAULT). */ |
| 358 | unsigned int numberOfBuffers; /*!< Number of stream buffers. */ |
| 359 | std::string streamName; /*!< A stream name (currently used only in Jack). */ |
| 360 | int priority; /*!< Scheduling priority of callback thread (only used with flag RTAUDIO_SCHEDULE_REALTIME). */ |
| 361 | |
| 362 | StreamOptions() |
| 363 | : flags(0), numberOfBuffers(0), priority(0) |
| 364 | { } |
| 365 | }; |
| 366 | |
| 367 | //! A static function to determine the current RtAudio version. |
| 368 | static std::string getVersion( void ); |
| 369 | |
| 370 | //! A static function to determine the available compiled audio APIs. |
| 371 | /*! |
| 372 | The values returned in the std::vector can be compared against |
| 373 | the enumerated list values. Note that there can be more than one |
| 374 | API compiled for certain operating systems. |
| 375 | */ |
| 376 | static void getCompiledApi( std::vector<RtAudio::Api> &apis ); |
| 377 | |
| 378 | //! Return the name of a specified compiled audio API. |
| 379 | /*! |
| 380 | This obtains a short lower-case name used for identification purposes. |
| 381 | This value is guaranteed to remain identical across library versions. |
| 382 | If the API is unknown, this function will return the empty string. |
| 383 | */ |
| 384 | static std::string getApiName( RtAudio::Api api ); |
| 385 | |
| 386 | //! Return the display name of a specified compiled audio API. |
| 387 | /*! |
| 388 | This obtains a long name used for display purposes. |
| 389 | If the API is unknown, this function will return the empty string. |
| 390 | */ |
| 391 | static std::string getApiDisplayName( RtAudio::Api api ); |
| 392 | |
| 393 | //! Return the compiled audio API having the given name. |
| 394 | /*! |
| 395 | A case insensitive comparison will check the specified name |
| 396 | against the list of compiled APIs, and return the one which |
| 397 | matches. On failure, the function returns UNSPECIFIED. |
| 398 | */ |
| 399 | static RtAudio::Api getCompiledApiByName( const std::string &name ); |
| 400 | |
| 401 | //! The class constructor. |
| 402 | /*! |
| 403 | The constructor attempts to create an RtApi instance. |
| 404 | |
| 405 | If an API argument is specified but that API has not been |
| 406 | compiled, a warning is issued and an instance of an available API |
| 407 | is created. If no compiled API is found, the routine will abort |
| 408 | (though this should be impossible because RtDummy is the default |
| 409 | if no API-specific preprocessor definition is provided to the |
| 410 | compiler). If no API argument is specified and multiple API |
| 411 | support has been compiled, the default order of use is JACK, ALSA, |
| 412 | OSS (Linux systems) and ASIO, DS (Windows systems). |
| 413 | |
| 414 | An optional errorCallback function can be specified to |
| 415 | subsequently receive warning and error messages. |
| 416 | */ |
| 417 | RtAudio( RtAudio::Api api=UNSPECIFIED, RtAudioErrorCallback /* && */ errorCallback = 0 ); |
| 418 | |
| 419 | //! The destructor. |
| 420 | /*! |
| 421 | If a stream is running or open, it will be stopped and closed |
| 422 | automatically. |
| 423 | */ |
| 424 | ~RtAudio(); |
| 425 | |
| 426 | //! Returns the audio API specifier for the current instance of RtAudio. |
| 427 | RtAudio::Api getCurrentApi( void ); |
| 428 | |
| 429 | //! A public function that queries for the number of audio devices available. |
| 430 | /*! |
| 431 | This function performs a system query of available devices each time it |
| 432 | is called, thus supporting devices connected \e after instantiation. If |
| 433 | a system error occurs during processing, a warning will be issued. |
| 434 | */ |
| 435 | unsigned int getDeviceCount( void ); |
| 436 | |
| 437 | //! Return an RtAudio::DeviceInfo structure for a specified device number. |
| 438 | /*! |
| 439 | Any device integer between 0 and getDeviceCount() - 1 is valid. |
| 440 | If an invalid argument is provided, an RTAUDIO_INVALID_USE |
| 441 | will be passed to the user-provided errorCallback function (or |
| 442 | otherwise printed to stderr), the structure member "probed" will |
| 443 | have a value of "false" and all other members will be undefined. |
| 444 | If a device is busy or otherwise unavailable, the structure member |
| 445 | "probed" will have a value of "false" and all other members will |
| 446 | be undefined. If the specified device is the current default |
| 447 | input or output device, the corresponding "isDefault" member will |
| 448 | have a value of "true". |
| 449 | */ |
| 450 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ); |
| 451 | |
| 452 | //! A function that returns the index of the default output device. |
| 453 | /*! |
| 454 | If the underlying audio API does not provide a "default |
| 455 | device", or if no devices are available, the return value will be |
| 456 | 0. Note that this is a valid device identifier and it is the |
| 457 | client's responsibility to verify that a device is available |
| 458 | before attempting to open a stream. |
| 459 | */ |
| 460 | unsigned int getDefaultOutputDevice( void ); |
| 461 | |
| 462 | //! A function that returns the index of the default input device. |
| 463 | /*! |
| 464 | If the underlying audio API does not provide a "default |
| 465 | device", or if no devices are available, the return value will be |
| 466 | 0. Note that this is a valid device identifier and it is the |
| 467 | client's responsibility to verify that a device is available |
| 468 | before attempting to open a stream. |
| 469 | */ |
| 470 | unsigned int getDefaultInputDevice( void ); |
| 471 | |
| 472 | //! A public function for opening a stream with the specified parameters. |
| 473 | /*! |
| 474 | An RTAUDIO_SYSTEM_ERROR is returned if a stream cannot be |
| 475 | opened with the specified parameters or an error occurs during |
| 476 | processing. An RTAUDIO_INVALID_USE is returned if a stream |
| 477 | is already open or any invalid stream parameters are specified. |
| 478 | |
| 479 | \param outputParameters Specifies output stream parameters to use |
| 480 | when opening a stream, including a device ID, number of channels, |
| 481 | and starting channel number. For input-only streams, this |
| 482 | argument should be NULL. The device ID is an index value between |
| 483 | 0 and getDeviceCount() - 1. |
| 484 | \param inputParameters Specifies input stream parameters to use |
| 485 | when opening a stream, including a device ID, number of channels, |
| 486 | and starting channel number. For output-only streams, this |
| 487 | argument should be NULL. The device ID is an index value between |
| 488 | 0 and getDeviceCount() - 1. |
| 489 | \param format An RtAudioFormat specifying the desired sample data format. |
| 490 | \param sampleRate The desired sample rate (sample frames per second). |
| 491 | \param bufferFrames A pointer to a value indicating the desired |
| 492 | internal buffer size in sample frames. The actual value |
| 493 | used by the device is returned via the same pointer. A |
| 494 | value of zero can be specified, in which case the lowest |
| 495 | allowable value is determined. |
| 496 | \param callback A client-defined function that will be invoked |
| 497 | when input data is available and/or output data is needed. |
| 498 | \param userData An optional pointer to data that can be accessed |
| 499 | from within the callback function. |
| 500 | \param options An optional pointer to a structure containing various |
| 501 | global stream options, including a list of OR'ed RtAudioStreamFlags |
| 502 | and a suggested number of stream buffers that can be used to |
| 503 | control stream latency. More buffers typically result in more |
| 504 | robust performance, though at a cost of greater latency. If a |
| 505 | value of zero is specified, a system-specific median value is |
| 506 | chosen. If the RTAUDIO_MINIMIZE_LATENCY flag bit is set, the |
| 507 | lowest allowable value is used. The actual value used is |
| 508 | returned via the structure argument. The parameter is API dependent. |
| 509 | */ |
| 510 | RtAudioErrorType openStream( RtAudio::StreamParameters *outputParameters, |
| 511 | RtAudio::StreamParameters *inputParameters, |
| 512 | RtAudioFormat format, unsigned int sampleRate, |
| 513 | unsigned int *bufferFrames, RtAudioCallback callback, |
| 514 | void *userData = NULL, RtAudio::StreamOptions *options = NULL ); |
| 515 | |
| 516 | //! A function that closes a stream and frees any associated stream memory. |
| 517 | /*! |
| 518 | If a stream is not open, an RTAUDIO_WARNING will be passed to the |
| 519 | user-provided errorCallback function (or otherwise printed to |
| 520 | stderr). |
| 521 | */ |
| 522 | void closeStream( void ); |
| 523 | |
| 524 | //! A function that starts a stream. |
| 525 | /*! |
| 526 | An RTAUDIO_SYSTEM_ERROR is returned if an error occurs during |
| 527 | processing. An RTAUDIO_WARNING is returned if a stream is not open |
| 528 | or is already running. |
| 529 | */ |
| 530 | RtAudioErrorType startStream( void ); |
| 531 | |
| 532 | //! Stop a stream, allowing any samples remaining in the output queue to be played. |
| 533 | /*! |
| 534 | An RTAUDIO_SYSTEM_ERROR is returned if an error occurs during |
| 535 | processing. An RTAUDIO_WARNING is returned if a stream is not |
| 536 | open or is already stopped. |
| 537 | */ |
| 538 | RtAudioErrorType stopStream( void ); |
| 539 | |
| 540 | //! Stop a stream, discarding any samples remaining in the input/output queue. |
| 541 | /*! |
| 542 | An RTAUDIO_SYSTEM_ERROR is returned if an error occurs during |
| 543 | processing. An RTAUDIO_WARNING is returned if a stream is not |
| 544 | open or is already stopped. |
| 545 | */ |
| 546 | RtAudioErrorType abortStream( void ); |
| 547 | |
| 548 | //! Returns true if a stream is open and false if not. |
| 549 | bool isStreamOpen( void ) const; |
| 550 | |
| 551 | //! Returns true if the stream is running and false if it is stopped or not open. |
| 552 | bool isStreamRunning( void ) const; |
| 553 | |
| 554 | //! Returns the number of seconds of processed data since the stream was started. |
| 555 | /*! |
| 556 | The stream time is calculated from the number of sample frames |
| 557 | processed by the underlying audio system, which will increment by |
| 558 | units of the audio buffer size. It is not an absolute running |
| 559 | time. If a stream is not open, the returned value may not be |
| 560 | valid. |
| 561 | */ |
| 562 | double getStreamTime( void ); |
| 563 | |
| 564 | //! Set the stream time to a time in seconds greater than or equal to 0.0. |
| 565 | void setStreamTime( double time ); |
| 566 | |
| 567 | //! Returns the internal stream latency in sample frames. |
| 568 | /*! |
| 569 | The stream latency refers to delay in audio input and/or output |
| 570 | caused by internal buffering by the audio system and/or hardware. |
| 571 | For duplex streams, the returned value will represent the sum of |
| 572 | the input and output latencies. If a stream is not open, the |
| 573 | returned value will be invalid. If the API does not report |
| 574 | latency, the return value will be zero. |
| 575 | */ |
| 576 | long getStreamLatency( void ); |
| 577 | |
| 578 | //! Returns actual sample rate in use by the (open) stream. |
| 579 | /*! |
| 580 | On some systems, the sample rate used may be slightly different |
| 581 | than that specified in the stream parameters. If a stream is not |
| 582 | open, a value of zero is returned. |
| 583 | */ |
| 584 | unsigned int getStreamSampleRate( void ); |
| 585 | |
| 586 | //! Set a client-defined function that will be invoked when an error or warning occurs. |
| 587 | void setErrorCallback( RtAudioErrorCallback errorCallback ); |
| 588 | |
| 589 | //! Specify whether warning messages should be output or not. |
| 590 | /*! |
| 591 | The default behaviour is for warning messages to be output, |
| 592 | either to a client-defined error callback function (if specified) |
| 593 | or to stderr. |
| 594 | */ |
| 595 | void showWarnings( bool value = true ); |
| 596 | |
| 597 | protected: |
| 598 | |
| 599 | void openRtApi( RtAudio::Api api ); |
| 600 | RtApi *rtapi_; |
| 601 | }; |
| 602 | |
| 603 | // Operating system dependent thread functionality. |
| 604 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 605 | |
| 606 | #ifndef NOMINMAX |
| 607 | #define NOMINMAX |
| 608 | #endif |
| 609 | #include <windows.h> |
| 610 | #include <process.h> |
| 611 | #include <stdint.h> |
| 612 | |
| 613 | typedef uintptr_t ThreadHandle; |
| 614 | typedef CRITICAL_SECTION StreamMutex; |
| 615 | |
| 616 | #else |
| 617 | |
| 618 | // Using pthread library for various flavors of unix. |
| 619 | #include <pthread.h> |
| 620 | |
| 621 | typedef pthread_t ThreadHandle; |
| 622 | typedef pthread_mutex_t StreamMutex; |
| 623 | |
| 624 | #endif |
| 625 | |
| 626 | // Setup for "dummy" behavior if no apis specified. |
| 627 | #if !(defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_WASAPI__) \ |
| 628 | || defined(__LINUX_ALSA__) || defined(__LINUX_PULSE__) || defined(__UNIX_JACK__) \ |
| 629 | || defined(__LINUX_OSS__) || defined(__MACOSX_CORE__)) |
| 630 | |
| 631 | #define __RTAUDIO_DUMMY__ |
| 632 | |
| 633 | #endif |
| 634 | |
| 635 | // This global structure type is used to pass callback information |
| 636 | // between the private RtAudio stream structure and global callback |
| 637 | // handling functions. |
| 638 | struct CallbackInfo { |
| 639 | void *object; // Used as a "this" pointer. |
| 640 | ThreadHandle thread; |
| 641 | void *callback; |
| 642 | void *userData; |
| 643 | void *apiInfo; // void pointer for API specific callback information |
| 644 | bool isRunning; |
| 645 | bool doRealtime; |
| 646 | int priority; |
| 647 | bool deviceDisconnected; |
| 648 | |
| 649 | CallbackInfo() |
| 650 | : object(NULL), thread(0), callback(NULL), userData(NULL), |
| 651 | apiInfo(NULL), isRunning(false), doRealtime(false), priority(0), |
| 652 | deviceDisconnected(false) |
| 653 | { } |
| 654 | }; |
| 655 | |
| 656 | // **************************************************************** // |
| 657 | // |
| 658 | // RtApi class declaration. |
| 659 | // |
| 660 | // Subclasses of RtApi contain all API- and OS-specific code necessary |
| 661 | // to fully implement the RtAudio API. |
| 662 | // |
| 663 | // Note that RtApi is an abstract base class and cannot be |
| 664 | // explicitly instantiated. The class RtAudio will create an |
| 665 | // instance of an RtApi subclass (RtApiOss, RtApiAlsa, |
| 666 | // RtApiJack, RtApiCore, RtApiDs, or RtApiAsio). |
| 667 | // |
| 668 | // **************************************************************** // |
| 669 | |
| 670 | #pragma pack(push, 1) |
| 671 | class S24 { |
| 672 | |
| 673 | protected: |
| 674 | unsigned char c3[3]; |
| 675 | |
| 676 | public: |
| 677 | S24() {} |
| 678 | |
| 679 | S24& operator = ( const int& i ) { |
| 680 | c3[0] = (unsigned char)(i & 0x000000ff); |
| 681 | c3[1] = (unsigned char)((i & 0x0000ff00) >> 8); |
| 682 | c3[2] = (unsigned char)((i & 0x00ff0000) >> 16); |
| 683 | return *this; |
| 684 | } |
| 685 | |
| 686 | S24( const double& d ) { *this = (int) d; } |
| 687 | S24( const float& f ) { *this = (int) f; } |
| 688 | S24( const signed short& s ) { *this = (int) s; } |
| 689 | S24( const char& c ) { *this = (int) c; } |
| 690 | |
| 691 | int asInt() { |
| 692 | int i = c3[0] | (c3[1] << 8) | (c3[2] << 16); |
| 693 | if (i & 0x800000) i |= ~0xffffff; |
| 694 | return i; |
| 695 | } |
| 696 | }; |
| 697 | #pragma pack(pop) |
| 698 | |
| 699 | #if defined( HAVE_GETTIMEOFDAY ) |
| 700 | #include <sys/time.h> |
| 701 | #endif |
| 702 | |
| 703 | #include <sstream> |
| 704 | |
| 705 | class RTAUDIO_DLL_PUBLIC RtApi |
| 706 | { |
| 707 | public: |
| 708 | |
| 709 | RtApi(); |
| 710 | virtual ~RtApi(); |
| 711 | virtual RtAudio::Api getCurrentApi( void ) = 0; |
| 712 | virtual unsigned int getDeviceCount( void ) = 0; |
| 713 | virtual RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) = 0; |
| 714 | virtual unsigned int getDefaultInputDevice( void ); |
| 715 | virtual unsigned int getDefaultOutputDevice( void ); |
| 716 | RtAudioErrorType openStream( RtAudio::StreamParameters *outputParameters, |
| 717 | RtAudio::StreamParameters *inputParameters, |
| 718 | RtAudioFormat format, unsigned int sampleRate, |
| 719 | unsigned int *bufferFrames, RtAudioCallback callback, |
| 720 | void *userData, RtAudio::StreamOptions *options ); |
| 721 | virtual void closeStream( void ); |
| 722 | virtual RtAudioErrorType startStream( void ) = 0; |
| 723 | virtual RtAudioErrorType stopStream( void ) = 0; |
| 724 | virtual RtAudioErrorType abortStream( void ) = 0; |
| 725 | long getStreamLatency( void ); |
| 726 | unsigned int getStreamSampleRate( void ); |
| 727 | virtual double getStreamTime( void ) const { return stream_.streamTime; } |
| 728 | virtual void setStreamTime( double time ); |
| 729 | bool isStreamOpen( void ) const { return stream_.state != STREAM_CLOSED; } |
| 730 | bool isStreamRunning( void ) const { return stream_.state == STREAM_RUNNING; } |
| 731 | void setErrorCallback( RtAudioErrorCallback errorCallback ) { errorCallback_ = errorCallback; } |
| 732 | void showWarnings( bool value ) { showWarnings_ = value; } |
| 733 | |
| 734 | |
| 735 | protected: |
| 736 | |
| 737 | static const unsigned int MAX_SAMPLE_RATES; |
| 738 | static const unsigned int SAMPLE_RATES[]; |
| 739 | |
| 740 | enum { FAILURE, SUCCESS }; |
| 741 | |
| 742 | enum StreamState { |
| 743 | STREAM_STOPPED, |
| 744 | STREAM_STOPPING, |
| 745 | STREAM_RUNNING, |
| 746 | STREAM_CLOSED = -50 |
| 747 | }; |
| 748 | |
| 749 | enum StreamMode { |
| 750 | OUTPUT, |
| 751 | INPUT, |
| 752 | DUPLEX, |
| 753 | UNINITIALIZED = -75 |
| 754 | }; |
| 755 | |
| 756 | // A protected structure used for buffer conversion. |
| 757 | struct ConvertInfo { |
| 758 | int channels; |
| 759 | int inJump, outJump; |
| 760 | RtAudioFormat inFormat, outFormat; |
| 761 | std::vector<int> inOffset; |
| 762 | std::vector<int> outOffset; |
| 763 | }; |
| 764 | |
| 765 | // A protected structure for audio streams. |
| 766 | struct RtApiStream { |
| 767 | unsigned int device[2]; // Playback and record, respectively. |
| 768 | void *apiHandle; // void pointer for API specific stream handle information |
| 769 | StreamMode mode; // OUTPUT, INPUT, or DUPLEX. |
| 770 | StreamState state; // STOPPED, RUNNING, or CLOSED |
| 771 | char *userBuffer[2]; // Playback and record, respectively. |
| 772 | char *deviceBuffer; |
| 773 | bool doConvertBuffer[2]; // Playback and record, respectively. |
| 774 | bool userInterleaved; |
| 775 | bool deviceInterleaved[2]; // Playback and record, respectively. |
| 776 | bool doByteSwap[2]; // Playback and record, respectively. |
| 777 | unsigned int sampleRate; |
| 778 | unsigned int bufferSize; |
| 779 | unsigned int nBuffers; |
| 780 | unsigned int nUserChannels[2]; // Playback and record, respectively. |
| 781 | unsigned int nDeviceChannels[2]; // Playback and record channels, respectively. |
| 782 | unsigned int channelOffset[2]; // Playback and record, respectively. |
| 783 | unsigned long latency[2]; // Playback and record, respectively. |
| 784 | RtAudioFormat userFormat; |
| 785 | RtAudioFormat deviceFormat[2]; // Playback and record, respectively. |
| 786 | StreamMutex mutex; |
| 787 | CallbackInfo callbackInfo; |
| 788 | ConvertInfo convertInfo[2]; |
| 789 | double streamTime; // Number of elapsed seconds since the stream started. |
| 790 | |
| 791 | #if defined(HAVE_GETTIMEOFDAY) |
| 792 | struct timeval lastTickTimestamp; |
| 793 | #endif |
| 794 | |
| 795 | RtApiStream() |
| 796 | :apiHandle(0), deviceBuffer(0) { device[0] = 11111; device[1] = 11111; } |
| 797 | }; |
| 798 | |
| 799 | typedef S24 Int24; |
| 800 | typedef signed short Int16; |
| 801 | typedef signed int Int32; |
| 802 | typedef float Float32; |
| 803 | typedef double Float64; |
| 804 | |
| 805 | std::ostringstream errorStream_; |
| 806 | std::string errorText_; |
| 807 | RtAudioErrorCallback errorCallback_; |
| 808 | bool showWarnings_; |
| 809 | RtApiStream stream_; |
| 810 | |
| 811 | /*! |
| 812 | Protected, api-specific method that attempts to open a device |
| 813 | with the given parameters. This function MUST be implemented by |
| 814 | all subclasses. If an error is encountered during the probe, a |
| 815 | "warning" message is reported and FAILURE is returned. A |
| 816 | successful probe is indicated by a return value of SUCCESS. |
| 817 | */ |
| 818 | virtual bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 819 | unsigned int firstChannel, unsigned int sampleRate, |
| 820 | RtAudioFormat format, unsigned int *bufferSize, |
| 821 | RtAudio::StreamOptions *options ); |
| 822 | |
| 823 | //! A protected function used to increment the stream time. |
| 824 | void tickStreamTime( void ); |
| 825 | |
| 826 | //! Protected common method to clear an RtApiStream structure. |
| 827 | void clearStreamInfo(); |
| 828 | |
| 829 | //! Protected common error method to allow global control over error handling. |
| 830 | RtAudioErrorType error( RtAudioErrorType type ); |
| 831 | |
| 832 | /*! |
| 833 | Protected method used to perform format, channel number, and/or interleaving |
| 834 | conversions between the user and device buffers. |
| 835 | */ |
| 836 | void convertBuffer( char *outBuffer, char *inBuffer, ConvertInfo &info ); |
| 837 | |
| 838 | //! Protected common method used to perform byte-swapping on buffers. |
| 839 | void byteSwapBuffer( char *buffer, unsigned int samples, RtAudioFormat format ); |
| 840 | |
| 841 | //! Protected common method that returns the number of bytes for a given format. |
| 842 | unsigned int formatBytes( RtAudioFormat format ); |
| 843 | |
| 844 | //! Protected common method that sets up the parameters for buffer conversion. |
| 845 | void setConvertInfo( StreamMode mode, unsigned int firstChannel ); |
| 846 | }; |
| 847 | |
| 848 | // **************************************************************** // |
| 849 | // |
| 850 | // Inline RtAudio definitions. |
| 851 | // |
| 852 | // **************************************************************** // |
| 853 | |
| 854 | inline RtAudio::Api RtAudio :: getCurrentApi( void ) { return rtapi_->getCurrentApi(); } |
| 855 | inline unsigned int RtAudio :: getDeviceCount( void ) { return rtapi_->getDeviceCount(); } |
| 856 | inline RtAudio::DeviceInfo RtAudio :: getDeviceInfo( unsigned int device ) { return rtapi_->getDeviceInfo( device ); } |
| 857 | inline unsigned int RtAudio :: getDefaultInputDevice( void ) { return rtapi_->getDefaultInputDevice(); } |
| 858 | inline unsigned int RtAudio :: getDefaultOutputDevice( void ) { return rtapi_->getDefaultOutputDevice(); } |
| 859 | inline void RtAudio :: closeStream( void ) { return rtapi_->closeStream(); } |
| 860 | inline RtAudioErrorType RtAudio :: startStream( void ) { return rtapi_->startStream(); } |
| 861 | inline RtAudioErrorType RtAudio :: stopStream( void ) { return rtapi_->stopStream(); } |
| 862 | inline RtAudioErrorType RtAudio :: abortStream( void ) { return rtapi_->abortStream(); } |
| 863 | inline bool RtAudio :: isStreamOpen( void ) const { return rtapi_->isStreamOpen(); } |
| 864 | inline bool RtAudio :: isStreamRunning( void ) const { return rtapi_->isStreamRunning(); } |
| 865 | inline long RtAudio :: getStreamLatency( void ) { return rtapi_->getStreamLatency(); } |
| 866 | inline unsigned int RtAudio :: getStreamSampleRate( void ) { return rtapi_->getStreamSampleRate(); } |
| 867 | inline double RtAudio :: getStreamTime( void ) { return rtapi_->getStreamTime(); } |
| 868 | inline void RtAudio :: setStreamTime( double time ) { return rtapi_->setStreamTime( time ); } |
| 869 | inline void RtAudio :: setErrorCallback( RtAudioErrorCallback errorCallback ) { rtapi_->setErrorCallback( errorCallback ); } |
| 870 | inline void RtAudio :: showWarnings( bool value ) { rtapi_->showWarnings( value ); } |
| 871 | |
| 872 | // RtApi Subclass prototypes. |
| 873 | |
| 874 | #if defined(__MACOSX_CORE__) |
| 875 | |
| 876 | #include <CoreAudio/AudioHardware.h> |
| 877 | |
| 878 | class RtApiCore: public RtApi |
| 879 | { |
| 880 | public: |
| 881 | |
| 882 | RtApiCore(); |
| 883 | ~RtApiCore(); |
| 884 | RtAudio::Api getCurrentApi( void ) /* override */ { return RtAudio::MACOSX_CORE; } |
| 885 | unsigned int getDeviceCount( void ) /* override */; |
| 886 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) /* override */; |
| 887 | unsigned int getDefaultOutputDevice( void ) /* override */; |
| 888 | unsigned int getDefaultInputDevice( void ) /* override */; |
| 889 | void closeStream( void ) /* override */; |
| 890 | RtAudioErrorType startStream( void ) /* override */; |
| 891 | RtAudioErrorType stopStream( void ) /* override */; |
| 892 | RtAudioErrorType abortStream( void ) /* override */; |
| 893 | |
| 894 | // This function is intended for internal use only. It must be |
| 895 | // public because it is called by the internal callback handler, |
| 896 | // which is not a member of RtAudio. External use of this function |
| 897 | // will most likely produce highly undesirable results! |
| 898 | bool callbackEvent( AudioDeviceID deviceId, |
| 899 | const AudioBufferList *inBufferList, |
| 900 | const AudioBufferList *outBufferList ); |
| 901 | |
| 902 | private: |
| 903 | |
| 904 | bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 905 | unsigned int firstChannel, unsigned int sampleRate, |
| 906 | RtAudioFormat format, unsigned int *bufferSize, |
| 907 | RtAudio::StreamOptions *options ) /* override */; |
| 908 | static const char* getErrorCode( OSStatus code ); |
| 909 | }; |
| 910 | |
| 911 | #endif |
| 912 | |
| 913 | #if defined(__UNIX_JACK__) |
| 914 | |
| 915 | class RtApiJack: public RtApi |
| 916 | { |
| 917 | public: |
| 918 | |
| 919 | RtApiJack(); |
| 920 | ~RtApiJack(); |
| 921 | RtAudio::Api getCurrentApi( void ) /* override */ { return RtAudio::UNIX_JACK; } |
| 922 | unsigned int getDeviceCount( void ) /* override */; |
| 923 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) /* override */; |
| 924 | void closeStream( void ) /* override */; |
| 925 | RtAudioErrorType startStream( void ) /* override */; |
| 926 | RtAudioErrorType stopStream( void ) /* override */; |
| 927 | RtAudioErrorType abortStream( void ) /* override */; |
| 928 | |
| 929 | // This function is intended for internal use only. It must be |
| 930 | // public because it is called by the internal callback handler, |
| 931 | // which is not a member of RtAudio. External use of this function |
| 932 | // will most likely produce highly undesirable results! |
| 933 | bool callbackEvent( unsigned long nframes ); |
| 934 | |
| 935 | private: |
| 936 | |
| 937 | bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 938 | unsigned int firstChannel, unsigned int sampleRate, |
| 939 | RtAudioFormat format, unsigned int *bufferSize, |
| 940 | RtAudio::StreamOptions *options ) /* override */; |
| 941 | |
| 942 | bool shouldAutoconnect_; |
| 943 | }; |
| 944 | |
| 945 | #endif |
| 946 | |
| 947 | #if defined(__WINDOWS_ASIO__) |
| 948 | |
| 949 | class RtApiAsio: public RtApi |
| 950 | { |
| 951 | public: |
| 952 | |
| 953 | RtApiAsio(); |
| 954 | ~RtApiAsio(); |
| 955 | RtAudio::Api getCurrentApi( void ) /* override */ { return RtAudio::WINDOWS_ASIO; } |
| 956 | unsigned int getDeviceCount( void ) /* override */; |
| 957 | unsigned int getDefaultOutputDevice( void ) /* override */; |
| 958 | unsigned int getDefaultInputDevice( void ) /* override */; |
| 959 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) /* override */; |
| 960 | void closeStream( void ) /* override */; |
| 961 | RtAudioErrorType startStream( void ) /* override */; |
| 962 | RtAudioErrorType stopStream( void ) /* override */; |
| 963 | RtAudioErrorType abortStream( void ) /* override */; |
| 964 | |
| 965 | // This function is intended for internal use only. It must be |
| 966 | // public because it is called by the internal callback handler, |
| 967 | // which is not a member of RtAudio. External use of this function |
| 968 | // will most likely produce highly undesirable results! |
| 969 | bool callbackEvent( long bufferIndex ); |
| 970 | |
| 971 | private: |
| 972 | |
| 973 | std::vector<RtAudio::DeviceInfo> devices_; |
| 974 | void saveDeviceInfo( void ); |
| 975 | bool coInitialized_; |
| 976 | bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 977 | unsigned int firstChannel, unsigned int sampleRate, |
| 978 | RtAudioFormat format, unsigned int *bufferSize, |
| 979 | RtAudio::StreamOptions *options ) /* override */; |
| 980 | }; |
| 981 | |
| 982 | #endif |
| 983 | |
| 984 | #if defined(__WINDOWS_DS__) |
| 985 | |
| 986 | class RtApiDs: public RtApi |
| 987 | { |
| 988 | public: |
| 989 | |
| 990 | RtApiDs(); |
| 991 | ~RtApiDs(); |
| 992 | RtAudio::Api getCurrentApi( void ) /* override */ { return RtAudio::WINDOWS_DS; } |
| 993 | unsigned int getDeviceCount( void ) /* override */; |
| 994 | unsigned int getDefaultOutputDevice( void ) /* override */; |
| 995 | unsigned int getDefaultInputDevice( void ) /* override */; |
| 996 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) /* override */; |
| 997 | void closeStream( void ) /* override */; |
| 998 | RtAudioErrorType startStream( void ) /* override */; |
| 999 | RtAudioErrorType stopStream( void ) /* override */; |
| 1000 | RtAudioErrorType abortStream( void ) /* override */; |
| 1001 | |
| 1002 | // This function is intended for internal use only. It must be |
| 1003 | // public because it is called by the internal callback handler, |
| 1004 | // which is not a member of RtAudio. External use of this function |
| 1005 | // will most likely produce highly undesirable results! |
| 1006 | void callbackEvent( void ); |
| 1007 | |
| 1008 | private: |
| 1009 | |
| 1010 | bool coInitialized_; |
| 1011 | bool buffersRolling; |
| 1012 | long duplexPrerollBytes; |
| 1013 | std::vector<struct DsDevice> dsDevices; |
| 1014 | bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 1015 | unsigned int firstChannel, unsigned int sampleRate, |
| 1016 | RtAudioFormat format, unsigned int *bufferSize, |
| 1017 | RtAudio::StreamOptions *options ) /* override */; |
| 1018 | }; |
| 1019 | |
| 1020 | #endif |
| 1021 | |
| 1022 | #if defined(__WINDOWS_WASAPI__) |
| 1023 | |
| 1024 | struct IMMDeviceEnumerator; |
| 1025 | |
| 1026 | class RtApiWasapi : public RtApi |
| 1027 | { |
| 1028 | public: |
| 1029 | RtApiWasapi(); |
| 1030 | virtual ~RtApiWasapi(); |
| 1031 | |
| 1032 | RtAudio::Api getCurrentApi( void ) /* override */ { return RtAudio::WINDOWS_WASAPI; } |
| 1033 | unsigned int getDeviceCount( void ) /* override */; |
| 1034 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) /* override */; |
| 1035 | void closeStream( void ) /* override */; |
| 1036 | RtAudioErrorType startStream( void ) /* override */; |
| 1037 | RtAudioErrorType stopStream( void ) /* override */; |
| 1038 | RtAudioErrorType abortStream( void ) /* override */; |
| 1039 | |
| 1040 | private: |
| 1041 | bool coInitialized_; |
| 1042 | IMMDeviceEnumerator* deviceEnumerator_; |
| 1043 | |
| 1044 | bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 1045 | unsigned int firstChannel, unsigned int sampleRate, |
| 1046 | RtAudioFormat format, unsigned int* bufferSize, |
| 1047 | RtAudio::StreamOptions* options ) /* override */; |
| 1048 | |
| 1049 | static DWORD WINAPI runWasapiThread( void* wasapiPtr ); |
| 1050 | static DWORD WINAPI stopWasapiThread( void* wasapiPtr ); |
| 1051 | static DWORD WINAPI abortWasapiThread( void* wasapiPtr ); |
| 1052 | void wasapiThread(); |
| 1053 | }; |
| 1054 | |
| 1055 | #endif |
| 1056 | |
| 1057 | #if defined(__LINUX_ALSA__) |
| 1058 | |
| 1059 | class RtApiAlsa: public RtApi |
| 1060 | { |
| 1061 | public: |
| 1062 | |
| 1063 | RtApiAlsa(); |
| 1064 | ~RtApiAlsa(); |
| 1065 | RtAudio::Api getCurrentApi() /* override */ { return RtAudio::LINUX_ALSA; } |
| 1066 | unsigned int getDeviceCount( void ) /* override */; |
| 1067 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) /* override */; |
| 1068 | void closeStream( void ) /* override */; |
| 1069 | RtAudioErrorType startStream( void ) /* override */; |
| 1070 | RtAudioErrorType stopStream( void ) /* override */; |
| 1071 | RtAudioErrorType abortStream( void ) /* override */; |
| 1072 | |
| 1073 | // This function is intended for internal use only. It must be |
| 1074 | // public because it is called by the internal callback handler, |
| 1075 | // which is not a member of RtAudio. External use of this function |
| 1076 | // will most likely produce highly undesirable results! |
| 1077 | void callbackEvent( void ); |
| 1078 | |
| 1079 | private: |
| 1080 | |
| 1081 | std::vector<RtAudio::DeviceInfo> devices_; |
| 1082 | void saveDeviceInfo( void ); |
| 1083 | bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 1084 | unsigned int firstChannel, unsigned int sampleRate, |
| 1085 | RtAudioFormat format, unsigned int *bufferSize, |
| 1086 | RtAudio::StreamOptions *options ) /* override */; |
| 1087 | }; |
| 1088 | |
| 1089 | #endif |
| 1090 | |
| 1091 | #if defined(__LINUX_PULSE__) |
| 1092 | |
| 1093 | class RtApiPulse: public RtApi |
| 1094 | { |
| 1095 | public: |
| 1096 | ~RtApiPulse(); |
| 1097 | RtAudio::Api getCurrentApi() /* override */ { return RtAudio::LINUX_PULSE; } |
| 1098 | unsigned int getDeviceCount( void ) /* override */; |
| 1099 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) /* override */; |
| 1100 | void closeStream( void ) /* override */; |
| 1101 | RtAudioErrorType startStream( void ) /* override */; |
| 1102 | RtAudioErrorType stopStream( void ) /* override */; |
| 1103 | RtAudioErrorType abortStream( void ) /* override */; |
| 1104 | |
| 1105 | // This function is intended for internal use only. It must be |
| 1106 | // public because it is called by the internal callback handler, |
| 1107 | // which is not a member of RtAudio. External use of this function |
| 1108 | // will most likely produce highly undesirable results! |
| 1109 | void callbackEvent( void ); |
| 1110 | |
| 1111 | private: |
| 1112 | |
| 1113 | void collectDeviceInfo( void ); |
| 1114 | bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 1115 | unsigned int firstChannel, unsigned int sampleRate, |
| 1116 | RtAudioFormat format, unsigned int *bufferSize, |
| 1117 | RtAudio::StreamOptions *options ) /* override */; |
| 1118 | }; |
| 1119 | |
| 1120 | #endif |
| 1121 | |
| 1122 | #if defined(__LINUX_OSS__) |
| 1123 | |
| 1124 | class RtApiOss: public RtApi |
| 1125 | { |
| 1126 | public: |
| 1127 | |
| 1128 | RtApiOss(); |
| 1129 | ~RtApiOss(); |
| 1130 | RtAudio::Api getCurrentApi() /* override */ { return RtAudio::LINUX_OSS; } |
| 1131 | unsigned int getDeviceCount( void ) /* override */; |
| 1132 | RtAudio::DeviceInfo getDeviceInfo( unsigned int device ) /* override */; |
| 1133 | void closeStream( void ) /* override */; |
| 1134 | RtAudioErrorType startStream( void ) /* override */; |
| 1135 | RtAudioErrorType stopStream( void ) /* override */; |
| 1136 | RtAudioErrorType abortStream( void ) /* override */; |
| 1137 | |
| 1138 | // This function is intended for internal use only. It must be |
| 1139 | // public because it is called by the internal callback handler, |
| 1140 | // which is not a member of RtAudio. External use of this function |
| 1141 | // will most likely produce highly undesirable results! |
| 1142 | void callbackEvent( void ); |
| 1143 | |
| 1144 | private: |
| 1145 | |
| 1146 | bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, |
| 1147 | unsigned int firstChannel, unsigned int sampleRate, |
| 1148 | RtAudioFormat format, unsigned int *bufferSize, |
| 1149 | RtAudio::StreamOptions *options ) /* override */; |
| 1150 | }; |
| 1151 | |
| 1152 | #endif |
| 1153 | |
| 1154 | #if defined(__RTAUDIO_DUMMY__) |
| 1155 | |
| 1156 | class RtApiDummy: public RtApi |
| 1157 | { |
| 1158 | public: |
| 1159 | |
| 1160 | RtApiDummy() { errorText_ = "RtApiDummy: This class provides no functionality."; error( RTAUDIO_WARNING ); } |
| 1161 | RtAudio::Api getCurrentApi( void ) /* override */ { return RtAudio::RTAUDIO_DUMMY; } |
| 1162 | unsigned int getDeviceCount( void ) /* override */ { return 0; } |
| 1163 | RtAudio::DeviceInfo getDeviceInfo( unsigned int /*device*/ ) /* override */ { RtAudio::DeviceInfo info; return info; } |
| 1164 | void closeStream( void ) /* override */ {} |
| 1165 | RtAudioErrorType startStream( void ) /* override */ { return RTAUDIO_NO_ERROR; } |
| 1166 | RtAudioErrorType stopStream( void ) /* override */ { return RTAUDIO_NO_ERROR; } |
| 1167 | RtAudioErrorType abortStream( void ) /* override */ { return RTAUDIO_NO_ERROR; } |
| 1168 | |
| 1169 | private: |
| 1170 | |
| 1171 | bool probeDeviceOpen( unsigned int /*device*/, StreamMode /*mode*/, unsigned int /*channels*/, |
| 1172 | unsigned int /*firstChannel*/, unsigned int /*sampleRate*/, |
| 1173 | RtAudioFormat /*format*/, unsigned int * /*bufferSize*/, |
| 1174 | RtAudio::StreamOptions * /*options*/ ) /* override */ { return false; } |
| 1175 | }; |
| 1176 | |
| 1177 | #endif |
| 1178 | |
| 1179 | #endif |
| 1180 | |
| 1181 | // Indentation settings for Vim and Emacs |
| 1182 | // |
| 1183 | // Local Variables: |
| 1184 | // c-basic-offset: 2 |
| 1185 | // indent-tabs-mode: nil |
| 1186 | // End: |
| 1187 | // |
| 1188 | // vim: et sts=2 sw=2 |