1 /******************************************/
4 by Gary P. Scavone, 2006-2007.
6 This program opens a duplex stream and passes
7 input directly through to the output.
9 /******************************************/
18 #define FORMAT RTAUDIO_SINT8
21 typedef signed short MY_TYPE;
22 #define FORMAT RTAUDIO_SINT16
26 #define FORMAT RTAUDIO_SINT24
28 typedef signed long MY_TYPE;
29 #define FORMAT RTAUDIO_SINT32
31 typedef float MY_TYPE;
32 #define FORMAT RTAUDIO_FLOAT32
34 typedef double MY_TYPE;
35 #define FORMAT RTAUDIO_FLOAT64
39 // Error function in case of incorrect command-line
40 // argument specifications
41 std::cout << "\nuseage: duplex N fs <iDevice> <oDevice> <iChannelOffset> <oChannelOffset>\n";
42 std::cout << " where N = number of channels,\n";
43 std::cout << " fs = the sample rate,\n";
44 std::cout << " iDevice = optional input device to use (default = 0),\n";
45 std::cout << " oDevice = optional output device to use (default = 0),\n";
46 std::cout << " iChannelOffset = an optional input channel offset (default = 0),\n";
47 std::cout << " and oChannelOffset = optional output channel offset (default = 0).\n\n";
51 int inout( void *outputBuffer, void *inputBuffer, unsigned int /*nBufferFrames*/,
52 double /*streamTime*/, RtAudioStreamStatus status, void *data )
54 // Since the number of input and output channels is equal, we can do
55 // a simple buffer copy operation here.
56 if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
58 unsigned int *bytes = (unsigned int *) data;
59 memcpy( outputBuffer, inputBuffer, *bytes );
63 int main( int argc, char *argv[] )
65 unsigned int channels, fs, bufferBytes, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
67 // Minimal command-line checking
68 if (argc < 3 || argc > 7 ) usage();
71 if ( adac.getDeviceCount() < 1 ) {
72 std::cout << "\nNo audio devices found!\n";
76 channels = (unsigned int) atoi(argv[1]);
77 fs = (unsigned int) atoi(argv[2]);
79 iDevice = (unsigned int) atoi(argv[3]);
81 oDevice = (unsigned int) atoi(argv[4]);
83 iOffset = (unsigned int) atoi(argv[5]);
85 oOffset = (unsigned int) atoi(argv[6]);
87 // Let RtAudio print messages to stderr.
88 adac.showWarnings( true );
90 // Set the same number of channels for both input and output.
91 unsigned int bufferFrames = 512;
92 RtAudio::StreamParameters iParams, oParams;
93 iParams.deviceId = iDevice;
94 iParams.nChannels = channels;
95 iParams.firstChannel = iOffset;
96 oParams.deviceId = oDevice;
97 oParams.nChannels = channels;
98 oParams.firstChannel = oOffset;
101 iParams.deviceId = adac.getDefaultInputDevice();
103 oParams.deviceId = adac.getDefaultOutputDevice();
105 RtAudio::StreamOptions options;
106 //options.flags |= RTAUDIO_NONINTERLEAVED;
109 adac.openStream( &oParams, &iParams, FORMAT, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
111 catch ( RtAudioError& e ) {
112 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
116 bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
118 // Test RtAudio functionality for reporting latency.
119 std::cout << "\nStream latency = " << adac.getStreamLatency() << " frames" << std::endl;
125 std::cout << "\nRunning ... press <enter> to quit (buffer frames = " << bufferFrames << ").\n";
131 catch ( RtAudioError& e ) {
132 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
137 if ( adac.isStreamOpen() ) adac.closeStream();