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.

296 lines
20KB

  1. <HTML>
  2. <HEAD>
  3. <TITLE>The RtAudio Tutorial</TITLE>
  4. <LINK HREF="doxygen.css" REL="stylesheet" TYPE="text/css">
  5. </HEAD>
  6. <BODY BGCOLOR="#FFFFFF">
  7. <CENTER>
  8. <a class="qindex" href="index.html">Tutorial</a> &nbsp; <a class="qindex" href="annotated.html">Class/Enum List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </CENTER>
  9. <HR>
  10. <!-- Generated by Doxygen 1.2.8.1 -->
  11. <h1>RtAudio.h</h1><a href="RtAudio_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <font class="comment">/******************************************/</font>
  12. 00002 <font class="comment">/*</font>
  13. 00003 <font class="comment"> RtAudio - realtime sound I/O C++ class</font>
  14. 00004 <font class="comment"> Version 2.0 by Gary P. Scavone, 2001-2002.</font>
  15. 00005 <font class="comment">*/</font>
  16. 00006 <font class="comment">/******************************************/</font>
  17. 00007
  18. 00008 <font class="preprocessor">#if !defined(__RtAudio_h)</font>
  19. <a name="l00009"></a><a class="code" href="RtAudio_8h.html#a0">00009</a> <font class="preprocessor"></font><font class="preprocessor">#define __RtAudio_h</font>
  20. 00010 <font class="preprocessor"></font>
  21. 00011 <font class="preprocessor">#include &lt;map&gt;</font>
  22. 00012
  23. 00013 <font class="preprocessor">#if defined(__LINUX_ALSA_)</font>
  24. 00014 <font class="preprocessor"></font><font class="preprocessor"> #include &lt;alsa/asoundlib.h&gt;</font>
  25. 00015 <font class="preprocessor"> #include &lt;pthread.h&gt;</font>
  26. 00016 <font class="preprocessor"> #include &lt;unistd.h&gt;</font>
  27. 00017
  28. 00018 <font class="keyword">typedef</font> snd_pcm_t *AUDIO_HANDLE;
  29. 00019 <font class="keyword">typedef</font> <font class="keywordtype">int</font> DEVICE_ID;
  30. 00020 <font class="keyword">typedef</font> pthread_t THREAD_HANDLE;
  31. 00021 <font class="keyword">typedef</font> pthread_mutex_t MUTEX;
  32. 00022
  33. 00023 <font class="preprocessor">#elif defined(__LINUX_OSS_)</font>
  34. 00024 <font class="preprocessor"></font><font class="preprocessor"> #include &lt;pthread.h&gt;</font>
  35. 00025 <font class="preprocessor"> #include &lt;unistd.h&gt;</font>
  36. 00026
  37. 00027 <font class="keyword">typedef</font> <font class="keywordtype">int</font> AUDIO_HANDLE;
  38. 00028 <font class="keyword">typedef</font> <font class="keywordtype">int</font> DEVICE_ID;
  39. 00029 <font class="keyword">typedef</font> pthread_t THREAD_HANDLE;
  40. 00030 <font class="keyword">typedef</font> pthread_mutex_t MUTEX;
  41. 00031
  42. 00032 <font class="preprocessor">#elif defined(__WINDOWS_DS_)</font>
  43. 00033 <font class="preprocessor"></font><font class="preprocessor"> #include &lt;windows.h&gt;</font>
  44. 00034 <font class="preprocessor"> #include &lt;process.h&gt;</font>
  45. 00035
  46. 00036 <font class="comment">// The following struct is used to hold the extra variables</font>
  47. 00037 <font class="comment">// specific to the DirectSound implementation.</font>
  48. 00038 <font class="keyword">typedef</font> <font class="keyword">struct </font>{
  49. 00039 <font class="keywordtype">void</font> * object;
  50. 00040 <font class="keywordtype">void</font> * buffer;
  51. 00041 UINT bufferPointer;
  52. 00042 } AUDIO_HANDLE;
  53. 00043
  54. 00044 <font class="keyword">typedef</font> LPGUID DEVICE_ID;
  55. 00045 <font class="keyword">typedef</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> THREAD_HANDLE;
  56. 00046 <font class="keyword">typedef</font> CRITICAL_SECTION MUTEX;
  57. 00047
  58. 00048 <font class="preprocessor">#elif defined(__IRIX_AL_)</font>
  59. 00049 <font class="preprocessor"></font><font class="preprocessor"> #include &lt;dmedia/audio.h&gt;</font>
  60. 00050 <font class="preprocessor"> #include &lt;pthread.h&gt;</font>
  61. 00051 <font class="preprocessor"> #include &lt;unistd.h&gt;</font>
  62. 00052
  63. 00053 <font class="keyword">typedef</font> ALport AUDIO_HANDLE;
  64. 00054 <font class="keyword">typedef</font> <font class="keywordtype">int</font> DEVICE_ID;
  65. 00055 <font class="keyword">typedef</font> pthread_t THREAD_HANDLE;
  66. 00056 <font class="keyword">typedef</font> pthread_mutex_t MUTEX;
  67. 00057
  68. 00058 <font class="preprocessor">#endif</font>
  69. 00059 <font class="preprocessor"></font>
  70. 00060
  71. 00061 <font class="comment">// *************************************************** //</font>
  72. 00062 <font class="comment">//</font>
  73. 00063 <font class="comment">// RtAudioError class declaration.</font>
  74. 00064 <font class="comment">//</font>
  75. 00065 <font class="comment">// *************************************************** //</font>
  76. 00066
  77. <a name="l00067"></a><a class="code" href="classRtAudioError.html">00067</a> <font class="keyword">class </font><a class="code" href="classRtAudioError.html">RtAudioError</a>
  78. 00068 {
  79. 00069 <font class="keyword">public</font>:
  80. <a name="l00070"></a><a class="code" href="classRtAudioError.html#s11">00070</a> <font class="keyword">enum</font> TYPE {
  81. 00071 WARNING,
  82. 00072 DEBUG_WARNING,
  83. 00073 UNSPECIFIED,
  84. 00074 NO_DEVICES_FOUND,
  85. 00075 INVALID_DEVICE,
  86. 00076 INVALID_STREAM,
  87. 00077 MEMORY_ERROR,
  88. 00078 INVALID_PARAMETER,
  89. 00079 DRIVER_ERROR,
  90. 00080 SYSTEM_ERROR,
  91. 00081 THREAD_ERROR
  92. 00082 };
  93. 00083
  94. 00084 <font class="keyword">protected</font>:
  95. <a name="l00085"></a><a class="code" href="classRtAudioError.html#n0">00085</a> <font class="keywordtype">char</font> error_message[256];
  96. <a name="l00086"></a><a class="code" href="classRtAudioError.html#n1">00086</a> TYPE type;
  97. 00087
  98. 00088 <font class="keyword">public</font>:
  99. 00090 <a class="code" href="classRtAudioError.html#a0">RtAudioError</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *p, TYPE tipe = RtAudioError::UNSPECIFIED);
  100. 00091
  101. 00093 <font class="keyword">virtual</font> <a class="code" href="classRtAudioError.html#a1">~RtAudioError</a>(<font class="keywordtype">void</font>);
  102. 00094
  103. 00096 <font class="keyword">virtual</font> <font class="keywordtype">void</font> <a class="code" href="classRtAudioError.html#a2">printMessage</a>(<font class="keywordtype">void</font>);
  104. 00097
  105. <a name="l00099"></a><a class="code" href="classRtAudioError.html#a3">00099</a> <font class="keyword">virtual</font> <font class="keyword">const</font> TYPE&amp; <a class="code" href="classRtAudioError.html#a3">getType</a>(<font class="keywordtype">void</font>)<font class="keyword"> </font>{ <font class="keywordflow">return</font> type; }
  106. 00100
  107. <a name="l00102"></a><a class="code" href="classRtAudioError.html#a4">00102</a> <font class="keyword">virtual</font> <font class="keyword">const</font> <font class="keywordtype">char</font> *<a class="code" href="classRtAudioError.html#a4">getMessage</a>(<font class="keywordtype">void</font>)<font class="keyword"> </font>{ <font class="keywordflow">return</font> error_message; }
  108. 00103 };
  109. 00104
  110. 00105
  111. 00106 <font class="comment">// *************************************************** //</font>
  112. 00107 <font class="comment">//</font>
  113. 00108 <font class="comment">// RtAudio class declaration.</font>
  114. 00109 <font class="comment">//</font>
  115. 00110 <font class="comment">// *************************************************** //</font>
  116. 00111
  117. <a name="l00112"></a><a class="code" href="classRtAudio.html">00112</a> <font class="keyword">class </font><a class="code" href="classRtAudio.html">RtAudio</a>
  118. 00113 {
  119. 00114 <font class="keyword">public</font>:
  120. 00115
  121. 00116 <font class="comment">// Support for signed integers and floats. Audio data fed to/from</font>
  122. 00117 <font class="comment">// the tickStream() routine is assumed to ALWAYS be in host</font>
  123. 00118 <font class="comment">// byte order. The internal routines will automatically take care of</font>
  124. 00119 <font class="comment">// any necessary byte-swapping between the host format and the</font>
  125. 00120 <font class="comment">// soundcard. Thus, endian-ness is not a concern in the following</font>
  126. 00121 <font class="comment">// format definitions.</font>
  127. <a name="l00122"></a><a class="code" href="classRtAudio.html#s0">00122</a> <font class="keyword">typedef</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> RTAUDIO_FORMAT;
  128. <a name="l00123"></a><a class="code" href="classRtAudio.html#p0">00123</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_SINT8;
  129. <a name="l00124"></a><a class="code" href="classRtAudio.html#p1">00124</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_SINT16;
  130. <a name="l00125"></a><a class="code" href="classRtAudio.html#p2">00125</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_SINT24;
  131. <a name="l00126"></a><a class="code" href="classRtAudio.html#p3">00126</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_SINT32;
  132. <a name="l00127"></a><a class="code" href="classRtAudio.html#p4">00127</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_FLOAT32;
  133. <a name="l00128"></a><a class="code" href="classRtAudio.html#p5">00128</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_FLOAT64;
  134. 00130 <font class="comment">//static const int MAX_SAMPLE_RATES = 14;</font>
  135. 00131 <font class="keyword">enum</font> { MAX_SAMPLE_RATES = 14 };
  136. 00132
  137. 00133 <font class="keyword">typedef</font> int (*RTAUDIO_CALLBACK)(<font class="keywordtype">char</font> *buffer, <font class="keywordtype">int</font> bufferSize, <font class="keywordtype">void</font> *userData);
  138. 00134
  139. <a name="l00135"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html">00135</a> <font class="keyword">typedef</font> <font class="keyword">struct </font>{
  140. <a name="l00136"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m0">00136</a> <font class="keywordtype">char</font> name[128];
  141. <a name="l00137"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m1">00137</a> DEVICE_ID id[2];
  142. <a name="l00138"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m2">00138</a> <font class="keywordtype">bool</font> probed;
  143. <a name="l00139"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m3">00139</a> <font class="keywordtype">int</font> maxOutputChannels;
  144. <a name="l00140"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m4">00140</a> <font class="keywordtype">int</font> maxInputChannels;
  145. <a name="l00141"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m5">00141</a> <font class="keywordtype">int</font> maxDuplexChannels;
  146. <a name="l00142"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m6">00142</a> <font class="keywordtype">int</font> minOutputChannels;
  147. <a name="l00143"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m7">00143</a> <font class="keywordtype">int</font> minInputChannels;
  148. <a name="l00144"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m8">00144</a> <font class="keywordtype">int</font> minDuplexChannels;
  149. <a name="l00145"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m9">00145</a> <font class="keywordtype">bool</font> hasDuplexSupport;
  150. <a name="l00146"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m10">00146</a> <font class="keywordtype">int</font> nSampleRates;
  151. <a name="l00147"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m11">00147</a> <font class="keywordtype">int</font> sampleRates[MAX_SAMPLE_RATES];
  152. <a name="l00148"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m12">00148</a> RTAUDIO_FORMAT nativeFormats;
  153. 00149 } RTAUDIO_DEVICE;
  154. 00150
  155. 00152
  156. 00159 <a class="code" href="classRtAudio.html#a0">RtAudio</a>();
  157. 00160
  158. 00162
  159. 00173 <a class="code" href="classRtAudio.html#a0">RtAudio</a>(<font class="keywordtype">int</font> *streamID,
  160. 00174 <font class="keywordtype">int</font> outputDevice, <font class="keywordtype">int</font> outputChannels,
  161. 00175 <font class="keywordtype">int</font> inputDevice, <font class="keywordtype">int</font> inputChannels,
  162. 00176 RTAUDIO_FORMAT format, <font class="keywordtype">int</font> sampleRate,
  163. 00177 <font class="keywordtype">int</font> *bufferSize, <font class="keywordtype">int</font> numberOfBuffers);
  164. 00178
  165. 00180
  166. 00184 <a class="code" href="classRtAudio.html#a2">~RtAudio</a>();
  167. 00185
  168. 00187
  169. 00214 <font class="keywordtype">int</font> <a class="code" href="classRtAudio.html#a3">openStream</a>(<font class="keywordtype">int</font> outputDevice, <font class="keywordtype">int</font> outputChannels,
  170. 00215 <font class="keywordtype">int</font> inputDevice, <font class="keywordtype">int</font> inputChannels,
  171. 00216 RTAUDIO_FORMAT format, <font class="keywordtype">int</font> sampleRate,
  172. 00217 <font class="keywordtype">int</font> *bufferSize, <font class="keywordtype">int</font> numberOfBuffers);
  173. 00218
  174. 00220
  175. 00239 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a4">setStreamCallback</a>(<font class="keywordtype">int</font> streamID, RTAUDIO_CALLBACK callback, <font class="keywordtype">void</font> *userData);
  176. 00240
  177. 00242
  178. 00249 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a5">cancelStreamCallback</a>(<font class="keywordtype">int</font> streamID);
  179. 00250
  180. 00252 <font class="keywordtype">int</font> <a class="code" href="classRtAudio.html#a6">getDeviceCount</a>(<font class="keywordtype">void</font>);
  181. 00253
  182. 00255
  183. 00265 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a7">getDeviceInfo</a>(<font class="keywordtype">int</font> device, RTAUDIO_DEVICE *info);
  184. 00266
  185. 00268
  186. 00273 <font class="keywordtype">char</font> * <font class="keyword">const</font> <a class="code" href="classRtAudio.html#a8">getStreamBuffer</a>(<font class="keywordtype">int</font> streamID);
  187. 00274
  188. 00276
  189. 00281 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a9">tickStream</a>(<font class="keywordtype">int</font> streamID);
  190. 00282
  191. 00284
  192. 00288 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a10">closeStream</a>(<font class="keywordtype">int</font> streamID);
  193. 00289
  194. 00291
  195. 00295 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a11">startStream</a>(<font class="keywordtype">int</font> streamID);
  196. 00296
  197. 00298
  198. 00302 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a12">stopStream</a>(<font class="keywordtype">int</font> streamID);
  199. 00303
  200. 00305
  201. 00309 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a13">abortStream</a>(<font class="keywordtype">int</font> streamID);
  202. 00310
  203. 00312
  204. 00317 <font class="keywordtype">int</font> <a class="code" href="classRtAudio.html#a14">streamWillBlock</a>(<font class="keywordtype">int</font> streamID);
  205. 00318
  206. 00319 <font class="keyword">protected</font>:
  207. 00320
  208. 00321 <font class="keyword">private</font>:
  209. 00322
  210. 00323 <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> SAMPLE_RATES[MAX_SAMPLE_RATES];
  211. 00324
  212. 00325 <font class="keyword">enum</font> { FAILURE, SUCCESS };
  213. 00326
  214. 00327 <font class="keyword">enum</font> STREAM_MODE {
  215. 00328 PLAYBACK,
  216. 00329 RECORD,
  217. 00330 DUPLEX,
  218. 00331 UNINITIALIZED = -75
  219. 00332 };
  220. 00333
  221. 00334 <font class="keyword">enum</font> STREAM_STATE {
  222. 00335 STREAM_STOPPED,
  223. 00336 STREAM_RUNNING
  224. 00337 };
  225. 00338
  226. 00339 <font class="keyword">typedef</font> <font class="keyword">struct </font>{
  227. 00340 <font class="keywordtype">int</font> device[2]; <font class="comment">// Playback and record, respectively.</font>
  228. 00341 STREAM_MODE mode; <font class="comment">// PLAYBACK, RECORD, or DUPLEX.</font>
  229. 00342 AUDIO_HANDLE handle[2]; <font class="comment">// Playback and record handles, respectively.</font>
  230. 00343 STREAM_STATE state; <font class="comment">// STOPPED or RUNNING</font>
  231. 00344 <font class="keywordtype">char</font> *userBuffer;
  232. 00345 <font class="keywordtype">char</font> *deviceBuffer;
  233. 00346 <font class="keywordtype">bool</font> doConvertBuffer[2]; <font class="comment">// Playback and record, respectively.</font>
  234. 00347 <font class="keywordtype">bool</font> deInterleave[2]; <font class="comment">// Playback and record, respectively.</font>
  235. 00348 <font class="keywordtype">bool</font> doByteSwap[2]; <font class="comment">// Playback and record, respectively.</font>
  236. 00349 <font class="keywordtype">int</font> sampleRate;
  237. 00350 <font class="keywordtype">int</font> bufferSize;
  238. 00351 <font class="keywordtype">int</font> nBuffers;
  239. 00352 <font class="keywordtype">int</font> nUserChannels[2]; <font class="comment">// Playback and record, respectively.</font>
  240. 00353 <font class="keywordtype">int</font> nDeviceChannels[2]; <font class="comment">// Playback and record channels, respectively.</font>
  241. 00354 RTAUDIO_FORMAT userFormat;
  242. 00355 RTAUDIO_FORMAT deviceFormat[2]; <font class="comment">// Playback and record, respectively.</font>
  243. 00356 <font class="keywordtype">bool</font> usingCallback;
  244. 00357 THREAD_HANDLE thread;
  245. 00358 MUTEX mutex;
  246. 00359 RTAUDIO_CALLBACK callback;
  247. 00360 <font class="keywordtype">void</font> *userData;
  248. 00361 } RTAUDIO_STREAM;
  249. 00362
  250. 00363 <font class="keyword">typedef</font> <font class="keywordtype">signed</font> <font class="keywordtype">short</font> INT16;
  251. 00364 <font class="keyword">typedef</font> <font class="keywordtype">signed</font> <font class="keywordtype">int</font> INT32;
  252. 00365 <font class="keyword">typedef</font> <font class="keywordtype">float</font> FLOAT32;
  253. 00366 <font class="keyword">typedef</font> <font class="keywordtype">double</font> FLOAT64;
  254. 00367
  255. 00368 <font class="keywordtype">char</font> message[256];
  256. 00369 <font class="keywordtype">int</font> nDevices;
  257. 00370 RTAUDIO_DEVICE *devices;
  258. 00371
  259. 00372 std::map&lt;int, void *&gt; streams;
  260. 00373
  261. 00375 <font class="keywordtype">void</font> error(RtAudioError::TYPE type);
  262. 00376
  263. 00381 <font class="keywordtype">void</font> initialize(<font class="keywordtype">void</font>);
  264. 00382
  265. 00384 <font class="keywordtype">void</font> clearDeviceInfo(RTAUDIO_DEVICE *info);
  266. 00385
  267. 00393 <font class="keywordtype">void</font> probeDeviceInfo(RTAUDIO_DEVICE *info);
  268. 00394
  269. 00401 <font class="keywordtype">bool</font> probeDeviceOpen(<font class="keywordtype">int</font> device, RTAUDIO_STREAM *stream,
  270. 00402 STREAM_MODE mode, <font class="keywordtype">int</font> channels,
  271. 00403 <font class="keywordtype">int</font> sampleRate, RTAUDIO_FORMAT format,
  272. 00404 <font class="keywordtype">int</font> *bufferSize, <font class="keywordtype">int</font> numberOfBuffers);
  273. 00405
  274. 00412 <font class="keywordtype">void</font> *verifyStream(<font class="keywordtype">int</font> streamID);
  275. 00413
  276. 00418 <font class="keywordtype">void</font> convertStreamBuffer(RTAUDIO_STREAM *stream, STREAM_MODE mode);
  277. 00419
  278. 00421 <font class="keywordtype">void</font> byteSwapBuffer(<font class="keywordtype">char</font> *buffer, <font class="keywordtype">int</font> samples, RTAUDIO_FORMAT format);
  279. 00422
  280. 00424 <font class="keywordtype">int</font> formatBytes(RTAUDIO_FORMAT format);
  281. 00425 };
  282. 00426
  283. 00427 <font class="comment">// Uncomment the following definition to have extra information spewed to stderr.</font>
  284. 00428 <font class="comment">//#define RTAUDIO_DEBUG</font>
  285. 00429
  286. 00430 <font class="preprocessor">#endif</font>
  287. </font></pre></div><HR>
  288. <table><tr><td><img src="../ccrma.gif">
  289. <td>&copy;2001-2002 CCRMA, Stanford University. All Rights Reserved.<br>
  290. Maintained by Gary P. Scavone, <a href="mailto:gary@ccrma.stanford.edu">gary@ccrma.stanford.edu</a><P>
  291. </table>
  292. </BODY>
  293. </HTML>