From c6b112f512d93b12104e9fb9491692b6c4542dfd Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sat, 25 Dec 2021 19:43:10 -0800 Subject: [PATCH] CoreAudio output: Fix buffer underruns on output that could result in buffer looping in CoreAudio, by feeding silence from the callback instead of returning incomplete or possibly empty buffers --- Audio/Output/OutputCoreAudio.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Audio/Output/OutputCoreAudio.m b/Audio/Output/OutputCoreAudio.m index 28791a8eb..6a1526d0b 100644 --- a/Audio/Output/OutputCoreAudio.m +++ b/Audio/Output/OutputCoreAudio.m @@ -59,8 +59,17 @@ static OSStatus Sound_Renderer(void *inRefCon, AudioUnitRenderActionFlags *ioAc amountRead2 = [output->outputController readData:(readPointer+amountRead) amount:amountToRead-amountRead]; amountRead += amountRead2; } + + if (amountRead < amountToRead) + { + // Either underrun, or no data at all. Caller output tends to just + // buffer loop if it doesn't get anything, so always produce a full + // buffer, and silence anything we couldn't supply. + memset(readPointer + amountRead, 0, amountToRead - amountRead); + amountRead = amountToRead; + } - ioData->mBuffers[0].mDataByteSize = amountRead; + //ioData->mBuffers[0].mDataByteSize = amountRead; ioData->mBuffers[0].mNumberChannels = output->deviceFormat.mChannelsPerFrame; ioData->mNumberBuffers = 1;