annotate src/portaudio/qa/paqa_devs.c @ 105:c83a7e2af39c

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