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.

526 lines
19KB

  1. /************************************************************************/
  2. /*! \class RtAudio
  3. \brief Realtime audio i/o C++ class.
  4. RtAudio provides a common API (Application Programming Interface)
  5. for realtime audio input/output across Linux (native ALSA and
  6. OSS), SGI, Macintosh OS X (CoreAudio), and Windows (DirectSound
  7. and ASIO) operating systems.
  8. RtAudio WWW site: http://www-ccrma.stanford.edu/~gary/rtaudio/
  9. RtAudio: a realtime audio i/o C++ class
  10. Copyright (c) 2001-2002 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. #if !defined(__RTAUDIO_H)
  33. #define __RTAUDIO_H
  34. #include <map>
  35. #if defined(__LINUX_ALSA__)
  36. #include <alsa/asoundlib.h>
  37. #include <pthread.h>
  38. #include <unistd.h>
  39. typedef snd_pcm_t *AUDIO_HANDLE;
  40. typedef int DEVICE_ID;
  41. typedef pthread_t THREAD_HANDLE;
  42. typedef pthread_mutex_t MUTEX;
  43. #elif defined(__LINUX_OSS__)
  44. #include <pthread.h>
  45. #include <unistd.h>
  46. typedef int AUDIO_HANDLE;
  47. typedef int DEVICE_ID;
  48. typedef pthread_t THREAD_HANDLE;
  49. typedef pthread_mutex_t MUTEX;
  50. #elif defined(__WINDOWS_DS__)
  51. #include <windows.h>
  52. #include <process.h>
  53. // The following struct is used to hold the extra variables
  54. // specific to the DirectSound implementation.
  55. typedef struct {
  56. void * object;
  57. void * buffer;
  58. UINT bufferPointer;
  59. } AUDIO_HANDLE;
  60. typedef LPGUID DEVICE_ID;
  61. typedef unsigned long THREAD_HANDLE;
  62. typedef CRITICAL_SECTION MUTEX;
  63. #elif defined(__WINDOWS_ASIO__)
  64. #include <windows.h>
  65. #include <process.h>
  66. typedef int AUDIO_HANDLE;
  67. typedef int DEVICE_ID;
  68. typedef unsigned long THREAD_HANDLE;
  69. typedef CRITICAL_SECTION MUTEX;
  70. #elif defined(__IRIX_AL__)
  71. #include <dmedia/audio.h>
  72. #include <pthread.h>
  73. #include <unistd.h>
  74. typedef ALport AUDIO_HANDLE;
  75. typedef long DEVICE_ID;
  76. typedef pthread_t THREAD_HANDLE;
  77. typedef pthread_mutex_t MUTEX;
  78. #elif defined(__MACOSX_CORE__)
  79. #include <CoreAudio/AudioHardware.h>
  80. #include <pthread.h>
  81. typedef unsigned int AUDIO_HANDLE;
  82. typedef AudioDeviceID DEVICE_ID;
  83. typedef pthread_t THREAD_HANDLE;
  84. typedef pthread_mutex_t MUTEX;
  85. #endif
  86. /************************************************************************/
  87. /*! \class RtError
  88. \brief Exception handling class for RtAudio.
  89. The RtError class is quite simple but it does allow errors to be
  90. "caught" by RtError::TYPE. Almost all RtAudio methods can "throw"
  91. an RtError, most typically if an invalid stream identifier is
  92. supplied to a method or a driver error occurs. There are a number
  93. of cases within RtAudio where warning messages may be displayed
  94. but an exception is not thrown. There is a private RtAudio method,
  95. error(), which can be modified to globally control how these
  96. messages are handled and reported.
  97. */
  98. /************************************************************************/
  99. class RtError
  100. {
  101. public:
  102. //! Defined RtError types.
  103. enum TYPE {
  104. WARNING,
  105. DEBUG_WARNING,
  106. UNSPECIFIED,
  107. NO_DEVICES_FOUND,
  108. INVALID_DEVICE,
  109. INVALID_STREAM,
  110. MEMORY_ERROR,
  111. INVALID_PARAMETER,
  112. DRIVER_ERROR,
  113. SYSTEM_ERROR,
  114. THREAD_ERROR
  115. };
  116. protected:
  117. char error_message[256];
  118. TYPE type;
  119. public:
  120. //! The constructor.
  121. RtError(const char *p, TYPE tipe = RtError::UNSPECIFIED);
  122. //! The destructor.
  123. virtual ~RtError(void);
  124. //! Prints "thrown" error message to stdout.
  125. virtual void printMessage(void);
  126. //! Returns the "thrown" error message TYPE.
  127. virtual const TYPE& getType(void) { return type; }
  128. //! Returns the "thrown" error message string.
  129. virtual const char *getMessage(void) { return error_message; }
  130. };
  131. // This public structure type is used to pass callback information
  132. // between the private RtAudio stream structure and global callback
  133. // handling functions.
  134. typedef struct {
  135. void *object; // Used as a "this" pointer.
  136. int streamId;
  137. DEVICE_ID device[2];
  138. THREAD_HANDLE thread;
  139. void *callback;
  140. void *buffers;
  141. unsigned long waitTime;
  142. bool blockTick;
  143. bool stopStream;
  144. bool usingCallback;
  145. void *userData;
  146. } CALLBACK_INFO;
  147. // *************************************************** //
  148. //
  149. // RtAudio class declaration.
  150. //
  151. // *************************************************** //
  152. class RtAudio
  153. {
  154. public:
  155. // Support for signed integers and floats. Audio data fed to/from
  156. // the tickStream() routine is assumed to ALWAYS be in host
  157. // byte order. The internal routines will automatically take care of
  158. // any necessary byte-swapping between the host format and the
  159. // soundcard. Thus, endian-ness is not a concern in the following
  160. // format definitions.
  161. typedef unsigned long RTAUDIO_FORMAT;
  162. static const RTAUDIO_FORMAT RTAUDIO_SINT8; /*!< 8-bit signed integer. */
  163. static const RTAUDIO_FORMAT RTAUDIO_SINT16; /*!< 16-bit signed integer. */
  164. static const RTAUDIO_FORMAT RTAUDIO_SINT24; /*!< Upper 3 bytes of 32-bit signed integer. */
  165. static const RTAUDIO_FORMAT RTAUDIO_SINT32; /*!< 32-bit signed integer. */
  166. static const RTAUDIO_FORMAT RTAUDIO_FLOAT32; /*!< Normalized between plus/minus 1.0. */
  167. static const RTAUDIO_FORMAT RTAUDIO_FLOAT64; /*!< Normalized between plus/minus 1.0. */
  168. //static const int MAX_SAMPLE_RATES = 14;
  169. enum { MAX_SAMPLE_RATES = 14 };
  170. typedef int (*RTAUDIO_CALLBACK)(char *buffer, int bufferSize, void *userData);
  171. //! The public device information structure for passing queried values.
  172. typedef struct {
  173. char name[128]; /*!< Character string device identifier. */
  174. DEVICE_ID id[2]; /* No value reported by getDeviceInfo(). */
  175. bool probed; /*!< true if the device capabilities were successfully probed. */
  176. int maxOutputChannels; /*!< Maximum output channels supported by device. */
  177. int maxInputChannels; /*!< Maximum input channels supported by device. */
  178. int maxDuplexChannels; /*!< Maximum simultaneous input/output channels supported by device. */
  179. int minOutputChannels; /*!< Minimum output channels supported by device. */
  180. int minInputChannels; /*!< Minimum input channels supported by device. */
  181. int minDuplexChannels; /*!< Minimum simultaneous input/output channels supported by device. */
  182. bool hasDuplexSupport; /*!< true if device supports duplex mode. */
  183. bool isDefault; /*!< true if this is the default output or input device. */
  184. int nSampleRates; /*!< Number of discrete rates or -1 if range supported. */
  185. int sampleRates[MAX_SAMPLE_RATES]; /*!< Supported rates or (min, max) if range. */
  186. RTAUDIO_FORMAT nativeFormats; /*!< Bit mask of supported data formats. */
  187. } RTAUDIO_DEVICE;
  188. //! The default constructor.
  189. /*!
  190. Probes the system to make sure at least one audio input/output
  191. device is available and determines the api-specific identifier for
  192. each device found. An RtError error can be thrown if no devices
  193. are found or if a memory allocation error occurs.
  194. */
  195. RtAudio();
  196. //! A constructor which can be used to open a stream during instantiation.
  197. /*!
  198. The specified output and/or input device identifiers correspond
  199. to those enumerated via the getDeviceInfo() method. If device =
  200. 0, the default or first available devices meeting the given
  201. parameters is selected. If an output or input channel value is
  202. zero, the corresponding device value is ignored. When a stream is
  203. successfully opened, its identifier is returned via the "streamId"
  204. pointer. An RtError can be thrown if no devices are found
  205. for the given parameters, if a memory allocation error occurs, or
  206. if a driver error occurs. \sa openStream()
  207. */
  208. RtAudio(int *streamId,
  209. int outputDevice, int outputChannels,
  210. int inputDevice, int inputChannels,
  211. RTAUDIO_FORMAT format, int sampleRate,
  212. int *bufferSize, int numberOfBuffers);
  213. //! The destructor.
  214. /*!
  215. Stops and closes any open streams and devices and deallocates
  216. buffer and structure memory.
  217. */
  218. ~RtAudio();
  219. //! A public method for opening a stream with the specified parameters.
  220. /*!
  221. If successful, the opened stream ID is returned. Otherwise, an
  222. RtError is thrown.
  223. \param outputDevice: If equal to 0, the default or first device
  224. found meeting the given parameters is opened. Otherwise, the
  225. device number should correspond to one of those enumerated via
  226. the getDeviceInfo() method.
  227. \param outputChannels: The desired number of output channels. If
  228. equal to zero, the outputDevice identifier is ignored.
  229. \param inputDevice: If equal to 0, the default or first device
  230. found meeting the given parameters is opened. Otherwise, the
  231. device number should correspond to one of those enumerated via
  232. the getDeviceInfo() method.
  233. \param inputChannels: The desired number of input channels. If
  234. equal to zero, the inputDevice identifier is ignored.
  235. \param format: An RTAUDIO_FORMAT specifying the desired sample data format.
  236. \param sampleRate: The desired sample rate (sample frames per second).
  237. \param *bufferSize: A pointer value indicating the desired internal buffer
  238. size in sample frames. The actual value used by the device is
  239. returned via the same pointer. A value of zero can be specified,
  240. in which case the lowest allowable value is determined.
  241. \param numberOfBuffers: A value which can be used to help control device
  242. latency. More buffers typically result in more robust performance,
  243. though at a cost of greater latency. A value of zero can be
  244. specified, in which case the lowest allowable value is used.
  245. */
  246. int openStream(int outputDevice, int outputChannels,
  247. int inputDevice, int inputChannels,
  248. RTAUDIO_FORMAT format, int sampleRate,
  249. int *bufferSize, int numberOfBuffers);
  250. //! A public method which sets a user-defined callback function for a given stream.
  251. /*!
  252. This method assigns a callback function to a specific,
  253. previously opened stream for non-blocking stream functionality. A
  254. separate process is initiated, though the user function is called
  255. only when the stream is "running" (between calls to the
  256. startStream() and stopStream() methods, respectively). The
  257. callback process remains active for the duration of the stream and
  258. is automatically shutdown when the stream is closed (via the
  259. closeStream() method or by object destruction). The callback
  260. process can also be shutdown and the user function de-referenced
  261. through an explicit call to the cancelStreamCallback() method.
  262. Note that a single stream can use only blocking or callback
  263. functionality at the same time, though it is possible to alternate
  264. modes on the same stream through the use of the
  265. setStreamCallback() and cancelStreamCallback() methods (the
  266. blocking tickStream() method can be used before a callback is set
  267. and/or after a callback is cancelled). An RtError will be thrown
  268. for an invalid device argument.
  269. */
  270. void setStreamCallback(int streamId, RTAUDIO_CALLBACK callback, void *userData);
  271. //! A public method which cancels a callback process and function for a given stream.
  272. /*!
  273. This method shuts down a callback process and de-references the
  274. user function for a specific stream. Callback functionality can
  275. subsequently be restarted on the stream via the
  276. setStreamCallback() method. An RtError will be thrown for an
  277. invalid device argument.
  278. */
  279. void cancelStreamCallback(int streamId);
  280. //! A public method which returns the number of audio devices found.
  281. int getDeviceCount(void);
  282. //! Fill a user-supplied RTAUDIO_DEVICE structure for a specified device number.
  283. /*!
  284. Any device integer between 1 and getDeviceCount() is valid. If
  285. a device is busy or otherwise unavailable, the structure member
  286. "probed" will have a value of "false" and all other members are
  287. undefined. If the specified device is the current default input
  288. or output device, the "isDefault" member will have a value of
  289. "true". An RtError will be thrown for an invalid device argument.
  290. */
  291. void getDeviceInfo(int device, RTAUDIO_DEVICE *info);
  292. //! A public method which returns a pointer to the buffer for an open stream.
  293. /*!
  294. The user should fill and/or read the buffer data in interleaved format
  295. and then call the tickStream() method. An RtError will be
  296. thrown for an invalid stream identifier.
  297. */
  298. char * const getStreamBuffer(int streamId);
  299. //! Public method used to trigger processing of input/output data for a stream.
  300. /*!
  301. This method blocks until all buffer data is read/written. An
  302. RtError will be thrown for an invalid stream identifier or if
  303. a driver error occurs.
  304. */
  305. void tickStream(int streamId);
  306. //! Public method which closes a stream and frees any associated buffers.
  307. /*!
  308. If an invalid stream identifier is specified, this method
  309. issues a warning and returns (an RtError is not thrown).
  310. */
  311. void closeStream(int streamId);
  312. //! Public method which starts a stream.
  313. /*!
  314. An RtError will be thrown for an invalid stream identifier
  315. or if a driver error occurs.
  316. */
  317. void startStream(int streamId);
  318. //! Stop a stream, allowing any samples remaining in the queue to be played out and/or read in.
  319. /*!
  320. An RtError will be thrown for an invalid stream identifier
  321. or if a driver error occurs.
  322. */
  323. void stopStream(int streamId);
  324. //! Stop a stream, discarding any samples remaining in the input/output queue.
  325. /*!
  326. An RtError will be thrown for an invalid stream identifier
  327. or if a driver error occurs.
  328. */
  329. void abortStream(int streamId);
  330. //! Queries a stream to determine whether a call to the tickStream() method will block.
  331. /*!
  332. A return value of 0 indicates that the stream will NOT block. A positive
  333. return value indicates the number of sample frames that cannot yet be
  334. processed without blocking.
  335. */
  336. int streamWillBlock(int streamId);
  337. #if (defined(__MACOSX_CORE__) || defined(__WINDOWS_ASIO__))
  338. // This function is intended for internal use only. It must be
  339. // public because it is called by the internal callback handler,
  340. // which is not a member of RtAudio. External use of this function
  341. // will most likely produce highly undesireable results!
  342. void callbackEvent(int streamId, DEVICE_ID deviceId, void *inData, void *outData);
  343. #endif
  344. protected:
  345. private:
  346. static const unsigned int SAMPLE_RATES[MAX_SAMPLE_RATES];
  347. enum { FAILURE, SUCCESS };
  348. enum STREAM_MODE {
  349. OUTPUT,
  350. INPUT,
  351. DUPLEX,
  352. UNINITIALIZED = -75
  353. };
  354. enum STREAM_STATE {
  355. STREAM_STOPPED,
  356. STREAM_RUNNING
  357. };
  358. typedef struct {
  359. int device[2]; // Playback and record, respectively.
  360. STREAM_MODE mode; // OUTPUT, INPUT, or DUPLEX.
  361. AUDIO_HANDLE handle[2]; // Playback and record handles, respectively.
  362. STREAM_STATE state; // STOPPED or RUNNING
  363. char *userBuffer;
  364. char *deviceBuffer;
  365. bool doConvertBuffer[2]; // Playback and record, respectively.
  366. bool deInterleave[2]; // Playback and record, respectively.
  367. bool doByteSwap[2]; // Playback and record, respectively.
  368. int sampleRate;
  369. int bufferSize;
  370. int nBuffers;
  371. int nUserChannels[2]; // Playback and record, respectively.
  372. int nDeviceChannels[2]; // Playback and record channels, respectively.
  373. RTAUDIO_FORMAT userFormat;
  374. RTAUDIO_FORMAT deviceFormat[2]; // Playback and record, respectively.
  375. MUTEX mutex;
  376. CALLBACK_INFO callbackInfo;
  377. } RTAUDIO_STREAM;
  378. typedef signed short INT16;
  379. typedef signed int INT32;
  380. typedef float FLOAT32;
  381. typedef double FLOAT64;
  382. char message[256];
  383. int nDevices;
  384. RTAUDIO_DEVICE *devices;
  385. std::map<int, void *> streams;
  386. //! Private error method to allow global control over error handling.
  387. void error(RtError::TYPE type);
  388. /*!
  389. Private method to count the system audio devices, allocate the
  390. RTAUDIO_DEVICE structures, and probe the device capabilities.
  391. */
  392. void initialize(void);
  393. /*!
  394. Private method which returns the index in the devices array to
  395. the default input device.
  396. */
  397. int getDefaultInputDevice(void);
  398. /*!
  399. Private method which returns the index in the devices array to
  400. the default output device.
  401. */
  402. int getDefaultOutputDevice(void);
  403. //! Private method to clear an RTAUDIO_DEVICE structure.
  404. void clearDeviceInfo(RTAUDIO_DEVICE *info);
  405. /*!
  406. Private method which attempts to fill an RTAUDIO_DEVICE
  407. structure for a given device. If an error is encountered during
  408. the probe, a "warning" message is reported and the value of
  409. "probed" remains false (no exception is thrown). A successful
  410. probe is indicated by probed = true.
  411. */
  412. void probeDeviceInfo(RTAUDIO_DEVICE *info);
  413. /*!
  414. Private method which attempts to open a device with the given parameters.
  415. If an error is encountered during the probe, a "warning" message is
  416. reported and FAILURE is returned (no exception is thrown). A
  417. successful probe is indicated by a return value of SUCCESS.
  418. */
  419. bool probeDeviceOpen(int device, RTAUDIO_STREAM *stream,
  420. STREAM_MODE mode, int channels,
  421. int sampleRate, RTAUDIO_FORMAT format,
  422. int *bufferSize, int numberOfBuffers);
  423. /*!
  424. Private common method used to check validity of a user-passed
  425. stream ID. When the ID is valid, this method returns a pointer to
  426. an RTAUDIO_STREAM structure (in the form of a void pointer).
  427. Otherwise, an "invalid identifier" exception is thrown.
  428. */
  429. void *verifyStream(int streamId);
  430. /*!
  431. Private method used to perform format, channel number, and/or interleaving
  432. conversions between the user and device buffers.
  433. */
  434. void convertStreamBuffer(RTAUDIO_STREAM *stream, STREAM_MODE mode);
  435. //! Private method used to perform byte-swapping on buffers.
  436. void byteSwapBuffer(char *buffer, int samples, RTAUDIO_FORMAT format);
  437. //! Private method which returns the number of bytes for a given format.
  438. int formatBytes(RTAUDIO_FORMAT format);
  439. };
  440. // Define the following flag to have extra information spewed to stderr.
  441. //#define __RTAUDIO_DEBUG__
  442. #endif