Various updates and fixes before 4.0.5 release (GS).
[rtaudio-cdist.git] / doc / doxygen / duplex.txt
1 /*! \page duplex Duplex Mode
2
3 Finally, it is easy to use RtAudio for simultaneous audio input/output, or duplex operation.  In this example, we simply pass the input data back to the output.
4
5 \code
6 #include "RtAudio.h"
7 #include <iostream>
8 #include <cstdlib>
9 #include <cstring>
10
11 // Pass-through function.
12 int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
13            double streamTime, RtAudioStreamStatus status, void *data )
14 {
15   // Since the number of input and output channels is equal, we can do
16   // a simple buffer copy operation here.
17   if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
18
19   unsigned long *bytes = (unsigned long *) data;
20   memcpy( outputBuffer, inputBuffer, *bytes );
21   return 0;
22 }
23
24 int main()
25 {
26  RtAudio adac;
27   if ( adac.getDeviceCount() < 1 ) {
28     std::cout << "\nNo audio devices found!\n";
29     exit( 0 );
30   }
31
32   // Set the same number of channels for both input and output.
33   unsigned int bufferBytes, bufferFrames = 512;
34   RtAudio::StreamParameters iParams, oParams;
35   iParams.deviceId = 0; // first available device
36   iParams.nChannels = 2;
37   oParams.deviceId = 0; // first available device
38   oParams.nChannels = 2;
39
40   try {
41     adac.openStream( &oParams, &iParams, RTAUDIO_SINT32, 44100, &bufferFrames, &inout, (void *)&bufferBytes );
42   }
43   catch ( RtError& e ) {
44     e.printMessage();
45     exit( 0 );
46   }
47
48   bufferBytes = bufferFrames * 2 * 4;
49
50   try {
51     adac.startStream();
52
53     char input;
54     std::cout << "\nRunning ... press <enter> to quit.\n";
55     std::cin.get(input);
56
57     // Stop the stream.
58     adac.stopStream();
59   }
60   catch ( RtError& e ) {
61     e.printMessage();
62     goto cleanup;
63   }
64
65  cleanup:
66   if ( adac.isStreamOpen() ) adac.closeStream();
67
68   return 0;
69 }
70 \endcode
71
72 In this example, audio recorded by the stream input will be played out during the next round of audio processing.
73
74 Note that a duplex stream can make use of two different devices (except when using the Linux Jack and Windows ASIO APIs).  However, this may cause timing problems due to possible device clock variations, unless a common external "sync" is provided.
75
76 */