From 8f1ef5eb6b285d981385368cc1b115cc096f1fb2 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Tue, 11 Feb 2025 18:11:26 -0800 Subject: [PATCH] Audio: Adjust node buffering behavior a bit Change one remaining semaphore wait to 500us, and change the buffering so that it can always overflow the requested duration by one chunk, so that at least one chunk will always fit in the buffer. This also allows the DSP nodes to flush at the end of the stream without losing their output. Signed-off-by: Christopher Snowhill --- Audio/Chain/Node.m | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Audio/Chain/Node.m b/Audio/Chain/Node.m index b0fd1b21f..919ec1b91 100644 --- a/Audio/Chain/Node.m +++ b/Audio/Chain/Node.m @@ -67,11 +67,10 @@ [chunk setLossless:nodeLossless]; [chunk assignSamples:ptr frameCount:amount / nodeFormat.mBytesPerPacket]; - const double chunkDuration = [chunk duration]; double durationList = [buffer listDuration]; double durationLeft = [buffer maxDuration] - durationList; - if(shouldContinue == YES && durationList > durationPrebuffer) { + if(shouldContinue == YES && durationList >= durationPrebuffer) { if(initialBufferFilled == NO) { initialBufferFilled = YES; if([controller respondsToSelector:@selector(initialBufferFilled:)]) @@ -79,10 +78,10 @@ } } - while(shouldContinue == YES && chunkDuration > durationLeft) { - if(durationLeft < chunkDuration || shouldReset) { + while(shouldContinue == YES && durationLeft <= 0.0) { + if(durationLeft <= 0.0 || shouldReset) { [accessLock unlock]; - [semaphore wait]; + [semaphore timedWait:500]; [accessLock lock]; } @@ -97,11 +96,10 @@ - (void)writeChunk:(AudioChunk *)chunk { [accessLock lock]; - const double chunkDuration = [chunk duration]; double durationList = [buffer listDuration]; double durationLeft = [buffer maxDuration] - durationList; - if(shouldContinue == YES && durationList > durationPrebuffer) { + if(shouldContinue == YES && durationList >= durationPrebuffer) { if(initialBufferFilled == NO) { initialBufferFilled = YES; if([controller respondsToSelector:@selector(initialBufferFilled:)]) @@ -109,13 +107,13 @@ } } - while(shouldContinue == YES && chunkDuration > durationLeft) { + while(shouldContinue == YES && durationLeft <= 0.0) { if(previousNode && [previousNode shouldContinue] == NO) { shouldContinue = NO; break; } - if(durationLeft < chunkDuration || shouldReset) { + if(durationLeft <= 0.0 || shouldReset) { [accessLock unlock]; [semaphore timedWait:500]; [accessLock lock];