annotate src/portaudio_20140130/test/patest_stop_playout.c @ 160:cff480c41f97

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