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