Various updates and fixes before 4.0.5 release (GS).
[rtaudio.git] / doc / doxygen / playback.txt
1 /*! \page playback Playback
2
3 In this example, we provide a complete program that demonstrates the use of RtAudio for audio playback.  Our program produces a two-channel sawtooth waveform for output.
4
5 \code
6 #include "RtAudio.h"
7 #include <iostream>
8 #include <cstdlib>
9
10 // Two-channel sawtooth wave generator.
11 int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
12          double streamTime, RtAudioStreamStatus status, void *userData )
13 {
14   unsigned int i, j;
15   double *buffer = (double *) outputBuffer;
16   double *lastValues = (double *) userData;
17
18   if ( status )
19     std::cout << "Stream underflow detected!" << std::endl;
20
21   // Write interleaved audio data.
22   for ( i=0; i<nBufferFrames; i++ ) {
23     for ( j=0; j<2; j++ ) {
24       *buffer++ = lastValues[j];
25
26       lastValues[j] += 0.005 * (j+1+(j*0.1));
27       if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
28     }
29   }
30
31   return 0;
32 }
33
34 int main()
35 {
36   RtAudio dac;
37   if ( dac.getDeviceCount() < 1 ) {
38     std::cout << "\nNo audio devices found!\n";
39     exit( 0 );
40   }
41
42   RtAudio::StreamParameters parameters;
43   parameters.deviceId = dac.getDefaultOutputDevice();
44   parameters.nChannels = 2;
45   parameters.firstChannel = 0;
46   unsigned int sampleRate = 44100;
47   unsigned int bufferFrames = 256; // 256 sample frames
48   double data[2];
49
50   try {
51     dac.openStream( &parameters, NULL, RTAUDIO_FLOAT64,
52                     sampleRate, &bufferFrames, &saw, (void *)&data );
53     dac.startStream();
54   }
55   catch ( RtError& e ) {
56     e.printMessage();
57     exit( 0 );
58   }
59   
60   char input;
61   std::cout << "\nPlaying ... press <enter> to quit.\n";
62   std::cin.get( input );
63
64   try {
65     // Stop the stream
66     dac.stopStream();
67   }
68   catch (RtError& e) {
69     e.printMessage();
70   }
71
72   if ( dac.isStreamOpen() ) dac.closeStream();
73
74   return 0;
75 }
76 \endcode
77
78 We open the stream in exactly the same way as the previous example (except with a data format change) and specify the address of our callback function \e "saw()". The callback function will automatically be invoked when the underlying audio system needs data for output.  Note that the callback function is called only when the stream is "running" (between calls to the RtAudio::startStream() and RtAudio::stopStream() functions).  We can also pass a pointer value to the RtAudio::openStream() function that is made available in the callback function.  In this way, it is possible to gain access to arbitrary data created in our \e main() function from within the globally defined callback function.
79
80 In this example, we stop the stream with an explicit call to RtAudio::stopStream().  It is also possible to stop a stream by returning a non-zero value from the callback function.  A return value of 1 will cause the stream to finish draining its internal buffers and then halt (equivalent to calling the RtAudio::stopStream() function).   A return value of 2 will cause the stream to stop immediately (equivalent to calling the RtAudio::abortStream() function).
81
82 */