pa_mac_core_blocking: Fix timeout.

Change alignment of comment.
This commit is contained in:
Phil Burk 2016-09-21 15:46:30 -07:00
commit a38df79293
5 changed files with 28 additions and 17 deletions

View file

@ -1149,9 +1149,9 @@ PaError Pa_ReadStream( PaStream* stream,
will want to match this parameter to the framesPerBuffer parameter used will want to match this parameter to the framesPerBuffer parameter used
when opening the stream. when opening the stream.
@return On success PaNoError will be returned, @return On success PaNoError will be returned, or paOutputUnderflowed if
or paOutputUnderflowed if additional output data was inserted after the additional output data was inserted after the previous call and before this
previous call and before this call. call.
*/ */
PaError Pa_WriteStream( PaStream* stream, PaError Pa_WriteStream( PaStream* stream,
const void *buffer, const void *buffer,

View file

@ -2790,7 +2790,7 @@ static PaError StopStream( PaStream *s )
stream->state = STOPPING; stream->state = STOPPING;
VDBUG( ("Waiting for BLIO.\n") ); VDBUG( ("Waiting for BLIO.\n") );
paErr = waitUntilBlioWriteBufferIsEmpty( &stream->blio, stream->sampleRate ); paErr = waitUntilBlioWriteBufferIsEmpty( &stream->blio, stream->sampleRate, stream->outputFramesPerBuffer );
VDBUG( ( "waitUntilBlioWriteBufferIsEmpty returned %d\n", paErr ) ); VDBUG( ( "waitUntilBlioWriteBufferIsEmpty returned %d\n", paErr ) );
return FinishStoppingStream( stream ); return FinishStoppingStream( stream );

View file

@ -587,27 +587,36 @@ PaError WriteStream( PaStream* stream,
/* /*
* Wait until the data in the buffer has finished playing. * Wait until the data in the buffer has finished playing.
*/ */
PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate ) PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate,
size_t framesPerBuffer )
{ {
PaError result = paNoError; PaError result = paNoError;
if( blio->outputRingBuffer.buffer ) { if( blio->outputRingBuffer.buffer ) {
int timeout = 5; // don't wait forever ring_buffer_size_t framesLeft = PaUtil_GetRingBufferReadAvailable( &blio->outputRingBuffer );
ring_buffer_size_t framesInBuffer = PaUtil_GetRingBufferReadAvailable( &blio->outputRingBuffer );
while( framesInBuffer > 0 && timeout-- > 0 ) { /* Calculate when we should give up waiting. To be safe wait for two extra periods. */
long msecEstimated = 1 + (long)( 1000.0 * framesInBuffer / sampleRate); PaTime now = PaUtil_GetTime();
VDBUG(( "waitUntilBlioWriteBufferIsFlushed: framesInBuffer = %d, msecEstimated = %ld\n", framesInBuffer, msecEstimated )); PaTime startTime = now;
Pa_Sleep( msecEstimated ); PaTime timeoutTime = startTime + (framesLeft + (2 * framesPerBuffer)) / sampleRate;
framesInBuffer = PaUtil_GetRingBufferReadAvailable( &blio->outputRingBuffer );
long msecPerBuffer = 1 + (long)( 1000.0 * framesPerBuffer / sampleRate);
while( framesLeft > 0 && now < timeoutTime ) {
VDBUG(( "waitUntilBlioWriteBufferIsFlushed: framesLeft = %d, framesPerBuffer = %ld\n",
framesLeft, framesPerBuffer ));
Pa_Sleep( msecPerBuffer );
framesLeft = PaUtil_GetRingBufferReadAvailable( &blio->outputRingBuffer );
now = PaUtil_GetTime();
} }
if( framesInBuffer > 0 )
if( framesLeft > 0 )
{ {
VDBUG(( "waitUntilBlioWriteBufferIsFlushed: TIMED OUT - framesLeft = %d\n", framesLeft ));
result = paTimedOut; result = paTimedOut;
} }
} }
return result; return result;
} }
signed long GetStreamReadAvailable( PaStream* stream ) signed long GetStreamReadAvailable( PaStream* stream )
{ {
PaMacBlio *blio = & ((PaMacCoreStream*)stream) -> blio; PaMacBlio *blio = & ((PaMacCoreStream*)stream) -> blio;

View file

@ -128,6 +128,7 @@ int BlioCallback(
PaStreamCallbackFlags statusFlags, PaStreamCallbackFlags statusFlags,
void *userData ); void *userData );
PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate ); PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate,
size_t framesPerBuffer );
#endif /*PA_MAC_CORE_BLOCKING_H_*/ #endif /*PA_MAC_CORE_BLOCKING_H_*/

View file

@ -50,7 +50,7 @@
#include "portaudio.h" #include "portaudio.h"
#define SAMPLE_RATE (44100) #define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (4096) #define FRAMES_PER_BUFFER (2048)
static float s_buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */ static float s_buffer[FRAMES_PER_BUFFER][2]; /* stereo output buffer */
@ -75,10 +75,11 @@ void *stop_thread_proc(void *arg)
PaTime time; PaTime time;
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
/* ILLEGAL unsynchronised call to PA, see comment above */
time = Pa_GetStreamTime( stream ); time = Pa_GetStreamTime( stream );
printf("Stream time = %f\n", time); printf("Stream time = %f\n", time);
fflush(stdout); fflush(stdout);
Pa_Sleep(100); usleep(100 * 1000);
} }
printf("Call Pa_StopStream()\n"); printf("Call Pa_StopStream()\n");
fflush(stdout); fflush(stdout);