Merge pull request #156 from spmp/feature/spmp-changes
[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 #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 int inout( void *outputBuffer, void *inputBuffer, unsigned int /*nBufferFrames*/,
52            double /*streamTime*/, RtAudioStreamStatus status, void *data )
53 {
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;
57
58   unsigned int *bytes = (unsigned int *) data;
59   memcpy( outputBuffer, inputBuffer, *bytes );
60   return 0;
61 }
62
63 int main( int argc, char *argv[] )
64 {
65   unsigned int channels, fs, bufferBytes, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
66
67   // Minimal command-line checking
68   if (argc < 3 || argc > 7 ) usage();
69
70   RtAudio adac;
71   if ( adac.getDeviceCount() < 1 ) {
72     std::cout << "\nNo audio devices found!\n";
73     exit( 1 );
74   }
75
76   channels = (unsigned int) atoi(argv[1]);
77   fs = (unsigned int) atoi(argv[2]);
78   if ( argc > 3 )
79     iDevice = (unsigned int) atoi(argv[3]);
80   if ( argc > 4 )
81     oDevice = (unsigned int) atoi(argv[4]);
82   if ( argc > 5 )
83     iOffset = (unsigned int) atoi(argv[5]);
84   if ( argc > 6 )
85     oOffset = (unsigned int) atoi(argv[6]);
86
87   // Let RtAudio print messages to stderr.
88   adac.showWarnings( true );
89
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;
99
100   if ( iDevice == 0 )
101     iParams.deviceId = adac.getDefaultInputDevice();
102   if ( oDevice == 0 )
103     oParams.deviceId = adac.getDefaultOutputDevice();
104
105   RtAudio::StreamOptions options;
106   //options.flags |= RTAUDIO_NONINTERLEAVED;
107
108   bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
109   try {
110     adac.openStream( &oParams, &iParams, FORMAT, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
111   }
112   catch ( RtAudioError& e ) {
113     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
114     exit( 1 );
115   }
116
117   // Test RtAudio functionality for reporting latency.
118   std::cout << "\nStream latency = " << adac.getStreamLatency() << " frames" << std::endl;
119
120   try {
121     adac.startStream();
122
123     char input;
124     std::cout << "\nRunning ... press <enter> to quit (buffer frames = " << bufferFrames << ").\n";
125     std::cin.get(input);
126
127     // Stop the stream.
128     adac.stopStream();
129   }
130   catch ( RtAudioError& e ) {
131     std::cout << '\n' << e.getMessage() << '\n' << std::endl;
132     goto cleanup;
133   }
134
135  cleanup:
136   if ( adac.isStreamOpen() ) adac.closeStream();
137
138   return 0;
139 }