1 /******************************************/
4 by Gary P. Scavone, 2007-2008
6 This program will make a variety of calls
7 to extensively test RtAudio functionality.
9 /******************************************/
14 #define BASE_RATE 0.005
18 // Error function in case of incorrect command-line
19 // argument specifications
20 std::cout << "\nuseage: testall N fs <iDevice> <oDevice> <iChannelOffset> <oChannelOffset>\n";
21 std::cout << " where N = number of channels,\n";
22 std::cout << " fs = the sample rate,\n";
23 std::cout << " iDevice = optional input device to use (default = 0),\n";
24 std::cout << " oDevice = optional output device to use (default = 0),\n";
25 std::cout << " iChannelOffset = an optional input channel offset (default = 0),\n";
26 std::cout << " and oChannelOffset = optional output channel offset (default = 0).\n\n";
30 unsigned int channels;
32 // Interleaved buffers
33 int sawi( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
34 double streamTime, RtAudioStreamStatus status, void *data )
37 extern unsigned int channels;
38 double *buffer = (double *) outputBuffer;
39 double *lastValues = (double *) data;
42 std::cout << "Stream underflow detected!" << std::endl;
44 for ( i=0; i<nBufferFrames; i++ ) {
45 for ( j=0; j<channels; j++ ) {
46 *buffer++ = (double) lastValues[j];
47 lastValues[j] += BASE_RATE * (j+1+(j*0.1));
48 if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
55 // Non-interleaved buffers
56 int sawni( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
57 double streamTime, RtAudioStreamStatus status, void *data )
60 extern unsigned int channels;
61 double *buffer = (double *) outputBuffer;
62 double *lastValues = (double *) data;
65 std::cout << "Stream underflow detected!" << std::endl;
68 for ( j=0; j<channels; j++ ) {
69 increment = BASE_RATE * (j+1+(j*0.1));
70 for ( i=0; i<nBufferFrames; i++ ) {
71 *buffer++ = (double) lastValues[j];
72 lastValues[j] += increment;
73 if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
80 int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
81 double streamTime, RtAudioStreamStatus status, void *data )
83 // Since the number of input and output channels is equal, we can do
84 // a simple buffer copy operation here.
85 if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
87 unsigned long *bytes = (unsigned long *) data;
88 memcpy( outputBuffer, inputBuffer, *bytes );
92 int main( int argc, char *argv[] )
94 unsigned int bufferFrames, fs, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
97 // minimal command-line checking
98 if (argc < 3 || argc > 7 ) usage();
101 if ( dac.getDeviceCount() < 1 ) {
102 std::cout << "\nNo audio devices found!\n";
106 channels = (unsigned int) atoi( argv[1] );
107 fs = (unsigned int) atoi( argv[2] );
109 iDevice = (unsigned int) atoi( argv[3] );
111 oDevice = (unsigned int) atoi(argv[4]);
113 iOffset = (unsigned int) atoi(argv[5]);
115 oOffset = (unsigned int) atoi(argv[6]);
117 double *data = (double *) calloc( channels, sizeof( double ) );
119 // Let RtAudio print messages to stderr.
120 dac.showWarnings( true );
122 // Set our stream parameters for output only.
124 RtAudio::StreamParameters oParams, iParams;
125 oParams.deviceId = oDevice;
126 oParams.nChannels = channels;
127 oParams.firstChannel = oOffset;
129 RtAudio::StreamOptions options;
130 options.flags = RTAUDIO_HOG_DEVICE;
132 dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawi, (void *)data, &options );
133 std::cout << "\nStream latency = " << dac.getStreamLatency() << std::endl;
137 std::cout << "\nPlaying ... press <enter> to stop.\n";
138 std::cin.get( input );
144 std::cout << "Press <enter> to restart.\n";
145 std::cin.get( input );
148 // Test abort function
149 std::cout << "Playing again ... press <enter> to abort.\n";
150 std::cin.get( input );
153 // Restart another time
154 std::cout << "Press <enter> to restart again.\n";
155 std::cin.get( input );
158 std::cout << "Playing again ... press <enter> to close the stream.\n";
159 std::cin.get( input );
161 catch ( RtError& e ) {
166 if ( dac.isStreamOpen() ) dac.closeStream();
168 // Test non-interleaved functionality
169 options.flags = RTAUDIO_NONINTERLEAVED;
171 dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawni, (void *)data, &options );
173 std::cout << "Press <enter> to start non-interleaved playback.\n";
174 std::cin.get( input );
178 std::cout << "\nPlaying ... press <enter> to stop.\n";
179 std::cin.get( input );
181 catch ( RtError& e ) {
186 if ( dac.isStreamOpen() ) dac.closeStream();
188 // Now open a duplex stream.
189 unsigned int bufferBytes;
190 iParams.deviceId = iDevice;
191 iParams.nChannels = channels;
192 iParams.firstChannel = iOffset;
193 options.flags = RTAUDIO_NONINTERLEAVED;
195 dac.openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
197 bufferBytes = bufferFrames * channels * 4;
199 std::cout << "Press <enter> to start duplex operation.\n";
200 std::cin.get( input );
204 std::cout << "\nRunning ... press <enter> to stop.\n";
205 std::cin.get( input );
209 std::cout << "\nStopped ... press <enter> to restart.\n";
210 std::cin.get( input );
212 // Restart the stream
214 std::cout << "\nRunning ... press <enter> to stop.\n";
215 std::cin.get( input );
217 catch ( RtError& e ) {
222 if ( dac.isStreamOpen() ) dac.closeStream();