Fix warning re S24 not trivially copyable by removing copy constructor.
[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   double 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 = 512;
126   RtAudio::StreamParameters oParams, iParams;
127   oParams.deviceId = oDevice;
128   oParams.nChannels = channels;
129   oParams.firstChannel = oOffset;
130
131   if ( oDevice == 0 )
132     oParams.deviceId = dac.getDefaultOutputDevice();
133
134   RtAudio::StreamOptions options;
135   options.flags = RTAUDIO_HOG_DEVICE;
136   try {
137     dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawi, (void *)data, &options );
138     std::cout << "\nStream latency = " << dac.getStreamLatency() << std::endl;
139
140     // Start the stream
141     dac.startStream();
142     std::cout << "\nPlaying ... press <enter> to stop.\n";
143     std::cin.get( input );
144
145     // Stop the stream
146     dac.stopStream();
147
148     // Restart again
149     std::cout << "Press <enter> to restart.\n";
150     std::cin.get( input );
151     dac.startStream();
152
153     // Test abort function
154     std::cout << "Playing again ... press <enter> to abort.\n";
155     std::cin.get( input );
156     dac.abortStream();
157
158     // Restart another time
159     std::cout << "Press <enter> to restart again.\n";
160     std::cin.get( input );
161     dac.startStream();
162
163     std::cout << "Playing again ... press <enter> to close the stream.\n";
164     std::cin.get( input );
165   }
166   catch ( RtAudioError& e ) {
167     e.printMessage();
168     goto cleanup;
169   }
170
171   if ( dac.isStreamOpen() ) dac.closeStream();
172
173   // Test non-interleaved functionality
174   options.flags = RTAUDIO_NONINTERLEAVED;
175   try {
176     dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawni, (void *)data, &options );
177
178     std::cout << "Press <enter> to start non-interleaved playback.\n";
179     std::cin.get( input );
180
181     // Start the stream
182     dac.startStream();
183     std::cout << "\nPlaying ... press <enter> to stop.\n";
184     std::cin.get( input );
185   }
186   catch ( RtAudioError& e ) {
187     e.printMessage();
188     goto cleanup;
189   }
190
191   if ( dac.isStreamOpen() ) dac.closeStream();
192
193   // Now open a duplex stream.
194   unsigned int bufferBytes;
195   iParams.deviceId = iDevice;
196   iParams.nChannels = channels;
197   iParams.firstChannel = iOffset;
198   if ( iDevice == 0 )
199     iParams.deviceId = dac.getDefaultInputDevice();
200   options.flags = RTAUDIO_NONINTERLEAVED;
201   try {
202     dac.openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
203
204     bufferBytes = bufferFrames * channels * 4;
205
206     std::cout << "Press <enter> to start duplex operation.\n";
207     std::cin.get( input );
208
209     // Start the stream
210     dac.startStream();
211     std::cout << "\nRunning ... press <enter> to stop.\n";
212     std::cin.get( input );
213
214     // Stop the stream
215     dac.stopStream();
216     std::cout << "\nStopped ... press <enter> to restart.\n";
217     std::cin.get( input );
218
219     // Restart the stream
220     dac.startStream();
221     std::cout << "\nRunning ... press <enter> to stop.\n";
222     std::cin.get( input );
223   }
224   catch ( RtAudioError& e ) {
225     e.printMessage();
226   }
227
228  cleanup:
229   if ( dac.isStreamOpen() ) dac.closeStream();
230   free( data );
231
232   return 0;
233 }