Version 3.0.3
[rtaudio.git] / doc / doxygen / tutorial.txt
index 9b45bbc5d9d07ef9cd97b011b72b6ed7ece54b7b..802fe24c4f58e534c49490f27d9589e4bb7ff780 100644 (file)
@@ -1,8 +1,6 @@
 /*! \mainpage The RtAudio Tutorial
 
-<BODY BGCOLOR="white">
-
-<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 acknowledge &nbsp;&nbsp; \ref license</CENTER>
+<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>
 
 \section intro Introduction
 
@@ -38,7 +36,7 @@ The RtError class declaration and definition have been extracted to a separate f
 
 \section download Download
 
-Latest Release (22 March 2004): <A href="http://music.mcgill.ca/~gary/rtaudio/release/rtaudio-3.0.1.tar.gz">Version 3.0.1 (200 kB tar/gzipped)</A>
+Latest Release (18 November 2005): <A href="http://music.mcgill.ca/~gary/rtaudio/release/rtaudio-3.0.3.tar.gz">Version 3.0.3</A>
 
 \section start Getting Started
 
@@ -52,7 +50,7 @@ The first thing that must be done when using RtAudio is to create an instance of
 
 int main()
 {
-  RtAudio *audio;
+  RtAudio *audio = 0;
 
   // Default RtAudio constructor
   try {
@@ -68,12 +66,12 @@ int main()
 }
 \endcode
 
-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.
+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 that can throw a C++ exception be called within a try/catch block.
 
 
 \section error Error Handling
 
-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.
+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(), that 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.
 
 
 \section probing Probing Device Capabilities
@@ -89,7 +87,7 @@ A programmer may wish to query the available audio device capabilities before de
 
 int main()
 {
-  RtAudio *audio;
+  RtAudio *audio = 0;
 
   // Default RtAudio constructor
   try {
@@ -154,7 +152,7 @@ The following data formats are defined and fully supported by RtAudio:
   static const RtAudioFormat  RTAUDIO_FLOAT64; // 64-bit double normalized between +/- 1.0
 \endcode
 
-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.
+The <I>nativeFormats</I> member of the RtAudioDeviceInfo structure is a bit mask of the above formats that 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.
 
 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.
 
@@ -176,7 +174,7 @@ int main()
   int bufferSize = 256;  // 256 sample frames
   int nBuffers = 4;      // number of internal buffers used by device
   int device = 0;        // 0 indicates the default or first available device
-  RtAudio *audio;
+  RtAudio *audio = 0;
 
   // Instantiate RtAudio and open a stream within a try/catch block
   try {
@@ -203,11 +201,11 @@ int main()
 }
 \endcode
 
-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()).
+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 that 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()).
 
-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.
+RtAudio provides four signed integer and two floating point data formats that 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.
 
-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.
+The <I>bufferSize</I> parameter specifies the desired number of sample frames that 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.
 
 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.
 
@@ -230,7 +228,7 @@ int main()
   int nBuffers = 4;      // number of internal buffers used by device
   float *buffer;
   int device = 0;        // 0 indicates the default or first available device
-  RtAudio *audio;
+  RtAudio *audio = 0;
 
   // Open a stream during RtAudio instantiation
   try {
@@ -254,7 +252,7 @@ int main()
     goto cleanup;
   }
 
-  // An example loop which runs for 40000 sample frames
+  // An example loop that runs for 40000 sample frames
   count = 0;
   while (count < 40000) {
     // Generate your samples and fill the buffer with bufferSize sample frames of data
@@ -297,7 +295,7 @@ In general, one should call the RtAudio::stopStream() and RtAudio::closeStream()
 
 \section playbackc Playback (callback functionality)
 
-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.
+The primary difference in using RtAudio with callback functionality involves the creation of a user-defined callback function.  Here is an example that produces a sawtooth waveform for playback.
 
 \code
 
@@ -333,7 +331,7 @@ int main()
   int device = 0;        // 0 indicates the default or first available device
   double data[2];
   char input;
-  RtAudio *audio;
+  RtAudio *audio = 0;
 
   // Open a stream during RtAudio instantiation
   try {
@@ -376,7 +374,7 @@ int main()
 }
 \endcode
 
-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.
+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) that 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.
 
 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.
 
@@ -401,7 +399,7 @@ int main()
   int nBuffers = 4;      // number of internal buffers used by device
   float *buffer;
   int device = 0;        // 0 indicates the default or first available device
-  RtAudio *audio;
+  RtAudio *audio = 0;
 
   // Instantiate RtAudio and open a stream.
   try {
@@ -425,7 +423,7 @@ int main()
     goto cleanup;
   }
 
-  // An example loop which runs for about 40000 sample frames
+  // An example loop that runs for about 40000 sample frames
   count = 0;
   while (count < 40000) {
 
@@ -496,7 +494,7 @@ int main()
   int nBuffers = 4;      // number of internal buffers used by device
   int device = 0;        // 0 indicates the default or first available device
   char input;
-  RtAudio *audio;
+  RtAudio *audio = 0;
 
   // Open a stream during RtAudio instantiation
   try {
@@ -647,7 +645,7 @@ The example compiler statements above could be used to compile the <TT>probe.cpp
 
 \section debug Debugging
 
-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.
+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 that 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.
 
 \section apinotes API Notes
 
@@ -657,7 +655,7 @@ RtAudio is designed to provide a common API across the various supported operati
 
 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.
 
-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.
+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 that have ALSA drivers installed.
 
 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.
 
@@ -675,25 +673,67 @@ The Irix version of RtAudio was written and tested on an SGI Indy running Irix v
 
 \subsection windowsds Windows (DirectSound):
 
-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.
+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, especially since RtAudio version 3.0.2.  Input audio latency still tends to be bad but better since version 3.0.2.  RtAudio was originally developed with Visual C++ version 6.0 but has been tested with .NET.
+
+The DirectSound version of RtAudio can be compiled with or without the UNICODE preprocessor definition.
 
 \subsection windowsasio Windows (ASIO):
 
 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.
 
-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.
+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; iasiothiscallresolver.h,cpp</TT>.  The Visual C++ projects found in <TT>/tests/Windows/</TT> compile both ASIO and DirectSound support.
+
+The Steinberg provided <TT>asiolist</TT> class does not compile when the preprocessor definition UNICODE is defined.  Note that this could be an issue when using RtAudio with Qt, though Qt programs appear to compile without the UNICODE definition (try <tt>DEFINES -= UNICODE</tt> in your .pro file).  RtAudio with ASIO support has been tested using the MinGW compiler under Windows XP, as well as in the Visual Studio environment.
+
+\section wishlist Possible Future Changes
 
+There are a few issues that still need to be addressed in future versions of RtAudio, including:
+
+<ul>
+<li>Provide a mechanism so the user can "pre-fill" audio output buffers to allow precise measurement of an acoustic response;</li>
+<li>Allow the user to read / write non-interleaved data to / from the audio buffer;</li>
+<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>
+<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>
+</ul>
 
 \section acknowledge Acknowledgements
 
-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.
+Many thanks to the following people for providing bug fixes and improvements:
+<UL>
+<LI>Robin Davies (Windows DS and ASIO)</LI>
+<LI>Ryan Williams (Windows non-MS compiler ASIO support)</LI>
+<LI>Ed Wildgoose (Linux ALSA and Jack)</LI>
+</UL>
 
-The early 2.0 version of RtAudio 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 and the <A href="http://www.acoustics.hut.fi/">Laboratory of Acoustics and Audio Signal Processing</A> at the Helsinki University of Technology, Finland.  Much subsequent development happened while working at 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>.  The most recent version of RtAudio was finished while working as an assistant professor of <a href="http://www.music.mcgill.ca/musictech/">Music Technology</a> at <a href="http://www.mcgill.ca/">McGill University</a>.  This work was supported in part by the United States Air Force Office of Scientific Research (grant \#F49620-99-1-0293).
+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.
+
+The early 2.0 version of RtAudio 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 and the <A
+href="http://www.acoustics.hut.fi/">Laboratory of Acoustics and Audio
+Signal Processing</A> at the Helsinki University of Technology,
+Finland.  Much subsequent development happened while working at 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>.  The most
+recent version of RtAudio was finished while working as an assistant
+professor of <a href="http://www.music.mcgill.ca/musictech/">Music
+Technology</a> at <a href="http://www.mcgill.ca/">McGill
+University</a>.  This work was supported in part by the United States
+Air Force Office of Scientific Research (grant \#F49620-99-1-0293).
 
 \section license License
 
     RtAudio: a realtime audio i/o C++ classes<BR>
-    Copyright (c) 2001-2004 Gary P. Scavone
+    Copyright (c) 2001-2005 Gary P. Scavone
 
     Permission is hereby granted, free of charge, to any person
     obtaining a copy of this software and associated documentation files