Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/paqa_errs.c

raw symbols SCIP
1 /** @file paqa_errs.c
2 @brief Self Testing Quality Assurance app for PortAudio
3 Do lots of bad things to test error reporting.
4 @author Phil Burk http://www.softsynth.com
5 Pieter Suurmond adapted to V19 API.
6 */
7 /*
8 * $Id: paqa_errs.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
9 *
10 * This program uses the PortAudio Portable Audio Library.
11 * For more information see: http://www.portaudio.com
12 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
13 *
14 * Permission is hereby granted, free of charge, to any person obtaining
15 * a copy of this software and associated documentation files
16 * (the "Software"), to deal in the Software without restriction,
17 * including without limitation the rights to use, copy, modify, merge,
18 * publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so,
20 * subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice shall be
23 * included in all copies or substantial portions of the Software.
24 *
25 * Any person wishing to distribute modifications to the Software is
26 * requested to send the modifications to the original developer so that
27 * they can be incorporated into the canonical version.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
33 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 *
37 */
38
39 #include <stdio.h>
40 #include <math.h>
41
42 #include "portaudio.h"
43
44 /*--------- Definitions ---------*/
45 #define MODE_INPUT (0)
46 #define MODE_OUTPUT (1)
47 #define FRAMES_PER_BUFFER (64)
48 #define SAMPLE_RATE (44100.0)
49
50 typedef struct PaQaData
51 {
52 unsigned long framesLeft;
53 int numChannels;
54 int bytesPerSample;
55 int mode;
56 }
57 PaQaData;
58
59 static int gNumPassed = 0; /* Two globals */
60 static int gNumFailed = 0;
61
62 /*------------------- Macros ------------------------------*/
63 /* Print ERROR if it fails. Tally success or failure. Odd */
64 /* do-while wrapper seems to be needed for some compilers. */
65
66 #define EXPECT(_exp) \
67 do \
68 { \
69 if ((_exp)) {\
70 gNumPassed++; \
71 } \
72 else { \
73 printf("\nERROR - 0x%x - %s for %s\n", result, Pa_GetErrorText(result), #_exp ); \
74 gNumFailed++; \
75 goto error; \
76 } \
77 } while(0)
78
79 #define HOPEFOR(_exp) \
80 do \
81 { \
82 if ((_exp)) {\
83 gNumPassed++; \
84 } \
85 else { \
86 printf("\nERROR - 0x%x - %s for %s\n", result, Pa_GetErrorText(result), #_exp ); \
87 gNumFailed++; \
88 } \
89 } while(0)
90
91 /*-------------------------------------------------------------------------*/
92 /* This routine will be called by the PortAudio engine when audio is needed.
93 It may be called at interrupt level on some machines so don't do anything
94 that could mess up the system like calling malloc() or free().
95 */
96 static int QaCallback( const void* inputBuffer,
97 void* outputBuffer,
98 unsigned long framesPerBuffer,
99 const PaStreamCallbackTimeInfo* timeInfo,
100 PaStreamCallbackFlags statusFlags,
101 void* userData )
102 {
103 unsigned long i;
104 unsigned char* out = (unsigned char *) outputBuffer;
105 PaQaData* data = (PaQaData *) userData;
106
107 (void)inputBuffer; /* Prevent "unused variable" warnings. */
108
109 /* Zero out buffer so we don't hear terrible noise. */
110 if( data->mode == MODE_OUTPUT )
111 {
112 unsigned long numBytes = framesPerBuffer * data->numChannels * data->bytesPerSample;
113 for( i=0; i<numBytes; i++ )
114 {
115 *out++ = 0;
116 }
117 }
118 /* Are we through yet? */
119 if( data->framesLeft > framesPerBuffer )
120 {
121 data->framesLeft -= framesPerBuffer;
122 return 0;
123 }
124 else
125 {
126 data->framesLeft = 0;
127 return 1;
128 }
129 }
130
131 static PaDeviceIndex FindInputOnlyDevice(void)
132 {
133 PaDeviceIndex result = Pa_GetDefaultInputDevice();
134 if( result != paNoDevice && Pa_GetDeviceInfo(result)->maxOutputChannels == 0 )
135 return result;
136
137 for( result = 0; result < Pa_GetDeviceCount(); ++result )
138 {
139 if( Pa_GetDeviceInfo(result)->maxOutputChannels == 0 )
140 return result;
141 }
142
143 return paNoDevice;
144 }
145
146 static PaDeviceIndex FindOutputOnlyDevice(void)
147 {
148 PaDeviceIndex result = Pa_GetDefaultOutputDevice();
149 if( result != paNoDevice && Pa_GetDeviceInfo(result)->maxInputChannels == 0 )
150 return result;
151
152 for( result = 0; result < Pa_GetDeviceCount(); ++result )
153 {
154 if( Pa_GetDeviceInfo(result)->maxInputChannels == 0 )
155 return result;
156 }
157
158 return paNoDevice;
159 }
160
161 /*-------------------------------------------------------------------------------------------------*/
162 static int TestBadOpens( void )
163 {
164 PaStream* stream = NULL;
165 PaError result;
166 PaQaData myData;
167 PaStreamParameters ipp, opp;
168
169 /* Setup data for synthesis thread. */
170 myData.framesLeft = (unsigned long) (SAMPLE_RATE * 100); /* 100 seconds */
171 myData.numChannels = 1;
172 myData.mode = MODE_OUTPUT;
173
174 /*----------------------------- No devices specified: */
175 ipp.device = opp.device = paNoDevice;
176 ipp.channelCount = opp.channelCount = 0; /* Also no channels. */
177 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
178 ipp.sampleFormat = opp.sampleFormat = paFloat32;
179 /* Take the low latency of the default device for all subsequent tests. */
180 ipp.suggestedLatency = Pa_GetDeviceInfo(Pa_GetDefaultInputDevice())->defaultLowInputLatency;
181 opp.suggestedLatency = Pa_GetDeviceInfo(Pa_GetDefaultOutputDevice())->defaultLowOutputLatency;
182 HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, &opp,
183 SAMPLE_RATE, FRAMES_PER_BUFFER,
184 paClipOff, QaCallback, &myData )) == paInvalidDevice));
185
186 /*----------------------------- No devices specified #2: */
187 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, NULL,
188 SAMPLE_RATE, FRAMES_PER_BUFFER,
189 paClipOff, QaCallback, &myData )) == paInvalidDevice));
190
191 /*----------------------------- Out of range input device specified: */
192 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
193 ipp.sampleFormat = opp.sampleFormat = paFloat32;
194 ipp.channelCount = 0; ipp.device = Pa_GetDeviceCount(); /* And no output device, and no channels. */
195 opp.channelCount = 0; opp.device = paNoDevice;
196 HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, NULL,
197 SAMPLE_RATE, FRAMES_PER_BUFFER,
198 paClipOff, QaCallback, &myData )) == paInvalidDevice));
199
200 /*----------------------------- Out of range output device specified: */
201 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
202 ipp.sampleFormat = opp.sampleFormat = paFloat32;
203 ipp.channelCount = 0; ipp.device = paNoDevice; /* And no input device, and no channels. */
204 opp.channelCount = 0; opp.device = Pa_GetDeviceCount();
205 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp,
206 SAMPLE_RATE, FRAMES_PER_BUFFER,
207 paClipOff, QaCallback, &myData )) == paInvalidDevice));
208
209 /*----------------------------- Zero input channels: */
210 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
211 ipp.sampleFormat = opp.sampleFormat = paFloat32;
212 ipp.channelCount = 0; ipp.device = Pa_GetDefaultInputDevice();
213 opp.channelCount = 0; opp.device = paNoDevice; /* And no output device, and no output channels. */
214 HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, NULL,
215 SAMPLE_RATE, FRAMES_PER_BUFFER,
216 paClipOff, QaCallback, &myData )) == paInvalidChannelCount));
217
218 /*----------------------------- Zero output channels: */
219 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
220 ipp.sampleFormat = opp.sampleFormat = paFloat32;
221 ipp.channelCount = 0; ipp.device = paNoDevice; /* And no input device, and no input channels. */
222 opp.channelCount = 0; opp.device = Pa_GetDefaultOutputDevice();
223 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp,
224 SAMPLE_RATE, FRAMES_PER_BUFFER,
225 paClipOff, QaCallback, &myData )) == paInvalidChannelCount));
226
227 /*----------------------------- Nonzero input and output channels but no output device: */
228 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
229 ipp.sampleFormat = opp.sampleFormat = paFloat32;
230 ipp.channelCount = 2; ipp.device = Pa_GetDefaultInputDevice(); /* Both stereo. */
231 opp.channelCount = 2; opp.device = paNoDevice;
232 HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, &opp,
233 SAMPLE_RATE, FRAMES_PER_BUFFER,
234 paClipOff, QaCallback, &myData )) == paInvalidDevice));
235
236 /*----------------------------- Nonzero input and output channels but no input device: */
237 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
238 ipp.sampleFormat = opp.sampleFormat = paFloat32;
239 ipp.channelCount = 2; ipp.device = paNoDevice;
240 opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice();
241 HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, &opp,
242 SAMPLE_RATE, FRAMES_PER_BUFFER,
243 paClipOff, QaCallback, &myData )) == paInvalidDevice));
244
245 /*----------------------------- NULL stream pointer: */
246 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
247 ipp.sampleFormat = opp.sampleFormat = paFloat32;
248 ipp.channelCount = 0; ipp.device = paNoDevice; /* Output is more likely than input. */
249 opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice(); /* Only 2 output channels. */
250 HOPEFOR(((result = Pa_OpenStream(NULL, &ipp, &opp,
251 SAMPLE_RATE, FRAMES_PER_BUFFER,
252 paClipOff, QaCallback, &myData )) == paBadStreamPtr));
253
254 /*----------------------------- Low sample rate: */
255 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
256 ipp.sampleFormat = opp.sampleFormat = paFloat32;
257 ipp.channelCount = 0; ipp.device = paNoDevice;
258 opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice();
259 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp,
260 1.0, FRAMES_PER_BUFFER, /* 1 cycle per second (1 Hz) is too low. */
261 paClipOff, QaCallback, &myData )) == paInvalidSampleRate));
262
263 /*----------------------------- High sample rate: */
264 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
265 ipp.sampleFormat = opp.sampleFormat = paFloat32;
266 ipp.channelCount = 0; ipp.device = paNoDevice;
267 opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice();
268 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp,
269 10000000.0, FRAMES_PER_BUFFER, /* 10^6 cycles per second (10 MHz) is too high. */
270 paClipOff, QaCallback, &myData )) == paInvalidSampleRate));
271
272 /*----------------------------- NULL callback: */
273 /* NULL callback is valid in V19 -- it means use blocking read/write stream
274
275 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
276 ipp.sampleFormat = opp.sampleFormat = paFloat32;
277 ipp.channelCount = 0; ipp.device = paNoDevice;
278 opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice();
279 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp,
280 SAMPLE_RATE, FRAMES_PER_BUFFER,
281 paClipOff,
282 NULL,
283 &myData )) == paNullCallback));
284 */
285
286 /*----------------------------- Bad flag: */
287 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
288 ipp.sampleFormat = opp.sampleFormat = paFloat32;
289 ipp.channelCount = 0; ipp.device = paNoDevice;
290 opp.channelCount = 2; opp.device = Pa_GetDefaultOutputDevice();
291 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp,
292 SAMPLE_RATE, FRAMES_PER_BUFFER,
293 255, /* Is 8 maybe legal V19 API? */
294 QaCallback, &myData )) == paInvalidFlag));
295
296 /*----------------------------- using input device as output device: */
297 if( FindInputOnlyDevice() != paNoDevice )
298 {
299 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
300 ipp.sampleFormat = opp.sampleFormat = paFloat32;
301 ipp.channelCount = 0; ipp.device = paNoDevice; /* And no input device, and no channels. */
302 opp.channelCount = 2; opp.device = FindInputOnlyDevice();
303 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, &opp,
304 SAMPLE_RATE, FRAMES_PER_BUFFER,
305 paClipOff, QaCallback, &myData )) == paInvalidChannelCount));
306 }
307
308 /*----------------------------- using output device as input device: */
309 if( FindOutputOnlyDevice() != paNoDevice )
310 {
311 ipp.hostApiSpecificStreamInfo = opp.hostApiSpecificStreamInfo = NULL;
312 ipp.sampleFormat = opp.sampleFormat = paFloat32;
313 ipp.channelCount = 2; ipp.device = FindOutputOnlyDevice();
314 opp.channelCount = 0; opp.device = paNoDevice; /* And no output device, and no channels. */
315 HOPEFOR(((result = Pa_OpenStream(&stream, &ipp, NULL,
316 SAMPLE_RATE, FRAMES_PER_BUFFER,
317 paClipOff, QaCallback, &myData )) == paInvalidChannelCount));
318
319 }
320
321 if( stream != NULL ) Pa_CloseStream( stream );
322 return result;
323 }
324
325 /*-----------------------------------------------------------------------------------------*/
326 static int TestBadActions( void )
327 {
328 PaStream* stream = NULL;
329 PaError result;
330 PaQaData myData;
331 PaStreamParameters opp;
332
333 /* Setup data for synthesis thread. */
334 myData.framesLeft = (unsigned long)(SAMPLE_RATE * 100); /* 100 seconds */
335 myData.numChannels = 1;
336 myData.mode = MODE_OUTPUT;
337
338 opp.device = Pa_GetDefaultOutputDevice(); /* Default output. */
339 opp.channelCount = 2; /* Stereo output. */
340 opp.hostApiSpecificStreamInfo = NULL;
341 opp.sampleFormat = paFloat32;
342 opp.suggestedLatency = Pa_GetDeviceInfo(opp.device)->defaultLowOutputLatency;
343
344 HOPEFOR(((result = Pa_OpenStream(&stream, NULL, /* Take NULL as input parame- */
345 &opp, /* ters, meaning try only output. */
346 SAMPLE_RATE, FRAMES_PER_BUFFER,
347 paClipOff, QaCallback, &myData )) == paNoError));
348
349 HOPEFOR(((result = Pa_StartStream(NULL)) == paBadStreamPtr));
350 HOPEFOR(((result = Pa_StopStream(NULL)) == paBadStreamPtr));
351 HOPEFOR(((result = Pa_IsStreamStopped(NULL)) == paBadStreamPtr));
352 HOPEFOR(((result = Pa_IsStreamActive(NULL)) == paBadStreamPtr));
353 HOPEFOR(((result = Pa_CloseStream(NULL)) == paBadStreamPtr));
354 HOPEFOR(((result = Pa_SetStreamFinishedCallback(NULL, NULL)) == paBadStreamPtr));
355 HOPEFOR(((result = !Pa_GetStreamInfo(NULL))));
356 HOPEFOR(((result = Pa_GetStreamTime(NULL)) == 0.0));
357 HOPEFOR(((result = Pa_GetStreamCpuLoad(NULL)) == 0.0));
358 HOPEFOR(((result = Pa_ReadStream(NULL, NULL, 0)) == paBadStreamPtr));
359 HOPEFOR(((result = Pa_WriteStream(NULL, NULL, 0)) == paBadStreamPtr));
360
361 /** @todo test Pa_GetStreamReadAvailable and Pa_GetStreamWriteAvailable */
362
363 if (stream != NULL) Pa_CloseStream(stream);
364 return result;
365 }
366
367 /*---------------------------------------------------------------------*/
368 int main(void);
369 int main(void)
370 {
371 PaError result;
372
373 EXPECT(((result = Pa_Initialize()) == paNoError));
374 TestBadOpens();
375 TestBadActions();
376 error:
377 Pa_Terminate();
378 printf("QA Report: %d passed, %d failed.\n", gNumPassed, gNumFailed);
379 return 0;
380 }