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