Merge 2.0.1 into releases
[rtaudio.git] / doc / html / index.html
1 <HTML>\r
2 <HEAD>\r
3 <TITLE>The RtAudio Tutorial</TITLE>\r
4 <LINK HREF="doxygen.css" REL="stylesheet" TYPE="text/css">\r
5 </HEAD>\r
6 <BODY BGCOLOR="#FFFFFF">\r
7 <CENTER>\r
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>\r
9 <HR>
10 <!-- Generated by Doxygen 1.2.8.1 -->
11 <h1>The <a class="el" href="classRtAudio.html">RtAudio</a> Tutorial</h1>
12 <p>
13  
14 <p>
15 <center>
16  <a href="index.html#intro">Introduction</a> &nbsp;&nbsp; <a href="index.html#download">Download</a> &nbsp;&nbsp; <a href="index.html#start">Getting Started</a> &nbsp;&nbsp; <a href="index.html#error">Error Handling</a> &nbsp;&nbsp; <a href="index.html#probing">Probing Device Capabilities</a> &nbsp;&nbsp; <a href="index.html#settings">Device Settings</a> &nbsp;&nbsp; <a href="index.html#playbackb">Playback (blocking functionality)</a> &nbsp;&nbsp; <a href="index.html#playbackc">Playback (callback functionality)</a> &nbsp;&nbsp; <a href="index.html#recording">Recording</a> &nbsp;&nbsp; <a href="index.html#duplex">Duplex Mode</a> &nbsp;&nbsp; <a href="index.html#methods">Summary of Methods</a> &nbsp;&nbsp; <a href="index.html#compiling">Compiling</a> &nbsp;&nbsp; <a href="index.html#osnotes">OS Notes</a> &nbsp;&nbsp; <a href="index.html#acknowledge">Acknowledgments</a> </center>
17
18 <p>
19 <a name="intro"><h2>Introduction</h2></a>
20
21 <p>
22 <a class="el" href="classRtAudio.html">RtAudio</a> is a C++ class which provides a common API (Application Programming Interface) for realtime audio input/output across Linux (native ALSA and OSS), SGI, and Windows operating systems. <a class="el" href="classRtAudio.html">RtAudio</a> significantly simplifies the process of interacting with computer audio hardware. It was designed with the following goals:
23 <p>
24 <ul>
25  <li>object oriented C++ design <li>simple, common API across all supported platforms <li>single independent header and source file for easy inclusion in programming projects (no libraries!) <li>blocking functionality <li>callback functionality <li>extensive audio device parameter control <li>audio device capability probing <li>automatic internal conversion for data format, channel number compensation, de-interleaving, and byte-swapping <li>control over multiple audio streams and devices with a single instance </ul>
26
27 <p>
28 <a class="el" href="classRtAudio.html">RtAudio</a> incorporates the concept of audio streams, which represent audio output (playback) and/or input (recording). Available audio devices and their capabilities can be enumerated and then specified when opening a stream. Multiple streams can run at the same time and, when allowed by the underlying audio API, a single device can serve multiple streams.
29 <p>
30 The <a class="el" href="classRtAudio.html">RtAudio</a> API provides both blocking (synchronous) and callback (asynchronous) functionality. Callbacks are typically used in conjunction with graphical user interfaces (GUI). Blocking functionality is often necessary for explicit control of multiple input/output stream synchronization or when audio must be synchronized with other system events.
31 <p>
32 <a name="download"><h2>Download</h2></a>
33
34 <p>
35 Latest Release (22 January 2002): <a href="release/rtaudio-2.0.tgz">Version 2.0 (111 kB tar/gzipped)</a>
36 <p>
37 <a name="start"><h2>Getting Started</h2></a>
38
39 <p>
40 The first thing that must be done when using <a class="el" href="classRtAudio.html">RtAudio</a> is to create an instance of the class. The default constructor <a class="el" href="classRtAudio.html#a0">RtAudio::RtAudio</a>() scans the underlying audio system to verify that at least one device is available. <a class="el" href="classRtAudio.html">RtAudio</a> often uses C++ exceptions to report errors, necessitating try/catch blocks around most member functions. The following code example demonstrates default object construction and destruction:
41 <p>
42 <div class="fragment"><pre><font class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</font>
43
44 <font class="keywordtype">int</font> main()<font class="keyword"></font>
45 <font class="keyword"></font>{
46   <a class="code" href="classRtAudio.html">RtAudio</a> *audio;
47
48   <font class="comment">// Default RtAudio constructor</font>
49   <font class="keywordflow">try</font> {
50     audio = <font class="keyword">new</font> RtAudio();
51   }
52   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
53     <font class="comment">// Handle the exception here</font>
54   }
55
56   <font class="comment">// Clean up</font>
57   <font class="keyword">delete</font> audio;
58 }</pre></div>
59 <p>
60 Obviously, this example doesn't demonstrate any of the real functionality of <a class="el" href="classRtAudio.html">RtAudio</a>. However, all uses of <a class="el" href="classRtAudio.html">RtAudio</a> must begin with a constructor (either default or overloaded varieties) and must end with class destruction. Further, it is necessary that all class methods which can throw a C++ exception be called within a try/catch block.
61 <p>
62 <a name="error"><h2>Error Handling</h2></a>
63
64 <p>
65 <a class="el" href="classRtAudio.html">RtAudio</a> uses a C++ exception handler called <a class="el" href="classRtAudioError.html">RtAudioError</a>, which is declared and defined within the <a class="el" href="classRtAudio.html">RtAudio</a> class files. The <a class="el" href="classRtAudioError.html">RtAudioError</a> class is quite simple but it does allow errors to be "caught" by <a class="el" href="classRtAudioError.html#s11">RtAudioError::TYPE</a>. Almost all <a class="el" href="classRtAudio.html">RtAudio</a> methods can "throw" an <a class="el" href="classRtAudioError.html">RtAudioError</a>, most typically if an invalid stream identifier is supplied to a method or a driver error occurs. There are a number of cases within <a class="el" href="classRtAudio.html">RtAudio</a> where warning messages may be displayed but an exception is not thrown. There is a private <a class="el" href="classRtAudio.html">RtAudio</a> method, error(), which can be modified to globally control how these messages are handled and reported.
66 <p>
67 <a name="probing"><h2>Probing Device Capabilities</h2></a>
68
69 <p>
70 A programmer may wish to query the available audio device capabilities before deciding which to use. The following example outlines how this can be done.
71 <p>
72 <div class="fragment"><pre><font class="comment">// probe.cpp</font>
73
74 <font class="preprocessor">#include &lt;iostream.h&gt;</font>
75 <font class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</font>
76
77 <font class="keywordtype">int</font> main()<font class="keyword"></font>
78 <font class="keyword"></font>{
79   <a class="code" href="classRtAudio.html">RtAudio</a> *audio;
80
81   <font class="comment">// Default RtAudio constructor</font>
82   <font class="keywordflow">try</font> {
83     audio = <font class="keyword">new</font> RtAudio();
84   }
85   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
86     error.printMessage();
87     exit(EXIT_FAILURE);
88   }
89
90   <font class="comment">// Determine the number of devices available</font>
91   <font class="keywordtype">int</font> devices = audio-&gt;<a class="code" href="classRtAudio.html#a6">getDeviceCount</a>();
92
93   <font class="comment">// Scan through devices for various capabilities</font>
94   <a class="code" href="structRtAudio_1_1RTAUDIO__DEVICE.html">RtAudio::RTAUDIO_DEVICE</a> info;
95   <font class="keywordflow">for</font> (<font class="keywordtype">int</font> i=0; i&lt;devices; i++) {
96
97     <font class="keywordflow">try</font> {
98       audio-&gt;<a class="code" href="classRtAudio.html#a7">getDeviceInfo</a>(i, &amp;info);
99     }
100     <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
101       error.printMessage();
102       <font class="keywordflow">break</font>;
103     }
104
105     <font class="comment">// Print, for example, the maximum number of output channels for each device</font>
106     cout &lt;&lt; <font class="stringliteral">"device = "</font> &lt;&lt; i;
107     cout &lt;&lt; <font class="stringliteral">": maximum output channels = "</font> &lt;&lt; info.maxOutputChannels &lt;&lt; endl;
108   }
109
110   <font class="comment">// Clean up</font>
111   <font class="keyword">delete</font> audio;
112
113   <font class="keywordflow">return</font> 0;
114 }</pre></div>
115 <p>
116 The RTAUDIO_DEVICE structure is defined in <a class="el" href="RtAudio_8h.html">RtAudio.h</a> and provides a variety of information useful in assessing the capabilities of a device:
117 <p>
118 <div class="fragment"><pre>  <font class="keyword">typedef</font> <font class="keyword">struct </font>{
119     <font class="keywordtype">char</font> name[128];
120     DEVICE_ID id[2];                      <font class="comment">// No value reported by getDeviceInfo().</font>
121     <font class="keywordtype">bool</font> probed;                          <font class="comment">// true if the device probe was successful.</font>
122     <font class="keywordtype">int</font> maxOutputChannels;
123     <font class="keywordtype">int</font> maxInputChannels;
124     <font class="keywordtype">int</font> maxDuplexChannels;
125     <font class="keywordtype">int</font> minOutputChannels;
126     <font class="keywordtype">int</font> minInputChannels;
127     <font class="keywordtype">int</font> minDuplexChannels;
128     <font class="keywordtype">bool</font> hasDuplexSupport;                <font class="comment">// true if duplex supported</font>
129     <font class="keywordtype">int</font> nSampleRates;                     <font class="comment">// Number of discrete rates, or -1 if range supported.</font>
130     <font class="keywordtype">double</font> sampleRates[MAX_SAMPLE_RATES]; <font class="comment">// Supported sample rates, or {min, max} if range.</font>
131     RTAUDIO_FORMAT nativeFormats;
132   } RTAUDIO_DEVICE;</pre></div>
133 <p>
134 The following data formats are defined and fully supported by <a class="el" href="classRtAudio.html">RtAudio</a>:
135 <p>
136 <div class="fragment"><pre>  <font class="keyword">typedef</font> <font class="keywordtype">unsigned</font> <font class="keywordtype">long</font> RTAUDIO_FORMAT;
137   <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT  RTAUDIO_SINT8;   <font class="comment">// Signed 8-bit integer</font>
138   <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT  RTAUDIO_SINT16;  <font class="comment">// Signed 16-bit integer</font>
139   <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT  RTAUDIO_SINT24;  <font class="comment">// Signed 24-bit integer</font>
140   <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT  RTAUDIO_SINT32;  <font class="comment">// Signed 32-bit integer</font>
141   <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT  RTAUDIO_FLOAT32; <font class="comment">// 32-bit float</font>
142   <font class="keyword">static</font> <font class="keyword">const</font> RTAUDIO_FORMAT  RTAUDIO_FLOAT64; <font class="comment">// 64-bit double</font></pre></div>
143 <p>
144 The <em>nativeFormats</em> member of the <a class="el" href="structRtAudio_1_1RTAUDIO__DEVICE.html">RtAudio::RTAUDIO_DEVICE</a> structure is a bit mask of the above formats which are natively supported by the device. However, <a class="el" href="classRtAudio.html">RtAudio</a> will automatically provide format conversion if a particular format is not natively supported. When the <em>probed</em> member of the RTAUDIO_DEVICE structure is false, the remaining structure members are likely unknown and the device is probably unusable.
145 <p>
146 In general, the user need not be concerned with the minimum channel values reported in the RTAUDIO_DEVICE structure. While some audio devices may require a minimum channel value &gt; 1, <a class="el" href="classRtAudio.html">RtAudio</a> will provide automatic channel number compensation when the number of channels set by the user is less than that required by the device. Channel compensation is <em>NOT</em> possible when the number of channels set by the user is greater than that supported by the device.
147 <p>
148 It should be noted that the capabilities reported by a device driver or underlying audio API are not always accurate and/or may be dependent on a combination of device settings.
149 <p>
150 <a name="settings"><h2>Device Settings</h2></a>
151
152 <p>
153 The next step in using <a class="el" href="classRtAudio.html">RtAudio</a> is to open a stream with a particular set of device settings.
154 <p>
155 <div class="fragment"><pre><font class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</font>
156
157 <font class="keywordtype">int</font> main()<font class="keyword"></font>
158 <font class="keyword"></font>{
159   <font class="keywordtype">int</font> channels = 2;
160   <font class="keywordtype">int</font> sample_rate = 44100;
161   <font class="keywordtype">int</font> buffer_size = 256;  <font class="comment">// 256 sample frames</font>
162   <font class="keywordtype">int</font> n_buffers = 4;      <font class="comment">// number of internal buffers used by device</font>
163   <font class="keywordtype">int</font> device = 0;         <font class="comment">// 0 indicates the default or first available device</font>
164   <font class="keywordtype">int</font> stream;             <font class="comment">// our stream identifier</font>
165   <a class="code" href="classRtAudio.html">RtAudio</a> *audio;
166
167   <font class="comment">// Instantiate RtAudio and open a stream within a try/catch block</font>
168   <font class="keywordflow">try</font> {
169     audio = <font class="keyword">new</font> RtAudio();
170     stream = audio-&gt;<a class="code" href="classRtAudio.html#a3">openStream</a>(device, channels, 0, 0, RtAudio::RTAUDIO_FLOAT32,
171                                sample_rate, &amp;buffer_size, n_buffers);
172   }
173   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
174     error.printMessage();
175     exit(EXIT_FAILURE);
176   }
177
178   <font class="comment">// Clean up</font>
179   <font class="keyword">delete</font> audio;
180
181   <font class="keywordflow">return</font> 0;
182 }</pre></div>
183 <p>
184 The <a class="el" href="classRtAudio.html#a3">RtAudio::openStream</a>() method attempts to open a stream with a specified set of parameter values. When successful, a stream identifier is returned. In this case, we attempt to open a playback stream on device 0 with two channels, 32-bit floating point data, a sample rate of 44100 Hz, a frame rate of 256 sample frames per read/write, and 4 internal device buffers. When device = 0, <a class="el" href="classRtAudio.html">RtAudio</a> first attempts to open the default audio device with the given parameters. If that attempt fails, an attempt is made to find a device or set of devices which will meet the given parameters. If all attempts are unsuccessful, an <a class="el" href="classRtAudioError.html">RtAudioError</a> is thrown. When a non-zero device value is specified, an attempt is made to open that device only.
185 <p>
186 <a class="el" href="classRtAudio.html">RtAudio</a> provides four signed integer and two floating point data formats which can be specified using the <a class="el" href="classRtAudio.html#s0">RtAudio::RTAUDIO_FORMAT</a> parameter values mentioned earlier. If the opened device does not natively support the given format, <a class="el" href="classRtAudio.html">RtAudio</a> will automatically perform the necessary data format conversion.
187 <p>
188 Buffer sizes in <a class="el" href="classRtAudio.html">RtAudio</a> are <em>ALWAYS</em> given in sample frame units. For example, if you open an output stream with 4 channels and set <em>bufferSize</em> to 512, you will have to write 2048 samples of data to the output buffer within your callback or between calls to <a class="el" href="classRtAudio.html#a9">RtAudio::tickStream</a>(). In this case, a single sample frame of data contains 4 samples of data.
189 <p>
190 The <em>bufferSize</em> parameter specifies the desired number of sample frames which will be written to and/or read from a device per write/read operation. The <em>nBuffers</em> parameter is used in setting the underlying device buffer parameters. Both the <em>bufferSize</em> and <em>nBuffers</em> parameters can be used to control stream latency though there is no guarantee that the passed values will be those used by a device. In general, lower values for both parameters will produce less latency but perhaps less robust performance. Both parameters can be specified with values of zero, in which case the smallest allowable values will be used. The <em>bufferSize</em> parameter is passed as a pointer and the actual value used by the stream is set during the device setup procedure. <em>bufferSize</em> values should be a power of two. Optimal and allowable buffer values tend to vary between systems and devices. Check the <a href="index.html#osnotes">OS Notes</a> section for general guidelines.
191 <p>
192 As noted earlier, the device capabilities reported by a driver or underlying audio API are not always accurate and/or may be dependent on a combination of device settings. Because of this, <a class="el" href="classRtAudio.html">RtAudio</a> does not attempt to query a device's capabilities or use previously reported values when opening a device. Instead, <a class="el" href="classRtAudio.html">RtAudio</a> simply attempts to set the given parameters on a specified device and then checks whether the setup is successful or not.
193 <p>
194 <a name="playbackb"><h2>Playback (blocking functionality)</h2></a>
195
196 <p>
197 Once the device is open for playback, there are only a few final steps necessary for realtime audio output. We'll first provide an example (blocking functionality) and then discuss the details.
198 <p>
199 <div class="fragment"><pre><font class="comment">// playback.cpp</font>
200
201 <font class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</font>
202
203 <font class="keywordtype">int</font> main()<font class="keyword"></font>
204 <font class="keyword"></font>{
205   <font class="keywordtype">int</font> count;
206   <font class="keywordtype">int</font> channels = 2;
207   <font class="keywordtype">int</font> sample_rate = 44100;
208   <font class="keywordtype">int</font> buffer_size = 256;  <font class="comment">// 256 sample frames</font>
209   <font class="keywordtype">int</font> n_buffers = 4;      <font class="comment">// number of internal buffers used by device</font>
210   <font class="keywordtype">float</font> *buffer;
211   <font class="keywordtype">int</font> device = 0;         <font class="comment">// 0 indicates the default or first available device</font>
212   <font class="keywordtype">int</font> stream;             <font class="comment">// our stream identifier</font>
213   <a class="code" href="classRtAudio.html">RtAudio</a> *audio;
214
215   <font class="comment">// Open a stream during RtAudio instantiation</font>
216   <font class="keywordflow">try</font> {
217     audio = <font class="keyword">new</font> RtAudio(&amp;stream, device, channels, 0, 0, RtAudio::RTAUDIO_FLOAT32,
218                         sample_rate, &amp;buffer_size, n_buffers);
219   }
220   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
221     error.printMessage();
222     exit(EXIT_FAILURE);
223   }
224
225   <font class="keywordflow">try</font> {
226     <font class="comment">// Get a pointer to the stream buffer</font>
227     buffer = (<font class="keywordtype">float</font> *) audio-&gt;<a class="code" href="classRtAudio.html#a8">getStreamBuffer</a>(stream);
228
229     <font class="comment">// Start the stream</font>
230     audio-&gt;<a class="code" href="classRtAudio.html#a11">startStream</a>(stream);
231   }
232   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
233     error.printMessage();
234     <font class="keywordflow">goto</font> cleanup;
235   }
236
237   <font class="comment">// An example loop which runs for about 40000 sample frames</font>
238   count = 0;
239   <font class="keywordflow">while</font> (count &lt; 40000) {
240     <font class="comment">// Generate your samples and fill the buffer with buffer_size sample frames of data</font>
241     ...
242
243     <font class="comment">// Trigger the output of the data buffer</font>
244     <font class="keywordflow">try</font> {
245       audio-&gt;<a class="code" href="classRtAudio.html#a9">tickStream</a>(stream);
246     }
247     <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
248       error.printMessage();
249       <font class="keywordflow">goto</font> cleanup;
250     }
251
252     count += buffer_size;
253   }
254
255   <font class="keywordflow">try</font> {
256     <font class="comment">// Stop and close the stream</font>
257     audio-&gt;<a class="code" href="classRtAudio.html#a12">stopStream</a>(stream);
258     audio-&gt;<a class="code" href="classRtAudio.html#a10">closeStream</a>(stream);
259   }
260   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
261     error.printMessage();
262   }
263
264  cleanup:
265   <font class="keyword">delete</font> audio;
266
267   <font class="keywordflow">return</font> 0;
268 }</pre></div>
269 <p>
270 The first thing to notice in this example is that we attempt to open a stream during class instantiation with an overloaded constructor. This constructor simply combines the functionality of the default constructor, used earlier, and the <a class="el" href="classRtAudio.html#a3">RtAudio::openStream</a>() method. Again, we have specified a device value of 0, indicating that the default or first available device meeting the given parameters should be used. The integer identifier of the opened stream is returned via the <em>stream</em> pointer value. An attempt is made to open the stream with the specified <em>bufferSize</em> value. However, it is possible that the device will not accept this value, in which case the closest allowable size is used and returned via the pointer value. The constructor can fail if no available devices are found, or a memory allocation or device driver error occurs. Note that you should not call the <a class="el" href="classRtAudio.html">RtAudio</a> destructor if an exception is thrown during instantiation.
271 <p>
272 Because <a class="el" href="classRtAudio.html">RtAudio</a> can be used to simultaneously control more than a single stream, it is necessary that the stream identifier be provided to nearly all public methods. Assuming the constructor is successful, it is necessary to get a pointer to the buffer, provided by <a class="el" href="classRtAudio.html">RtAudio</a>, for use in feeding data to/from the opened stream. Note that the user should <em>NOT</em> attempt to deallocate the stream buffer memory ... memory management for the stream buffer will be automatically controlled by <a class="el" href="classRtAudio.html">RtAudio</a>. After starting the stream with <a class="el" href="classRtAudio.html#a11">RtAudio::startStream</a>(), one simply fills that buffer, which is of length equal to the returned <em>bufferSize</em> value, with interleaved audio data (in the specified format) for playback. Finally, a call to the <a class="el" href="classRtAudio.html#a9">RtAudio::tickStream</a>() routine triggers a blocking write call for the stream.
273 <p>
274 In general, one should call the <a class="el" href="classRtAudio.html#a12">RtAudio::stopStream</a>() and <a class="el" href="classRtAudio.html#a10">RtAudio::closeStream</a>() methods after finishing with a stream. However, both methods will implicitly be called during object destruction if necessary.
275 <p>
276 <a name="playbackc"><h2>Playback (callback functionality)</h2></a>
277
278 <p>
279 The primary difference in using <a class="el" href="classRtAudio.html">RtAudio</a> with callback functionality involves the creation of a user-defined callback function. Here is an example which produces a sawtooth waveform for playback.
280 <p>
281 <div class="fragment"><pre><font class="preprocessor">#include &lt;iostream.h&gt;</font>
282 <font class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</font>
283
284 <font class="comment">// Two-channel sawtooth wave generator.</font>
285 <font class="keywordtype">int</font> sawtooth(<font class="keywordtype">char</font> *buffer, <font class="keywordtype">int</font> buffer_size, <font class="keywordtype">void</font> *data)<font class="keyword"></font>
286 <font class="keyword"></font>{
287   <font class="keywordtype">int</font> i, j;
288   <font class="keywordtype">double</font> *my_buffer = (<font class="keywordtype">double</font> *) buffer;
289   <font class="keywordtype">double</font> *my_data = (<font class="keywordtype">double</font> *) data;
290
291   <font class="comment">// Write interleaved audio data.</font>
292   <font class="keywordflow">for</font> (i=0; i&lt;buffer_size; i++) {
293     <font class="keywordflow">for</font> (j=0; j&lt;2; j++) {
294       *my_buffer++ = my_data[j];
295
296       my_data[j] += 0.005 * (j+1+(j*0.1));
297       <font class="keywordflow">if</font> (my_data[j] &gt;= 1.0) my_data[j] -= 2.0;
298     }
299   }
300
301   <font class="keywordflow">return</font> 0;
302 }
303
304 <font class="keywordtype">int</font> main()<font class="keyword"></font>
305 <font class="keyword"></font>{
306   <font class="keywordtype">int</font> channels = 2;
307   <font class="keywordtype">int</font> sample_rate = 44100;
308   <font class="keywordtype">int</font> buffer_size = 256;  <font class="comment">// 256 sample frames</font>
309   <font class="keywordtype">int</font> n_buffers = 4;      <font class="comment">// number of internal buffers used by device</font>
310   <font class="keywordtype">int</font> device = 0;         <font class="comment">// 0 indicates the default or first available device</font>
311   <font class="keywordtype">int</font> stream;             <font class="comment">// our stream identifier</font>
312   <font class="keywordtype">double</font> data[2];
313   <font class="keywordtype">char</font> input;
314   <a class="code" href="classRtAudio.html">RtAudio</a> *audio;
315
316   <font class="comment">// Open a stream during RtAudio instantiation</font>
317   <font class="keywordflow">try</font> {
318     audio = <font class="keyword">new</font> RtAudio(&amp;stream, device, channels, 0, 0, RtAudio::RTAUDIO_FLOAT64,
319                         sample_rate, &amp;buffer_size, n_buffers);
320   }
321   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
322     error.printMessage();
323     exit(EXIT_FAILURE);
324   }
325
326   <font class="keywordflow">try</font> {
327     <font class="comment">// Set the stream callback function</font>
328     audio-&gt;<a class="code" href="classRtAudio.html#a4">setStreamCallback</a>(stream, &amp;sawtooth, (<font class="keywordtype">void</font> *)data);
329
330     <font class="comment">// Start the stream</font>
331     audio-&gt;<a class="code" href="classRtAudio.html#a11">startStream</a>(stream);
332   }
333   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
334     error.printMessage();
335     <font class="keywordflow">goto</font> cleanup;
336   }
337
338   cout &lt;&lt; <font class="stringliteral">"\nPlaying ... press &lt;enter&gt; to quit.\n"</font>;
339   cin.get(input);
340
341   <font class="keywordflow">try</font> {
342     <font class="comment">// Stop and close the stream</font>
343     audio-&gt;<a class="code" href="classRtAudio.html#a12">stopStream</a>(stream);
344     audio-&gt;<a class="code" href="classRtAudio.html#a10">closeStream</a>(stream);
345   }
346   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
347     error.printMessage();
348   }
349
350  cleanup:
351   <font class="keyword">delete</font> audio;
352
353   <font class="keywordflow">return</font> 0;
354 }</pre></div>
355 <p>
356 After opening the device in exactly the same way as the previous example (except with a data format change), we must set our callback function for the stream using <a class="el" href="classRtAudio.html#a4">RtAudio::setStreamCallback</a>(). This method will spawn a new process (or thread) which automatically calls the callback function when more data is needed. Note that the callback function is called only when the stream is "running" (between calls to the <a class="el" href="classRtAudio.html#a11">RtAudio::startStream</a>() and <a class="el" href="classRtAudio.html#a12">RtAudio::stopStream</a>() methods). The last argument to <a class="el" href="classRtAudio.html#a4">RtAudio::setStreamCallback</a>() is a pointer to arbitrary data that you wish to access from within your callback function.
357 <p>
358 In this example, we stop the stream with an explicit call to <a class="el" href="classRtAudio.html#a12">RtAudio::stopStream</a>(). When using callback functionality, it is also possible to stop a stream by returning a non-zero value from the callback function.
359 <p>
360 Once set with <a class="el" href="classRtAudio.html#a4">RtAudio::setStreamCallback</a>, the callback process will continue to run for the life of the stream (until the stream is closed with <a class="el" href="classRtAudio.html#a10">RtAudio::closeStream</a>() or the <a class="el" href="classRtAudio.html">RtAudio</a> instance is deleted). It is possible to disassociate a callback function and cancel its process for an open stream using the <a class="el" href="classRtAudio.html#a5">RtAudio::cancelStreamCallback</a>() method. The stream can then be used with blocking functionality or a new callback can be associated with it.
361 <p>
362 <a name="recording"><h2>Recording</h2></a>
363
364 <p>
365 Using <a class="el" href="classRtAudio.html">RtAudio</a> for audio input is almost identical to the way it is used for playback. Here's the blocking playback example rewritten for recording:
366 <p>
367 <div class="fragment"><pre><font class="comment">// record.cpp</font>
368
369 <font class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</font>
370
371 <font class="keywordtype">int</font> main()<font class="keyword"></font>
372 <font class="keyword"></font>{
373   <font class="keywordtype">int</font> count;
374   <font class="keywordtype">int</font> channels = 2;
375   <font class="keywordtype">int</font> sample_rate = 44100;
376   <font class="keywordtype">int</font> buffer_size = 256;  <font class="comment">// 256 sample frames</font>
377   <font class="keywordtype">int</font> n_buffers = 4;      <font class="comment">// number of internal buffers used by device</font>
378   <font class="keywordtype">float</font> *buffer;
379   <font class="keywordtype">int</font> device = 0;         <font class="comment">// 0 indicates the default or first available device</font>
380   <font class="keywordtype">int</font> stream;             <font class="comment">// our stream identifier</font>
381   <a class="code" href="classRtAudio.html">RtAudio</a> *audio;
382
383   <font class="comment">// Instantiate RtAudio and open a stream.</font>
384   <font class="keywordflow">try</font> {
385     audio = <font class="keyword">new</font> RtAudio(&amp;stream, 0, 0, device, channels,
386                         RtAudio::RTAUDIO_FLOAT32, sample_rate, &amp;buffer_size, n_buffers);
387   }
388   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
389     error.printMessage();
390     exit(EXIT_FAILURE);
391   }
392
393   <font class="keywordflow">try</font> {
394     <font class="comment">// Get a pointer to the stream buffer</font>
395     buffer = (<font class="keywordtype">float</font> *) audio-&gt;<a class="code" href="classRtAudio.html#a8">getStreamBuffer</a>(stream);
396
397     <font class="comment">// Start the stream</font>
398     audio-&gt;<a class="code" href="classRtAudio.html#a11">startStream</a>(stream);
399   }
400   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
401     error.printMessage();
402     <font class="keywordflow">goto</font> cleanup;
403   }
404
405   <font class="comment">// An example loop which runs for about 40000 sample frames</font>
406   count = 0;
407   <font class="keywordflow">while</font> (count &lt; 40000) {
408
409     <font class="comment">// Read a buffer of data</font>
410     <font class="keywordflow">try</font> {
411       audio-&gt;<a class="code" href="classRtAudio.html#a9">tickStream</a>(stream);
412     }
413     <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
414       error.printMessage();
415       <font class="keywordflow">goto</font> cleanup;
416     }
417
418     <font class="comment">// Process the input samples (buffer_size sample frames) that were read</font>
419     ...
420
421     count += buffer_size;
422   }
423
424   <font class="keywordflow">try</font> {
425     <font class="comment">// Stop the stream</font>
426     audio-&gt;<a class="code" href="classRtAudio.html#a12">stopStream</a>(stream);
427   }
428   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
429     error.printMessage();
430   }
431
432  cleanup:
433   <font class="keyword">delete</font> audio;
434
435   <font class="keywordflow">return</font> 0;
436 }</pre></div>
437 <p>
438 In this example, the stream was opened for recording with a non-zero <em>inputChannels</em> value. The only other difference between this example and that for playback involves the order of data processing in the loop, where it is necessary to first read a buffer of input data before manipulating it.
439 <p>
440 <a name="duplex"><h2>Duplex Mode</h2></a>
441
442 <p>
443 Finally, it is easy to use <a class="el" href="classRtAudio.html">RtAudio</a> for simultaneous audio input/output, or duplex operation. In this example, we use a callback function and pass our recorded data directly through for playback.
444 <p>
445 <div class="fragment"><pre><font class="comment">// duplex.cpp</font>
446
447 <font class="preprocessor">#include &lt;iostream.h&gt;</font>
448 <font class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</font>
449
450 <font class="comment">// Pass-through function.</font>
451 <font class="keywordtype">int</font> pass(<font class="keywordtype">char</font> *buffer, <font class="keywordtype">int</font> buffer_size, <font class="keywordtype">void</font> *)<font class="keyword"></font>
452 <font class="keyword"></font>{
453   <font class="comment">// Surprise!!  We do nothing to pass the data through.</font>
454   <font class="keywordflow">return</font> 0;
455 }
456
457 <font class="keywordtype">int</font> main()<font class="keyword"></font>
458 <font class="keyword"></font>{
459   <font class="keywordtype">int</font> channels = 2;
460   <font class="keywordtype">int</font> sample_rate = 44100;
461   <font class="keywordtype">int</font> buffer_size = 256;  <font class="comment">// 256 sample frames</font>
462   <font class="keywordtype">int</font> n_buffers = 4;      <font class="comment">// number of internal buffers used by device</font>
463   <font class="keywordtype">int</font> device = 0;         <font class="comment">// 0 indicates the default or first available device</font>
464   <font class="keywordtype">int</font> stream;             <font class="comment">// our stream identifier</font>
465   <font class="keywordtype">double</font> data[2];
466   <font class="keywordtype">char</font> input;
467   <a class="code" href="classRtAudio.html">RtAudio</a> *audio;
468
469   <font class="comment">// Open a stream during RtAudio instantiation</font>
470   <font class="keywordflow">try</font> {
471     audio = <font class="keyword">new</font> RtAudio(&amp;stream, device, channels, device, channels, RtAudio::RTAUDIO_FLOAT64,
472                         sample_rate, &amp;buffer_size, n_buffers);
473   }
474   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
475     error.printMessage();
476     exit(EXIT_FAILURE);
477   }
478
479   <font class="keywordflow">try</font> {
480     <font class="comment">// Set the stream callback function</font>
481     audio-&gt;<a class="code" href="classRtAudio.html#a4">setStreamCallback</a>(stream, &amp;pass, NULL);
482
483     <font class="comment">// Start the stream</font>
484     audio-&gt;<a class="code" href="classRtAudio.html#a11">startStream</a>(stream);
485   }
486   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
487     error.printMessage();
488     <font class="keywordflow">goto</font> cleanup;
489   }
490
491   cout &lt;&lt; <font class="stringliteral">"\nRunning duplex ... press &lt;enter&gt; to quit.\n"</font>;
492   cin.get(input);
493
494   <font class="keywordflow">try</font> {
495     <font class="comment">// Stop and close the stream</font>
496     audio-&gt;<a class="code" href="classRtAudio.html#a12">stopStream</a>(stream);
497     audio-&gt;<a class="code" href="classRtAudio.html#a10">closeStream</a>(stream);
498   }
499   <font class="keywordflow">catch</font> (<a class="code" href="classRtAudioError.html">RtAudioError</a> &amp;error) {
500     error.printMessage();
501   }
502
503  cleanup:
504   <font class="keyword">delete</font> audio;
505
506   <font class="keywordflow">return</font> 0;
507 }</pre></div>
508 <p>
509 When an <a class="el" href="classRtAudio.html">RtAudio</a> stream is running in duplex mode (nonzero input <em>AND</em> output channels), the audio write (playback) operation always occurs before the audio read (record) operation. This sequence allows the use of a single buffer to store both output and input data.
510 <p>
511 As we see with this example, the write-read sequence of operations does not preclude the use of <a class="el" href="classRtAudio.html">RtAudio</a> in situations where input data is first processed and then output through a duplex stream. When the stream buffer is first allocated, it is initialized with zeros, which produces no audible result when output to the device. In this example, anything recorded by the audio stream input will be played out during the next round of audio processing.
512 <p>
513 Note that duplex operation can also be achieved by opening one output stream and one input stream using the same or different devices. However, there may be timing problems when attempting to use two different devices, due to possible device clock variations. This becomes even more difficult to achieve using two separate callback streams because it is not possible to explicitly control the calling order of the callback functions.
514 <p>
515 <a name="methods"><h2>Summary of Methods</h2></a>
516
517 <p>
518 The following is short summary of public methods (not including constructors and the destructor) provided by <a class="el" href="classRtAudio.html">RtAudio</a>:
519 <p>
520 <ul>
521  <li><a class="el" href="classRtAudio.html#a3">RtAudio::openStream</a>(): opens a stream with the specified parameters. <li><a class="el" href="classRtAudio.html#a4">RtAudio::setStreamCallback</a>(): sets a user-defined callback function for a given stream. <li><a class="el" href="classRtAudio.html#a5">RtAudio::cancelStreamCallback</a>(): cancels a callback process and function for a given stream. <li><a class="el" href="classRtAudio.html#a6">RtAudio::getDeviceCount</a>(): returns the number of audio devices available. <li><a class="el" href="classRtAudio.html#a7">RtAudio::getDeviceInfo</a>(): fills a user-supplied RTAUDIO_DEVICE structure for a specified device. <li><a class="el" href="classRtAudio.html#a8">RtAudio::getStreamBuffer</a>(): returns a pointer to the stream buffer. <li><a class="el" href="classRtAudio.html#a9">RtAudio::tickStream</a>(): triggers processing of input/output data for a stream (blocking). <li><a class="el" href="classRtAudio.html#a10">RtAudio::closeStream</a>(): closes the specified stream (implicitly called during object destruction). Once a stream is closed, the stream identifier is invalid and should not be used in calling any other <a class="el" href="classRtAudio.html">RtAudio</a> methods. <li><a class="el" href="classRtAudio.html#a11">RtAudio::startStream</a>(): (re)starts the specified stream, typically after it has been stopped with either stopStream() or abortStream() or after first opening the stream. <li><a class="el" href="classRtAudio.html#a12">RtAudio::stopStream</a>(): stops the specified stream, allowing any remaining samples in the queue to be played out and/or read in. This does not implicitly call <a class="el" href="classRtAudio.html#a10">RtAudio::closeStream</a>(). <li><a class="el" href="classRtAudio.html#a13">RtAudio::abortStream</a>(): stops the specified stream, discarding any remaining samples in the queue. This does not implicitly call closeStream(). <li><a class="el" href="classRtAudio.html#a14">RtAudio::streamWillBlock</a>(): queries a stream to determine whether a call to the <em>tickStream()</em> method will block. A return value of 0 indicates that the stream will NOT block. A positive return value indicates the number of sample frames that cannot yet be processed without blocking. </ul>
522
523 <p>
524 <a name="compiling"><h2>Compiling</h2></a>
525
526 <p>
527 In order to compile <a class="el" href="classRtAudio.html">RtAudio</a> for a specific OS and audio API, it is necessary to supply the appropriate preprocessor definition and library within the compiler statement: 
528 <p>
529
530 <p>
531   <table border=1 cellspacing=3 cellpadding=3><tr><td><b>OS:</b> </td><td><b>Audio API:</b> </td><td><b>Preprocessor Definition:</b> </td><td><b>Library:</b> </td><td><b>Example Compiler Statement:</b>   </td></tr>
532 <tr><td>Linux </td><td>ALSA </td><td>__LINUX_ALSA_ </td><td><code>libasound, libpthread</code> </td><td><code>g++ -Wall -D__LINUX_ALSA_ -o probe probe.cpp RtAudio.cpp -lasound -lpthread</code>   </td></tr>
533 <tr><td>Linux </td><td>OSS </td><td>__LINUX_OSS_ </td><td><code>libpthread</code> </td><td><code>g++ -Wall -D__LINUX_OSS_ -o probe probe.cpp RtAudio.cpp -lpthread</code>   </td></tr>
534 <tr><td>Irix </td><td>AL </td><td>__IRIX_AL_ </td><td><code>libaudio, libpthread</code> </td><td><code>CC -Wall -D__IRIX_AL_ -o probe probe.cpp RtAudio.cpp -laudio -lpthread</code>   </td></tr>
535 <tr><td>Windows </td><td>Direct Sound </td><td>__WINDOWS_DS_ </td><td><code>dsound.lib (ver. 5.0 or higher), multithreaded</code> </td><td><em>compiler specific</em>  </td></table>
536  
537 <p>
538
539 <p>
540 The example compiler statements above could be used to compile the <code>probe.cpp</code> example file, assuming that <code>probe.cpp</code>, <code><a class="el" href="RtAudio_8h.html">RtAudio.h</a></code>, and <code>RtAudio.cpp</code> all exist in the same directory.
541 <p>
542 <a name="osnotes"><h2>OS Notes</h2></a>
543
544 <p>
545 <a class="el" href="classRtAudio.html">RtAudio</a> is designed to provide a common API across the various supported operating systems and audio libraries. Despite that, however, some issues need to be mentioned with regard to each.
546 <p>
547 <a name="linux"><h3>Linux:</h3></a>
548
549 <p>
550 <a class="el" href="classRtAudio.html">RtAudio</a> for Linux was developed under Redhat distributions 7.0 - 7.2. Two different audio APIs are supported on Linux platforms: OSS and <a href="http://www.alsa-project.org/">ALSA</a>. The OSS API has existed for at least 6 years and the Linux kernel is distributed with free versions of OSS audio drivers. Therefore, a generic Linux system is most likely to have OSS support. The ALSA API is relatively new and at this time is not part of the Linux kernel distribution. Work is in progress to make ALSA part of the 2.5 development kernel series. Despite that, the ALSA API offers significantly better functionality than the OSS API. <a class="el" href="classRtAudio.html">RtAudio</a> provides support for the 0.9 and higher versions of ALSA. Input/output latency on the order of 15-20 milliseconds can typically be achieved under both OSS or ALSA by fine-tuning the <a class="el" href="classRtAudio.html">RtAudio</a> buffer parameters (without kernel modifications). Latencies on the order of 5 milliseconds or less can be achieved using a low-latency kernel patch and increasing FIFO scheduling priority. The pthread library, which is used for callback functionality, is a standard component of all Linux distributions.
551 <p>
552 The ALSA library includes OSS emulation support. That means that you can run programs compiled for the OSS API even when using the ALSA drivers and library. It should be noted however that OSS emulation under ALSA is not perfect. Specifically, channel number queries seem to consistently produce invalid results. While OSS emulation is successful for the majority of <a class="el" href="classRtAudio.html">RtAudio</a> tests, it is recommended that the native ALSA implementation of <a class="el" href="classRtAudio.html">RtAudio</a> be used on systems which have ALSA drivers installed.
553 <p>
554 The ALSA implementation of <a class="el" href="classRtAudio.html">RtAudio</a> makes no use of the ALSA "plug" interface. All necessary data format conversions, channel compensation, deinterleaving, and byte-swapping is handled by internal <a class="el" href="classRtAudio.html">RtAudio</a> routines.
555 <p>
556 <a name="irix"><h3>Irix (SGI):</h3></a>
557
558 <p>
559 The Irix version of <a class="el" href="classRtAudio.html">RtAudio</a> was written and tested on an SGI Indy running Irix version 6.5 and the newer "al" audio library. <a class="el" href="classRtAudio.html">RtAudio</a> does not compile under Irix version 6.3 because the C++ compiler is too old. Despite the relatively slow speed of the Indy, <a class="el" href="classRtAudio.html">RtAudio</a> was found to behave quite well and input/output latency was very good. No problems were found with respect to using the pthread library.
560 <p>
561 <a name="windows"><h3>Windows:</h3></a>
562
563 <p>
564 <a class="el" href="classRtAudio.html">RtAudio</a> under Windows is written using the DirectSound API. In order to compile <a class="el" href="classRtAudio.html">RtAudio</a> under Windows, you must have the header and source files for DirectSound version 0.5 or higher. As far as I know, you cannot compile <a class="el" href="classRtAudio.html">RtAudio</a> for Windows NT because there is not sufficient DirectSound support. Audio output latency with DirectSound can be reasonably good (on the order of 20 milliseconds). On the other hand, input audio latency tends to be terrible (100 milliseconds or more). Further, DirectSound drivers tend to crash easily when experimenting with buffer parameters. On my system, I found it necessary to use values around nBuffers = 8 and bufferSize = 512 to avoid crashing my system. <a class="el" href="classRtAudio.html">RtAudio</a> was developed with Visual C++ version 6.0. I was forced in several instances to modify code in order to get it to compile under the non-standard version of C++ that Microsoft so unprofessionally implemented. We can only hope that the developers of Visual C++ 7.0 will have time to read the C++ standard.
565 <p>
566 <a name="acknowledge"><h2>Acknowledgments</h2></a>
567
568 <p>
569 The <a class="el" href="classRtAudio.html">RtAudio</a> API incorporates many of the concepts developed in the <a href="http://www.portaudio.com/">PortAudio</a> project by Phil Burk and Ross Bencina. Early development also incorporated ideas from Bill Schottstaedt's <a href="http://www-ccrma.stanford.edu/software/snd/sndlib/">sndlib</a>. The CCRMA <a href="http://www-ccrma.stanford.edu/groups/soundwire/">SoundWire group</a> provided valuable feedback during the API proposal stages.
570 <p>
571 <a class="el" href="classRtAudio.html">RtAudio</a> was slowly developed over the course of many months while in residence at the <a href="http://www.iua.upf.es/">Institut Universitari de L'Audiovisual (IUA)</a> in Barcelona, Spain, the <a href="http://www.acoustics.hut.fi/">Laboratory of Acoustics and Audio Signal Processing</a> at the Helsinki University of Technology, Finland, and the <a href="http://www-ccrma.stanford.edu/">Center for Computer Research in Music and Acoustics (CCRMA)</a> at <a href="http://www.stanford.edu/">Stanford University</a>. This work was supported in part by the United States Air Force Office of Scientific Research (grant #F49620-99-1-0293).
572 <p>
573 These documentation files were generated using <a href="http://www.doxygen.org/">doxygen</a> by Dimitri van Heesch.
574 <p>
575 <HR>\r
576 \r
577 <table><tr><td><img src="../ccrma.gif">\r
578   <td>&copy;2001-2002 CCRMA, Stanford University. All Rights Reserved.<br>\r
579   Maintained by Gary P. Scavone, <a href="mailto:gary@ccrma.stanford.edu">gary@ccrma.stanford.edu</a><P>\r
580 </table>\r
581 \r
582 </BODY>\r
583 </HTML>