From 5622e9289901857a07dd64e89eda647daadfbf45 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sat, 5 Mar 2022 20:40:18 -0800 Subject: [PATCH] Resampler: Fix gapless output The resampler wasn't being given enough room to flush its final output, so a function was added to determine the current output latency, and more sample data is requested, allowing the full output flush to occur. Signed-off-by: Christopher Snowhill --- Audio/Chain/ConverterNode.mm | 3 +++ Audio/ThirdParty/r8bstate.h | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/Audio/Chain/ConverterNode.mm b/Audio/Chain/ConverterNode.mm index 47decf594..708a16533 100644 --- a/Audio/Chain/ConverterNode.mm +++ b/Audio/Chain/ConverterNode.mm @@ -754,6 +754,9 @@ tryagain: size_t outputDone = 0; if(!skipResampler) { + // This is needed at least for the flush, or else there won't be room for it + ioNumberPackets += ((r8bstate *)_r8bstate)->latency(); + #ifdef _DEBUG [BadSampleCleaner cleanSamples:(float *)(((uint8_t *)inputBuffer) + inpOffset) amount:inputSamples * floatFormat.mChannelsPerFrame diff --git a/Audio/ThirdParty/r8bstate.h b/Audio/ThirdParty/r8bstate.h index ac854b7b4..2a3bba8d8 100644 --- a/Audio/ThirdParty/r8bstate.h +++ b/Audio/ThirdParty/r8bstate.h @@ -43,6 +43,10 @@ struct r8bstate { delete[] Resamps; } + double latency() { + return ((double)inProcessed * sampleRatio) - (double)outProcessed; + } + int resample(const float *input, size_t inCount, size_t *inDone, float *output, size_t outMax) { int ret = 0; int i;