1 /******************************************/
4 by Gary P. Scavone, 2007-2008
6 This program will make a variety of calls
7 to extensively test RtAudio functionality.
9 /******************************************/
16 #define BASE_RATE 0.005
20 // Error function in case of incorrect command-line
21 // argument specifications
22 std::cout << "\nuseage: testall N fs <iDevice> <oDevice> <iChannelOffset> <oChannelOffset>\n";
23 std::cout << " where N = number of channels,\n";
24 std::cout << " fs = the sample rate,\n";
25 std::cout << " iDevice = optional input device to use (default = 0),\n";
26 std::cout << " oDevice = optional output device to use (default = 0),\n";
27 std::cout << " iChannelOffset = an optional input channel offset (default = 0),\n";
28 std::cout << " and oChannelOffset = optional output channel offset (default = 0).\n\n";
32 unsigned int channels;
34 // Interleaved buffers
35 int sawi( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
36 double /*streamTime*/, RtAudioStreamStatus status, void *data )
39 extern unsigned int channels;
40 double *buffer = (double *) outputBuffer;
41 double *lastValues = (double *) data;
44 std::cout << "Stream underflow detected!" << std::endl;
46 for ( i=0; i<nBufferFrames; i++ ) {
47 for ( j=0; j<channels; j++ ) {
48 *buffer++ = (double) lastValues[j];
49 lastValues[j] += BASE_RATE * (j+1+(j*0.1));
50 if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
57 // Non-interleaved buffers
58 int sawni( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
59 double /*streamTime*/, RtAudioStreamStatus status, void *data )
62 extern unsigned int channels;
63 double *buffer = (double *) outputBuffer;
64 double *lastValues = (double *) data;
67 std::cout << "Stream underflow detected!" << std::endl;
70 for ( j=0; j<channels; j++ ) {
71 increment = BASE_RATE * (j+1+(j*0.1));
72 for ( i=0; i<nBufferFrames; i++ ) {
73 *buffer++ = (double) lastValues[j];
74 lastValues[j] += increment;
75 if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
82 int inout( void *outputBuffer, void *inputBuffer, unsigned int /*nBufferFrames*/,
83 double /*streamTime*/, RtAudioStreamStatus status, void *data )
85 // Since the number of input and output channels is equal, we can do
86 // a simple buffer copy operation here.
87 if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
89 unsigned int *bytes = (unsigned int *) data;
90 memcpy( outputBuffer, inputBuffer, *bytes );
94 int main( int argc, char *argv[] )
96 unsigned int bufferFrames, fs, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
99 // minimal command-line checking
100 if (argc < 3 || argc > 7 ) usage();
103 if ( dac.getDeviceCount() < 1 ) {
104 std::cout << "\nNo audio devices found!\n";
108 channels = (unsigned int) atoi( argv[1] );
109 fs = (unsigned int) atoi( argv[2] );
111 iDevice = (unsigned int) atoi( argv[3] );
113 oDevice = (unsigned int) atoi(argv[4]);
115 iOffset = (unsigned int) atoi(argv[5]);
117 oOffset = (unsigned int) atoi(argv[6]);
119 double *data = (double *) calloc( channels, sizeof( double ) );
121 // Let RtAudio print messages to stderr.
122 dac.showWarnings( true );
124 // Set our stream parameters for output only.
126 RtAudio::StreamParameters oParams, iParams;
127 oParams.deviceId = oDevice;
128 oParams.nChannels = channels;
129 oParams.firstChannel = oOffset;
132 oParams.deviceId = dac.getDefaultOutputDevice();
134 RtAudio::StreamOptions options;
135 options.flags = RTAUDIO_HOG_DEVICE;
137 dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawi, (void *)data, &options );
138 std::cout << "\nStream latency = " << dac.getStreamLatency() << std::endl;
142 std::cout << "\nPlaying ... press <enter> to stop.\n";
143 std::cin.get( input );
149 std::cout << "Press <enter> to restart.\n";
150 std::cin.get( input );
153 // Test abort function
154 std::cout << "Playing again ... press <enter> to abort.\n";
155 std::cin.get( input );
158 // Restart another time
159 std::cout << "Press <enter> to restart again.\n";
160 std::cin.get( input );
163 std::cout << "Playing again ... press <enter> to close the stream.\n";
164 std::cin.get( input );
166 catch ( RtAudioError& e ) {
171 if ( dac.isStreamOpen() ) dac.closeStream();
173 // Test non-interleaved functionality
174 options.flags = RTAUDIO_NONINTERLEAVED;
176 dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawni, (void *)data, &options );
178 std::cout << "Press <enter> to start non-interleaved playback.\n";
179 std::cin.get( input );
183 std::cout << "\nPlaying ... press <enter> to stop.\n";
184 std::cin.get( input );
186 catch ( RtAudioError& e ) {
191 if ( dac.isStreamOpen() ) dac.closeStream();
193 // Now open a duplex stream.
194 unsigned int bufferBytes;
195 iParams.deviceId = iDevice;
196 iParams.nChannels = channels;
197 iParams.firstChannel = iOffset;
199 iParams.deviceId = dac.getDefaultInputDevice();
200 options.flags = RTAUDIO_NONINTERLEAVED;
202 dac.openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
204 bufferBytes = bufferFrames * channels * 4;
206 std::cout << "Press <enter> to start duplex operation.\n";
207 std::cin.get( input );
211 std::cout << "\nRunning ... press <enter> to stop.\n";
212 std::cin.get( input );
216 std::cout << "\nStopped ... press <enter> to restart.\n";
217 std::cin.get( input );
219 // Restart the stream
221 std::cout << "\nRunning ... press <enter> to stop.\n";
222 std::cin.get( input );
224 catch ( RtAudioError& e ) {
229 if ( dac.isStreamOpen() ) dac.closeStream();