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