annotate src/portaudio_20161030_catalina_patch/qa/paqa_devs.c @ 81:7029a4916348

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