| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#include <stdio.h> |
| 44 |
#include <math.h> |
| 45 |
#include "portaudio.h" |
| 46 |
#include "pa_trace.h" |
| 47 |
|
| 48 |
|
| 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 |
|
| 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 |
|
| 76 |
static int gNumPassed = 0; |
| 77 |
static int gNumFailed = 0; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#define EXPECT(_exp) \ |
| 83 |
do \ |
| 84 |
{ \ |
| 85 |
if ((_exp)) {\ |
| 86 |
\ |
| 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 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 }; |
| 220 |
int numDevices = Pa_GetDeviceCount(); |
| 221 |
for( id=0; id<numDevices; id++ ) |
| 222 |
{ |
| 223 |
pdi = Pa_GetDeviceInfo( id ); |
| 224 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 260 |
myData.framesLeft = (unsigned long) (sampleRate * 100); |
| 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 |
|
| 272 |
|
| 273 |
|
| 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 ) |
| 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, |
| 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 |
|
| 321 |
|
| 322 |
|
| 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 |
|
| 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 |
|
| 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 |
} |