Version 2.1.1
[rtaudio-cdist.git] / doc / doxygen / tutorial.txt
1 /*! \mainpage The RtAudio Tutorial
2
3 <BODY BGCOLOR="white">
4
5 - \ref intro
6 - \ref download
7 - \ref start
8 - \ref error
9 - \ref probing
10 - \ref settings
11 - \ref playbackb
12 - \ref playbackc
13 - \ref recording
14 - \ref duplex
15 - \ref methods
16 - \ref compiling
17 - \ref debug
18 - \ref osnotes
19 - \ref acknowledge
20 - \ref license
21
22 \section intro Introduction
23
24 RtAudio is a C++ class which provides a common API (Application Programming Interface) for realtime audio input/output across Linux (native ALSA and OSS), Macintosh OS X, SGI, and Windows (DirectSound and ASIO) operating systems.  RtAudio significantly simplifies the process of interacting with computer audio hardware.  It was designed with the following goals:
25
26 <UL>
27   <LI>object oriented C++ design</LI>
28   <LI>simple, common API across all supported platforms</LI>
29   <LI>single independent header and source file for easy inclusion in programming projects</LI>
30   <LI>blocking functionality</LI>
31   <LI>callback functionality</LI>
32   <LI>extensive audio device parameter control</LI>
33   <LI>audio device capability probing</LI>
34   <LI>automatic internal conversion for data format, channel number compensation, de-interleaving, and byte-swapping</LI>
35   <LI>control over multiple audio streams and devices with a single instance</LI>
36 </UL>
37
38 RtAudio 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.  When allowed by the underlying audio API, multiple streams can run at the same time and a single device can serve multiple streams.  See the \ref osnotes section for information specific to each of the supported audio APIs.
39
40 The RtAudio API provides both blocking (synchronous) and callback (asyncronous) 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.
41
42 \section download Download
43
44 Latest Release (24 October 2002): <A href="http://www-ccrma.stanford.edu/~gary/rtaudio/release/rtaudio-2.1.1.tar.gz">Version 2.1.1 (165 kB tar/gzipped)</A>
45
46
47 \section start Getting Started
48
49 The first thing that must be done when using RtAudio is to create an instance of the class.  The default constructor RtAudio::RtAudio() scans the underlying audio system to verify that at least one device is available.  RtAudio 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:
50
51 \code
52
53 #include "RtAudio.h"
54
55 int main()
56 {
57   RtAudio *audio;
58
59   // Default RtAudio constructor
60   try {
61     audio = new RtAudio();
62   }
63   catch (RtError &error) {
64     // Handle the exception here
65   }
66
67   // Clean up
68   delete audio;
69 }
70 \endcode
71
72 Obviously, this example doesn't demonstrate any of the real functionality of RtAudio.  However, all uses of RtAudio 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.
73
74
75 \section error Error Handling
76
77 RtAudio uses a C++ exception handler called RtError, which is declared and defined within the RtAudio class files.  The RtError class is quite simple but it does allow errors to be "caught" by RtError::TYPE.  Almost all RtAudio methods can "throw" an RtError, most typically if an invalid stream identifier is supplied to a method or a driver error occurs.  There are a number of cases within RtAudio where warning messages may be displayed but an exception is not thrown.  There is a private RtAudio method, error(), which can be modified to globally control how these messages are handled and reported.
78
79
80 \section probing Probing Device Capabilities
81
82 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.
83
84 \code
85
86 // probe.cpp
87
88 #include <iostream>
89 #include "RtAudio.h"
90
91 int main()
92 {
93   RtAudio *audio;
94
95   // Default RtAudio constructor
96   try {
97     audio = new RtAudio();
98   }
99   catch (RtError &error) {
100     error.printMessage();
101     exit(EXIT_FAILURE);
102   }
103
104   // Determine the number of devices available
105   int devices = audio->getDeviceCount();
106
107   // Scan through devices for various capabilities
108   RtAudio::RTAUDIO_DEVICE info;
109   for (int i=1; i<=devices; i++) {
110
111     try {
112       audio->getDeviceInfo(i, &info);
113     }
114     catch (RtError &error) {
115       error.printMessage();
116       break;
117     }
118
119     // Print, for example, the maximum number of output channels for each device
120     cout << "device = " << i;
121     cout << ": maximum output channels = " << info.maxOutputChannels << "\n";
122   }
123
124   // Clean up
125   delete audio;
126
127   return 0;
128 }
129 \endcode
130
131 The RTAUDIO_DEVICE structure is defined in RtAudio.h and provides a variety of information useful in assessing the capabilities of a device:
132
133 \code
134   typedef struct {
135     char name[128];
136     bool probed;                          // true if the device probe was successful.
137     int maxOutputChannels;
138     int maxInputChannels;
139     int maxDuplexChannels;
140     int minOutputChannels;
141     int minInputChannels;
142     int minDuplexChannels;
143     bool hasDuplexSupport;                // true if duplex mode is supported.
144     bool isDefault;                       // true if this is the default output or input device.
145     int nSampleRates;                     // Number of discrete rates, or -1 if range supported.
146     double sampleRates[MAX_SAMPLE_RATES]; // Supported sample rates, or {min, max} if range.
147     RTAUDIO_FORMAT nativeFormats;
148   } RTAUDIO_DEVICE;
149 \endcode
150
151 The following data formats are defined and fully supported by RtAudio:
152
153 \code
154   typedef unsigned long RTAUDIO_FORMAT;
155   static const RTAUDIO_FORMAT  RTAUDIO_SINT8;   // Signed 8-bit integer
156   static const RTAUDIO_FORMAT  RTAUDIO_SINT16;  // Signed 16-bit integer
157   static const RTAUDIO_FORMAT  RTAUDIO_SINT24;  // Signed 24-bit integer (upper 3 bytes of 32-bit signed integer.)
158   static const RTAUDIO_FORMAT  RTAUDIO_SINT32;  // Signed 32-bit integer
159   static const RTAUDIO_FORMAT  RTAUDIO_FLOAT32; // 32-bit float normalized between +/- 1.0
160   static const RTAUDIO_FORMAT  RTAUDIO_FLOAT64; // 64-bit double normalized between +/- 1.0
161 \endcode
162
163 The <I>nativeFormats</I> member of the RtAudio::RTAUDIO_DEVICE structure is a bit mask of the above formats which are natively supported by the device.  However, RtAudio will automatically provide format conversion if a particular format is not natively supported.  When the <I>probed</I> member of the RTAUDIO_DEVICE structure is false, the remaining structure members are undefined and the device is probably unuseable.
164
165 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 > 1, RtAudio 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 <I>NOT</I> possible when the number of channels set by the user is greater than that supported by the device.
166
167 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.  For this reason, RtAudio does not rely on the reported values when attempting to open a stream.
168
169
170 \section settings Device Settings
171
172 The next step in using RtAudio is to open a stream with particular device and parameter settings.
173
174 \code
175
176 #include "RtAudio.h"
177
178 int main()
179 {
180   int channels = 2;
181   int sample_rate = 44100;
182   int buffer_size = 256;  // 256 sample frames
183   int n_buffers = 4;      // number of internal buffers used by device
184   int device = 0;         // 0 indicates the default or first available device
185   int stream;             // our stream identifier
186   RtAudio *audio;
187
188   // Instantiate RtAudio and open a stream within a try/catch block
189   try {
190     audio = new RtAudio();
191     stream = audio->openStream(device, channels, 0, 0, RtAudio::RTAUDIO_FLOAT32,
192                                sample_rate, &buffer_size, n_buffers);
193   }
194   catch (RtError &error) {
195     error.printMessage();
196     exit(EXIT_FAILURE);
197   }
198
199   // Clean up
200   delete audio;
201
202   return 0;
203 }
204 \endcode
205
206 The RtAudio::openStream() 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 two channel playback stream with the default output device, 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, RtAudio first attempts to open the default audio device with the given parameters.  If that attempt fails, RtAudio searches through the remaining available devices in an effort to find a device which will meet the given parameters.  If all attempts are unsuccessful, an RtError is thrown.  When a non-zero device value is specified, an attempt is made to open that device only (device = 1 specifies the first identified device, as reported by RtAudio::getDeviceInfo()).
207
208 RtAudio provides four signed integer and two floating point data formats which can be specified using the RtAudio::RTAUDIO_FORMAT parameter values mentioned earlier.  If the opened device does not natively support the given format, RtAudio will automatically perform the necessary data format conversion.
209
210 The <I>bufferSize</I> parameter specifies the desired number of sample frames which will be written to and/or read from a device per write/read operation.  The <I>nBuffers</I> parameter is used in setting the underlying device buffer parameters.  Both the <I>bufferSize</I> and <I>nBuffers</I> parameters can be used to control stream latency though there is no guarantee that the passed values will be those used by a device (the <I>nBuffers</I> parameter is ignored when using the OS X CoreAudio and the Windows ASIO APIs).  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 <I>bufferSize</I> parameter is passed as a pointer and the actual value used by the stream is set during the device setup procedure.  <I>bufferSize</I> values should be a power of two.  Optimal and allowable buffer values tend to vary between systems and devices.  Check the \ref osnotes section for general guidelines.
211
212 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, RtAudio does not attempt to query a device's capabilities or use previously reported values when opening a device.  Instead, RtAudio simply attempts to set the given parameters on a specified device and then checks whether the setup is successful or not.
213
214
215 \section playbackb Playback (blocking functionality)
216
217 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.
218
219 \code
220 // playback.cpp
221
222 #include "RtAudio.h"
223
224 int main()
225 {
226   int count;
227   int channels = 2;
228   int sample_rate = 44100;
229   int buffer_size = 256;  // 256 sample frames
230   int n_buffers = 4;      // number of internal buffers used by device
231   float *buffer;
232   int device = 0;         // 0 indicates the default or first available device
233   int stream;             // our stream identifier
234   RtAudio *audio;
235
236   // Open a stream during RtAudio instantiation
237   try {
238     audio = new RtAudio(&stream, device, channels, 0, 0, RtAudio::RTAUDIO_FLOAT32,
239                         sample_rate, &buffer_size, n_buffers);
240   }
241   catch (RtError &error) {
242     error.printMessage();
243     exit(EXIT_FAILURE);
244   }
245
246   try {
247     // Get a pointer to the stream buffer
248     buffer = (float *) audio->getStreamBuffer(stream);
249
250     // Start the stream
251     audio->startStream(stream);
252   }
253   catch (RtError &error) {
254     error.printMessage();
255     goto cleanup;
256   }
257
258   // An example loop which runs for about 40000 sample frames
259   count = 0;
260   while (count < 40000) {
261     // Generate your samples and fill the buffer with buffer_size sample frames of data
262     ...
263
264     // Trigger the output of the data buffer
265     try {
266       audio->tickStream(stream);
267     }
268     catch (RtError &error) {
269       error.printMessage();
270       goto cleanup;
271     }
272
273     count += buffer_size;
274   }
275
276   try {
277     // Stop and close the stream
278     audio->stopStream(stream);
279     audio->closeStream(stream);
280   }
281   catch (RtError &error) {
282     error.printMessage();
283   }
284
285  cleanup:
286   delete audio;
287
288   return 0;
289 }
290 \endcode
291
292 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 RtAudio::openStream() 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 <I>stream</I> pointer value.  An attempt is made to open the stream with the specified <I>bufferSize</I> 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 RtAudio destructor if an exception is thrown during instantiation.
293
294 Because RtAudio can typically 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 RtAudio, for use in feeding data to/from the opened stream.  Note that the user should <I>NOT</I> attempt to deallocate the stream buffer memory ... memory management for the stream buffer will be automatically controlled by RtAudio.  After starting the stream with RtAudio::startStream(), one simply fills that buffer, which is of length equal to the returned <I>bufferSize</I> value, with interleaved audio data (in the specified format) for playback.  Finally, a call to the RtAudio::tickStream() routine triggers a blocking write call for the stream.
295
296 In general, one should call the RtAudio::stopStream() and RtAudio::closeStream() methods after finishing with a stream.  However, both methods will implicitly be called during object destruction if necessary.
297
298
299 \section playbackc Playback (callback functionality)
300
301 The primary difference in using RtAudio with callback functionality involves the creation of a user-defined callback function.  Here is an example which produces a sawtooth waveform for playback.
302
303 \code
304
305 #include <iostream>
306 #include "RtAudio.h"
307
308 // Two-channel sawtooth wave generator.
309 int sawtooth(char *buffer, int buffer_size, void *data)
310 {
311   int i, j;
312   double *my_buffer = (double *) buffer;
313   double *my_data = (double *) data;
314
315   // Write interleaved audio data.
316   for (i=0; i<buffer_size; i++) {
317     for (j=0; j<2; j++) {
318       *my_buffer++ = my_data[j];
319
320       my_data[j] += 0.005 * (j+1+(j*0.1));
321       if (my_data[j] >= 1.0) my_data[j] -= 2.0;
322     }
323   }
324
325   return 0;
326 }
327
328 int main()
329 {
330   int channels = 2;
331   int sample_rate = 44100;
332   int buffer_size = 256;  // 256 sample frames
333   int n_buffers = 4;      // number of internal buffers used by device
334   int device = 0;         // 0 indicates the default or first available device
335   int stream;             // our stream identifier
336   double data[2];
337   char input;
338   RtAudio *audio;
339
340   // Open a stream during RtAudio instantiation
341   try {
342     audio = new RtAudio(&stream, device, channels, 0, 0, RtAudio::RTAUDIO_FLOAT64,
343                         sample_rate, &buffer_size, n_buffers);
344   }
345   catch (RtError &error) {
346     error.printMessage();
347     exit(EXIT_FAILURE);
348   }
349
350   try {
351     // Set the stream callback function
352     audio->setStreamCallback(stream, &sawtooth, (void *)data);
353
354     // Start the stream
355     audio->startStream(stream);
356   }
357   catch (RtError &error) {
358     error.printMessage();
359     goto cleanup;
360   }
361
362   cout << "\nPlaying ... press <enter> to quit.\n";
363   cin.get(input);
364
365   try {
366     // Stop and close the stream
367     audio->stopStream(stream);
368     audio->closeStream(stream);
369   }
370   catch (RtError &error) {
371     error.printMessage();
372   }
373
374  cleanup:
375   delete audio;
376
377   return 0;
378 }
379 \endcode
380
381 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 RtAudio::setStreamCallback().  When the underlying audio API uses blocking calls (OSS, ALSA, SGI, and Windows DirectSound), this method will spawn a new process (or thread) which automatically calls the callback function when more data is needed.  Callback-based audio APIs (OS X CoreAudio and ASIO) implement their own event notification schemes.  Note that the callback function is called only when the stream is "running" (between calls to the RtAudio::startStream() and RtAudio::stopStream() methods).  The last argument to RtAudio::setStreamCallback() is a pointer to arbitrary data that you wish to access from within your callback function.
382
383 In this example, we stop the stream with an explicit call to RtAudio::stopStream().  When using callback functionality, it is also possible to stop a stream by returning a non-zero value from the callback function.
384
385 Once set with RtAudio::setStreamCallback, the callback process exists for the life of the stream (until the stream is closed with RtAudio::closeStream() or the RtAudio instance is deleted).  It is possible to disassociate a callback function and cancel its process for an open stream using the RtAudio::cancelStreamCallback() method.  The stream can then be used with blocking functionality or a new callback can be associated with it.
386
387
388 \section recording Recording
389
390 Using RtAudio for audio input is almost identical to the way it is used for playback.  Here's the blocking playback example rewritten for recording:
391
392 \code
393 // record.cpp
394
395 #include "RtAudio.h"
396
397 int main()
398 {
399   int count;
400   int channels = 2;
401   int sample_rate = 44100;
402   int buffer_size = 256;  // 256 sample frames
403   int n_buffers = 4;      // number of internal buffers used by device
404   float *buffer;
405   int device = 0;         // 0 indicates the default or first available device
406   int stream;             // our stream identifier
407   RtAudio *audio;
408
409   // Instantiate RtAudio and open a stream.
410   try {
411     audio = new RtAudio(&stream, 0, 0, device, channels,
412                         RtAudio::RTAUDIO_FLOAT32, sample_rate, &buffer_size, n_buffers);
413   }
414   catch (RtError &error) {
415     error.printMessage();
416     exit(EXIT_FAILURE);
417   }
418
419   try {
420     // Get a pointer to the stream buffer
421     buffer = (float *) audio->getStreamBuffer(stream);
422
423     // Start the stream
424     audio->startStream(stream);
425   }
426   catch (RtError &error) {
427     error.printMessage();
428     goto cleanup;
429   }
430
431   // An example loop which runs for about 40000 sample frames
432   count = 0;
433   while (count < 40000) {
434
435     // Read a buffer of data
436     try {
437       audio->tickStream(stream);
438     }
439     catch (RtError &error) {
440       error.printMessage();
441       goto cleanup;
442     }
443
444     // Process the input samples (buffer_size sample frames) that were read
445     ...
446
447     count += buffer_size;
448   }
449
450   try {
451     // Stop the stream
452     audio->stopStream(stream);
453   }
454   catch (RtError &error) {
455     error.printMessage();
456   }
457
458  cleanup:
459   delete audio;
460
461   return 0;
462 }
463 \endcode
464
465 In this example, the stream was opened for recording with a non-zero <I>inputChannels</I> 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.
466
467
468 \section duplex Duplex Mode
469
470 Finally, it is easy to use RtAudio for simultaneous audio input/output, or duplex operation.  In this example, we use a callback function and simply scale the input data before sending it back to the output.
471
472 \code
473 // duplex.cpp
474
475 #include <iostream>
476 #include "RtAudio.h"
477
478 // Pass-through function.
479 int scale(char *buffer, int buffer_size, void *)
480 {
481   // Note: do nothing here for pass through.
482   double *my_buffer = (double *) buffer;
483
484   // Scale input data for output.
485   for (int i=0; i<buffer_size; i++) {
486     // Do for two channels.
487     *my_buffer++ *= 0.5;
488     *my_buffer++ *= 0.5;
489   }
490
491   return 0;
492 }
493
494 int main()
495 {
496   int channels = 2;
497   int sample_rate = 44100;
498   int buffer_size = 256;  // 256 sample frames
499   int n_buffers = 4;      // number of internal buffers used by device
500   int device = 0;         // 0 indicates the default or first available device
501   int stream;             // our stream identifier
502   char input;
503   RtAudio *audio;
504
505   // Open a stream during RtAudio instantiation
506   try {
507     audio = new RtAudio(&stream, device, channels, device, channels, RtAudio::RTAUDIO_FLOAT64,
508                         sample_rate, &buffer_size, n_buffers);
509   }
510   catch (RtError &error) {
511     error.printMessage();
512     exit(EXIT_FAILURE);
513   }
514
515   try {
516     // Set the stream callback function
517     audio->setStreamCallback(stream, &scale, NULL);
518
519     // Start the stream
520     audio->startStream(stream);
521   }
522   catch (RtError &error) {
523     error.printMessage();
524     goto cleanup;
525   }
526
527   cout << "\nRunning duplex ... press <enter> to quit.\n";
528   cin.get(input);
529
530   try {
531     // Stop and close the stream
532     audio->stopStream(stream);
533     audio->closeStream(stream);
534   }
535   catch (RtError &error) {
536     error.printMessage();
537   }
538
539  cleanup:
540   delete audio;
541
542   return 0;
543 }
544 \endcode
545
546 When an RtAudio stream is running in duplex mode (nonzero input <I>AND</I> 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.
547
548 As we see with this example, the write-read sequence of operations does not preclude the use of RtAudio 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 scaled and played out during the next round of audio processing.
549
550 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, unless a common external "sync" is provided.  This becomes even more difficult to achieve using two separate callback streams because it is not possible to <I>explicitly</I> control the calling order of the callback functions.
551
552
553 \section methods Summary of Methods
554
555 The following is short summary of public methods (not including constructors and the destructor) provided by RtAudio:
556
557 <UL>
558 <LI>RtAudio::openStream(): opens a stream with the specified parameters.</LI>
559 <LI>RtAudio::setStreamCallback(): sets a user-defined callback function for a given stream.</LI>
560 <LI>RtAudio::cancelStreamCallback(): cancels a callback process and function for a given stream.</LI>
561 <LI>RtAudio::getDeviceCount(): returns the number of audio devices available.</LI>
562 <LI>RtAudio::getDeviceInfo(): fills a user-supplied RTAUDIO_DEVICE structure for a specified device.</LI>
563 <LI>RtAudio::getStreamBuffer(): returns a pointer to the stream buffer.</LI>
564 <LI>RtAudio::tickStream(): triggers processing of input/output data for a stream (blocking).</LI>
565 <LI>RtAudio::closeStream(): 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 RtAudio methods.</LI>
566 <LI>RtAudio::startStream(): (re)starts the specified stream, typically after it has been stopped with either stopStream() or abortStream() or after first opening the stream.</LI>
567 <LI>RtAudio::stopStream(): stops the specified stream, allowing any remaining samples in the queue to be played out and/or read in.  This does not implicitly call RtAudio::closeStream().</LI>
568 <LI>RtAudio::abortStream(): stops the specified stream, discarding any remaining samples in the queue.  This does not implicitly call closeStream().</LI>
569 <LI>RtAudio::streamWillBlock(): queries a stream to determine whether a call to the <I>tickStream()</I> 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.</LI>
570 </UL>
571
572
573 \section compiling Compiling
574
575 In order to compile RtAudio for a specific OS and audio API, it is necessary to supply the appropriate preprocessor definition and library within the compiler statement:
576 <P>
577
578 <TABLE BORDER=2 COLS=5 WIDTH="100%">
579 <TR BGCOLOR="beige">
580   <TD WIDTH="5%"><B>OS:</B></TD>
581   <TD WIDTH="5%"><B>Audio API:</B></TD>
582   <TD WIDTH="5%"><B>Preprocessor Definition:</B></TD>
583   <TD WIDTH="5%"><B>Library or Framework:</B></TD>
584   <TD><B>Example Compiler Statement:</B></TD>
585 </TR>
586 <TR>
587   <TD>Linux</TD>
588   <TD>ALSA</TD>
589   <TD>__LINUX_ALSA__</TD>
590   <TD><TT>asound, pthread</TT></TD>
591   <TD><TT>g++ -Wall -D__LINUX_ALSA__ -o probe probe.cpp RtAudio.cpp -lasound -lpthread</TT></TD>
592 </TR>
593 <TR>
594   <TD>Linux</TD>
595   <TD>OSS</TD>
596   <TD>__LINUX_OSS__</TD>
597   <TD><TT>pthread</TT></TD>
598   <TD><TT>g++ -Wall -D__LINUX_OSS__ -o probe probe.cpp RtAudio.cpp -lpthread</TT></TD>
599 </TR>
600 <TR>
601   <TD>Macintosh OS X</TD>
602   <TD>CoreAudio</TD>
603   <TD>__MACOSX_CORE__</TD>
604   <TD><TT>pthread, stdc++, CoreAudio</TT></TD>
605   <TD><TT>CC -Wall -D__MACOSX_CORE__ -o probe probe.cpp RtAudio.cpp -framework CoreAudio -lstdc++ -lpthread</TT></TD>
606 </TR>
607 <TR>
608   <TD>Irix</TD>
609   <TD>AL</TD>
610   <TD>__IRIX_AL__</TD>
611   <TD><TT>audio, pthread</TT></TD>
612   <TD><TT>CC -Wall -D__IRIX_AL__ -o probe probe.cpp RtAudio.cpp -laudio -lpthread</TT></TD>
613 </TR>
614 <TR>
615   <TD>Windows</TD>
616   <TD>Direct Sound</TD>
617   <TD>__WINDOWS_DS__</TD>
618   <TD><TT>dsound.lib (ver. 5.0 or higher), multithreaded</TT></TD>
619   <TD><I>compiler specific</I></TD>
620 </TR>
621 <TR>
622   <TD>Windows</TD>
623   <TD>ASIO</TD>
624   <TD>__WINDOWS_ASIO__</TD>
625   <TD><I>various ASIO header and source files</I></TD>
626   <TD><I>compiler specific</I></TD>
627 </TR>
628 </TABLE>
629 <P>
630
631 The example compiler statements above could be used to compile the <TT>probe.cpp</TT> example file, assuming that <TT>probe.cpp</TT>, <TT>RtAudio.h</TT>, and <TT>RtAudio.cpp</TT> all exist in the same directory.
632
633 \section debug Debugging
634
635 If you are having problems getting RtAudio to run on your system, try passing the preprocessor definition <TT>__RTAUDIO_DEBUG__</TT> to the compiler (or uncomment the definition at the bottom of RtAudio.h).  A variety of warning messages will be displayed which may help in determining the problem.
636
637 \section osnotes OS Notes
638
639 RtAudio is designed to provide a common API across the various supported operating systems and audio libraries.  Despite that, some issues need to be mentioned with regard to each.
640
641 \subsection linux Linux:
642
643 RtAudio 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, although relatively new, is now part of the Linux development kernel and offers significantly better functionality than the OSS API.  RtAudio provides support for the 0.9 and higher versions of ALSA.  Input/output latency on the order of 15 milliseconds can typically be achieved under both OSS or ALSA by fine-tuning the RtAudio 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.
644
645 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 RtAudio tests, it is recommended that the native ALSA implementation of RtAudio be used on systems which have ALSA drivers installed.
646
647 The ALSA implementation of RtAudio makes no use of the ALSA "plug" interface.  All necessary data format conversions, channel compensation, de-interleaving, and byte-swapping is handled by internal RtAudio routines.
648
649 \subsection macosx Macintosh OS X (CoreAudio):
650
651 The Apple CoreAudio API is based on a callback scheme.  RtAudio provides blocking functionality, in addition to callback functionality, within the context of that behavior.  CoreAudio is designed to use a separate callback procedure for each of its audio devices.  A single RtAudio duplex stream using two different devices is supported, though it cannot be guaranteed to always behave correctly because we cannot synchronize these two callbacks.  This same functionality can be achieved with better synchrony by opening two separate streams for the devices and using RtAudio blocking calls (i.e. RtAudio::tickStream()).  The <I>numberOfBuffers</I> parameter to the RtAudio::openStream() function has no affect in this implementation.  It is not currently possible to have multiple simultaneous RtAudio streams accessing the same device.
652
653 \subsection irix Irix (SGI):
654
655 The Irix version of RtAudio was written and tested on an SGI Indy running Irix version 6.5.4 and the newer "al" audio library.  RtAudio does not compile under Irix version 6.3, mainly because the C++ compiler is too old.  Despite the relatively slow speed of the Indy, RtAudio was found to behave quite well and input/output latency was very good.  No problems were found with respect to using the pthread library.
656
657 \subsection windowsds Windows (DirectSound):
658
659 In order to compile RtAudio under Windows for the DirectSound API, you must have the header and source files for DirectSound version 5.0 or higher.  As far as I know, there is no DirectSoundCapture support for Windows NT.  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 crashes.  RtAudio 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.  Unfortunately, it appears they are continuing to undermine the C++ standard with more recent compiler releases.
660
661 \subsection windowsasio Windows (ASIO):
662
663 The Steinberg ASIO audio API is based on a callback scheme.  In addition, the API allows only a single device driver to be loaded and accessed at a time.  Therefore, it is not possible to have multiple simultaneous RtAudio streams running concurrently with this API.  ASIO device drivers must be supplied by audio hardware manufacturers, though ASIO emulation is possible on top of systems with DirectSound drivers.  The <I>numberOfBuffers</I> parameter to the RtAudio::openStream() function has no affect in this implementation.
664
665 A number of ASIO source and header files are required for use with RtAudio.  Specifically, an RtAudio project must include the following files: <TT>asio.h,cpp; asiodrivers.h,cpp; asiolist.h,cpp; asiodrvr.h; asiosys.h; ginclude.h; iasiodrv.h</TT>.  See the <TT>/tests/asio/</TT> directory for example Visual C++ 6.0 projects.
666
667
668 \section acknowledge Acknowledgements
669
670 The RtAudio 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.
671
672 RtAudio, version 2.0, 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).
673
674 \section license License
675
676     RtAudio: a realtime audio i/o C++ class<BR>
677     Copyright (c) 2001-2002 Gary P. Scavone
678
679     Permission is hereby granted, free of charge, to any person
680     obtaining a copy of this software and associated documentation files
681     (the "Software"), to deal in the Software without restriction,
682     including without limitation the rights to use, copy, modify, merge,
683     publish, distribute, sublicense, and/or sell copies of the Software,
684     and to permit persons to whom the Software is furnished to do so,
685     subject to the following conditions:
686
687     The above copyright notice and this permission notice shall be
688     included in all copies or substantial portions of the Software.
689
690     Any person wishing to distribute modifications to the Software is
691     requested to send the modifications to the original developer so that
692     they can be incorporated into the canonical version.
693
694     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
695     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
696     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
697     IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
698     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
699     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
700     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
701
702 */