Chris@4: /** @file patest_stop_playout.c Chris@4: @ingroup test_src Chris@4: @brief Test whether all queued samples are played when Pa_StopStream() Chris@4: is used with a callback or read/write stream, or when the callback Chris@4: returns paComplete. Chris@4: @author Ross Bencina Chris@4: */ Chris@4: /* Chris@4: * $Id: patest_stop_playout.c 1446 2010-01-24 12:27:31Z rossb $ Chris@4: * Chris@4: * This program uses the PortAudio Portable Audio Library. Chris@4: * For more information see: http://www.portaudio.com/ Chris@4: * Copyright (c) 1999-2004 Ross Bencina and Phil Burk Chris@4: * Chris@4: * Permission is hereby granted, free of charge, to any person obtaining Chris@4: * a copy of this software and associated documentation files Chris@4: * (the "Software"), to deal in the Software without restriction, Chris@4: * including without limitation the rights to use, copy, modify, merge, Chris@4: * publish, distribute, sublicense, and/or sell copies of the Software, Chris@4: * and to permit persons to whom the Software is furnished to do so, Chris@4: * subject to the following conditions: Chris@4: * Chris@4: * The above copyright notice and this permission notice shall be Chris@4: * included in all copies or substantial portions of the Software. Chris@4: * Chris@4: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@4: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@4: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@4: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR Chris@4: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@4: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@4: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@4: */ Chris@4: Chris@4: /* Chris@4: * The text above constitutes the entire PortAudio license; however, Chris@4: * the PortAudio community also makes the following non-binding requests: Chris@4: * Chris@4: * Any person wishing to distribute modifications to the Software is Chris@4: * requested to send the modifications to the original developer so that Chris@4: * they can be incorporated into the canonical version. It is also Chris@4: * requested that these non-binding requests be included along with the Chris@4: * license above. Chris@4: */ Chris@4: #include Chris@4: #include Chris@4: #include "portaudio.h" Chris@4: Chris@4: #define SAMPLE_RATE (44100) Chris@4: #define FRAMES_PER_BUFFER (1024) Chris@4: Chris@4: #define TONE_SECONDS (1) /* long tone */ Chris@4: #define TONE_FADE_SECONDS (.04) /* fades at start and end of long tone */ Chris@4: #define GAP_SECONDS (.25) /* gap between long tone and blip */ Chris@4: #define BLIP_SECONDS (.035) /* short blip */ Chris@4: Chris@4: #define NUM_REPEATS (3) Chris@4: Chris@4: #ifndef M_PI Chris@4: #define M_PI (3.14159265) Chris@4: #endif Chris@4: Chris@4: #define TABLE_SIZE (2048) Chris@4: typedef struct Chris@4: { Chris@4: float sine[TABLE_SIZE+1]; Chris@4: Chris@4: int repeatCount; Chris@4: Chris@4: double phase; Chris@4: double lowIncrement, highIncrement; Chris@4: Chris@4: int gap1Length, toneLength, toneFadesLength, gap2Length, blipLength; Chris@4: int gap1Countdown, toneCountdown, gap2Countdown, blipCountdown; Chris@4: } Chris@4: TestData; Chris@4: Chris@4: Chris@4: static void RetriggerTestSignalGenerator( TestData *data ) Chris@4: { Chris@4: data->phase = 0.; Chris@4: data->gap1Countdown = data->gap1Length; Chris@4: data->toneCountdown = data->toneLength; Chris@4: data->gap2Countdown = data->gap2Length; Chris@4: data->blipCountdown = data->blipLength; Chris@4: } Chris@4: Chris@4: Chris@4: static void ResetTestSignalGenerator( TestData *data ) Chris@4: { Chris@4: data->repeatCount = 0; Chris@4: RetriggerTestSignalGenerator( data ); Chris@4: } Chris@4: Chris@4: Chris@4: static void InitTestSignalGenerator( TestData *data ) Chris@4: { Chris@4: int signalLengthModBufferLength, i; Chris@4: Chris@4: /* initialise sinusoidal wavetable */ Chris@4: for( i=0; isine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); Chris@4: } Chris@4: data->sine[TABLE_SIZE] = data->sine[0]; /* guard point for linear interpolation */ Chris@4: Chris@4: Chris@4: Chris@4: data->lowIncrement = (330. / SAMPLE_RATE) * TABLE_SIZE; Chris@4: data->highIncrement = (1760. / SAMPLE_RATE) * TABLE_SIZE; Chris@4: Chris@4: data->gap1Length = GAP_SECONDS * SAMPLE_RATE; Chris@4: data->toneLength = TONE_SECONDS * SAMPLE_RATE; Chris@4: data->toneFadesLength = TONE_FADE_SECONDS * SAMPLE_RATE; Chris@4: data->gap2Length = GAP_SECONDS * SAMPLE_RATE; Chris@4: data->blipLength = BLIP_SECONDS * SAMPLE_RATE; Chris@4: Chris@4: /* adjust signal length to be a multiple of the buffer length */ Chris@4: signalLengthModBufferLength = (data->gap1Length + data->toneLength + data->gap2Length + data->blipLength) % FRAMES_PER_BUFFER; Chris@4: if( signalLengthModBufferLength > 0 ) Chris@4: data->toneLength += signalLengthModBufferLength; Chris@4: Chris@4: ResetTestSignalGenerator( data ); Chris@4: } Chris@4: Chris@4: Chris@4: #define MIN( a, b ) (((a)<(b))?(a):(b)) Chris@4: Chris@4: static void GenerateTestSignal( TestData *data, float *stereo, int frameCount ) Chris@4: { Chris@4: int framesGenerated = 0; Chris@4: float output; Chris@4: long index; Chris@4: float fraction; Chris@4: int count, i; Chris@4: Chris@4: while( framesGenerated < frameCount && data->repeatCount < NUM_REPEATS ) Chris@4: { Chris@4: if( framesGenerated < frameCount && data->gap1Countdown > 0 ){ Chris@4: count = MIN( frameCount - framesGenerated, data->gap1Countdown ); Chris@4: for( i=0; i < count; ++i ) Chris@4: { Chris@4: *stereo++ = 0.f; Chris@4: *stereo++ = 0.f; Chris@4: } Chris@4: Chris@4: data->gap1Countdown -= count; Chris@4: framesGenerated += count; Chris@4: } Chris@4: Chris@4: if( framesGenerated < frameCount && data->toneCountdown > 0 ){ Chris@4: count = MIN( frameCount - framesGenerated, data->toneCountdown ); Chris@4: for( i=0; i < count; ++i ) Chris@4: { Chris@4: /* tone with data->lowIncrement phase increment */ Chris@4: index = (long)data->phase; Chris@4: fraction = data->phase - index; Chris@4: output = data->sine[ index ] + (data->sine[ index + 1 ] - data->sine[ index ]) * fraction; Chris@4: Chris@4: data->phase += data->lowIncrement; Chris@4: while( data->phase >= TABLE_SIZE ) Chris@4: data->phase -= TABLE_SIZE; Chris@4: Chris@4: /* apply fade to ends */ Chris@4: Chris@4: if( data->toneCountdown < data->toneFadesLength ) Chris@4: { Chris@4: /* cosine-bell fade out at end */ Chris@4: output *= (-cos(((float)data->toneCountdown / (float)data->toneFadesLength) * M_PI) + 1.) * .5; Chris@4: } Chris@4: else if( data->toneCountdown > data->toneLength - data->toneFadesLength ) Chris@4: { Chris@4: /* cosine-bell fade in at start */ Chris@4: output *= (cos(((float)(data->toneCountdown - (data->toneLength - data->toneFadesLength)) / (float)data->toneFadesLength) * M_PI) + 1.) * .5; Chris@4: } Chris@4: Chris@4: output *= .5; /* play tone half as loud as blip */ Chris@4: Chris@4: *stereo++ = output; Chris@4: *stereo++ = output; Chris@4: Chris@4: data->toneCountdown--; Chris@4: } Chris@4: Chris@4: framesGenerated += count; Chris@4: } Chris@4: Chris@4: if( framesGenerated < frameCount && data->gap2Countdown > 0 ){ Chris@4: count = MIN( frameCount - framesGenerated, data->gap2Countdown ); Chris@4: for( i=0; i < count; ++i ) Chris@4: { Chris@4: *stereo++ = 0.f; Chris@4: *stereo++ = 0.f; Chris@4: } Chris@4: Chris@4: data->gap2Countdown -= count; Chris@4: framesGenerated += count; Chris@4: } Chris@4: Chris@4: if( framesGenerated < frameCount && data->blipCountdown > 0 ){ Chris@4: count = MIN( frameCount - framesGenerated, data->blipCountdown ); Chris@4: for( i=0; i < count; ++i ) Chris@4: { Chris@4: /* tone with data->highIncrement phase increment */ Chris@4: index = (long)data->phase; Chris@4: fraction = data->phase - index; Chris@4: output = data->sine[ index ] + (data->sine[ index + 1 ] - data->sine[ index ]) * fraction; Chris@4: Chris@4: data->phase += data->highIncrement; Chris@4: while( data->phase >= TABLE_SIZE ) Chris@4: data->phase -= TABLE_SIZE; Chris@4: Chris@4: /* cosine-bell envelope over whole blip */ Chris@4: output *= (-cos( ((float)data->blipCountdown / (float)data->blipLength) * 2. * M_PI) + 1.) * .5; Chris@4: Chris@4: *stereo++ = output; Chris@4: *stereo++ = output; Chris@4: Chris@4: data->blipCountdown--; Chris@4: } Chris@4: Chris@4: framesGenerated += count; Chris@4: } Chris@4: Chris@4: Chris@4: if( data->blipCountdown == 0 ) Chris@4: { Chris@4: RetriggerTestSignalGenerator( data ); Chris@4: data->repeatCount++; Chris@4: } Chris@4: } Chris@4: Chris@4: if( framesGenerated < frameCount ) Chris@4: { Chris@4: count = frameCount - framesGenerated; Chris@4: for( i=0; i < count; ++i ) Chris@4: { Chris@4: *stereo++ = 0.f; Chris@4: *stereo++ = 0.f; Chris@4: } Chris@4: } Chris@4: } Chris@4: Chris@4: Chris@4: static int IsTestSignalFinished( TestData *data ) Chris@4: { Chris@4: if( data->repeatCount >= NUM_REPEATS ) Chris@4: return 1; Chris@4: else Chris@4: return 0; Chris@4: } Chris@4: Chris@4: Chris@4: static int TestCallback1( const void *inputBuffer, void *outputBuffer, Chris@4: unsigned long frameCount, Chris@4: const PaStreamCallbackTimeInfo* timeInfo, Chris@4: PaStreamCallbackFlags statusFlags, Chris@4: void *userData ) Chris@4: { Chris@4: (void) inputBuffer; /* Prevent unused variable warnings. */ Chris@4: (void) timeInfo; Chris@4: (void) statusFlags; Chris@4: Chris@4: GenerateTestSignal( (TestData*)userData, (float*)outputBuffer, frameCount ); Chris@4: Chris@4: if( IsTestSignalFinished( (TestData*)userData ) ) Chris@4: return paComplete; Chris@4: else Chris@4: return paContinue; Chris@4: } Chris@4: Chris@4: Chris@4: volatile int testCallback2Finished = 0; Chris@4: Chris@4: static int TestCallback2( const void *inputBuffer, void *outputBuffer, Chris@4: unsigned long frameCount, Chris@4: const PaStreamCallbackTimeInfo* timeInfo, Chris@4: PaStreamCallbackFlags statusFlags, Chris@4: void *userData ) Chris@4: { Chris@4: (void) inputBuffer; /* Prevent unused variable warnings. */ Chris@4: (void) timeInfo; Chris@4: (void) statusFlags; Chris@4: Chris@4: GenerateTestSignal( (TestData*)userData, (float*)outputBuffer, frameCount ); Chris@4: Chris@4: if( IsTestSignalFinished( (TestData*)userData ) ) Chris@4: testCallback2Finished = 1; Chris@4: Chris@4: return paContinue; Chris@4: } Chris@4: Chris@4: /*******************************************************************/ Chris@4: int main(void); Chris@4: int main(void) Chris@4: { Chris@4: PaStreamParameters outputParameters; Chris@4: PaStream *stream; Chris@4: PaError err; Chris@4: TestData data; Chris@4: float writeBuffer[ FRAMES_PER_BUFFER * 2 ]; Chris@4: Chris@4: printf("PortAudio Test: check that stopping stream plays out all queued samples. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); Chris@4: Chris@4: InitTestSignalGenerator( &data ); Chris@4: Chris@4: err = Pa_Initialize(); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ Chris@4: outputParameters.channelCount = 2; /* stereo output */ Chris@4: outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ Chris@4: outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; Chris@4: outputParameters.hostApiSpecificStreamInfo = NULL; Chris@4: Chris@4: /* test paComplete ---------------------------------------------------------- */ Chris@4: Chris@4: ResetTestSignalGenerator( &data ); Chris@4: Chris@4: err = Pa_OpenStream( Chris@4: &stream, Chris@4: NULL, /* no input */ Chris@4: &outputParameters, Chris@4: SAMPLE_RATE, Chris@4: FRAMES_PER_BUFFER, Chris@4: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@4: TestCallback1, Chris@4: &data ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: err = Pa_StartStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: printf("\nPlaying 'tone-blip' %d times using callback, stops by returning paComplete from callback.\n", NUM_REPEATS ); Chris@4: printf("If final blip is not intact, callback+paComplete implementation may be faulty.\n\n" ); Chris@4: Chris@4: while( (err = Pa_IsStreamActive( stream )) == 1 ) Chris@4: Pa_Sleep( 2 ); Chris@4: Chris@4: if( err != 0 ) goto error; Chris@4: Chris@4: err = Pa_StopStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: err = Pa_CloseStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: Pa_Sleep( 500 ); Chris@4: Chris@4: Chris@4: /* test paComplete ---------------------------------------------------------- */ Chris@4: Chris@4: ResetTestSignalGenerator( &data ); Chris@4: Chris@4: err = Pa_OpenStream( Chris@4: &stream, Chris@4: NULL, /* no input */ Chris@4: &outputParameters, Chris@4: SAMPLE_RATE, Chris@4: FRAMES_PER_BUFFER, Chris@4: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@4: TestCallback1, Chris@4: &data ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: err = Pa_StartStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: printf("\nPlaying 'tone-blip' %d times using callback, stops by returning paComplete from callback.\n", NUM_REPEATS ); Chris@4: printf("If final blip is not intact or is followed by garbage, callback+paComplete implementation may be faulty.\n\n" ); Chris@4: Chris@4: while( (err = Pa_IsStreamActive( stream )) == 1 ) Chris@4: Pa_Sleep( 5 ); Chris@4: Chris@4: printf("Waiting 5 seconds after paComplete before stopping the stream. Tests that buffers are flushed correctly.\n"); Chris@4: Pa_Sleep( 5000 ); Chris@4: Chris@4: if( err != 0 ) goto error; Chris@4: Chris@4: err = Pa_StopStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: err = Pa_CloseStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: Pa_Sleep( 500 ); Chris@4: Chris@4: Chris@4: /* test Pa_StopStream() with callback --------------------------------------- */ Chris@4: Chris@4: ResetTestSignalGenerator( &data ); Chris@4: Chris@4: testCallback2Finished = 0; Chris@4: Chris@4: err = Pa_OpenStream( Chris@4: &stream, Chris@4: NULL, /* no input */ Chris@4: &outputParameters, Chris@4: SAMPLE_RATE, Chris@4: FRAMES_PER_BUFFER, Chris@4: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@4: TestCallback2, Chris@4: &data ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: err = Pa_StartStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: Chris@4: printf("\nPlaying 'tone-blip' %d times using callback, stops by calling Pa_StopStream.\n", NUM_REPEATS ); Chris@4: printf("If final blip is not intact, callback+Pa_StopStream implementation may be faulty.\n\n" ); Chris@4: Chris@4: /* note that polling a volatile flag is not a good way to synchronise with Chris@4: the callback, but it's the best we can do portably. */ Chris@4: while( !testCallback2Finished ) Chris@4: Pa_Sleep( 2 ); Chris@4: Chris@4: Pa_Sleep( 500 ); Chris@4: Chris@4: err = Pa_StopStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: Chris@4: err = Pa_CloseStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: Pa_Sleep( 500 ); Chris@4: Chris@4: /* test Pa_StopStream() with Pa_WriteStream --------------------------------- */ Chris@4: Chris@4: ResetTestSignalGenerator( &data ); Chris@4: Chris@4: err = Pa_OpenStream( Chris@4: &stream, Chris@4: NULL, /* no input */ Chris@4: &outputParameters, Chris@4: SAMPLE_RATE, Chris@4: FRAMES_PER_BUFFER, Chris@4: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@4: NULL, /* no callback, use blocking API */ Chris@4: NULL ); /* no callback, so no callback userData */ Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: err = Pa_StartStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: Chris@4: printf("\nPlaying 'tone-blip' %d times using Pa_WriteStream, stops by calling Pa_StopStream.\n", NUM_REPEATS ); Chris@4: printf("If final blip is not intact, Pa_WriteStream+Pa_StopStream implementation may be faulty.\n\n" ); Chris@4: Chris@4: do{ Chris@4: GenerateTestSignal( &data, writeBuffer, FRAMES_PER_BUFFER ); Chris@4: err = Pa_WriteStream( stream, writeBuffer, FRAMES_PER_BUFFER ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: }while( !IsTestSignalFinished( &data ) ); Chris@4: Chris@4: err = Pa_StopStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: Chris@4: err = Pa_CloseStream( stream ); Chris@4: if( err != paNoError ) goto error; Chris@4: Chris@4: /* -------------------------------------------------------------------------- */ Chris@4: Chris@4: Pa_Terminate(); Chris@4: printf("Test finished.\n"); Chris@4: Chris@4: return err; Chris@4: Chris@4: error: Chris@4: Pa_Terminate(); Chris@4: fprintf( stderr, "An error occured while using the portaudio stream\n" ); Chris@4: fprintf( stderr, "Error number: %d\n", err ); Chris@4: fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); Chris@4: return err; Chris@4: }