annotate src/portaudio_20140130/qa/paqa_devs.c @ 168:ceec0dd9ec9c

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