c5abc3b60a5401e3999fa4cd5901310194899124
[rtaudio.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 #include <cstdlib>
14 #include <cstring>
15
16 /*
17 typedef char MY_TYPE;
18 #define FORMAT RTAUDIO_SINT8
19 */
20
21 typedef signed short MY_TYPE;
22 #define FORMAT RTAUDIO_SINT16
23
24 /*
25 typedef S24 MY_TYPE;
26 #define FORMAT RTAUDIO_SINT24
27
28 typedef signed long MY_TYPE;
29 #define FORMAT RTAUDIO_SINT32
30
31 typedef float MY_TYPE;
32 #define FORMAT RTAUDIO_FLOAT32
33
34 typedef double MY_TYPE;
35 #define FORMAT RTAUDIO_FLOAT64
36 */
37
38 void usage( void ) {
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";
48   exit( 0 );
49 }
50
51 double streamTimePrintIncrement = 1.0; // seconds
52 double streamTimePrintTime = 1.0; // seconds
53
54 int inout( void *outputBuffer, void *inputBuffer, unsigned int /*nBufferFrames*/,
55            double streamTime, RtAudioStreamStatus status, void *data )
56 {
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;
60
61   if ( streamTime >= streamTimePrintTime ) {
62     std::cout << "streamTime = " << streamTime << std::endl;
63     streamTimePrintTime += streamTimePrintIncrement;
64   }
65
66   unsigned int *bytes = (unsigned int *) data;
67   memcpy( outputBuffer, inputBuffer, *bytes );
68   return 0;
69 }
70
71 int main( int argc, char *argv[] )
72 {
73   unsigned int channels, fs, bufferBytes, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
74
75   // Minimal command-line checking
76   if (argc < 3 || argc > 7 ) usage();
77
78   RtAudio adac;
79   if ( adac.getDeviceCount() < 1 ) {
80     std::cout << "\nNo audio devices found!\n";
81     exit( 1 );
82   }
83
84   channels = (unsigned int) atoi(argv[1]);
85   fs = (unsigned int) atoi(argv[2]);
86   if ( argc > 3 )
87     iDevice = (unsigned int) atoi(argv[3]);
88   if ( argc > 4 )
89     oDevice = (unsigned int) atoi(argv[4]);
90   if ( argc > 5 )
91     iOffset = (unsigned int) atoi(argv[5]);
92   if ( argc > 6 )
93     oOffset = (unsigned int) atoi(argv[6]);
94
95   // Let RtAudio print messages to stderr.
96   adac.showWarnings( true );
97
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;
107
108   if ( iDevice == 0 )
109     iParams.deviceId = adac.getDefaultInputDevice();
110   if ( oDevice == 0 )
111     oParams.deviceId = adac.getDefaultOutputDevice();
112
113   RtAudio::StreamOptions options;
114   //options.flags |= RTAUDIO_NONINTERLEAVED;
115
116   bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
117   try {
118     adac.openStream( &oParams, &iParams, FORMAT, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
119   }
120   catch ( RtAudioError& e ) {
121     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
122     exit( 1 );
123   }
124
125   // Test RtAudio functionality for reporting latency.
126   std::cout << "\nStream latency = " << adac.getStreamLatency() << " frames" << std::endl;
127
128   try {
129     adac.startStream();
130
131     char input;
132     std::cout << "\nRunning ... press <enter> to quit (buffer frames = " << bufferFrames << ").\n";
133     std::cin.get(input);
134
135     // Stop the stream.
136     adac.stopStream();
137   }
138   catch ( RtAudioError& e ) {
139     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
140     goto cleanup;
141   }
142
143  cleanup:
144   if ( adac.isStreamOpen() ) adac.closeStream();
145
146   return 0;
147 }