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.

301 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"> 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="preprocessor"> #define THREAD_TYPE</font>
  29. 00019 <font class="preprocessor"></font> <font class="keyword">typedef</font> snd_pcm_t *AUDIO_HANDLE;
  30. 00020 <font class="keyword">typedef</font> <font class="keywordtype">int</font> DEVICE_ID;
  31. 00021 <font class="keyword">typedef</font> pthread_t THREAD_HANDLE;
  32. 00022 <font class="keyword">typedef</font> pthread_mutex_t MUTEX;
  33. 00023
  34. 00024 <font class="preprocessor">#elif defined(__LINUX_OSS__)</font>
  35. 00025 <font class="preprocessor"></font><font class="preprocessor"> #include &lt;pthread.h&gt;</font>
  36. 00026 <font class="preprocessor"> #include &lt;unistd.h&gt;</font>
  37. 00027
  38. 00028 <font class="preprocessor"> #define THREAD_TYPE</font>
  39. 00029 <font class="preprocessor"></font> <font class="keyword">typedef</font> <font class="keywordtype">int</font> AUDIO_HANDLE;
  40. 00030 <font class="keyword">typedef</font> <font class="keywordtype">int</font> DEVICE_ID;
  41. 00031 <font class="keyword">typedef</font> pthread_t THREAD_HANDLE;
  42. 00032 <font class="keyword">typedef</font> pthread_mutex_t MUTEX;
  43. 00033
  44. 00034 <font class="preprocessor">#elif defined(__WINDOWS_DS__)</font>
  45. 00035 <font class="preprocessor"></font><font class="preprocessor"> #include &lt;windows.h&gt;</font>
  46. 00036 <font class="preprocessor"> #include &lt;process.h&gt;</font>
  47. 00037
  48. 00038 <font class="comment">// The following struct is used to hold the extra variables</font>
  49. 00039 <font class="comment">// specific to the DirectSound implementation.</font>
  50. 00040 <font class="keyword">typedef</font> <font class="keyword">struct </font>{
  51. 00041 <font class="keywordtype">void</font> * object;
  52. 00042 <font class="keywordtype">void</font> * buffer;
  53. 00043 UINT bufferPointer;
  54. 00044 } AUDIO_HANDLE;
  55. 00045
  56. 00046 <font class="preprocessor"> #define THREAD_TYPE __stdcall</font>
  57. 00047 <font class="preprocessor"></font> <font class="keyword">typedef</font> LPGUID DEVICE_ID;
  58. 00048 <font class="keyword">typedef</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> THREAD_HANDLE;
  59. 00049 <font class="keyword">typedef</font> CRITICAL_SECTION MUTEX;
  60. 00050
  61. 00051 <font class="preprocessor">#elif defined(__IRIX_AL__)</font>
  62. 00052 <font class="preprocessor"></font><font class="preprocessor"> #include &lt;dmedia/audio.h&gt;</font>
  63. 00053 <font class="preprocessor"> #include &lt;pthread.h&gt;</font>
  64. 00054 <font class="preprocessor"> #include &lt;unistd.h&gt;</font>
  65. 00055
  66. 00056 <font class="preprocessor"> #define THREAD_TYPE</font>
  67. 00057 <font class="preprocessor"></font> <font class="keyword">typedef</font> ALport AUDIO_HANDLE;
  68. 00058 <font class="keyword">typedef</font> <font class="keywordtype">int</font> DEVICE_ID;
  69. 00059 <font class="keyword">typedef</font> pthread_t THREAD_HANDLE;
  70. 00060 <font class="keyword">typedef</font> pthread_mutex_t MUTEX;
  71. 00061
  72. 00062 <font class="preprocessor">#endif</font>
  73. 00063 <font class="preprocessor"></font>
  74. 00064
  75. 00065 <font class="comment">// *************************************************** //</font>
  76. 00066 <font class="comment">//</font>
  77. 00067 <font class="comment">// RtError class declaration.</font>
  78. 00068 <font class="comment">//</font>
  79. 00069 <font class="comment">// *************************************************** //</font>
  80. 00070
  81. <a name="l00071"></a><a class="code" href="classRtError.html">00071</a> <font class="keyword">class </font><a class="code" href="classRtError.html">RtError</a>
  82. 00072 {
  83. 00073 <font class="keyword">public</font>:
  84. <a name="l00074"></a><a class="code" href="classRtError.html#s11">00074</a> <font class="keyword">enum</font> TYPE {
  85. 00075 WARNING,
  86. 00076 DEBUG_WARNING,
  87. 00077 UNSPECIFIED,
  88. 00078 NO_DEVICES_FOUND,
  89. 00079 INVALID_DEVICE,
  90. 00080 INVALID_STREAM,
  91. 00081 MEMORY_ERROR,
  92. 00082 INVALID_PARAMETER,
  93. 00083 DRIVER_ERROR,
  94. 00084 SYSTEM_ERROR,
  95. 00085 THREAD_ERROR
  96. 00086 };
  97. 00087
  98. 00088 <font class="keyword">protected</font>:
  99. <a name="l00089"></a><a class="code" href="classRtError.html#n0">00089</a> <font class="keywordtype">char</font> error_message[256];
  100. <a name="l00090"></a><a class="code" href="classRtError.html#n1">00090</a> TYPE type;
  101. 00091
  102. 00092 <font class="keyword">public</font>:
  103. 00094 <a class="code" href="classRtError.html#a0">RtError</a>(<font class="keyword">const</font> <font class="keywordtype">char</font> *p, TYPE tipe = RtError::UNSPECIFIED);
  104. 00095
  105. 00097 <font class="keyword">virtual</font> <a class="code" href="classRtError.html#a1">~RtError</a>(<font class="keywordtype">void</font>);
  106. 00098
  107. 00100 <font class="keyword">virtual</font> <font class="keywordtype">void</font> <a class="code" href="classRtError.html#a2">printMessage</a>(<font class="keywordtype">void</font>);
  108. 00101
  109. <a name="l00103"></a><a class="code" href="classRtError.html#a3">00103</a> <font class="keyword">virtual</font> <font class="keyword">const</font> TYPE&amp; <a class="code" href="classRtError.html#a3">getType</a>(<font class="keywordtype">void</font>)<font class="keyword"> </font>{ <font class="keywordflow">return</font> type; }
  110. 00104
  111. <a name="l00106"></a><a class="code" href="classRtError.html#a4">00106</a> <font class="keyword">virtual</font> <font class="keyword">const</font> <font class="keywordtype">char</font> *<a class="code" href="classRtError.html#a4">getMessage</a>(<font class="keywordtype">void</font>)<font class="keyword"> </font>{ <font class="keywordflow">return</font> error_message; }
  112. 00107 };
  113. 00108
  114. 00109
  115. 00110 <font class="comment">// *************************************************** //</font>
  116. 00111 <font class="comment">//</font>
  117. 00112 <font class="comment">// RtAudio class declaration.</font>
  118. 00113 <font class="comment">//</font>
  119. 00114 <font class="comment">// *************************************************** //</font>
  120. 00115
  121. <a name="l00116"></a><a class="code" href="classRtAudio.html">00116</a> <font class="keyword">class </font><a class="code" href="classRtAudio.html">RtAudio</a>
  122. 00117 {
  123. 00118 <font class="keyword">public</font>:
  124. 00119
  125. 00120 <font class="comment">// Support for signed integers and floats. Audio data fed to/from</font>
  126. 00121 <font class="comment">// the tickStream() routine is assumed to ALWAYS be in host</font>
  127. 00122 <font class="comment">// byte order. The internal routines will automatically take care of</font>
  128. 00123 <font class="comment">// any necessary byte-swapping between the host format and the</font>
  129. 00124 <font class="comment">// soundcard. Thus, endian-ness is not a concern in the following</font>
  130. 00125 <font class="comment">// format definitions.</font>
  131. <a name="l00126"></a><a class="code" href="classRtAudio.html#s0">00126</a> <font class="keyword">typedef</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> RTAUDIO_FORMAT;
  132. <a name="l00127"></a><a class="code" href="classRtAudio.html#p0">00127</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_SINT8;
  133. <a name="l00128"></a><a class="code" href="classRtAudio.html#p1">00128</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_SINT16;
  134. <a name="l00129"></a><a class="code" href="classRtAudio.html#p2">00129</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_SINT24;
  135. <a name="l00130"></a><a class="code" href="classRtAudio.html#p3">00130</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_SINT32;
  136. <a name="l00131"></a><a class="code" href="classRtAudio.html#p4">00131</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_FLOAT32;
  137. <a name="l00132"></a><a class="code" href="classRtAudio.html#p5">00132</a> <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT RTAUDIO_FLOAT64;
  138. 00133
  139. 00134 <font class="comment">//static const int MAX_SAMPLE_RATES = 14;</font>
  140. 00135 <font class="keyword">enum</font> { MAX_SAMPLE_RATES = 14 };
  141. 00136
  142. 00137 <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);
  143. 00138
  144. <a name="l00139"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html">00139</a> <font class="keyword">typedef</font> <font class="keyword">struct </font>{
  145. <a name="l00140"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m0">00140</a> <font class="keywordtype">char</font> name[128];
  146. <a name="l00141"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m1">00141</a> DEVICE_ID id[2];
  147. <a name="l00142"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m2">00142</a> <font class="keywordtype">bool</font> probed;
  148. <a name="l00143"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m3">00143</a> <font class="keywordtype">int</font> maxOutputChannels;
  149. <a name="l00144"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m4">00144</a> <font class="keywordtype">int</font> maxInputChannels;
  150. <a name="l00145"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m5">00145</a> <font class="keywordtype">int</font> maxDuplexChannels;
  151. <a name="l00146"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m6">00146</a> <font class="keywordtype">int</font> minOutputChannels;
  152. <a name="l00147"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m7">00147</a> <font class="keywordtype">int</font> minInputChannels;
  153. <a name="l00148"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m8">00148</a> <font class="keywordtype">int</font> minDuplexChannels;
  154. <a name="l00149"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m9">00149</a> <font class="keywordtype">bool</font> hasDuplexSupport;
  155. <a name="l00150"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m10">00150</a> <font class="keywordtype">int</font> nSampleRates;
  156. <a name="l00151"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m11">00151</a> <font class="keywordtype">int</font> sampleRates[MAX_SAMPLE_RATES];
  157. <a name="l00152"></a><a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html#m12">00152</a> RTAUDIO_FORMAT nativeFormats;
  158. 00153 } RTAUDIO_DEVICE;
  159. 00154
  160. 00156
  161. 00163 <a class="code" href="classRtAudio.html#a0">RtAudio</a>();
  162. 00164
  163. 00166
  164. 00177 <a class="code" href="classRtAudio.html#a0">RtAudio</a>(<font class="keywordtype">int</font> *streamId,
  165. 00178 <font class="keywordtype">int</font> outputDevice, <font class="keywordtype">int</font> outputChannels,
  166. 00179 <font class="keywordtype">int</font> inputDevice, <font class="keywordtype">int</font> inputChannels,
  167. 00180 RTAUDIO_FORMAT format, <font class="keywordtype">int</font> sampleRate,
  168. 00181 <font class="keywordtype">int</font> *bufferSize, <font class="keywordtype">int</font> numberOfBuffers);
  169. 00182
  170. 00184
  171. 00188 <a class="code" href="classRtAudio.html#a2">~RtAudio</a>();
  172. 00189
  173. 00191
  174. 00218 <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,
  175. 00219 <font class="keywordtype">int</font> inputDevice, <font class="keywordtype">int</font> inputChannels,
  176. 00220 RTAUDIO_FORMAT format, <font class="keywordtype">int</font> sampleRate,
  177. 00221 <font class="keywordtype">int</font> *bufferSize, <font class="keywordtype">int</font> numberOfBuffers);
  178. 00222
  179. 00224
  180. 00243 <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);
  181. 00244
  182. 00246
  183. 00253 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a5">cancelStreamCallback</a>(<font class="keywordtype">int</font> streamId);
  184. 00254
  185. 00256 <font class="keywordtype">int</font> <a class="code" href="classRtAudio.html#a6">getDeviceCount</a>(<font class="keywordtype">void</font>);
  186. 00257
  187. 00259
  188. 00269 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a7">getDeviceInfo</a>(<font class="keywordtype">int</font> device, RTAUDIO_DEVICE *info);
  189. 00270
  190. 00272
  191. 00277 <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);
  192. 00278
  193. 00280
  194. 00285 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a9">tickStream</a>(<font class="keywordtype">int</font> streamId);
  195. 00286
  196. 00288
  197. 00292 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a10">closeStream</a>(<font class="keywordtype">int</font> streamId);
  198. 00293
  199. 00295
  200. 00299 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a11">startStream</a>(<font class="keywordtype">int</font> streamId);
  201. 00300
  202. 00302
  203. 00306 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a12">stopStream</a>(<font class="keywordtype">int</font> streamId);
  204. 00307
  205. 00309
  206. 00313 <font class="keywordtype">void</font> <a class="code" href="classRtAudio.html#a13">abortStream</a>(<font class="keywordtype">int</font> streamId);
  207. 00314
  208. 00316
  209. 00321 <font class="keywordtype">int</font> <a class="code" href="classRtAudio.html#a14">streamWillBlock</a>(<font class="keywordtype">int</font> streamId);
  210. 00322
  211. 00323 <font class="keyword">protected</font>:
  212. 00324
  213. 00325 <font class="keyword">private</font>:
  214. 00326
  215. 00327 <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];
  216. 00328
  217. 00329 <font class="keyword">enum</font> { FAILURE, SUCCESS };
  218. 00330
  219. 00331 <font class="keyword">enum</font> STREAM_MODE {
  220. 00332 PLAYBACK,
  221. 00333 RECORD,
  222. 00334 DUPLEX,
  223. 00335 UNINITIALIZED = -75
  224. 00336 };
  225. 00337
  226. 00338 <font class="keyword">enum</font> STREAM_STATE {
  227. 00339 STREAM_STOPPED,
  228. 00340 STREAM_RUNNING
  229. 00341 };
  230. 00342
  231. 00343 <font class="keyword">typedef</font> <font class="keyword">struct </font>{
  232. 00344 <font class="keywordtype">int</font> device[2]; <font class="comment">// Playback and record, respectively.</font>
  233. 00345 STREAM_MODE mode; <font class="comment">// PLAYBACK, RECORD, or DUPLEX.</font>
  234. 00346 AUDIO_HANDLE handle[2]; <font class="comment">// Playback and record handles, respectively.</font>
  235. 00347 STREAM_STATE state; <font class="comment">// STOPPED or RUNNING</font>
  236. 00348 <font class="keywordtype">char</font> *userBuffer;
  237. 00349 <font class="keywordtype">char</font> *deviceBuffer;
  238. 00350 <font class="keywordtype">bool</font> doConvertBuffer[2]; <font class="comment">// Playback and record, respectively.</font>
  239. 00351 <font class="keywordtype">bool</font> deInterleave[2]; <font class="comment">// Playback and record, respectively.</font>
  240. 00352 <font class="keywordtype">bool</font> doByteSwap[2]; <font class="comment">// Playback and record, respectively.</font>
  241. 00353 <font class="keywordtype">int</font> sampleRate;
  242. 00354 <font class="keywordtype">int</font> bufferSize;
  243. 00355 <font class="keywordtype">int</font> nBuffers;
  244. 00356 <font class="keywordtype">int</font> nUserChannels[2]; <font class="comment">// Playback and record, respectively.</font>
  245. 00357 <font class="keywordtype">int</font> nDeviceChannels[2]; <font class="comment">// Playback and record channels, respectively.</font>
  246. 00358 RTAUDIO_FORMAT userFormat;
  247. 00359 RTAUDIO_FORMAT deviceFormat[2]; <font class="comment">// Playback and record, respectively.</font>
  248. 00360 <font class="keywordtype">bool</font> usingCallback;
  249. 00361 THREAD_HANDLE thread;
  250. 00362 MUTEX mutex;
  251. 00363 RTAUDIO_CALLBACK callback;
  252. 00364 <font class="keywordtype">void</font> *userData;
  253. 00365 } RTAUDIO_STREAM;
  254. 00366
  255. 00367 <font class="keyword">typedef</font> <font class="keywordtype">signed</font> <font class="keywordtype">short</font> INT16;
  256. 00368 <font class="keyword">typedef</font> <font class="keywordtype">signed</font> <font class="keywordtype">int</font> INT32;
  257. 00369 <font class="keyword">typedef</font> <font class="keywordtype">float</font> FLOAT32;
  258. 00370 <font class="keyword">typedef</font> <font class="keywordtype">double</font> FLOAT64;
  259. 00371
  260. 00372 <font class="keywordtype">char</font> message[256];
  261. 00373 <font class="keywordtype">int</font> nDevices;
  262. 00374 RTAUDIO_DEVICE *devices;
  263. 00375
  264. 00376 std::map&lt;int, void *&gt; streams;
  265. 00377
  266. 00379 <font class="keywordtype">void</font> error(RtError::TYPE type);
  267. 00380
  268. 00385 <font class="keywordtype">void</font> initialize(<font class="keywordtype">void</font>);
  269. 00386
  270. 00388 <font class="keywordtype">void</font> clearDeviceInfo(RTAUDIO_DEVICE *info);
  271. 00389
  272. 00397 <font class="keywordtype">void</font> probeDeviceInfo(RTAUDIO_DEVICE *info);
  273. 00398
  274. 00405 <font class="keywordtype">bool</font> probeDeviceOpen(<font class="keywordtype">int</font> device, RTAUDIO_STREAM *stream,
  275. 00406 STREAM_MODE mode, <font class="keywordtype">int</font> channels,
  276. 00407 <font class="keywordtype">int</font> sampleRate, RTAUDIO_FORMAT format,
  277. 00408 <font class="keywordtype">int</font> *bufferSize, <font class="keywordtype">int</font> numberOfBuffers);
  278. 00409
  279. 00416 <font class="keywordtype">void</font> *verifyStream(<font class="keywordtype">int</font> streamId);
  280. 00417
  281. 00422 <font class="keywordtype">void</font> convertStreamBuffer(RTAUDIO_STREAM *stream, STREAM_MODE mode);
  282. 00423
  283. 00425 <font class="keywordtype">void</font> byteSwapBuffer(<font class="keywordtype">char</font> *buffer, <font class="keywordtype">int</font> samples, RTAUDIO_FORMAT format);
  284. 00426
  285. 00428 <font class="keywordtype">int</font> formatBytes(RTAUDIO_FORMAT format);
  286. 00429 };
  287. 00430
  288. 00431 <font class="comment">// Uncomment the following definition to have extra information spewed to stderr.</font>
  289. 00432 <font class="comment">//#define RTAUDIO_DEBUG</font>
  290. 00433
  291. 00434 <font class="preprocessor">#endif</font>
  292. </font></pre></div><HR>
  293. <table><tr><td><img src="../images/ccrma.gif">
  294. <td>&copy;2001-2002 CCRMA, Stanford University. All Rights Reserved.<br>
  295. Maintained by Gary P. Scavone, <a href="mailto:gary@ccrma.stanford.edu">gary@ccrma.stanford.edu</a><P>
  296. </table>
  297. </BODY>
  298. </HTML>