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 double streamTimePrintIncrement = 1.0; // seconds
52 double streamTimePrintTime = 1.0; // seconds
54 int inout( void *outputBuffer, void *inputBuffer, unsigned int /*nBufferFrames*/,
55 double streamTime, RtAudioStreamStatus status, void *data )
57 // Since the number of input and output channels is equal, we can do
58 // a simple buffer copy operation here.
59 if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
61 if ( streamTime >= streamTimePrintTime ) {
62 std::cout << "streamTime = " << streamTime << std::endl;
63 streamTimePrintTime += streamTimePrintIncrement;
66 unsigned int *bytes = (unsigned int *) data;
67 memcpy( outputBuffer, inputBuffer, *bytes );
71 int main( int argc, char *argv[] )
73 unsigned int channels, fs, bufferBytes, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
75 // Minimal command-line checking
76 if (argc < 3 || argc > 7 ) usage();
79 if ( adac.getDeviceCount() < 1 ) {
80 std::cout << "\nNo audio devices found!\n";
84 channels = (unsigned int) atoi(argv[1]);
85 fs = (unsigned int) atoi(argv[2]);
87 iDevice = (unsigned int) atoi(argv[3]);
89 oDevice = (unsigned int) atoi(argv[4]);
91 iOffset = (unsigned int) atoi(argv[5]);
93 oOffset = (unsigned int) atoi(argv[6]);
95 // Let RtAudio print messages to stderr.
96 adac.showWarnings( true );
98 // Set the same number of channels for both input and output.
99 unsigned int bufferFrames = 512;
100 RtAudio::StreamParameters iParams, oParams;
101 iParams.deviceId = iDevice;
102 iParams.nChannels = channels;
103 iParams.firstChannel = iOffset;
104 oParams.deviceId = oDevice;
105 oParams.nChannels = channels;
106 oParams.firstChannel = oOffset;
109 iParams.deviceId = adac.getDefaultInputDevice();
111 oParams.deviceId = adac.getDefaultOutputDevice();
113 RtAudio::StreamOptions options;
114 //options.flags |= RTAUDIO_NONINTERLEAVED;
116 bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
118 adac.openStream( &oParams, &iParams, FORMAT, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
120 catch ( RtAudioError& e ) {
121 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
125 // Test RtAudio functionality for reporting latency.
126 std::cout << "\nStream latency = " << adac.getStreamLatency() << " frames" << std::endl;
132 std::cout << "\nRunning ... press <enter> to quit (buffer frames = " << bufferFrames << ").\n";
138 catch ( RtAudioError& e ) {
139 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
144 if ( adac.isStreamOpen() ) adac.closeStream();