From ed0f7d81ab7e8971e1e5d48dc7d6a7eee5241400 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Fri, 4 Mar 2022 20:44:36 -0800 Subject: [PATCH] Various fixes for R8Brain resampler wrapper Fixes to the resampler wrapper, such that it will survive some close encounters with the edge of the buffer, if necessary. Also so it will obey the buffer size limit for the output buffer. Signed-off-by: Christopher Snowhill --- Audio/ThirdParty/r8bstate.h | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/Audio/ThirdParty/r8bstate.h b/Audio/ThirdParty/r8bstate.h index 913e614f6..47942e543 100644 --- a/Audio/ThirdParty/r8bstate.h +++ b/Audio/ThirdParty/r8bstate.h @@ -8,9 +8,12 @@ #ifndef r8bstate_h #define r8bstate_h -#include "CDSPResampler.h" +#include + #include "r8bbase.h" +#include "CDSPResampler.h" + struct r8bstate { int channelCount; int bufferCapacity; @@ -53,6 +56,7 @@ struct r8bstate { } remainder -= blockCount; output += channelCount * blockCount; + outProcessed += blockCount; outMax -= blockCount; ret += blockCount; if(!outMax) @@ -78,13 +82,15 @@ struct r8bstate { } } } + size_t outputActual = outputDone - remainder; input += channelCount * blockCount; - output += channelCount * outputDone; + output += channelCount * outputActual; inCount -= blockCount; if(inDone) *inDone += blockCount; inProcessed += blockCount; - outProcessed += outputDone; - ret += outputDone; + outProcessed += outputActual; + outMax -= outputActual; + ret += outputActual; if(remainder) break; } @@ -103,6 +109,7 @@ struct r8bstate { } remainder -= blockCount; output += channelCount * blockCount; + outProcessed += blockCount; outMax -= blockCount; ret += blockCount; if(!outMax) @@ -118,12 +125,23 @@ struct r8bstate { if(outputDone) { if(outputDone > (outputWanted - outProcessed)) outputDone = (int)(outputWanted - outProcessed); - vDSP_vdpsp(outputPointer, 1, output + i, channelCount, outputDone); + if(outputDone > outMax) { + vDSP_vdpsp(outputPointer, 1, output + i, channelCount, outMax); + remainder = outputDone - outMax; + OutBufs[i].alloc((int)remainder); + memcpy(&OutBufs[i][0], outputPointer + outMax, remainder); + } else { + vDSP_vdpsp(outputPointer, 1, output + i, channelCount, outputDone); + } } } - outProcessed += outputDone; - output += channelCount * outputDone; - ret += outputDone; + size_t outputActual = outputDone - remainder; + outProcessed += outputActual; + output += channelCount * outputActual; + outMax -= outputActual; + ret += outputActual; + if(remainder) + break; } return ret; }