Version 3.0.2
[rtaudio.git] / doc / doxygen / tutorial.txt
1 /*! \mainpage The RtAudio Tutorial
2
3 <CENTER>\ref intro &nbsp;&nbsp; \ref changes &nbsp;&nbsp;\ref download &nbsp;&nbsp; \ref start &nbsp;&nbsp; \ref error &nbsp;&nbsp; \ref probing &nbsp;&nbsp; \ref settings &nbsp;&nbsp; \ref playbackb &nbsp;&nbsp; \ref playbackc &nbsp;&nbsp; \ref recording &nbsp;&nbsp; \ref duplex &nbsp;&nbsp; \ref multi &nbsp;&nbsp; \ref methods &nbsp;&nbsp; \ref compiling &nbsp;&nbsp; \ref debug &nbsp;&nbsp; \ref apinotes &nbsp;&nbsp; \ref wishlist &nbsp;&nbsp; \ref acknowledge &nbsp;&nbsp; \ref license</CENTER>
4
5 \section intro Introduction
6
7 RtAudio is a set of C++ classes which provide a common API (Application Programming Interface) for realtime audio input/output across Linux (native ALSA, JACK, 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:
8
9 <UL>
10   <LI>object oriented C++ design</LI>
11   <LI>simple, common API across all supported platforms</LI>
12   <LI>only two header files and one source file for easy inclusion in programming projects</LI>
13   <LI>allow simultaneous multi-api support</LI>
14   <LI>blocking functionality</LI>
15   <LI>callback functionality</LI>
16   <LI>extensive audio device parameter control</LI>
17   <LI>audio device capability probing</LI>
18   <LI>automatic internal conversion for data format, channel number compensation, de-interleaving, and byte-swapping</LI>
19 </UL>
20
21 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.  Where applicable, multiple API support can be compiled and a particular API specified when creating an RtAudio instance.  See the \ref apinotes section for information specific to each of the supported audio APIs.
22
23 The RtAudio 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.
24
25 \section changes What's New (Version 3.0)
26
27 RtAudio now allows simultaneous multi-api support.  For example, you can compile RtAudio to provide both DirectSound and ASIO support on Windows platforms or ALSA, JACK, and OSS support on Linux platforms.  This was accomplished by creating an abstract base class, RtApi, with subclasses for each supported API (RtApiAlsa, RtApiJack, RtApiOss, RtApiDs, RtApiAsio, RtApiCore, and RtApiAl).  The class RtAudio is now a "controller" which creates an instance of an RtApi subclass based on the user's API choice via an optional RtAudio::RtAudioApi instantiation argument.  If no API is specified, RtAudio attempts to make a "logical" API selection.
28
29 Support for the JACK low-latency audio server has been added with this version of RtAudio.  It is necessary to have the JACK server running before creating an instance of RtAudio.
30
31 Several API changes have been made in version 3.0 of RtAudio in an effort to provide more consistent behavior across all supported audio APIs.  The most significant of these changes is that multiple stream support from a single RtAudio instance has been discontinued.  As a result, stream identifier input arguments are no longer required.  Also, the RtAudio::streamWillBlock() function was poorly supported by most APIs and has been deprecated (though the function still exists in those subclasses of RtApi that do allow it to be implemented).
32
33 The RtAudio::getDeviceInfo() function was modified to return a globally defined RtAudioDeviceInfo structure.  This structure is a simplified version of the previous RTAUDIO_DEVICE structure.  In addition, the RTAUDIO_FORMAT structure was renamed RtAudioFormat and defined globally within RtAudio.h.  These changes were made for clarity and to better conform with standard C++ programming practices.
34
35 The RtError class declaration and definition have been extracted to a separate file (RtError.h).  This was done in preparation for a new release of the RtMidi class (planned for Summer 2004).
36
37 \section download Download
38
39 Latest Release (14 October 2005): <A href="http://music.mcgill.ca/~gary/rtaudio/release/rtaudio-3.0.2.tar.gz">Version 3.0.2</A>
40
41 \section start Getting Started
42
43 With version 3.0, it is now possible to compile multiple API support on a given platform and to specify an API choice during class instantiation.  In the examples that follow, no API will be specified (in which case, RtAudio attempts to select the most "logical" available API).
44
45 The first thing that must be done when using RtAudio is to create an instance of the class.  The default constructor 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:
46
47 \code
48
49 #include "RtAudio.h"
50
51 int main()
52 {
53   RtAudio *audio = 0;
54
55   // Default RtAudio constructor
56   try {
57     audio = new RtAudio();
58   }
59   catch (RtError &error) {
60     // Handle the exception here
61     error.printMessage();
62   }
63
64   // Clean up
65   delete audio;
66 }
67 \endcode
68
69 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.
70
71
72 \section error Error Handling
73
74 RtAudio uses a C++ exception handler called RtError, which is declared and defined in RtError.h.  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 a driver error occurs or a stream function is called when no stream is open.  There are a number of cases within RtAudio where warning messages may be displayed but an exception is not thrown.  There is a protected RtAudio method, error(), which can be modified to globally control how these messages are handled and reported.  By default, error messages are not automatically displayed in RtAudio unless the preprocessor definition __RTAUDIO_DEBUG__ is defined.  Messages associated with caught exceptions can be displayed with, for example, the RtError::printMessage() function.
75
76
77 \section probing Probing Device Capabilities
78
79 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.
80
81 \code
82
83 // probe.cpp
84
85 #include <iostream>
86 #include "RtAudio.h"
87
88 int main()
89 {
90   RtAudio *audio = 0;
91
92   // Default RtAudio constructor
93   try {
94     audio = new RtAudio();
95   }
96   catch (RtError &error) {
97     error.printMessage();
98     exit(EXIT_FAILURE);
99   }
100
101   // Determine the number of devices available
102   int devices = audio->getDeviceCount();
103
104   // Scan through devices for various capabilities
105   RtAudioDeviceInfo info;
106   for (int i=1; i<=devices; i++) {
107
108     try {
109       info = audio->getDeviceInfo(i);
110     }
111     catch (RtError &error) {
112       error.printMessage();
113       break;
114     }
115
116     // Print, for example, the maximum number of output channels for each device
117     std::cout << "device = " << i;
118     std::cout << ": maximum output channels = " << info.outputChannels << "\n";
119   }
120
121   // Clean up
122   delete audio;
123
124   return 0;
125 }
126 \endcode
127
128 The RtAudioDeviceInfo structure is defined in RtAudio.h and provides a variety of information useful in assessing the capabilities of a device:
129
130 \code
131   typedef struct RtAudioDeviceInfo{
132     std::string name;             // Character string device identifier.
133     bool probed;                  // true if the device capabilities were successfully probed.
134     int outputChannels;           // Maximum output channels supported by device.
135     int inputChannels;            // Maximum input channels supported by device.
136     int duplexChannels;           // Maximum simultaneous input/output channels supported by device.
137     bool isDefault;               // true if this is the default output or input device.
138     std::vector<int> sampleRates; // Supported sample rates.
139     RtAudioFormat nativeFormats;  // Bit mask of supported data formats.
140   };
141 \endcode
142
143 The following data formats are defined and fully supported by RtAudio:
144
145 \code
146   typedef unsigned long RtAudioFormat;
147   static const RtAudioFormat  RTAUDIO_SINT8;   // Signed 8-bit integer
148   static const RtAudioFormat  RTAUDIO_SINT16;  // Signed 16-bit integer
149   static const RtAudioFormat  RTAUDIO_SINT24;  // Signed 24-bit integer (upper 3 bytes of 32-bit signed integer.)
150   static const RtAudioFormat  RTAUDIO_SINT32;  // Signed 32-bit integer
151   static const RtAudioFormat  RTAUDIO_FLOAT32; // 32-bit float normalized between +/- 1.0
152   static const RtAudioFormat  RTAUDIO_FLOAT64; // 64-bit double normalized between +/- 1.0
153 \endcode
154
155 The <I>nativeFormats</I> member of the RtAudioDeviceInfo 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 RtAudioDeviceInfo structure is false, the remaining structure members are undefined and the device is probably unuseable.
156
157 While some audio devices may require a minimum channel value greater than one, 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.
158
159 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 typically rely on the queried values when attempting to open a stream.
160
161
162 \section settings Device Settings
163
164 The next step in using RtAudio is to open a stream with particular device and parameter settings.
165
166 \code
167
168 #include "RtAudio.h"
169
170 int main()
171 {
172   int channels = 2;
173   int sampleRate = 44100;
174   int bufferSize = 256;  // 256 sample frames
175   int nBuffers = 4;      // number of internal buffers used by device
176   int device = 0;        // 0 indicates the default or first available device
177   RtAudio *audio = 0;
178
179   // Instantiate RtAudio and open a stream within a try/catch block
180   try {
181     audio = new RtAudio();
182   }
183   catch (RtError &error) {
184     error.printMessage();
185     exit(EXIT_FAILURE);
186   }
187
188   try {
189     audio->openStream(device, channels, 0, 0, RTAUDIO_FLOAT32,
190                       sampleRate, &bufferSize, nBuffers);
191   }
192   catch (RtError &error) {
193     error.printMessage();
194     // Perhaps try other parameters?
195   }
196
197   // Clean up
198   delete audio;
199
200   return 0;
201 }
202 \endcode
203
204 The RtAudio::openStream() method attempts to open a stream with a specified set of parameter values.  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 \e ONLY (device = 1 specifies the first identified device, as reported by RtAudio::getDeviceInfo()).
205
206 RtAudio provides four signed integer and two floating point data formats which can be specified using the RtAudioFormat parameter values mentioned earlier.  If the opened device does not natively support the given format, RtAudio will automatically perform the necessary data format conversion.
207
208 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, Linux Jack, 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 apinotes section for general guidelines.
209
210 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.
211
212
213 \section playbackb Playback (blocking functionality)
214
215 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.
216
217 \code
218 // playback.cpp
219
220 #include "RtAudio.h"
221
222 int main()
223 {
224   int count;
225   int channels = 2;
226   int sampleRate = 44100;
227   int bufferSize = 256;  // 256 sample frames
228   int nBuffers = 4;      // number of internal buffers used by device
229   float *buffer;
230   int device = 0;        // 0 indicates the default or first available device
231   RtAudio *audio = 0;
232
233   // Open a stream during RtAudio instantiation
234   try {
235     audio = new RtAudio(device, channels, 0, 0, RTAUDIO_FLOAT32,
236                         sampleRate, &bufferSize, nBuffers);
237   }
238   catch (RtError &error) {
239     error.printMessage();
240     exit(EXIT_FAILURE);
241   }
242
243   try {
244     // Get a pointer to the stream buffer
245     buffer = (float *) audio->getStreamBuffer();
246
247     // Start the stream
248     audio->startStream();
249   }
250   catch (RtError &error) {
251     error.printMessage();
252     goto cleanup;
253   }
254
255   // An example loop which runs for 40000 sample frames
256   count = 0;
257   while (count < 40000) {
258     // Generate your samples and fill the buffer with bufferSize sample frames of data
259     ...
260
261     // Trigger the output of the data buffer
262     try {
263       audio->tickStream();
264     }
265     catch (RtError &error) {
266       error.printMessage();
267       goto cleanup;
268     }
269
270     count += bufferSize;
271   }
272
273   try {
274     // Stop and close the stream
275     audio->stopStream();
276     audio->closeStream();
277   }
278   catch (RtError &error) {
279     error.printMessage();
280   }
281
282  cleanup:
283   delete audio;
284
285   return 0;
286 }
287 \endcode
288
289 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.  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.
290
291 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.
292
293 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.
294
295
296 \section playbackc Playback (callback functionality)
297
298 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.
299
300 \code
301
302 #include <iostream>
303 #include "RtAudio.h"
304
305 // Two-channel sawtooth wave generator.
306 int sawtooth(char *buffer, int bufferSize, void *data)
307 {
308   int i, j;
309   double *my_buffer = (double *) buffer;
310   double *my_data = (double *) data;
311
312   // Write interleaved audio data.
313   for (i=0; i<bufferSize; i++) {
314     for (j=0; j<2; j++) {
315       *my_buffer++ = my_data[j];
316
317       my_data[j] += 0.005 * (j+1+(j*0.1));
318       if (my_data[j] >= 1.0) my_data[j] -= 2.0;
319     }
320   }
321
322   return 0;
323 }
324
325 int main()
326 {
327   int channels = 2;
328   int sampleRate = 44100;
329   int bufferSize = 256;  // 256 sample frames
330   int nBuffers = 4;      // number of internal buffers used by device
331   int device = 0;        // 0 indicates the default or first available device
332   double data[2];
333   char input;
334   RtAudio *audio = 0;
335
336   // Open a stream during RtAudio instantiation
337   try {
338     audio = new RtAudio(device, channels, 0, 0, RTAUDIO_FLOAT64,
339                         sampleRate, &bufferSize, nBuffers);
340   }
341   catch (RtError &error) {
342     error.printMessage();
343     exit(EXIT_FAILURE);
344   }
345
346   try {
347     // Set the stream callback function
348     audio->setStreamCallback(&sawtooth, (void *)data);
349
350     // Start the stream
351     audio->startStream();
352   }
353   catch (RtError &error) {
354     error.printMessage();
355     goto cleanup;
356   }
357
358   std::cout << "\nPlaying ... press <enter> to quit.\n";
359   std::cin.get(input);
360
361   try {
362     // Stop and close the stream
363     audio->stopStream();
364     audio->closeStream();
365   }
366   catch (RtError &error) {
367     error.printMessage();
368   }
369
370  cleanup:
371   delete audio;
372
373   return 0;
374 }
375 \endcode
376
377 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 Linux Jack, 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.
378
379 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.
380
381 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.
382
383
384 \section recording Recording
385
386 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:
387
388 \code
389 // record.cpp
390
391 #include "RtAudio.h"
392
393 int main()
394 {
395   int count;
396   int channels = 2;
397   int sampleRate = 44100;
398   int bufferSize = 256;  // 256 sample frames
399   int nBuffers = 4;      // number of internal buffers used by device
400   float *buffer;
401   int device = 0;        // 0 indicates the default or first available device
402   RtAudio *audio = 0;
403
404   // Instantiate RtAudio and open a stream.
405   try {
406     audio = new RtAudio(&stream, 0, 0, device, channels,
407                         RTAUDIO_FLOAT32, sampleRate, &bufferSize, nBuffers);
408   }
409   catch (RtError &error) {
410     error.printMessage();
411     exit(EXIT_FAILURE);
412   }
413
414   try {
415     // Get a pointer to the stream buffer
416     buffer = (float *) audio->getStreamBuffer();
417
418     // Start the stream
419     audio->startStream();
420   }
421   catch (RtError &error) {
422     error.printMessage();
423     goto cleanup;
424   }
425
426   // An example loop which runs for about 40000 sample frames
427   count = 0;
428   while (count < 40000) {
429
430     // Read a buffer of data
431     try {
432       audio->tickStream();
433     }
434     catch (RtError &error) {
435       error.printMessage();
436       goto cleanup;
437     }
438
439     // Process the input samples (bufferSize sample frames) that were read
440     ...
441
442     count += bufferSize;
443   }
444
445   try {
446     // Stop the stream
447     audio->stopStream();
448   }
449   catch (RtError &error) {
450     error.printMessage();
451   }
452
453  cleanup:
454   delete audio;
455
456   return 0;
457 }
458 \endcode
459
460 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.
461
462
463 \section duplex Duplex Mode
464
465 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.
466
467 \code
468 // duplex.cpp
469
470 #include <iostream>
471 #include "RtAudio.h"
472
473 // Pass-through function.
474 int scale(char *buffer, int bufferSize, void *)
475 {
476   // Note: do nothing here for pass through.
477   double *my_buffer = (double *) buffer;
478
479   // Scale input data for output.
480   for (int i=0; i<bufferSize; i++) {
481     // Do for two channels.
482     *my_buffer++ *= 0.5;
483     *my_buffer++ *= 0.5;
484   }
485
486   return 0;
487 }
488
489 int main()
490 {
491   int channels = 2;
492   int sampleRate = 44100;
493   int bufferSize = 256;  // 256 sample frames
494   int nBuffers = 4;      // number of internal buffers used by device
495   int device = 0;        // 0 indicates the default or first available device
496   char input;
497   RtAudio *audio = 0;
498
499   // Open a stream during RtAudio instantiation
500   try {
501     audio = new RtAudio(device, channels, device, channels, RTAUDIO_FLOAT64,
502                         sampleRate, &bufferSize, nBuffers);
503   }
504   catch (RtError &error) {
505     error.printMessage();
506     exit(EXIT_FAILURE);
507   }
508
509   try {
510     // Set the stream callback function
511     audio->setStreamCallback(&scale, NULL);
512
513     // Start the stream
514     audio->startStream();
515   }
516   catch (RtError &error) {
517     error.printMessage();
518     goto cleanup;
519   }
520
521   std::cout << "\nRunning duplex ... press <enter> to quit.\n";
522   std::cin.get(input);
523
524   try {
525     // Stop and close the stream
526     audio->stopStream();
527     audio->closeStream();
528   }
529   catch (RtError &error) {
530     error.printMessage();
531   }
532
533  cleanup:
534   delete audio;
535
536   return 0;
537 }
538 \endcode
539
540 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.
541
542 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.
543
544 Note that duplex operation can also be achieved by opening one output stream instance and one input stream instance 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.
545
546
547 \section multi Using Simultaneous Multiple APIs
548
549 Because support for each audio API is encapsulated in a specific RtApi subclass, it is possible to compile and instantiate multiple API-specific subclasses on a given operating system.  For example, one can compile both the RtApiDs and RtApiAsio classes on Windows operating systems by providing the appropriate preprocessor definitions, include files, and libraries for each.  In a run-time situation, one might first attempt to determine whether any ASIO device drivers exist.  This can be done by specifying the api argument RtAudio::WINDOWS_ASIO when attempting to create an instance of RtAudio.  If an RtError is thrown (indicating no available drivers), then an instance of RtAudio with the api argument RtAudio::WINDOWS_DS can be created.  Alternately, if no api argument is specified, RtAudio will first look for ASIO drivers and then DirectSound drivers (on Linux systems, the default API search order is Jack, Alsa, and finally OSS).  In theory, it should also be possible to have separate instances of RtAudio open at the same time with different underlying audio API support, though this has not been tested.  It is difficult to know how well different audio APIs can simultaneously coexist on a given operating system.  In particular, it is most unlikely that the same device could be simultaneously controlled with two different audio APIs.
550
551
552 \section methods Summary of Methods
553
554 The following is a short summary of public methods (not including constructors and the destructor) provided by RtAudio:
555
556 <UL>
557 <LI>RtAudio::openStream(): opens a stream with the specified parameters.</LI>
558 <LI>RtAudio::setStreamCallback(): sets a user-defined callback function for the stream.</LI>
559 <LI>RtAudio::cancelStreamCallback(): cancels a callback process and function for the stream.</LI>
560 <LI>RtAudio::getDeviceCount(): returns the number of audio devices available.</LI>
561 <LI>RtAudio::getDeviceInfo(): returns an RtAudioDeviceInfo structure for a specified device.</LI>
562 <LI>RtAudio::getStreamBuffer(): returns a pointer to the stream buffer.</LI>
563 <LI>RtAudio::tickStream(): triggers processing of input/output data for the stream (blocking).</LI>
564 <LI>RtAudio::closeStream(): closes the stream (implicitly called during object destruction).</LI>
565 <LI>RtAudio::startStream(): (re)starts the stream, typically after it has been stopped with either stopStream() or abortStream() or after first opening the stream.</LI>
566 <LI>RtAudio::stopStream(): stops the stream, allowing any remaining samples in the queue to be played out and/or read in.  This does not implicitly call RtAudio::closeStream().</LI>
567 <LI>RtAudio::abortStream(): stops the stream, discarding any remaining samples in the queue.  This does not implicitly call closeStream().</LI>
568 </UL>
569
570
571 \section compiling Compiling
572
573 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:
574 <P>
575
576 <TABLE BORDER=2 COLS=5 WIDTH="100%">
577 <TR BGCOLOR="beige">
578   <TD WIDTH="5%"><B>OS:</B></TD>
579   <TD WIDTH="5%"><B>Audio API:</B></TD>
580   <TD WIDTH="5%"><B>C++ Class:</B></TD>
581   <TD WIDTH="5%"><B>Preprocessor Definition:</B></TD>
582   <TD WIDTH="5%"><B>Library or Framework:</B></TD>
583   <TD><B>Example Compiler Statement:</B></TD>
584 </TR>
585 <TR>
586   <TD>Linux</TD>
587   <TD>ALSA</TD>
588   <TD>RtApiAlsa</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>Jack Audio Server</TD>
596   <TD>RtApiJack</TD>
597   <TD>__LINUX_JACK__</TD>
598   <TD><TT>jack, pthread</TT></TD>
599   <TD><TT>g++ -Wall -D__LINUX_JACK__ -o probe probe.cpp RtAudio.cpp `pkg-config --cflags --libs jack` -lpthread</TT></TD>
600 </TR>
601 <TR>
602   <TD>Linux</TD>
603   <TD>OSS</TD>
604   <TD>RtApiOss</TD>
605   <TD>__LINUX_OSS__</TD>
606   <TD><TT>pthread</TT></TD>
607   <TD><TT>g++ -Wall -D__LINUX_OSS__ -o probe probe.cpp RtAudio.cpp -lpthread</TT></TD>
608 </TR>
609 <TR>
610   <TD>Macintosh OS X</TD>
611   <TD>CoreAudio</TD>
612   <TD>RtApiCore</TD>
613   <TD>__MACOSX_CORE__</TD>
614   <TD><TT>pthread, stdc++, CoreAudio</TT></TD>
615   <TD><TT>g++ -Wall -D__MACOSX_CORE__ -o probe probe.cpp RtAudio.cpp -framework CoreAudio -lpthread</TT></TD>
616 </TR>
617 <TR>
618   <TD>Irix</TD>
619   <TD>AL</TD>
620   <TD>RtApiAl</TD>
621   <TD>__IRIX_AL__</TD>
622   <TD><TT>audio, pthread</TT></TD>
623   <TD><TT>CC -Wall -D__IRIX_AL__ -o probe probe.cpp RtAudio.cpp -laudio -lpthread</TT></TD>
624 </TR>
625 <TR>
626   <TD>Windows</TD>
627   <TD>Direct Sound</TD>
628   <TD>RtApiDs</TD>
629   <TD>__WINDOWS_DS__</TD>
630   <TD><TT>dsound.lib (ver. 5.0 or higher), multithreaded</TT></TD>
631   <TD><I>compiler specific</I></TD>
632 </TR>
633 <TR>
634   <TD>Windows</TD>
635   <TD>ASIO</TD>
636   <TD>RtApiAsio</TD>
637   <TD>__WINDOWS_ASIO__</TD>
638   <TD><I>various ASIO header and source files</I></TD>
639   <TD><I>compiler specific</I></TD>
640 </TR>
641 </TABLE>
642 <P>
643
644 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>, <tt>RtError.h</tt>, and <TT>RtAudio.cpp</TT> all exist in the same directory.
645
646 \section debug Debugging
647
648 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.  Also try using the programs included in the <tt>test</tt> directory.  The program <tt>info</tt> displays the queried capabilities of all hardware devices found.
649
650 \section apinotes API Notes
651
652 RtAudio is designed to provide a common API across the various supported operating systems and audio libraries.  Despite that, some issues should be mentioned with regard to each.
653
654 \subsection linux Linux:
655
656 RtAudio for Linux was developed under Redhat distributions 7.0 - Fedora.  Three different audio APIs are supported on Linux platforms: OSS, <A href="http://www.alsa-project.org/">ALSA</A>, and <A href="http://jackit.sourceforge.net/">Jack</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 (though the availability and quality of OSS drivers for new hardware is decreasing).  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 1.0 and higher versions of ALSA.  Jack, which is still in development, is a low-latency audio server, written primarily for the GNU/Linux operating system. It can connect a number of different applications to an audio device, as well as allow them to share audio between themselves.  Input/output latency on the order of 15 milliseconds can typically be achieved using any of the Linux APIs 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.
657
658 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.
659
660 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.
661
662 The Jack API is based on a callback scheme.  RtAudio provides blocking functionality, in addition to callback functionality, within the context of that behavior.  It should be noted, however, that the best performance is achieved when using RtAudio's callback functionality with the Jack API.  At the moment, only one RtAudio instance can be connected to the Jack server.  Because RtAudio does not provide a mechanism for allowing the user to specify particular channels (or ports) of a device, it simply opens the first <I>N</I> enumerated Jack ports for input/output.
663
664 \subsection macosx Macintosh OS X (CoreAudio):
665
666 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 might be achieved with better synchrony by creating separate instances of RtAudio for each device and making use of RtAudio blocking calls (i.e. RtAudio::tickStream()).  The <I>numberOfBuffers</I> parameter to the RtAudio::openStream() function has no affect in this implementation.
667
668 It is not possible to have multiple instances of RtAudio accessing the same CoreAudio device.
669
670 \subsection irix Irix (SGI):
671
672 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.
673
674 \subsection windowsds Windows (DirectSound):
675
676 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 originally developed with Visual C++ version 6.0.
677
678 \subsection windowsasio Windows (ASIO):
679
680 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.  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.
681
682 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>.  The Visual C++ projects found in <TT>/tests/Windows/</TT> compile both ASIO and DirectSound support.
683
684 \section wishlist Possible Future Changes
685
686 There are a few issues that still need to be addressed in future versions of RtAudio, including:
687
688 <ul>
689 <li>Provide a mechanism so the user can "pre-fill" audio output buffers to allow precise measurement of an acoustic response;</li>
690 <li>Allow the user to read / write non-interleaved data to / from the audio buffer;</li>
691 <li>Further support in Windows OS for multi-channel (>2) input / output.  This is currently only possible with ASIO interface (in large part due to limitations with the DirectSound API).  But perhaps a port to the WinMM API should be investigated?</li>
692 <li>Investigate the possibility of allowing the user to select specific channels of a soundcard.  For example, if an audio device supports 8 channels and the user wishes to send data out channels 7-8 only, it is currently necessary to open all 8 channels and write the two channels of output data to the correct positions in each audio frame of an interleaved data buffer.</li>
693 </ul>
694
695 \section acknowledge Acknowledgements
696
697 Thanks to Robin Davies for a number of bug fixes and improvements to
698 the DirectSound and ASIO implementations in the 3.0.2 release!
699
700 The RtAudio API incorporates many of the concepts developed in the <A
701 href="http://www.portaudio.com/">PortAudio</A> project by Phil Burk
702 and Ross Bencina.  Early development also incorporated ideas from Bill
703 Schottstaedt's <A
704 href="http://www-ccrma.stanford.edu/software/snd/sndlib/">sndlib</A>.
705 The CCRMA <A
706 href="http://www-ccrma.stanford.edu/groups/soundwire/">SoundWire
707 group</A> provided valuable feedback during the API proposal stages.
708
709 The early 2.0 version of RtAudio was slowly developed over the course
710 of many months while in residence at the <A
711 href="http://www.iua.upf.es/">Institut Universitari de L'Audiovisual
712 (IUA)</A> in Barcelona, Spain and the <A
713 href="http://www.acoustics.hut.fi/">Laboratory of Acoustics and Audio
714 Signal Processing</A> at the Helsinki University of Technology,
715 Finland.  Much subsequent development happened while working at the <A
716 href="http://www-ccrma.stanford.edu/">Center for Computer Research in
717 Music and Acoustics (CCRMA)</A> at <A
718 href="http://www.stanford.edu/">Stanford University</A>.  The most
719 recent version of RtAudio was finished while working as an assistant
720 professor of <a href="http://www.music.mcgill.ca/musictech/">Music
721 Technology</a> at <a href="http://www.mcgill.ca/">McGill
722 University</a>.  This work was supported in part by the United States
723 Air Force Office of Scientific Research (grant \#F49620-99-1-0293).
724
725 \section license License
726
727     RtAudio: a realtime audio i/o C++ classes<BR>
728     Copyright (c) 2001-2005 Gary P. Scavone
729
730     Permission is hereby granted, free of charge, to any person
731     obtaining a copy of this software and associated documentation files
732     (the "Software"), to deal in the Software without restriction,
733     including without limitation the rights to use, copy, modify, merge,
734     publish, distribute, sublicense, and/or sell copies of the Software,
735     and to permit persons to whom the Software is furnished to do so,
736     subject to the following conditions:
737
738     The above copyright notice and this permission notice shall be
739     included in all copies or substantial portions of the Software.
740
741     Any person wishing to distribute modifications to the Software is
742     requested to send the modifications to the original developer so that
743     they can be incorporated into the canonical version.
744
745     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
746     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
747     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
748     IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
749     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
750     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
751     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
752
753 */