Debian Codegraph @

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

raw symbols SCIP
1 /** @file paqa_devs.c
2 @brief Self Testing Quality Assurance app for PortAudio
3 Try to open each device and run through all the
4 possible configurations.
5
6 @author Phil Burk http://www.softsynth.com
7
8 Pieter adapted to V19 API. Test now relies heavily on
9 Pa_IsFormatSupported(). Uses same 'standard' sample rates
10 as in test pa_devs.c.
11 */
12 /*
13 * $Id: paqa_devs.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
14 *
15 * This program uses the PortAudio Portable Audio Library.
16 * For more information see: http://www.portaudio.com
17 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining
20 * a copy of this software and associated documentation files
21 * (the "Software"), to deal in the Software without restriction,
22 * including without limitation the rights to use, copy, modify, merge,
23 * publish, distribute, sublicense, and/or sell copies of the Software,
24 * and to permit persons to whom the Software is furnished to do so,
25 * subject to the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be
28 * included in all copies or substantial portions of the Software.
29 *
30 * Any person wishing to distribute modifications to the Software is
31 * requested to send the modifications to the original developer so that
32 * they can be incorporated into the canonical version.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
37 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
38 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
39 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
40 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 *
42 */
43 #include <stdio.h>
44 #include <math.h>
45 #include "portaudio.h"
46 #include "pa_trace.h"
47
48 /****************************************** Definitions ***********/
49 #define MODE_INPUT (0)
50 #define MODE_OUTPUT (1)
51
52 typedef struct PaQaData
53 {
54 unsigned long framesLeft;
55 int numChannels;
56 int bytesPerSample;
57 int mode;
58 short sawPhase;
59 PaSampleFormat format;
60 }
61 PaQaData;
62
63 /****************************************** Prototypes ***********/
64 static void TestDevices( int mode );
65 static void TestFormats( int mode, PaDeviceIndex deviceID, double sampleRate,
66 int numChannels );
67 static int TestAdvance( int mode, PaDeviceIndex deviceID, double sampleRate,
68 int numChannels, PaSampleFormat format );
69 static int QaCallback( const void *inputBuffer, void *outputBuffer,
70 unsigned long framesPerBuffer,
71 const PaStreamCallbackTimeInfo* timeInfo,
72 PaStreamCallbackFlags statusFlags,
73 void *userData );
74
75 /****************************************** Globals ***********/
76 static int gNumPassed = 0;
77 static int gNumFailed = 0;
78
79 /****************************************** Macros ***********/
80 /* Print ERROR if it fails. Tally success or failure. */
81 /* Odd do-while wrapper seems to be needed for some compilers. */
82 #define EXPECT(_exp) \
83 do \
84 { \
85 if ((_exp)) {\
86 /* printf("SUCCESS for %s\n", #_exp ); */ \
87 gNumPassed++; \
88 } \
89 else { \
90 printf("ERROR - 0x%x - %s for %s\n", result, \
91 ((result == 0) ? "-" : Pa_GetErrorText(result)), \
92 #_exp ); \
93 gNumFailed++; \
94 goto error; \
95 } \
96 } while(0)
97
98 /*******************************************************************/
99 /* This routine will be called by the PortAudio engine when audio is needed.
100 ** It may be called at interrupt level on some machines so don't do anything
101 ** that could mess up the system like calling malloc() or free().
102 */
103 static int QaCallback( const void *inputBuffer, void *outputBuffer,
104 unsigned long framesPerBuffer,
105 const PaStreamCallbackTimeInfo* timeInfo,
106 PaStreamCallbackFlags statusFlags,
107 void *userData )
108 {
109 unsigned long i;
110 short phase;
111 PaQaData *data = (PaQaData *) userData;
112 (void) inputBuffer;
113
114 /* Play simle sawtooth wave. */
115 if( data->mode == MODE_OUTPUT )
116 {
117 phase = data->sawPhase;
118 switch( data->format )
119 {
120 case paFloat32:
121 {
122 float *out = (float *) outputBuffer;
123 for( i=0; i<framesPerBuffer; i++ )
124 {
125 phase += 0x123;
126 *out++ = (float) (phase * (1.0 / 32768.0));
127 if( data->numChannels == 2 )
128 {
129 *out++ = (float) (phase * (1.0 / 32768.0));
130 }
131 }
132 }
133 break;
134
135 case paInt32:
136 {
137 int *out = (int *) outputBuffer;
138 for( i=0; i<framesPerBuffer; i++ )
139 {
140 phase += 0x123;
141 *out++ = ((int) phase ) << 16;
142 if( data->numChannels == 2 )
143 {
144 *out++ = ((int) phase ) << 16;
145 }
146 }
147 }
148 break;
149 case paInt16:
150 {
151 short *out = (short *) outputBuffer;
152 for( i=0; i<framesPerBuffer; i++ )
153 {
154 phase += 0x123;
155 *out++ = phase;
156 if( data->numChannels == 2 )
157 {
158 *out++ = phase;
159 }
160 }
161 }
162 break;
163
164 default:
165 {
166 unsigned char *out = (unsigned char *) outputBuffer;
167 unsigned long numBytes = framesPerBuffer * data->numChannels * data->bytesPerSample;
168 for( i=0; i<numBytes; i++ )
169 {
170 *out++ = 0;
171 }
172 }
173 break;
174 }
175 data->sawPhase = phase;
176 }
177 /* Are we through yet? */
178 if( data->framesLeft > framesPerBuffer )
179 {
180 PaUtil_AddTraceMessage("QaCallback: running. framesLeft", data->framesLeft );
181 data->framesLeft -= framesPerBuffer;
182 return 0;
183 }
184 else
185 {
186 PaUtil_AddTraceMessage("QaCallback: DONE! framesLeft", data->framesLeft );
187 data->framesLeft = 0;
188 return 1;
189 }
190 }
191
192 /*******************************************************************/
193 int main(void);
194 int main(void)
195 {
196 PaError result;
197 EXPECT( ((result=Pa_Initialize()) == 0) );
198 printf("Test OUTPUT ---------------\n");
199 TestDevices( MODE_OUTPUT );
200 printf("Test INPUT ---------------\n");
201 TestDevices( MODE_INPUT );
202 error:
203 Pa_Terminate();
204 printf("QA Report: %d passed, %d failed.\n", gNumPassed, gNumFailed );
205 return 0;
206 }
207
208 /*******************************************************************
209 * Try each output device, through its full range of capabilities. */
210 static void TestDevices( int mode )
211 {
212 int id, jc, i;
213 int maxChannels;
214 const PaDeviceInfo *pdi;
215 static double standardSampleRates[] = { 8000.0, 9600.0, 11025.0, 12000.0,
216 16000.0, 22050.0, 24000.0,
217 32000.0, 44100.0, 48000.0,
218 88200.0, 96000.0,
219 -1.0 }; /* Negative terminated list. */
220 int numDevices = Pa_GetDeviceCount();
221 for( id=0; id<numDevices; id++ ) /* Iterate through all devices. */
222 {
223 pdi = Pa_GetDeviceInfo( id );
224 /* Try 1 to maxChannels on each device. */
225 maxChannels = (( mode == MODE_INPUT ) ? pdi->maxInputChannels : pdi->maxOutputChannels);
226
227 for( jc=1; jc<=maxChannels; jc++ )
228 {
229 printf("Name = %s\n", pdi->name );
230 /* Try each standard sample rate. */
231 for( i=0; standardSampleRates[i] > 0; i++ )
232 {
233 TestFormats( mode, (PaDeviceIndex)id, standardSampleRates[i], jc );
234 }
235 }
236 }
237 }
238
239 /*******************************************************************/
240 static void TestFormats( int mode, PaDeviceIndex deviceID, double sampleRate,
241 int numChannels )
242 {
243 TestAdvance( mode, deviceID, sampleRate, numChannels, paFloat32 );
244 TestAdvance( mode, deviceID, sampleRate, numChannels, paInt16 );
245 TestAdvance( mode, deviceID, sampleRate, numChannels, paInt32 );
246 /* TestAdvance( mode, deviceID, sampleRate, numChannels, paInt24 ); */
247 }
248
249 /*******************************************************************/
250 static int TestAdvance( int mode, PaDeviceIndex deviceID, double sampleRate,
251 int numChannels, PaSampleFormat format )
252 {
253 PaStreamParameters inputParameters, outputParameters, *ipp, *opp;
254 PaStream *stream = NULL;
255 PaError result = paNoError;
256 PaQaData myData;
257 #define FRAMES_PER_BUFFER (64)
258
259 /* Setup data for synthesis thread. */
260 myData.framesLeft = (unsigned long) (sampleRate * 100); /* 100 seconds */
261 myData.numChannels = numChannels;
262 myData.mode = mode;
263 myData.format = format;
264 switch( format )
265 {
266 case paFloat32:
267 case paInt32:
268 case paInt24:
269 myData.bytesPerSample = 4;
270 break;
271 /* case paPackedInt24:
272 myData.bytesPerSample = 3;
273 break; */
274 default:
275 myData.bytesPerSample = 2;
276 break;
277 }
278
279 if( mode == MODE_INPUT )
280 {
281 inputParameters.device = deviceID;
282 inputParameters.channelCount = numChannels;
283 inputParameters.sampleFormat = format;
284 inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
285 inputParameters.hostApiSpecificStreamInfo = NULL;
286 ipp = &inputParameters;
287 }
288 else
289 ipp = NULL;
290 if( mode == MODE_OUTPUT ) /* Pa_GetDeviceInfo(paNoDevice) COREDUMPS!!! */
291 {
292 outputParameters.device = deviceID;
293 outputParameters.channelCount = numChannels;
294 outputParameters.sampleFormat = format;
295 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
296 outputParameters.hostApiSpecificStreamInfo = NULL;
297 opp = &outputParameters;
298 }
299 else
300 opp = NULL;
301
302 if(paFormatIsSupported == Pa_IsFormatSupported( ipp, opp, sampleRate ))
303 {
304 printf("------ TestAdvance: %s, device = %d, rate = %g, numChannels = %d, format = %lu -------\n",
305 ( mode == MODE_INPUT ) ? "INPUT" : "OUTPUT",
306 deviceID, sampleRate, numChannels, (unsigned long)format);
307 EXPECT( ((result = Pa_OpenStream( &stream,
308 ipp,
309 opp,
310 sampleRate,
311 FRAMES_PER_BUFFER,
312 paClipOff, /* we won't output out of range samples so don't bother clipping them */
313 QaCallback,
314 &myData ) ) == 0) );
315 if( stream )
316 {
317 PaTime oldStamp, newStamp;
318 unsigned long oldFrames;
319 int minDelay = ( mode == MODE_INPUT ) ? 1000 : 400;
320 /* Was:
321 int minNumBuffers = Pa_GetMinNumBuffers( FRAMES_PER_BUFFER, sampleRate );
322 int msec = (int) ((minNumBuffers * 3 * 1000.0 * FRAMES_PER_BUFFER) / sampleRate);
323 */
324 int msec = (int)( 3.0 *
325 (( mode == MODE_INPUT ) ? inputParameters.suggestedLatency : outputParameters.suggestedLatency ));
326 if( msec < minDelay ) msec = minDelay;
327 printf("msec = %d\n", msec); /**/
328 EXPECT( ((result=Pa_StartStream( stream )) == 0) );
329 /* Check to make sure PortAudio is advancing timeStamp. */
330 oldStamp = Pa_GetStreamTime(stream);
331 Pa_Sleep(msec);
332 newStamp = Pa_GetStreamTime(stream);
333 printf("oldStamp = %g,newStamp = %g\n", oldStamp, newStamp ); /**/
334 EXPECT( (oldStamp < newStamp) );
335 /* Check to make sure callback is decrementing framesLeft. */
336 oldFrames = myData.framesLeft;
337 Pa_Sleep(msec);
338 printf("oldFrames = %lu, myData.framesLeft = %lu\n", oldFrames, myData.framesLeft ); /**/
339 EXPECT( (oldFrames > myData.framesLeft) );
340 EXPECT( ((result=Pa_CloseStream( stream )) == 0) );
341 stream = NULL;
342 }
343 }
344 error:
345 if( stream != NULL ) Pa_CloseStream( stream );
346 return result;
347 }