annotate src/portaudio_20161030_catalina_patch/test/patest_stop_playout.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents d43aab368df9
children
rev   line source
cannam@162 1 /** @file patest_stop_playout.c
cannam@162 2 @ingroup test_src
cannam@162 3 @brief Test whether all queued samples are played when Pa_StopStream()
cannam@162 4 is used with a callback or read/write stream, or when the callback
cannam@162 5 returns paComplete.
cannam@162 6 @author Ross Bencina <rossb@audiomulch.com>
cannam@162 7 */
cannam@162 8 /*
cannam@162 9 * $Id$
cannam@162 10 *
cannam@162 11 * This program uses the PortAudio Portable Audio Library.
cannam@162 12 * For more information see: http://www.portaudio.com/
cannam@162 13 * Copyright (c) 1999-2004 Ross Bencina and Phil Burk
cannam@162 14 *
cannam@162 15 * Permission is hereby granted, free of charge, to any person obtaining
cannam@162 16 * a copy of this software and associated documentation files
cannam@162 17 * (the "Software"), to deal in the Software without restriction,
cannam@162 18 * including without limitation the rights to use, copy, modify, merge,
cannam@162 19 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@162 20 * and to permit persons to whom the Software is furnished to do so,
cannam@162 21 * subject to the following conditions:
cannam@162 22 *
cannam@162 23 * The above copyright notice and this permission notice shall be
cannam@162 24 * included in all copies or substantial portions of the Software.
cannam@162 25 *
cannam@162 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@162 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@162 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@162 29 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@162 30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@162 31 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@162 32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@162 33 */
cannam@162 34
cannam@162 35 /*
cannam@162 36 * The text above constitutes the entire PortAudio license; however,
cannam@162 37 * the PortAudio community also makes the following non-binding requests:
cannam@162 38 *
cannam@162 39 * Any person wishing to distribute modifications to the Software is
cannam@162 40 * requested to send the modifications to the original developer so that
cannam@162 41 * they can be incorporated into the canonical version. It is also
cannam@162 42 * requested that these non-binding requests be included along with the
cannam@162 43 * license above.
cannam@162 44 */
cannam@162 45 #include <stdio.h>
cannam@162 46 #include <math.h>
cannam@162 47 #include "portaudio.h"
cannam@162 48
cannam@162 49 #define SAMPLE_RATE (44100)
cannam@162 50 #define FRAMES_PER_BUFFER (1024)
cannam@162 51
cannam@162 52 #define TONE_SECONDS (1) /* long tone */
cannam@162 53 #define TONE_FADE_SECONDS (.04) /* fades at start and end of long tone */
cannam@162 54 #define GAP_SECONDS (.25) /* gap between long tone and blip */
cannam@162 55 #define BLIP_SECONDS (.035) /* short blip */
cannam@162 56
cannam@162 57 #define NUM_REPEATS (3)
cannam@162 58
cannam@162 59 #ifndef M_PI
cannam@162 60 #define M_PI (3.14159265)
cannam@162 61 #endif
cannam@162 62
cannam@162 63 #define TABLE_SIZE (2048)
cannam@162 64 typedef struct
cannam@162 65 {
cannam@162 66 float sine[TABLE_SIZE+1];
cannam@162 67
cannam@162 68 int repeatCount;
cannam@162 69
cannam@162 70 double phase;
cannam@162 71 double lowIncrement, highIncrement;
cannam@162 72
cannam@162 73 int gap1Length, toneLength, toneFadesLength, gap2Length, blipLength;
cannam@162 74 int gap1Countdown, toneCountdown, gap2Countdown, blipCountdown;
cannam@162 75 }
cannam@162 76 TestData;
cannam@162 77
cannam@162 78
cannam@162 79 static void RetriggerTestSignalGenerator( TestData *data )
cannam@162 80 {
cannam@162 81 data->phase = 0.;
cannam@162 82 data->gap1Countdown = data->gap1Length;
cannam@162 83 data->toneCountdown = data->toneLength;
cannam@162 84 data->gap2Countdown = data->gap2Length;
cannam@162 85 data->blipCountdown = data->blipLength;
cannam@162 86 }
cannam@162 87
cannam@162 88
cannam@162 89 static void ResetTestSignalGenerator( TestData *data )
cannam@162 90 {
cannam@162 91 data->repeatCount = 0;
cannam@162 92 RetriggerTestSignalGenerator( data );
cannam@162 93 }
cannam@162 94
cannam@162 95
cannam@162 96 static void InitTestSignalGenerator( TestData *data )
cannam@162 97 {
cannam@162 98 int signalLengthModBufferLength, i;
cannam@162 99
cannam@162 100 /* initialise sinusoidal wavetable */
cannam@162 101 for( i=0; i<TABLE_SIZE; i++ )
cannam@162 102 {
cannam@162 103 data->sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
cannam@162 104 }
cannam@162 105 data->sine[TABLE_SIZE] = data->sine[0]; /* guard point for linear interpolation */
cannam@162 106
cannam@162 107
cannam@162 108
cannam@162 109 data->lowIncrement = (330. / SAMPLE_RATE) * TABLE_SIZE;
cannam@162 110 data->highIncrement = (1760. / SAMPLE_RATE) * TABLE_SIZE;
cannam@162 111
cannam@162 112 data->gap1Length = GAP_SECONDS * SAMPLE_RATE;
cannam@162 113 data->toneLength = TONE_SECONDS * SAMPLE_RATE;
cannam@162 114 data->toneFadesLength = TONE_FADE_SECONDS * SAMPLE_RATE;
cannam@162 115 data->gap2Length = GAP_SECONDS * SAMPLE_RATE;
cannam@162 116 data->blipLength = BLIP_SECONDS * SAMPLE_RATE;
cannam@162 117
cannam@162 118 /* adjust signal length to be a multiple of the buffer length */
cannam@162 119 signalLengthModBufferLength = (data->gap1Length + data->toneLength + data->gap2Length + data->blipLength) % FRAMES_PER_BUFFER;
cannam@162 120 if( signalLengthModBufferLength > 0 )
cannam@162 121 data->toneLength += signalLengthModBufferLength;
cannam@162 122
cannam@162 123 ResetTestSignalGenerator( data );
cannam@162 124 }
cannam@162 125
cannam@162 126
cannam@162 127 #define MIN( a, b ) (((a)<(b))?(a):(b))
cannam@162 128
cannam@162 129 static void GenerateTestSignal( TestData *data, float *stereo, int frameCount )
cannam@162 130 {
cannam@162 131 int framesGenerated = 0;
cannam@162 132 float output;
cannam@162 133 long index;
cannam@162 134 float fraction;
cannam@162 135 int count, i;
cannam@162 136
cannam@162 137 while( framesGenerated < frameCount && data->repeatCount < NUM_REPEATS )
cannam@162 138 {
cannam@162 139 if( framesGenerated < frameCount && data->gap1Countdown > 0 ){
cannam@162 140 count = MIN( frameCount - framesGenerated, data->gap1Countdown );
cannam@162 141 for( i=0; i < count; ++i )
cannam@162 142 {
cannam@162 143 *stereo++ = 0.f;
cannam@162 144 *stereo++ = 0.f;
cannam@162 145 }
cannam@162 146
cannam@162 147 data->gap1Countdown -= count;
cannam@162 148 framesGenerated += count;
cannam@162 149 }
cannam@162 150
cannam@162 151 if( framesGenerated < frameCount && data->toneCountdown > 0 ){
cannam@162 152 count = MIN( frameCount - framesGenerated, data->toneCountdown );
cannam@162 153 for( i=0; i < count; ++i )
cannam@162 154 {
cannam@162 155 /* tone with data->lowIncrement phase increment */
cannam@162 156 index = (long)data->phase;
cannam@162 157 fraction = data->phase - index;
cannam@162 158 output = data->sine[ index ] + (data->sine[ index + 1 ] - data->sine[ index ]) * fraction;
cannam@162 159
cannam@162 160 data->phase += data->lowIncrement;
cannam@162 161 while( data->phase >= TABLE_SIZE )
cannam@162 162 data->phase -= TABLE_SIZE;
cannam@162 163
cannam@162 164 /* apply fade to ends */
cannam@162 165
cannam@162 166 if( data->toneCountdown < data->toneFadesLength )
cannam@162 167 {
cannam@162 168 /* cosine-bell fade out at end */
cannam@162 169 output *= (-cos(((float)data->toneCountdown / (float)data->toneFadesLength) * M_PI) + 1.) * .5;
cannam@162 170 }
cannam@162 171 else if( data->toneCountdown > data->toneLength - data->toneFadesLength )
cannam@162 172 {
cannam@162 173 /* cosine-bell fade in at start */
cannam@162 174 output *= (cos(((float)(data->toneCountdown - (data->toneLength - data->toneFadesLength)) / (float)data->toneFadesLength) * M_PI) + 1.) * .5;
cannam@162 175 }
cannam@162 176
cannam@162 177 output *= .5; /* play tone half as loud as blip */
cannam@162 178
cannam@162 179 *stereo++ = output;
cannam@162 180 *stereo++ = output;
cannam@162 181
cannam@162 182 data->toneCountdown--;
cannam@162 183 }
cannam@162 184
cannam@162 185 framesGenerated += count;
cannam@162 186 }
cannam@162 187
cannam@162 188 if( framesGenerated < frameCount && data->gap2Countdown > 0 ){
cannam@162 189 count = MIN( frameCount - framesGenerated, data->gap2Countdown );
cannam@162 190 for( i=0; i < count; ++i )
cannam@162 191 {
cannam@162 192 *stereo++ = 0.f;
cannam@162 193 *stereo++ = 0.f;
cannam@162 194 }
cannam@162 195
cannam@162 196 data->gap2Countdown -= count;
cannam@162 197 framesGenerated += count;
cannam@162 198 }
cannam@162 199
cannam@162 200 if( framesGenerated < frameCount && data->blipCountdown > 0 ){
cannam@162 201 count = MIN( frameCount - framesGenerated, data->blipCountdown );
cannam@162 202 for( i=0; i < count; ++i )
cannam@162 203 {
cannam@162 204 /* tone with data->highIncrement phase increment */
cannam@162 205 index = (long)data->phase;
cannam@162 206 fraction = data->phase - index;
cannam@162 207 output = data->sine[ index ] + (data->sine[ index + 1 ] - data->sine[ index ]) * fraction;
cannam@162 208
cannam@162 209 data->phase += data->highIncrement;
cannam@162 210 while( data->phase >= TABLE_SIZE )
cannam@162 211 data->phase -= TABLE_SIZE;
cannam@162 212
cannam@162 213 /* cosine-bell envelope over whole blip */
cannam@162 214 output *= (-cos( ((float)data->blipCountdown / (float)data->blipLength) * 2. * M_PI) + 1.) * .5;
cannam@162 215
cannam@162 216 *stereo++ = output;
cannam@162 217 *stereo++ = output;
cannam@162 218
cannam@162 219 data->blipCountdown--;
cannam@162 220 }
cannam@162 221
cannam@162 222 framesGenerated += count;
cannam@162 223 }
cannam@162 224
cannam@162 225
cannam@162 226 if( data->blipCountdown == 0 )
cannam@162 227 {
cannam@162 228 RetriggerTestSignalGenerator( data );
cannam@162 229 data->repeatCount++;
cannam@162 230 }
cannam@162 231 }
cannam@162 232
cannam@162 233 if( framesGenerated < frameCount )
cannam@162 234 {
cannam@162 235 count = frameCount - framesGenerated;
cannam@162 236 for( i=0; i < count; ++i )
cannam@162 237 {
cannam@162 238 *stereo++ = 0.f;
cannam@162 239 *stereo++ = 0.f;
cannam@162 240 }
cannam@162 241 }
cannam@162 242 }
cannam@162 243
cannam@162 244
cannam@162 245 static int IsTestSignalFinished( TestData *data )
cannam@162 246 {
cannam@162 247 if( data->repeatCount >= NUM_REPEATS )
cannam@162 248 return 1;
cannam@162 249 else
cannam@162 250 return 0;
cannam@162 251 }
cannam@162 252
cannam@162 253
cannam@162 254 static int TestCallback1( const void *inputBuffer, void *outputBuffer,
cannam@162 255 unsigned long frameCount,
cannam@162 256 const PaStreamCallbackTimeInfo* timeInfo,
cannam@162 257 PaStreamCallbackFlags statusFlags,
cannam@162 258 void *userData )
cannam@162 259 {
cannam@162 260 (void) inputBuffer; /* Prevent unused variable warnings. */
cannam@162 261 (void) timeInfo;
cannam@162 262 (void) statusFlags;
cannam@162 263
cannam@162 264 GenerateTestSignal( (TestData*)userData, (float*)outputBuffer, frameCount );
cannam@162 265
cannam@162 266 if( IsTestSignalFinished( (TestData*)userData ) )
cannam@162 267 return paComplete;
cannam@162 268 else
cannam@162 269 return paContinue;
cannam@162 270 }
cannam@162 271
cannam@162 272
cannam@162 273 volatile int testCallback2Finished = 0;
cannam@162 274
cannam@162 275 static int TestCallback2( const void *inputBuffer, void *outputBuffer,
cannam@162 276 unsigned long frameCount,
cannam@162 277 const PaStreamCallbackTimeInfo* timeInfo,
cannam@162 278 PaStreamCallbackFlags statusFlags,
cannam@162 279 void *userData )
cannam@162 280 {
cannam@162 281 (void) inputBuffer; /* Prevent unused variable warnings. */
cannam@162 282 (void) timeInfo;
cannam@162 283 (void) statusFlags;
cannam@162 284
cannam@162 285 GenerateTestSignal( (TestData*)userData, (float*)outputBuffer, frameCount );
cannam@162 286
cannam@162 287 if( IsTestSignalFinished( (TestData*)userData ) )
cannam@162 288 testCallback2Finished = 1;
cannam@162 289
cannam@162 290 return paContinue;
cannam@162 291 }
cannam@162 292
cannam@162 293 /*******************************************************************/
cannam@162 294 int main(void);
cannam@162 295 int main(void)
cannam@162 296 {
cannam@162 297 PaStreamParameters outputParameters;
cannam@162 298 PaStream *stream;
cannam@162 299 PaError err;
cannam@162 300 TestData data;
cannam@162 301 float writeBuffer[ FRAMES_PER_BUFFER * 2 ];
cannam@162 302
cannam@162 303 printf("PortAudio Test: check that stopping stream plays out all queued samples. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
cannam@162 304
cannam@162 305 InitTestSignalGenerator( &data );
cannam@162 306
cannam@162 307 err = Pa_Initialize();
cannam@162 308 if( err != paNoError ) goto error;
cannam@162 309
cannam@162 310 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
cannam@162 311 outputParameters.channelCount = 2; /* stereo output */
cannam@162 312 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
cannam@162 313 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
cannam@162 314 outputParameters.hostApiSpecificStreamInfo = NULL;
cannam@162 315
cannam@162 316 /* test paComplete ---------------------------------------------------------- */
cannam@162 317
cannam@162 318 ResetTestSignalGenerator( &data );
cannam@162 319
cannam@162 320 err = Pa_OpenStream(
cannam@162 321 &stream,
cannam@162 322 NULL, /* no input */
cannam@162 323 &outputParameters,
cannam@162 324 SAMPLE_RATE,
cannam@162 325 FRAMES_PER_BUFFER,
cannam@162 326 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@162 327 TestCallback1,
cannam@162 328 &data );
cannam@162 329 if( err != paNoError ) goto error;
cannam@162 330
cannam@162 331 err = Pa_StartStream( stream );
cannam@162 332 if( err != paNoError ) goto error;
cannam@162 333
cannam@162 334 printf("\nPlaying 'tone-blip' %d times using callback, stops by returning paComplete from callback.\n", NUM_REPEATS );
cannam@162 335 printf("If final blip is not intact, callback+paComplete implementation may be faulty.\n\n" );
cannam@162 336
cannam@162 337 while( (err = Pa_IsStreamActive( stream )) == 1 )
cannam@162 338 Pa_Sleep( 2 );
cannam@162 339
cannam@162 340 if( err != 0 ) goto error;
cannam@162 341
cannam@162 342 err = Pa_StopStream( stream );
cannam@162 343 if( err != paNoError ) goto error;
cannam@162 344
cannam@162 345 err = Pa_CloseStream( stream );
cannam@162 346 if( err != paNoError ) goto error;
cannam@162 347
cannam@162 348 Pa_Sleep( 500 );
cannam@162 349
cannam@162 350
cannam@162 351 /* test paComplete ---------------------------------------------------------- */
cannam@162 352
cannam@162 353 ResetTestSignalGenerator( &data );
cannam@162 354
cannam@162 355 err = Pa_OpenStream(
cannam@162 356 &stream,
cannam@162 357 NULL, /* no input */
cannam@162 358 &outputParameters,
cannam@162 359 SAMPLE_RATE,
cannam@162 360 FRAMES_PER_BUFFER,
cannam@162 361 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@162 362 TestCallback1,
cannam@162 363 &data );
cannam@162 364 if( err != paNoError ) goto error;
cannam@162 365
cannam@162 366 err = Pa_StartStream( stream );
cannam@162 367 if( err != paNoError ) goto error;
cannam@162 368
cannam@162 369 printf("\nPlaying 'tone-blip' %d times using callback, stops by returning paComplete from callback.\n", NUM_REPEATS );
cannam@162 370 printf("If final blip is not intact or is followed by garbage, callback+paComplete implementation may be faulty.\n\n" );
cannam@162 371
cannam@162 372 while( (err = Pa_IsStreamActive( stream )) == 1 )
cannam@162 373 Pa_Sleep( 5 );
cannam@162 374
cannam@162 375 printf("Waiting 5 seconds after paComplete before stopping the stream. Tests that buffers are flushed correctly.\n");
cannam@162 376 Pa_Sleep( 5000 );
cannam@162 377
cannam@162 378 if( err != 0 ) goto error;
cannam@162 379
cannam@162 380 err = Pa_StopStream( stream );
cannam@162 381 if( err != paNoError ) goto error;
cannam@162 382
cannam@162 383 err = Pa_CloseStream( stream );
cannam@162 384 if( err != paNoError ) goto error;
cannam@162 385
cannam@162 386 Pa_Sleep( 500 );
cannam@162 387
cannam@162 388
cannam@162 389 /* test Pa_StopStream() with callback --------------------------------------- */
cannam@162 390
cannam@162 391 ResetTestSignalGenerator( &data );
cannam@162 392
cannam@162 393 testCallback2Finished = 0;
cannam@162 394
cannam@162 395 err = Pa_OpenStream(
cannam@162 396 &stream,
cannam@162 397 NULL, /* no input */
cannam@162 398 &outputParameters,
cannam@162 399 SAMPLE_RATE,
cannam@162 400 FRAMES_PER_BUFFER,
cannam@162 401 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@162 402 TestCallback2,
cannam@162 403 &data );
cannam@162 404 if( err != paNoError ) goto error;
cannam@162 405
cannam@162 406 err = Pa_StartStream( stream );
cannam@162 407 if( err != paNoError ) goto error;
cannam@162 408
cannam@162 409
cannam@162 410 printf("\nPlaying 'tone-blip' %d times using callback, stops by calling Pa_StopStream.\n", NUM_REPEATS );
cannam@162 411 printf("If final blip is not intact, callback+Pa_StopStream implementation may be faulty.\n\n" );
cannam@162 412
cannam@162 413 /* note that polling a volatile flag is not a good way to synchronise with
cannam@162 414 the callback, but it's the best we can do portably. */
cannam@162 415 while( !testCallback2Finished )
cannam@162 416 Pa_Sleep( 2 );
cannam@162 417
cannam@162 418 Pa_Sleep( 500 );
cannam@162 419
cannam@162 420 err = Pa_StopStream( stream );
cannam@162 421 if( err != paNoError ) goto error;
cannam@162 422
cannam@162 423
cannam@162 424 err = Pa_CloseStream( stream );
cannam@162 425 if( err != paNoError ) goto error;
cannam@162 426
cannam@162 427 Pa_Sleep( 500 );
cannam@162 428
cannam@162 429 /* test Pa_StopStream() with Pa_WriteStream --------------------------------- */
cannam@162 430
cannam@162 431 ResetTestSignalGenerator( &data );
cannam@162 432
cannam@162 433 err = Pa_OpenStream(
cannam@162 434 &stream,
cannam@162 435 NULL, /* no input */
cannam@162 436 &outputParameters,
cannam@162 437 SAMPLE_RATE,
cannam@162 438 FRAMES_PER_BUFFER,
cannam@162 439 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@162 440 NULL, /* no callback, use blocking API */
cannam@162 441 NULL ); /* no callback, so no callback userData */
cannam@162 442 if( err != paNoError ) goto error;
cannam@162 443
cannam@162 444 err = Pa_StartStream( stream );
cannam@162 445 if( err != paNoError ) goto error;
cannam@162 446
cannam@162 447
cannam@162 448 printf("\nPlaying 'tone-blip' %d times using Pa_WriteStream, stops by calling Pa_StopStream.\n", NUM_REPEATS );
cannam@162 449 printf("If final blip is not intact, Pa_WriteStream+Pa_StopStream implementation may be faulty.\n\n" );
cannam@162 450
cannam@162 451 do{
cannam@162 452 GenerateTestSignal( &data, writeBuffer, FRAMES_PER_BUFFER );
cannam@162 453 err = Pa_WriteStream( stream, writeBuffer, FRAMES_PER_BUFFER );
cannam@162 454 if( err != paNoError ) goto error;
cannam@162 455
cannam@162 456 }while( !IsTestSignalFinished( &data ) );
cannam@162 457
cannam@162 458 err = Pa_StopStream( stream );
cannam@162 459 if( err != paNoError ) goto error;
cannam@162 460
cannam@162 461
cannam@162 462 err = Pa_CloseStream( stream );
cannam@162 463 if( err != paNoError ) goto error;
cannam@162 464
cannam@162 465 /* -------------------------------------------------------------------------- */
cannam@162 466
cannam@162 467 Pa_Terminate();
cannam@162 468 printf("Test finished.\n");
cannam@162 469
cannam@162 470 return err;
cannam@162 471
cannam@162 472 error:
cannam@162 473 Pa_Terminate();
cannam@162 474 fprintf( stderr, "An error occured while using the portaudio stream\n" );
cannam@162 475 fprintf( stderr, "Error number: %d\n", err );
cannam@162 476 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
cannam@162 477 return err;
cannam@162 478 }