Check in of new version 4.0.0 distribution (GS).
[rtaudio-cdist.git] / tests / duplex.cpp
1 /******************************************/
2 /*
3   duplex.cpp
4   by Gary P. Scavone, 2006-2007.
5
6   This program opens a duplex stream and passes
7   input directly through to the output.
8 */
9 /******************************************/
10
11 #include "RtAudio.h"
12 #include <iostream>
13
14 /*
15 typedef signed long  MY_TYPE;
16 #define FORMAT RTAUDIO_SINT24
17
18 typedef char  MY_TYPE;
19 #define FORMAT RTAUDIO_SINT8
20
21 typedef signed short  MY_TYPE;
22 #define FORMAT RTAUDIO_SINT16
23
24 typedef signed long  MY_TYPE;
25 #define FORMAT RTAUDIO_SINT32
26
27 typedef float  MY_TYPE;
28 #define FORMAT RTAUDIO_FLOAT32
29 */
30
31 typedef double  MY_TYPE;
32 #define FORMAT RTAUDIO_FLOAT64
33
34 void usage( void ) {
35   // Error function in case of incorrect command-line
36   // argument specifications
37   std::cout << "\nuseage: duplex N fs <iDevice> <oDevice> <iChannelOffset> <oChannelOffset>\n";
38   std::cout << "    where N = number of channels,\n";
39   std::cout << "    fs = the sample rate,\n";
40   std::cout << "    iDevice = optional input device to use (default = 0),\n";
41   std::cout << "    oDevice = optional output device to use (default = 0),\n";
42   std::cout << "    iChannelOffset = an optional input channel offset (default = 0),\n";
43   std::cout << "    and oChannelOffset = optional output channel offset (default = 0).\n\n";
44   exit( 0 );
45 }
46
47 int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
48            double streamTime, RtAudioStreamStatus status, void *data )
49 {
50   // Since the number of input and output channels is equal, we can do
51   // a simple buffer copy operation here.
52   if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
53
54   unsigned long *bytes = (unsigned long *) data;
55   memcpy( outputBuffer, inputBuffer, *bytes );
56   return 0;
57 }
58
59 int main(int argc, char *argv[])
60 {
61   unsigned int channels, fs, bufferBytes, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
62
63   // Minimal command-line checking
64   if (argc < 3 || argc > 7 ) usage();
65
66   RtAudio adac;
67   if ( adac.getDeviceCount() < 1 ) {
68     std::cout << "\nNo audio devices found!\n";
69     exit( 0 );
70   }
71
72   channels = (unsigned int) atoi(argv[1]);
73   fs = (unsigned int) atoi(argv[2]);
74   if ( argc > 3 )
75     iDevice = (unsigned int) atoi(argv[3]);
76   if ( argc > 4 )
77     oDevice = (unsigned int) atoi(argv[4]);
78   if ( argc > 5 )
79     iOffset = (unsigned int) atoi(argv[5]);
80   if ( argc > 6 )
81     oOffset = (unsigned int) atoi(argv[6]);
82
83   // Let RtAudio print messages to stderr.
84   adac.showWarnings( true );
85
86   // Set the same number of channels for both input and output.
87   unsigned int bufferFrames = 512;
88   RtAudio::StreamParameters iParams, oParams;
89   iParams.deviceId = iDevice;
90   iParams.nChannels = channels;
91   iParams.firstChannel = iOffset;
92   oParams.deviceId = oDevice;
93   oParams.nChannels = channels;
94   oParams.firstChannel = oOffset;
95
96   RtAudio::StreamOptions options;
97   //options.flags |= RTAUDIO_NONINTERLEAVED;
98
99   try {
100     adac.openStream( &oParams, &iParams, FORMAT, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
101   }
102   catch ( RtError& e ) {
103     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
104     exit( 0 );
105   }
106
107   bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
108
109   // Test RtAudio functionality for reporting latency.
110   std::cout << "\nStream latency = " << adac.getStreamLatency() << " frames" << std::endl;
111
112   try {
113     adac.startStream();
114
115     char input;
116     std::cout << "\nRunning ... press <enter> to quit (buffer frames = " << bufferFrames << ").\n";
117     std::cin.get(input);
118
119     // Stop the stream.
120     adac.stopStream();
121   }
122   catch ( RtError& e ) {
123     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
124     goto cleanup;
125   }
126
127  cleanup:
128   if ( adac.isStreamOpen() ) adac.closeStream();
129
130   return 0;
131 }