Updates for release 4.0.8, including new python binding, new teststops.cpp program...
[rtaudio-cdist.git] / tests / testall.cpp
1 /******************************************/
2 /*
3   testall.cpp
4   by Gary P. Scavone, 2007-2008
5
6   This program will make a variety of calls
7   to extensively test RtAudio functionality.
8 */
9 /******************************************/
10
11 #include "RtAudio.h"
12 #include <iostream>
13 #include <cstdlib>
14 #include <cstring>
15
16 #define BASE_RATE 0.005
17 #define TIME   1.0
18
19 void usage( void ) {
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";
29   exit( 0 );
30 }
31
32 unsigned int channels;
33
34 // Interleaved buffers
35 int sawi( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
36           double streamTime, RtAudioStreamStatus status, void *data )
37 {
38   unsigned int i, j;
39   extern unsigned int channels;
40   double *buffer = (double *) outputBuffer;
41   double *lastValues = (double *) data;
42
43   if ( status )
44     std::cout << "Stream underflow detected!" << std::endl;
45
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;
51     }
52   }
53
54   return 0;
55 }
56
57 // Non-interleaved buffers
58 int sawni( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
59            double streamTime, RtAudioStreamStatus status, void *data )
60 {
61   unsigned int i, j;
62   extern unsigned int channels;
63   double *buffer = (double *) outputBuffer;
64   double *lastValues = (double *) data;
65
66   if ( status )
67     std::cout << "Stream underflow detected!" << std::endl;
68
69   float increment;
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;
76     }
77   }
78
79   return 0;
80 }
81
82 int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
83            double streamTime, RtAudioStreamStatus status, void *data )
84 {
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;
88
89   unsigned int *bytes = (unsigned int *) data;
90   memcpy( outputBuffer, inputBuffer, *bytes );
91   return 0;
92 }
93
94 int main( int argc, char *argv[] )
95 {
96   unsigned int bufferFrames, fs, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
97   char input;
98
99   // minimal command-line checking
100   if (argc < 3 || argc > 7 ) usage();
101
102   RtAudio dac;
103   if ( dac.getDeviceCount() < 1 ) {
104     std::cout << "\nNo audio devices found!\n";
105     exit( 1 );
106   }
107
108   channels = (unsigned int) atoi( argv[1] );
109   fs = (unsigned int) atoi( argv[2] );
110   if ( argc > 3 )
111     iDevice = (unsigned int) atoi( argv[3] );
112   if ( argc > 4 )
113     oDevice = (unsigned int) atoi(argv[4]);
114   if ( argc > 5 )
115     iOffset = (unsigned int) atoi(argv[5]);
116   if ( argc > 6 )
117     oOffset = (unsigned int) atoi(argv[6]);
118
119   double *data = (double *) calloc( channels, sizeof( double ) );
120
121   // Let RtAudio print messages to stderr.
122   dac.showWarnings( true );
123
124   // Set our stream parameters for output only.
125   bufferFrames = 256;
126   RtAudio::StreamParameters oParams, iParams;
127   oParams.deviceId = oDevice;
128   oParams.nChannels = channels;
129   oParams.firstChannel = oOffset;
130
131   RtAudio::StreamOptions options;
132   options.flags = RTAUDIO_HOG_DEVICE;
133   try {
134     dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawi, (void *)data, &options );
135     std::cout << "\nStream latency = " << dac.getStreamLatency() << std::endl;
136
137     // Start the stream
138     dac.startStream();
139     std::cout << "\nPlaying ... press <enter> to stop.\n";
140     std::cin.get( input );
141
142     // Stop the stream
143     dac.stopStream();
144
145     // Restart again
146     std::cout << "Press <enter> to restart.\n";
147     std::cin.get( input );
148     dac.startStream();
149
150     // Test abort function
151     std::cout << "Playing again ... press <enter> to abort.\n";
152     std::cin.get( input );
153     dac.abortStream();
154
155     // Restart another time
156     std::cout << "Press <enter> to restart again.\n";
157     std::cin.get( input );
158     dac.startStream();
159
160     std::cout << "Playing again ... press <enter> to close the stream.\n";
161     std::cin.get( input );
162   }
163   catch ( RtError& e ) {
164     e.printMessage();
165     goto cleanup;
166   }
167
168   if ( dac.isStreamOpen() ) dac.closeStream();
169
170   // Test non-interleaved functionality
171   options.flags = RTAUDIO_NONINTERLEAVED;
172   try {
173     dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawni, (void *)data, &options );
174
175     std::cout << "Press <enter> to start non-interleaved playback.\n";
176     std::cin.get( input );
177
178     // Start the stream
179     dac.startStream();
180     std::cout << "\nPlaying ... press <enter> to stop.\n";
181     std::cin.get( input );
182   }
183   catch ( RtError& e ) {
184     e.printMessage();
185     goto cleanup;
186   }
187
188   if ( dac.isStreamOpen() ) dac.closeStream();
189
190   // Now open a duplex stream.
191   unsigned int bufferBytes;
192   iParams.deviceId = iDevice;
193   iParams.nChannels = channels;
194   iParams.firstChannel = iOffset;
195   options.flags = RTAUDIO_NONINTERLEAVED;
196   try {
197     dac.openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
198
199     bufferBytes = bufferFrames * channels * 4;
200
201     std::cout << "Press <enter> to start duplex operation.\n";
202     std::cin.get( input );
203
204     // Start the stream
205     dac.startStream();
206     std::cout << "\nRunning ... press <enter> to stop.\n";
207     std::cin.get( input );
208
209     // Stop the stream
210     dac.stopStream();
211     std::cout << "\nStopped ... press <enter> to restart.\n";
212     std::cin.get( input );
213
214     // Restart the stream
215     dac.startStream();
216     std::cout << "\nRunning ... press <enter> to stop.\n";
217     std::cin.get( input );
218   }
219   catch ( RtError& e ) {
220     e.printMessage();
221   }
222
223  cleanup:
224   if ( dac.isStreamOpen() ) dac.closeStream();
225   free( data );
226
227   return 0;
228 }