You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

814 lines
30KB

  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), SGI, Macintosh OS X (CoreAudio), and Windows
  7. (DirectSound and ASIO) operating systems.
  8. RtAudio WWW site: http://music.mcgill.ca/~gary/rtaudio/
  9. RtAudio: realtime audio i/o C++ classes
  10. Copyright (c) 2001-2005 Gary P. Scavone
  11. Permission is hereby granted, free of charge, to any person
  12. obtaining a copy of this software and associated documentation files
  13. (the "Software"), to deal in the Software without restriction,
  14. including without limitation the rights to use, copy, modify, merge,
  15. publish, distribute, sublicense, and/or sell copies of the Software,
  16. and to permit persons to whom the Software is furnished to do so,
  17. subject to the following conditions:
  18. The above copyright notice and this permission notice shall be
  19. included in all copies or substantial portions of the Software.
  20. Any person wishing to distribute modifications to the Software is
  21. requested to send the modifications to the original developer so that
  22. they can be incorporated into the canonical version.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  26. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  27. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  28. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. */
  31. /************************************************************************/
  32. // RtAudio: Version 3.0.3 (18 November 2005)
  33. #ifndef __RTAUDIO_H
  34. #define __RTAUDIO_H
  35. #include "RtError.h"
  36. #include <string>
  37. #include <vector>
  38. // Operating system dependent thread functionality.
  39. #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__)
  40. #include <windows.h>
  41. #include <process.h>
  42. typedef unsigned long ThreadHandle;
  43. typedef CRITICAL_SECTION StreamMutex;
  44. #else // Various unix flavors with pthread support.
  45. #include <pthread.h>
  46. typedef pthread_t ThreadHandle;
  47. typedef pthread_mutex_t StreamMutex;
  48. #endif
  49. // This global structure type is used to pass callback information
  50. // between the private RtAudio stream structure and global callback
  51. // handling functions.
  52. struct CallbackInfo {
  53. void *object; // Used as a "this" pointer.
  54. ThreadHandle thread;
  55. bool usingCallback;
  56. void *callback;
  57. void *userData;
  58. void *apiInfo; // void pointer for API specific callback information
  59. // Default constructor.
  60. CallbackInfo()
  61. :object(0), usingCallback(false), callback(0),
  62. userData(0), apiInfo(0) {}
  63. };
  64. // Support for signed integers and floats. Audio data fed to/from
  65. // the tickStream() routine is assumed to ALWAYS be in host
  66. // byte order. The internal routines will automatically take care of
  67. // any necessary byte-swapping between the host format and the
  68. // soundcard. Thus, endian-ness is not a concern in the following
  69. // format definitions.
  70. typedef unsigned long RtAudioFormat;
  71. static const RtAudioFormat RTAUDIO_SINT8 = 0x1; /*!< 8-bit signed integer. */
  72. static const RtAudioFormat RTAUDIO_SINT16 = 0x2; /*!< 16-bit signed integer. */
  73. static const RtAudioFormat RTAUDIO_SINT24 = 0x4; /*!< Upper 3 bytes of 32-bit signed integer. */
  74. static const RtAudioFormat RTAUDIO_SINT32 = 0x8; /*!< 32-bit signed integer. */
  75. static const RtAudioFormat RTAUDIO_FLOAT32 = 0x10; /*!< Normalized between plus/minus 1.0. */
  76. static const RtAudioFormat RTAUDIO_FLOAT64 = 0x20; /*!< Normalized between plus/minus 1.0. */
  77. typedef int (*RtAudioCallback)(char *buffer, int bufferSize, void *userData);
  78. //! The public device information structure for returning queried values.
  79. struct RtAudioDeviceInfo {
  80. std::string name; /*!< Character string device identifier. */
  81. bool probed; /*!< true if the device capabilities were successfully probed. */
  82. int outputChannels; /*!< Maximum output channels supported by device. */
  83. int inputChannels; /*!< Maximum input channels supported by device. */
  84. int duplexChannels; /*!< Maximum simultaneous input/output channels supported by device. */
  85. bool isDefault; /*!< true if this is the default output or input device. */
  86. std::vector<int> sampleRates; /*!< Supported sample rates (queried from list of standard rates). */
  87. RtAudioFormat nativeFormats; /*!< Bit mask of supported data formats. */
  88. // Default constructor.
  89. RtAudioDeviceInfo()
  90. :probed(false), outputChannels(0), inputChannels(0),
  91. duplexChannels(0), isDefault(false), nativeFormats(0) {}
  92. };
  93. // **************************************************************** //
  94. //
  95. // RtApi class declaration.
  96. //
  97. // Note that RtApi is an abstract base class and cannot be
  98. // explicitly instantiated. The class RtAudio will create an
  99. // instance of an RtApi subclass (RtApiOss, RtApiAlsa,
  100. // RtApiJack, RtApiCore, RtApiAl, RtApiDs, or RtApiAsio).
  101. //
  102. // **************************************************************** //
  103. class RtApi
  104. {
  105. public:
  106. enum StreamState {
  107. STREAM_STOPPED,
  108. STREAM_RUNNING
  109. };
  110. RtApi();
  111. virtual ~RtApi();
  112. void openStream( int outputDevice, int outputChannels,
  113. int inputDevice, int inputChannels,
  114. RtAudioFormat format, int sampleRate,
  115. int *bufferSize, int numberOfBuffers );
  116. void openStream( int outputDevice, int outputChannels,
  117. int inputDevice, int inputChannels,
  118. RtAudioFormat format, int sampleRate,
  119. int *bufferSize, int *numberOfBuffers );
  120. virtual void setStreamCallback( RtAudioCallback callback, void *userData ) = 0;
  121. virtual void cancelStreamCallback() = 0;
  122. int getDeviceCount(void);
  123. RtAudioDeviceInfo getDeviceInfo( int device );
  124. char * const getStreamBuffer();
  125. RtApi::StreamState getStreamState() const;
  126. virtual void tickStream() = 0;
  127. virtual void closeStream();
  128. virtual void startStream() = 0;
  129. virtual void stopStream() = 0;
  130. virtual void abortStream() = 0;
  131. protected:
  132. static const unsigned int MAX_SAMPLE_RATES;
  133. static const unsigned int SAMPLE_RATES[];
  134. enum { FAILURE, SUCCESS };
  135. enum StreamMode {
  136. OUTPUT,
  137. INPUT,
  138. DUPLEX,
  139. UNINITIALIZED = -75
  140. };
  141. // A protected structure used for buffer conversion.
  142. struct ConvertInfo {
  143. int channels;
  144. int inJump, outJump;
  145. RtAudioFormat inFormat, outFormat;
  146. std::vector<int> inOffset;
  147. std::vector<int> outOffset;
  148. };
  149. // A protected structure for audio streams.
  150. struct RtApiStream {
  151. int device[2]; // Playback and record, respectively.
  152. void *apiHandle; // void pointer for API specific stream handle information
  153. StreamMode mode; // OUTPUT, INPUT, or DUPLEX.
  154. StreamState state; // STOPPED or RUNNING
  155. char *userBuffer;
  156. char *deviceBuffer;
  157. bool doConvertBuffer[2]; // Playback and record, respectively.
  158. bool deInterleave[2]; // Playback and record, respectively.
  159. bool doByteSwap[2]; // Playback and record, respectively.
  160. int sampleRate;
  161. int bufferSize;
  162. int nBuffers;
  163. int nUserChannels[2]; // Playback and record, respectively.
  164. int nDeviceChannels[2]; // Playback and record channels, respectively.
  165. RtAudioFormat userFormat;
  166. RtAudioFormat deviceFormat[2]; // Playback and record, respectively.
  167. StreamMutex mutex;
  168. CallbackInfo callbackInfo;
  169. ConvertInfo convertInfo[2];
  170. RtApiStream()
  171. :apiHandle(0), userBuffer(0), deviceBuffer(0) {}
  172. };
  173. // A protected device structure for audio devices.
  174. struct RtApiDevice {
  175. std::string name; /*!< Character string device identifier. */
  176. bool probed; /*!< true if the device capabilities were successfully probed. */
  177. void *apiDeviceId; // void pointer for API specific device information
  178. int maxOutputChannels; /*!< Maximum output channels supported by device. */
  179. int maxInputChannels; /*!< Maximum input channels supported by device. */
  180. int maxDuplexChannels; /*!< Maximum simultaneous input/output channels supported by device. */
  181. int minOutputChannels; /*!< Minimum output channels supported by device. */
  182. int minInputChannels; /*!< Minimum input channels supported by device. */
  183. int minDuplexChannels; /*!< Minimum simultaneous input/output channels supported by device. */
  184. bool hasDuplexSupport; /*!< true if device supports duplex mode. */
  185. bool isDefault; /*!< true if this is the default output or input device. */
  186. std::vector<int> sampleRates; /*!< Supported sample rates. */
  187. RtAudioFormat nativeFormats; /*!< Bit mask of supported data formats. */
  188. // Default constructor.
  189. RtApiDevice()
  190. :probed(false), apiDeviceId(0), maxOutputChannels(0), maxInputChannels(0),
  191. maxDuplexChannels(0), minOutputChannels(0), minInputChannels(0),
  192. minDuplexChannels(0), isDefault(false), nativeFormats(0) {}
  193. };
  194. typedef signed short Int16;
  195. typedef signed int Int32;
  196. typedef float Float32;
  197. typedef double Float64;
  198. char message_[1024];
  199. int nDevices_;
  200. std::vector<RtApiDevice> devices_;
  201. RtApiStream stream_;
  202. /*!
  203. Protected, api-specific method to count and identify the system
  204. audio devices. This function MUST be implemented by all subclasses.
  205. */
  206. virtual void initialize(void) = 0;
  207. /*!
  208. Protected, api-specific method which attempts to fill an
  209. RtAudioDevice structure for a given device. This function MUST be
  210. implemented by all subclasses. If an error is encountered during
  211. the probe, a "warning" message is reported and the value of
  212. "probed" remains false (no exception is thrown). A successful
  213. probe is indicated by probed = true.
  214. */
  215. virtual void probeDeviceInfo( RtApiDevice *info );
  216. /*!
  217. Protected, api-specific method which attempts to open a device
  218. with the given parameters. This function MUST be implemented by
  219. all subclasses. If an error is encountered during the probe, a
  220. "warning" message is reported and FAILURE is returned (no
  221. exception is thrown). A successful probe is indicated by a return
  222. value of SUCCESS.
  223. */
  224. virtual bool probeDeviceOpen( int device, StreamMode mode, int channels,
  225. int sampleRate, RtAudioFormat format,
  226. int *bufferSize, int numberOfBuffers );
  227. /*!
  228. Protected method which returns the index in the devices array to
  229. the default input device.
  230. */
  231. virtual int getDefaultInputDevice(void);
  232. /*!
  233. Protected method which returns the index in the devices array to
  234. the default output device.
  235. */
  236. virtual int getDefaultOutputDevice(void);
  237. //! Protected common method to clear an RtApiDevice structure.
  238. void clearDeviceInfo( RtApiDevice *info );
  239. //! Protected common method to clear an RtApiStream structure.
  240. void clearStreamInfo();
  241. //! Protected common error method to allow global control over error handling.
  242. void error( RtError::Type type );
  243. /*!
  244. Protected common method used to check whether a stream is open.
  245. If not, an "invalid identifier" exception is thrown.
  246. */
  247. void verifyStream();
  248. /*!
  249. Protected method used to perform format, channel number, and/or interleaving
  250. conversions between the user and device buffers.
  251. */
  252. void convertBuffer( char *outBuffer, char *inBuffer, ConvertInfo &info );
  253. //! Protected common method used to perform byte-swapping on buffers.
  254. void byteSwapBuffer( char *buffer, int samples, RtAudioFormat format );
  255. //! Protected common method which returns the number of bytes for a given format.
  256. int formatBytes( RtAudioFormat format );
  257. };
  258. // **************************************************************** //
  259. //
  260. // RtAudio class declaration.
  261. //
  262. // RtAudio is a "controller" used to select an available audio i/o
  263. // interface. It presents a common API for the user to call but all
  264. // functionality is implemented by the class RtAudioApi and its
  265. // subclasses. RtAudio creates an instance of an RtAudioApi subclass
  266. // based on the user's API choice. If no choice is made, RtAudio
  267. // attempts to make a "logical" API selection.
  268. //
  269. // **************************************************************** //
  270. class RtAudio
  271. {
  272. public:
  273. //! Audio API specifier arguments.
  274. enum RtAudioApi {
  275. UNSPECIFIED, /*!< Search for a working compiled API. */
  276. LINUX_ALSA, /*!< The Advanced Linux Sound Architecture API. */
  277. LINUX_OSS, /*!< The Linux Open Sound System API. */
  278. LINUX_JACK, /*!< The Linux Jack Low-Latency Audio Server API. */
  279. MACOSX_CORE, /*!< Macintosh OS-X Core Audio API. */
  280. IRIX_AL, /*!< The Irix Audio Library API. */
  281. WINDOWS_ASIO, /*!< The Steinberg Audio Stream I/O API. */
  282. WINDOWS_DS /*!< The Microsoft Direct Sound API. */
  283. };
  284. //! The default class constructor.
  285. /*!
  286. Probes the system to make sure at least one audio input/output
  287. device is available and determines the api-specific identifier for
  288. each device found. An RtError error can be thrown if no devices
  289. are found or if a memory allocation error occurs.
  290. If no API argument is specified and multiple API support has been
  291. compiled, the default order of use is JACK, ALSA, OSS (Linux
  292. systems) and ASIO, DS (Windows systems).
  293. */
  294. RtAudio( RtAudioApi api=UNSPECIFIED );
  295. //! A constructor which can be used to open a stream during instantiation.
  296. /*!
  297. The specified output and/or input device identifiers correspond
  298. to those enumerated via the getDeviceInfo() method. If device =
  299. 0, the default or first available devices meeting the given
  300. parameters is selected. If an output or input channel value is
  301. zero, the corresponding device value is ignored. When a stream is
  302. successfully opened, its identifier is returned via the "streamId"
  303. pointer. An RtError can be thrown if no devices are found
  304. for the given parameters, if a memory allocation error occurs, or
  305. if a driver error occurs. \sa openStream()
  306. */
  307. RtAudio( int outputDevice, int outputChannels,
  308. int inputDevice, int inputChannels,
  309. RtAudioFormat format, int sampleRate,
  310. int *bufferSize, int numberOfBuffers, RtAudioApi api=UNSPECIFIED );
  311. //! An overloaded constructor which opens a stream and also returns \c numberOfBuffers parameter via pointer argument.
  312. /*!
  313. See the previous constructor call for details. This overloaded
  314. version differs only in that it takes a pointer argument for the
  315. \c numberOfBuffers parameter and returns the value used by the
  316. audio device (which may be different from that requested). Note
  317. that the \c numberofBuffers parameter is not used with the Linux
  318. Jack, Macintosh CoreAudio, and Windows ASIO APIs.
  319. */
  320. RtAudio( int outputDevice, int outputChannels,
  321. int inputDevice, int inputChannels,
  322. RtAudioFormat format, int sampleRate,
  323. int *bufferSize, int *numberOfBuffers, RtAudioApi api=UNSPECIFIED );
  324. //! The destructor.
  325. /*!
  326. Stops and closes an open stream and devices and deallocates
  327. buffer and structure memory.
  328. */
  329. ~RtAudio();
  330. //! A public method for opening a stream with the specified parameters.
  331. /*!
  332. An RtError is thrown if a stream cannot be opened.
  333. \param outputDevice: If equal to 0, the default or first device
  334. found meeting the given parameters is opened. Otherwise, the
  335. device number should correspond to one of those enumerated via
  336. the getDeviceInfo() method.
  337. \param outputChannels: The desired number of output channels. If
  338. equal to zero, the outputDevice identifier is ignored.
  339. \param inputDevice: If equal to 0, the default or first device
  340. found meeting the given parameters is opened. Otherwise, the
  341. device number should correspond to one of those enumerated via
  342. the getDeviceInfo() method.
  343. \param inputChannels: The desired number of input channels. If
  344. equal to zero, the inputDevice identifier is ignored.
  345. \param format: An RtAudioFormat specifying the desired sample data format.
  346. \param sampleRate: The desired sample rate (sample frames per second).
  347. \param *bufferSize: A pointer value indicating the desired internal buffer
  348. size in sample frames. The actual value used by the device is
  349. returned via the same pointer. A value of zero can be specified,
  350. in which case the lowest allowable value is determined.
  351. \param numberOfBuffers: A value which can be used to help control device
  352. latency. More buffers typically result in more robust performance,
  353. though at a cost of greater latency. A value of zero can be
  354. specified, in which case the lowest allowable value is used.
  355. */
  356. void openStream( int outputDevice, int outputChannels,
  357. int inputDevice, int inputChannels,
  358. RtAudioFormat format, int sampleRate,
  359. int *bufferSize, int numberOfBuffers );
  360. //! A public method for opening a stream and also returning \c numberOfBuffers parameter via pointer argument.
  361. /*!
  362. See the previous function call for details. This overloaded
  363. version differs only in that it takes a pointer argument for the
  364. \c numberOfBuffers parameter and returns the value used by the
  365. audio device (which may be different from that requested). Note
  366. that the \c numberofBuffers parameter is not used with the Linux
  367. Jack, Macintosh CoreAudio, and Windows ASIO APIs.
  368. */
  369. void openStream( int outputDevice, int outputChannels,
  370. int inputDevice, int inputChannels,
  371. RtAudioFormat format, int sampleRate,
  372. int *bufferSize, int *numberOfBuffers );
  373. //! A public method which sets a user-defined callback function for a given stream.
  374. /*!
  375. This method assigns a callback function to a previously opened
  376. stream for non-blocking stream functionality. A separate process
  377. is initiated, though the user function is called only when the
  378. stream is "running" (between calls to the startStream() and
  379. stopStream() methods, respectively). The callback process remains
  380. active for the duration of the stream and is automatically
  381. shutdown when the stream is closed (via the closeStream() method
  382. or by object destruction). The callback process can also be
  383. shutdown and the user function de-referenced through an explicit
  384. call to the cancelStreamCallback() method. Note that the stream
  385. can use only blocking or callback functionality at a particular
  386. time, though it is possible to alternate modes on the same stream
  387. through the use of the setStreamCallback() and
  388. cancelStreamCallback() methods (the blocking tickStream() method
  389. can be used before a callback is set and/or after a callback is
  390. cancelled). An RtError will be thrown if called when no stream is
  391. open or a thread errors occurs.
  392. */
  393. void setStreamCallback(RtAudioCallback callback, void *userData) { rtapi_->setStreamCallback( callback, userData ); };
  394. //! A public method which cancels a callback process and function for the stream.
  395. /*!
  396. This method shuts down a callback process and de-references the
  397. user function for the stream. Callback functionality can
  398. subsequently be restarted on the stream via the
  399. setStreamCallback() method. An RtError will be thrown if called
  400. when no stream is open.
  401. */
  402. void cancelStreamCallback() { rtapi_->cancelStreamCallback(); };
  403. //! A public method which returns the number of audio devices found.
  404. int getDeviceCount(void) { return rtapi_->getDeviceCount(); };
  405. //! Return an RtAudioDeviceInfo structure for a specified device number.
  406. /*!
  407. Any device integer between 1 and getDeviceCount() is valid. If
  408. a device is busy or otherwise unavailable, the structure member
  409. "probed" will have a value of "false" and all other members are
  410. undefined. If the specified device is the current default input
  411. or output device, the "isDefault" member will have a value of
  412. "true". An RtError will be thrown for an invalid device argument.
  413. */
  414. RtAudioDeviceInfo getDeviceInfo(int device) { return rtapi_->getDeviceInfo( device ); };
  415. //! A public method which returns a pointer to the buffer for an open stream.
  416. /*!
  417. The user should fill and/or read the buffer data in interleaved format
  418. and then call the tickStream() method. An RtError will be
  419. thrown if called when no stream is open.
  420. */
  421. char * const getStreamBuffer() { return rtapi_->getStreamBuffer(); };
  422. //! Public method used to trigger processing of input/output data for a stream.
  423. /*!
  424. This method blocks until all buffer data is read/written. An
  425. RtError will be thrown if a driver error occurs or if called when
  426. no stream is open.
  427. */
  428. void tickStream() { rtapi_->tickStream(); };
  429. //! Public method which closes a stream and frees any associated buffers.
  430. /*!
  431. If a stream is not open, this method issues a warning and
  432. returns (an RtError is not thrown).
  433. */
  434. void closeStream() { rtapi_->closeStream(); };
  435. //! Public method which starts a stream.
  436. /*!
  437. An RtError will be thrown if a driver error occurs or if called
  438. when no stream is open.
  439. */
  440. void startStream() { rtapi_->startStream(); };
  441. //! Stop a stream, allowing any samples remaining in the queue to be played out and/or read in.
  442. /*!
  443. An RtError will be thrown if a driver error occurs or if called
  444. when no stream is open.
  445. */
  446. void stopStream() { rtapi_->stopStream(); };
  447. //! Stop a stream, discarding any samples remaining in the input/output queue.
  448. /*!
  449. An RtError will be thrown if a driver error occurs or if called
  450. when no stream is open.
  451. */
  452. void abortStream() { rtapi_->abortStream(); };
  453. protected:
  454. void initialize( RtAudioApi api );
  455. RtApi *rtapi_;
  456. };
  457. // RtApi Subclass prototypes.
  458. #if defined(__LINUX_ALSA__)
  459. class RtApiAlsa: public RtApi
  460. {
  461. public:
  462. RtApiAlsa();
  463. ~RtApiAlsa();
  464. void tickStream();
  465. void closeStream();
  466. void startStream();
  467. void stopStream();
  468. void abortStream();
  469. int streamWillBlock();
  470. void setStreamCallback( RtAudioCallback callback, void *userData );
  471. void cancelStreamCallback();
  472. private:
  473. void initialize(void);
  474. bool primeOutputBuffer();
  475. void probeDeviceInfo( RtApiDevice *info );
  476. bool probeDeviceOpen( int device, StreamMode mode, int channels,
  477. int sampleRate, RtAudioFormat format,
  478. int *bufferSize, int numberOfBuffers );
  479. };
  480. #endif
  481. #if defined(__LINUX_JACK__)
  482. class RtApiJack: public RtApi
  483. {
  484. public:
  485. RtApiJack();
  486. ~RtApiJack();
  487. void tickStream();
  488. void closeStream();
  489. void startStream();
  490. void stopStream();
  491. void abortStream();
  492. void setStreamCallback( RtAudioCallback callback, void *userData );
  493. void cancelStreamCallback();
  494. // This function is intended for internal use only. It must be
  495. // public because it is called by the internal callback handler,
  496. // which is not a member of RtAudio. External use of this function
  497. // will most likely produce highly undesireable results!
  498. void callbackEvent( unsigned long nframes );
  499. private:
  500. void initialize(void);
  501. void probeDeviceInfo( RtApiDevice *info );
  502. bool probeDeviceOpen( int device, StreamMode mode, int channels,
  503. int sampleRate, RtAudioFormat format,
  504. int *bufferSize, int numberOfBuffers );
  505. };
  506. #endif
  507. #if defined(__LINUX_OSS__)
  508. class RtApiOss: public RtApi
  509. {
  510. public:
  511. RtApiOss();
  512. ~RtApiOss();
  513. void tickStream();
  514. void closeStream();
  515. void startStream();
  516. void stopStream();
  517. void abortStream();
  518. int streamWillBlock();
  519. void setStreamCallback( RtAudioCallback callback, void *userData );
  520. void cancelStreamCallback();
  521. private:
  522. void initialize(void);
  523. void probeDeviceInfo( RtApiDevice *info );
  524. bool probeDeviceOpen( int device, StreamMode mode, int channels,
  525. int sampleRate, RtAudioFormat format,
  526. int *bufferSize, int numberOfBuffers );
  527. };
  528. #endif
  529. #if defined(__MACOSX_CORE__)
  530. #include <CoreAudio/AudioHardware.h>
  531. class RtApiCore: public RtApi
  532. {
  533. public:
  534. RtApiCore();
  535. ~RtApiCore();
  536. int getDefaultOutputDevice(void);
  537. int getDefaultInputDevice(void);
  538. void tickStream();
  539. void closeStream();
  540. void startStream();
  541. void stopStream();
  542. void abortStream();
  543. void setStreamCallback( RtAudioCallback callback, void *userData );
  544. void cancelStreamCallback();
  545. // This function is intended for internal use only. It must be
  546. // public because it is called by the internal callback handler,
  547. // which is not a member of RtAudio. External use of this function
  548. // will most likely produce highly undesireable results!
  549. void callbackEvent( AudioDeviceID deviceId, void *inData, void *outData );
  550. private:
  551. void initialize(void);
  552. void probeDeviceInfo( RtApiDevice *info );
  553. bool probeDeviceOpen( int device, StreamMode mode, int channels,
  554. int sampleRate, RtAudioFormat format,
  555. int *bufferSize, int numberOfBuffers );
  556. };
  557. #endif
  558. #if defined(__WINDOWS_DS__)
  559. class RtApiDs: public RtApi
  560. {
  561. public:
  562. RtApiDs();
  563. ~RtApiDs();
  564. int getDefaultOutputDevice(void);
  565. int getDefaultInputDevice(void);
  566. void tickStream();
  567. void closeStream();
  568. void startStream();
  569. void stopStream();
  570. void abortStream();
  571. int streamWillBlock();
  572. void setStreamCallback( RtAudioCallback callback, void *userData );
  573. void cancelStreamCallback();
  574. public:
  575. // \brief Internal structure that provide debug information on the state of a running DSound device.
  576. struct RtDsStatistics {
  577. // \brief Sample Rate.
  578. long sampleRate;
  579. // \brief The size of one sample * number of channels on the input device.
  580. int inputFrameSize;
  581. // \brief The size of one sample * number of channels on the output device.
  582. int outputFrameSize;
  583. /* \brief The number of times the read pointer had to be adjusted to avoid reading from an unsafe buffer position.
  584. *
  585. * This field is only used when running in DUPLEX mode. INPUT mode devices just wait until the data is
  586. * available.
  587. */
  588. int numberOfReadOverruns;
  589. // \brief The number of times the write pointer had to be adjusted to avoid writing in an unsafe buffer position.
  590. int numberOfWriteUnderruns;
  591. // \brief Number of bytes by attribute to buffer configuration by which writing must lead the current write pointer.
  592. int writeDeviceBufferLeadBytes;
  593. // \brief Number of bytes by attributable to the device driver by which writing must lead the current write pointer on this output device.
  594. unsigned long writeDeviceSafeLeadBytes;
  595. // \brief Number of bytes by which reading must trail the current read pointer on this input device.
  596. unsigned long readDeviceSafeLeadBytes;
  597. /* \brief Estimated latency in seconds.
  598. *
  599. * For INPUT mode devices, based the latency of the device's safe read pointer, plus one buffer's
  600. * worth of additional latency.
  601. *
  602. * For OUTPUT mode devices, the latency of the device's safe write pointer, plus N buffers of
  603. * additional buffer latency.
  604. *
  605. * For DUPLEX devices, the sum of latencies for both input and output devices. DUPLEX devices
  606. * also back off the read pointers an additional amount in order to maintain synchronization
  607. * between out-of-phase read and write pointers. This time is also included.
  608. *
  609. * Note that most software packages report latency between the safe write pointer
  610. * and the software lead pointer, excluding the hardware device's safe write pointer
  611. * latency. Figures of 1 or 2ms of latency on Windows audio devices are invariably of this type.
  612. * The reality is that hardware devices often have latencies of 30ms or more (often much
  613. * higher for duplex operation).
  614. */
  615. double latency;
  616. };
  617. // \brief Report on the current state of a running DSound device.
  618. static RtDsStatistics getDsStatistics();
  619. private:
  620. void initialize(void);
  621. void probeDeviceInfo( RtApiDevice *info );
  622. bool probeDeviceOpen( int device, StreamMode mode, int channels,
  623. int sampleRate, RtAudioFormat format,
  624. int *bufferSize, int numberOfBuffers );
  625. bool coInitialized;
  626. bool buffersRolling;
  627. long duplexPrerollBytes;
  628. static RtDsStatistics statistics;
  629. };
  630. #endif
  631. #if defined(__WINDOWS_ASIO__)
  632. class RtApiAsio: public RtApi
  633. {
  634. public:
  635. RtApiAsio();
  636. ~RtApiAsio();
  637. void tickStream();
  638. void closeStream();
  639. void startStream();
  640. void stopStream();
  641. void abortStream();
  642. void setStreamCallback( RtAudioCallback callback, void *userData );
  643. void cancelStreamCallback();
  644. // This function is intended for internal use only. It must be
  645. // public because it is called by the internal callback handler,
  646. // which is not a member of RtAudio. External use of this function
  647. // will most likely produce highly undesireable results!
  648. void callbackEvent( long bufferIndex );
  649. private:
  650. void initialize(void);
  651. void probeDeviceInfo( RtApiDevice *info );
  652. bool probeDeviceOpen( int device, StreamMode mode, int channels,
  653. int sampleRate, RtAudioFormat format,
  654. int *bufferSize, int numberOfBuffers );
  655. bool coInitialized;
  656. };
  657. #endif
  658. #if defined(__IRIX_AL__)
  659. class RtApiAl: public RtApi
  660. {
  661. public:
  662. RtApiAl();
  663. ~RtApiAl();
  664. int getDefaultOutputDevice(void);
  665. int getDefaultInputDevice(void);
  666. void tickStream();
  667. void closeStream();
  668. void startStream();
  669. void stopStream();
  670. void abortStream();
  671. int streamWillBlock();
  672. void setStreamCallback( RtAudioCallback callback, void *userData );
  673. void cancelStreamCallback();
  674. private:
  675. void initialize(void);
  676. void probeDeviceInfo( RtApiDevice *info );
  677. bool probeDeviceOpen( int device, StreamMode mode, int channels,
  678. int sampleRate, RtAudioFormat format,
  679. int *bufferSize, int numberOfBuffers );
  680. };
  681. #endif
  682. // Define the following flag to have extra information spewed to stderr.
  683. //#define __RTAUDIO_DEBUG__
  684. #endif