RtAudio.h 47 KB

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