| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * paqa_devs.c |
|---|
| 4 | * Self Testing Quality Assurance app for PortAudio |
|---|
| 5 | * Try to open each device and run through all the |
|---|
| 6 | * possible configurations. |
|---|
| 7 | * |
|---|
| 8 | * Author: Phil Burk http://www.softsynth.com |
|---|
| 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 | #include <stdio.h> |
|---|
| 39 | #include <math.h> |
|---|
| 40 | #include "portaudio.h" |
|---|
| 41 | #include "pa_trace.h" |
|---|
| 42 | /****************************************** Definitions ***********/ |
|---|
| 43 | #define MODE_INPUT (0) |
|---|
| 44 | #define MODE_OUTPUT (1) |
|---|
| 45 | typedef struct PaQaData |
|---|
| 46 | { |
|---|
| 47 | unsigned long framesLeft; |
|---|
| 48 | int numChannels; |
|---|
| 49 | int bytesPerSample; |
|---|
| 50 | int mode; |
|---|
| 51 | short sawPhase; |
|---|
| 52 | PaSampleFormat format; |
|---|
| 53 | } |
|---|
| 54 | PaQaData; |
|---|
| 55 | /****************************************** Prototypes ***********/ |
|---|
| 56 | static void TestDevices( int mode ); |
|---|
| 57 | static void TestFormats( int mode, PaDeviceID deviceID, double sampleRate, |
|---|
| 58 | int numChannels ); |
|---|
| 59 | static int TestAdvance( int mode, PaDeviceID deviceID, double sampleRate, |
|---|
| 60 | int numChannels, PaSampleFormat format ); |
|---|
| 61 | static int QaCallback( void *inputBuffer, void *outputBuffer, |
|---|
| 62 | unsigned long framesPerBuffer, |
|---|
| 63 | PaTimestamp outTime, void *userData ); |
|---|
| 64 | /****************************************** Globals ***********/ |
|---|
| 65 | static int gNumPassed = 0; |
|---|
| 66 | static int gNumFailed = 0; |
|---|
| 67 | /****************************************** Macros ***********/ |
|---|
| 68 | /* Print ERROR if it fails. Tally success or failure. */ |
|---|
| 69 | /* Odd do-while wrapper seems to be needed for some compilers. */ |
|---|
| 70 | #define EXPECT(_exp) \ |
|---|
| 71 | do \ |
|---|
| 72 | { \ |
|---|
| 73 | if ((_exp)) {\ |
|---|
| 74 | /* printf("SUCCESS for %s\n", #_exp ); */ \ |
|---|
| 75 | gNumPassed++; \ |
|---|
| 76 | } \ |
|---|
| 77 | else { \ |
|---|
| 78 | printf("ERROR - 0x%x - %s for %s\n", result, \ |
|---|
| 79 | ((result == 0) ? "-" : Pa_GetErrorText(result)), \ |
|---|
| 80 | #_exp ); \ |
|---|
| 81 | gNumFailed++; \ |
|---|
| 82 | goto error; \ |
|---|
| 83 | } \ |
|---|
| 84 | } while(0) |
|---|
| 85 | /*******************************************************************/ |
|---|
| 86 | /* This routine will be called by the PortAudio engine when audio is needed. |
|---|
| 87 | ** It may be called at interrupt level on some machines so don't do anything |
|---|
| 88 | ** that could mess up the system like calling malloc() or free(). |
|---|
| 89 | */ |
|---|
| 90 | static int QaCallback( void *inputBuffer, void *outputBuffer, |
|---|
| 91 | unsigned long framesPerBuffer, |
|---|
| 92 | PaTimestamp outTime, void *userData ) |
|---|
| 93 | { |
|---|
| 94 | unsigned long i; |
|---|
| 95 | short phase; |
|---|
| 96 | PaQaData *data = (PaQaData *) userData; |
|---|
| 97 | (void) inputBuffer; |
|---|
| 98 | (void) outTime; |
|---|
| 99 | |
|---|
| 100 | /* Play simle sawtooth wave. */ |
|---|
| 101 | if( data->mode == MODE_OUTPUT ) |
|---|
| 102 | { |
|---|
| 103 | phase = data->sawPhase; |
|---|
| 104 | switch( data->format ) |
|---|
| 105 | { |
|---|
| 106 | case paFloat32: |
|---|
| 107 | { |
|---|
| 108 | float *out = (float *) outputBuffer; |
|---|
| 109 | for( i=0; i<framesPerBuffer; i++ ) |
|---|
| 110 | { |
|---|
| 111 | phase += 0x123; |
|---|
| 112 | *out++ = (float) (phase * (1.0 / 32768.0)); |
|---|
| 113 | if( data->numChannels == 2 ) |
|---|
| 114 | { |
|---|
| 115 | *out++ = (float) (phase * (1.0 / 32768.0)); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | break; |
|---|
| 120 | |
|---|
| 121 | case paInt32: |
|---|
| 122 | { |
|---|
| 123 | int *out = (int *) outputBuffer; |
|---|
| 124 | for( i=0; i<framesPerBuffer; i++ ) |
|---|
| 125 | { |
|---|
| 126 | phase += 0x123; |
|---|
| 127 | *out++ = ((int) phase ) << 16; |
|---|
| 128 | if( data->numChannels == 2 ) |
|---|
| 129 | { |
|---|
| 130 | *out++ = ((int) phase ) << 16; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | break; |
|---|
| 135 | case paInt16: |
|---|
| 136 | { |
|---|
| 137 | short *out = (short *) outputBuffer; |
|---|
| 138 | for( i=0; i<framesPerBuffer; i++ ) |
|---|
| 139 | { |
|---|
| 140 | phase += 0x123; |
|---|
| 141 | *out++ = phase; |
|---|
| 142 | if( data->numChannels == 2 ) |
|---|
| 143 | { |
|---|
| 144 | *out++ = phase; |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | break; |
|---|
| 149 | |
|---|
| 150 | default: |
|---|
| 151 | { |
|---|
| 152 | unsigned char *out = (unsigned char *) outputBuffer; |
|---|
| 153 | unsigned long numBytes = framesPerBuffer * data->numChannels * data->bytesPerSample; |
|---|
| 154 | for( i=0; i<numBytes; i++ ) |
|---|
| 155 | { |
|---|
| 156 | *out++ = 0; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | break; |
|---|
| 160 | } |
|---|
| 161 | data->sawPhase = phase; |
|---|
| 162 | } |
|---|
| 163 | /* Are we through yet? */ |
|---|
| 164 | if( data->framesLeft > framesPerBuffer ) |
|---|
| 165 | { |
|---|
| 166 | AddTraceMessage("QaCallback: running. framesLeft", data->framesLeft ); |
|---|
| 167 | data->framesLeft -= framesPerBuffer; |
|---|
| 168 | return 0; |
|---|
| 169 | } |
|---|
| 170 | else |
|---|
| 171 | { |
|---|
| 172 | AddTraceMessage("QaCallback: DONE! framesLeft", data->framesLeft ); |
|---|
| 173 | data->framesLeft = 0; |
|---|
| 174 | return 1; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | /*******************************************************************/ |
|---|
| 178 | int main(void); |
|---|
| 179 | int main(void) |
|---|
| 180 | { |
|---|
| 181 | PaError result; |
|---|
| 182 | EXPECT( ((result=Pa_Initialize()) == 0) ); |
|---|
| 183 | printf("Test OUTPUT ---------------\n"); |
|---|
| 184 | TestDevices( MODE_OUTPUT ); |
|---|
| 185 | printf("Test INPUT ---------------\n"); |
|---|
| 186 | TestDevices( MODE_INPUT ); |
|---|
| 187 | error: |
|---|
| 188 | Pa_Terminate(); |
|---|
| 189 | printf("QA Report: %d passed, %d failed.\n", gNumPassed, gNumFailed ); |
|---|
| 190 | } |
|---|
| 191 | /******************************************************************* |
|---|
| 192 | * Try each output device, through its full range of capabilities. */ |
|---|
| 193 | static void TestDevices( int mode ) |
|---|
| 194 | { |
|---|
| 195 | int id,jc,kr; |
|---|
| 196 | int maxChannels; |
|---|
| 197 | const PaDeviceInfo *pdi; |
|---|
| 198 | int numDevices = Pa_CountDevices(); |
|---|
| 199 | /* Iterate through all devices. */ |
|---|
| 200 | for( id=0; id<numDevices; id++ ) |
|---|
| 201 | { |
|---|
| 202 | pdi = Pa_GetDeviceInfo( id ); |
|---|
| 203 | /* Try 1 to maxChannels on each device. */ |
|---|
| 204 | maxChannels = ( mode == MODE_INPUT ) ? pdi->maxInputChannels : pdi->maxOutputChannels; |
|---|
| 205 | for( jc=1; jc<=maxChannels; jc++ ) |
|---|
| 206 | { |
|---|
| 207 | printf("Name = %s\n", pdi->name ); |
|---|
| 208 | /* Try each legal sample rate. */ |
|---|
| 209 | if( pdi->numSampleRates == -1 ) |
|---|
| 210 | { |
|---|
| 211 | double low, high; |
|---|
| 212 | low = pdi->sampleRates[0]; |
|---|
| 213 | high = pdi->sampleRates[1]; |
|---|
| 214 | if( low < 8000.0 ) low = 8000.0; |
|---|
| 215 | TestFormats( mode, id, low, jc ); |
|---|
| 216 | #define TESTSR(sr) {if(((sr)>=low) && ((sr)<=high)) TestFormats( mode, id, (sr), jc ); } |
|---|
| 217 | |
|---|
| 218 | TESTSR(11025.0); |
|---|
| 219 | TESTSR(22050.0); |
|---|
| 220 | TESTSR(44100.0); |
|---|
| 221 | TestFormats( mode, id, high, jc ); |
|---|
| 222 | } |
|---|
| 223 | else |
|---|
| 224 | { |
|---|
| 225 | for( kr=0; kr<pdi->numSampleRates; kr++ ) |
|---|
| 226 | { |
|---|
| 227 | TestFormats( mode, id, pdi->sampleRates[kr], jc ); |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | /*******************************************************************/ |
|---|
| 234 | static void TestFormats( int mode, PaDeviceID deviceID, double sampleRate, |
|---|
| 235 | int numChannels ) |
|---|
| 236 | { |
|---|
| 237 | TestAdvance( mode, deviceID, sampleRate, numChannels, paFloat32 ); /* */ |
|---|
| 238 | TestAdvance( mode, deviceID, sampleRate, numChannels, paInt16 ); /* */ |
|---|
| 239 | TestAdvance( mode, deviceID, sampleRate, numChannels, paInt32 ); /* */ |
|---|
| 240 | /* TestAdvance( mode, deviceID, sampleRate, numChannels, paInt24 ); */ |
|---|
| 241 | } |
|---|
| 242 | /*******************************************************************/ |
|---|
| 243 | static int TestAdvance( int mode, PaDeviceID deviceID, double sampleRate, |
|---|
| 244 | int numChannels, PaSampleFormat format ) |
|---|
| 245 | { |
|---|
| 246 | PortAudioStream *stream = NULL; |
|---|
| 247 | PaError result; |
|---|
| 248 | PaQaData myData; |
|---|
| 249 | #define FRAMES_PER_BUFFER (64) |
|---|
| 250 | printf("------ TestAdvance: %s, device = %d, rate = %g, numChannels = %d, format = %d -------\n", |
|---|
| 251 | ( mode == MODE_INPUT ) ? "INPUT" : "OUTPUT", |
|---|
| 252 | deviceID, sampleRate, numChannels, format); |
|---|
| 253 | /* Setup data for synthesis thread. */ |
|---|
| 254 | myData.framesLeft = (unsigned long) (sampleRate * 100); /* 100 seconds */ |
|---|
| 255 | myData.numChannels = numChannels; |
|---|
| 256 | myData.mode = mode; |
|---|
| 257 | myData.format = format; |
|---|
| 258 | switch( format ) |
|---|
| 259 | { |
|---|
| 260 | case paFloat32: |
|---|
| 261 | case paInt32: |
|---|
| 262 | case paInt24: |
|---|
| 263 | myData.bytesPerSample = 4; |
|---|
| 264 | break; |
|---|
| 265 | case paPackedInt24: |
|---|
| 266 | myData.bytesPerSample = 3; |
|---|
| 267 | break; |
|---|
| 268 | default: |
|---|
| 269 | myData.bytesPerSample = 2; |
|---|
| 270 | break; |
|---|
| 271 | } |
|---|
| 272 | EXPECT( ((result = Pa_OpenStream( |
|---|
| 273 | &stream, |
|---|
| 274 | ( mode == MODE_INPUT ) ? deviceID : paNoDevice, |
|---|
| 275 | ( mode == MODE_INPUT ) ? numChannels : 0, |
|---|
| 276 | format, |
|---|
| 277 | NULL, |
|---|
| 278 | ( mode == MODE_OUTPUT ) ? deviceID : paNoDevice, |
|---|
| 279 | ( mode == MODE_OUTPUT ) ? numChannels : 0, |
|---|
| 280 | format, |
|---|
| 281 | NULL, |
|---|
| 282 | sampleRate, |
|---|
| 283 | FRAMES_PER_BUFFER, /* frames per buffer */ |
|---|
| 284 | 0, /* number of buffers, if zero then use default minimum */ |
|---|
| 285 | paClipOff, /* we won't output out of range samples so don't bother clipping them */ |
|---|
| 286 | QaCallback, |
|---|
| 287 | &myData ) |
|---|
| 288 | ) == 0) ); |
|---|
| 289 | if( stream ) |
|---|
| 290 | { |
|---|
| 291 | PaTimestamp oldStamp, newStamp; |
|---|
| 292 | unsigned long oldFrames; |
|---|
| 293 | int minDelay = ( mode == MODE_INPUT ) ? 1000 : 400; |
|---|
| 294 | int minNumBuffers = Pa_GetMinNumBuffers( FRAMES_PER_BUFFER, sampleRate ); |
|---|
| 295 | int msec = (int) ((minNumBuffers * 3 * 1000.0 * FRAMES_PER_BUFFER) / sampleRate); |
|---|
| 296 | if( msec < minDelay ) msec = minDelay; |
|---|
| 297 | printf("msec = %d\n", msec); /**/ |
|---|
| 298 | EXPECT( ((result=Pa_StartStream( stream )) == 0) ); |
|---|
| 299 | /* Check to make sure PortAudio is advancing timeStamp. */ |
|---|
| 300 | result = paNoError; |
|---|
| 301 | oldStamp = Pa_StreamTime(stream); |
|---|
| 302 | Pa_Sleep(msec); |
|---|
| 303 | newStamp = Pa_StreamTime(stream); |
|---|
| 304 | printf("oldStamp = %g,newStamp = %g\n", oldStamp, newStamp ); /**/ |
|---|
| 305 | EXPECT( (oldStamp < newStamp) ); |
|---|
| 306 | /* Check to make sure callback is decrementing framesLeft. */ |
|---|
| 307 | oldFrames = myData.framesLeft; |
|---|
| 308 | Pa_Sleep(msec); |
|---|
| 309 | printf("oldFrames = %d, myData.framesLeft = %d\n", oldFrames, myData.framesLeft ); /**/ |
|---|
| 310 | EXPECT( (oldFrames > myData.framesLeft) ); |
|---|
| 311 | EXPECT( ((result=Pa_CloseStream( stream )) == 0) ); |
|---|
| 312 | stream = NULL; |
|---|
| 313 | } |
|---|
| 314 | error: |
|---|
| 315 | if( stream != NULL ) Pa_CloseStream( stream ); |
|---|
| 316 | return result; |
|---|
| 317 | } |
|---|