1
/** @file patest_stop_playout.c
3
@brief Test whether all queued samples are played when Pa_StopStream()
4
is used with a callback or read/write stream, or when the callback
6
@author Ross Bencina <rossb@audiomulch.com>
9
* $Id: patest_stop_playout.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
11
* This program uses the PortAudio Portable Audio Library.
12
* For more information see: http://www.portaudio.com/
13
* Copyright (c) 1999-2004 Ross Bencina and Phil Burk
15
* Permission is hereby granted, free of charge, to any person obtaining
16
* a copy of this software and associated documentation files
17
* (the "Software"), to deal in the Software without restriction,
18
* including without limitation the rights to use, copy, modify, merge,
19
* publish, distribute, sublicense, and/or sell copies of the Software,
20
* and to permit persons to whom the Software is furnished to do so,
21
* subject to the following conditions:
23
* The above copyright notice and this permission notice shall be
24
* included in all copies or substantial portions of the Software.
26
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
30
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
31
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36
* The text above constitutes the entire PortAudio license; however,
37
* the PortAudio community also makes the following non-binding requests:
39
* Any person wishing to distribute modifications to the Software is
40
* requested to send the modifications to the original developer so that
41
* they can be incorporated into the canonical version. It is also
42
* requested that these non-binding requests be included along with the
47
#include "portaudio.h"
49
#define SAMPLE_RATE (44100)
50
#define FRAMES_PER_BUFFER (1024)
52
#define TONE_SECONDS (1) /* long tone */
53
#define TONE_FADE_SECONDS (.04) /* fades at start and end of long tone */
54
#define GAP_SECONDS (.25) /* gap between long tone and blip */
55
#define BLIP_SECONDS (.035) /* short blip */
57
#define NUM_REPEATS (3)
60
#define M_PI (3.14159265)
63
#define TABLE_SIZE (2048)
66
float sine[TABLE_SIZE+1];
71
double lowIncrement, highIncrement;
73
int gap1Length, toneLength, toneFadesLength, gap2Length, blipLength;
74
int gap1Countdown, toneCountdown, gap2Countdown, blipCountdown;
79
static void RetriggerTestSignalGenerator( TestData *data )
82
data->gap1Countdown = data->gap1Length;
83
data->toneCountdown = data->toneLength;
84
data->gap2Countdown = data->gap2Length;
85
data->blipCountdown = data->blipLength;
89
static void ResetTestSignalGenerator( TestData *data )
91
data->repeatCount = 0;
92
RetriggerTestSignalGenerator( data );
96
static void InitTestSignalGenerator( TestData *data )
98
int signalLengthModBufferLength, i;
100
/* initialise sinusoidal wavetable */
101
for( i=0; i<TABLE_SIZE; i++ )
103
data->sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
105
data->sine[TABLE_SIZE] = data->sine[0]; /* guard point for linear interpolation */
109
data->lowIncrement = (330. / SAMPLE_RATE) * TABLE_SIZE;
110
data->highIncrement = (1760. / SAMPLE_RATE) * TABLE_SIZE;
112
data->gap1Length = GAP_SECONDS * SAMPLE_RATE;
113
data->toneLength = TONE_SECONDS * SAMPLE_RATE;
114
data->toneFadesLength = TONE_FADE_SECONDS * SAMPLE_RATE;
115
data->gap2Length = GAP_SECONDS * SAMPLE_RATE;
116
data->blipLength = BLIP_SECONDS * SAMPLE_RATE;
118
/* adjust signal length to be a multiple of the buffer length */
119
signalLengthModBufferLength = (data->gap1Length + data->toneLength + data->gap2Length + data->blipLength) % FRAMES_PER_BUFFER;
120
if( signalLengthModBufferLength > 0 )
121
data->toneLength += signalLengthModBufferLength;
123
ResetTestSignalGenerator( data );
127
#define MIN( a, b ) (((a)<(b))?(a):(b))
129
static void GenerateTestSignal( TestData *data, float *stereo, int frameCount )
131
int framesGenerated = 0;
137
while( framesGenerated < frameCount && data->repeatCount < NUM_REPEATS )
139
if( framesGenerated < frameCount && data->gap1Countdown > 0 ){
140
count = MIN( frameCount - framesGenerated, data->gap1Countdown );
141
for( i=0; i < count; ++i )
147
data->gap1Countdown -= count;
148
framesGenerated += count;
151
if( framesGenerated < frameCount && data->toneCountdown > 0 ){
152
count = MIN( frameCount - framesGenerated, data->toneCountdown );
153
for( i=0; i < count; ++i )
155
/* tone with data->lowIncrement phase increment */
156
index = (long)data->phase;
157
fraction = data->phase - index;
158
output = data->sine[ index ] + (data->sine[ index + 1 ] - data->sine[ index ]) * fraction;
160
data->phase += data->lowIncrement;
161
while( data->phase >= TABLE_SIZE )
162
data->phase -= TABLE_SIZE;
164
/* apply fade to ends */
166
if( data->toneCountdown < data->toneFadesLength )
168
/* cosine-bell fade out at end */
169
output *= (-cos(((float)data->toneCountdown / (float)data->toneFadesLength) * M_PI) + 1.) * .5;
171
else if( data->toneCountdown > data->toneLength - data->toneFadesLength )
173
/* cosine-bell fade in at start */
174
output *= (cos(((float)(data->toneCountdown - (data->toneLength - data->toneFadesLength)) / (float)data->toneFadesLength) * M_PI) + 1.) * .5;
177
output *= .5; /* play tone half as loud as blip */
182
data->toneCountdown--;
185
framesGenerated += count;
188
if( framesGenerated < frameCount && data->gap2Countdown > 0 ){
189
count = MIN( frameCount - framesGenerated, data->gap2Countdown );
190
for( i=0; i < count; ++i )
196
data->gap2Countdown -= count;
197
framesGenerated += count;
200
if( framesGenerated < frameCount && data->blipCountdown > 0 ){
201
count = MIN( frameCount - framesGenerated, data->blipCountdown );
202
for( i=0; i < count; ++i )
204
/* tone with data->highIncrement phase increment */
205
index = (long)data->phase;
206
fraction = data->phase - index;
207
output = data->sine[ index ] + (data->sine[ index + 1 ] - data->sine[ index ]) * fraction;
209
data->phase += data->highIncrement;
210
while( data->phase >= TABLE_SIZE )
211
data->phase -= TABLE_SIZE;
213
/* cosine-bell envelope over whole blip */
214
output *= (-cos( ((float)data->blipCountdown / (float)data->blipLength) * 2. * M_PI) + 1.) * .5;
219
data->blipCountdown--;
222
framesGenerated += count;
226
if( data->blipCountdown == 0 )
228
RetriggerTestSignalGenerator( data );
233
if( framesGenerated < frameCount )
235
count = frameCount - framesGenerated;
236
for( i=0; i < count; ++i )
245
static int IsTestSignalFinished( TestData *data )
247
if( data->repeatCount >= NUM_REPEATS )
254
static int TestCallback1( const void *inputBuffer, void *outputBuffer,
255
unsigned long frameCount,
256
const PaStreamCallbackTimeInfo* timeInfo,
257
PaStreamCallbackFlags statusFlags,
260
(void) inputBuffer; /* Prevent unused variable warnings. */
264
GenerateTestSignal( (TestData*)userData, (float*)outputBuffer, frameCount );
266
if( IsTestSignalFinished( (TestData*)userData ) )
273
volatile int testCallback2Finished = 0;
275
static int TestCallback2( const void *inputBuffer, void *outputBuffer,
276
unsigned long frameCount,
277
const PaStreamCallbackTimeInfo* timeInfo,
278
PaStreamCallbackFlags statusFlags,
281
(void) inputBuffer; /* Prevent unused variable warnings. */
285
GenerateTestSignal( (TestData*)userData, (float*)outputBuffer, frameCount );
287
if( IsTestSignalFinished( (TestData*)userData ) )
288
testCallback2Finished = 1;
293
/*******************************************************************/
297
PaStreamParameters outputParameters;
301
float writeBuffer[ FRAMES_PER_BUFFER * 2 ];
303
printf("PortAudio Test: check that stopping stream plays out all queued samples. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
305
InitTestSignalGenerator( &data );
307
err = Pa_Initialize();
308
if( err != paNoError ) goto error;
310
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
311
outputParameters.channelCount = 2; /* stereo output */
312
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
313
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
314
outputParameters.hostApiSpecificStreamInfo = NULL;
316
/* test paComplete ---------------------------------------------------------- */
318
ResetTestSignalGenerator( &data );
326
paClipOff, /* we won't output out of range samples so don't bother clipping them */
329
if( err != paNoError ) goto error;
331
err = Pa_StartStream( stream );
332
if( err != paNoError ) goto error;
335
printf("\nPlaying 'tone-blip' %d times using callback, stops by returning paComplete from callback.\n", NUM_REPEATS );
336
printf("If final blip is not intact, callback+paComplete implementation may be faulty.\n\n" );
338
while( (err = Pa_IsStreamActive( stream )) == 1 )
341
if( err != 0 ) goto error;
344
err = Pa_StopStream( stream );
345
if( err != paNoError ) goto error;
347
err = Pa_CloseStream( stream );
348
if( err != paNoError ) goto error;
352
/* test Pa_StopStream() with callback --------------------------------------- */
354
ResetTestSignalGenerator( &data );
356
testCallback2Finished = 0;
364
paClipOff, /* we won't output out of range samples so don't bother clipping them */
367
if( err != paNoError ) goto error;
369
err = Pa_StartStream( stream );
370
if( err != paNoError ) goto error;
373
printf("\nPlaying 'tone-blip' %d times using callback, stops by calling Pa_StopStream.\n", NUM_REPEATS );
374
printf("If final blip is not intact, callback+Pa_StopStream implementation may be faulty.\n\n" );
376
/* note that polling a volatile flag is not a good way to synchronise with
377
the callback, but it's the best we can do portably. */
378
while( !testCallback2Finished )
381
err = Pa_StopStream( stream );
382
if( err != paNoError ) goto error;
385
err = Pa_CloseStream( stream );
386
if( err != paNoError ) goto error;
390
/* test Pa_StopStream() with Pa_WriteStream --------------------------------- */
392
ResetTestSignalGenerator( &data );
400
paClipOff, /* we won't output out of range samples so don't bother clipping them */
401
NULL, /* no callback, use blocking API */
402
NULL ); /* no callback, so no callback userData */
403
if( err != paNoError ) goto error;
405
err = Pa_StartStream( stream );
406
if( err != paNoError ) goto error;
409
printf("\nPlaying 'tone-blip' %d times using Pa_WriteStream, stops by calling Pa_StopStream.\n", NUM_REPEATS );
410
printf("If final blip is not intact, Pa_WriteStream+Pa_StopStream implementation may be faulty.\n\n" );
413
GenerateTestSignal( &data, writeBuffer, FRAMES_PER_BUFFER );
414
err = Pa_WriteStream( stream, writeBuffer, FRAMES_PER_BUFFER );
415
if( err != paNoError ) goto error;
417
}while( !IsTestSignalFinished( &data ) );
419
err = Pa_StopStream( stream );
420
if( err != paNoError ) goto error;
423
err = Pa_CloseStream( stream );
424
if( err != paNoError ) goto error;
426
/* -------------------------------------------------------------------------- */
429
printf("Test finished.\n");
435
fprintf( stderr, "An error occured while using the portaudio stream\n" );
436
fprintf( stderr, "Error number: %d\n", err );
437
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );