annotate src/portaudio/test/patest_stop_playout.c @ 109:9d5448da9449

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