From ac9e404b237b6fa6de574f2bcb3460f681f4251b Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sun, 10 Jul 2022 16:24:08 -0700 Subject: [PATCH] [Audio API] Repair the damage to the input chain The input chain could hang up indefinitely, and MAD decoder didn't indicate end of file properly. Signed-off-by: Christopher Snowhill --- Audio/Chain/ConverterNode.m | 15 ++++++++++----- Audio/Chain/InputNode.m | 2 +- Plugins/MAD/MADDecoder.m | 19 +++++++++++++------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Audio/Chain/ConverterNode.m b/Audio/Chain/ConverterNode.m index 932fa0e73..2109334da 100644 --- a/Audio/Chain/ConverterNode.m +++ b/Audio/Chain/ConverterNode.m @@ -444,11 +444,16 @@ static void convert_be_to_le(uint8_t *buffer, size_t bitsPerSample, size_t bytes chunk = [self convert]; } if(!chunk) { - continue; - } - @autoreleasepool { - [self writeChunk:chunk]; - chunk = nil; + if(paused) { + continue; + } else if(!streamFormatChanged) { + break; + } + } else { + @autoreleasepool { + [self writeChunk:chunk]; + chunk = nil; + } } if(streamFormatChanged) { [self cleanUp]; diff --git a/Audio/Chain/InputNode.m b/Audio/Chain/InputNode.m index 4a1fb9471..a071fa34a 100644 --- a/Audio/Chain/InputNode.m +++ b/Audio/Chain/InputNode.m @@ -187,7 +187,7 @@ static void *kInputNodeContext = &kInputNodeContext; chunk = [decoder readAudio]; } - if(chunk) { + if(chunk && [chunk frameCount]) { @autoreleasepool { [self writeChunk:chunk]; chunk = nil; diff --git a/Plugins/MAD/MADDecoder.m b/Plugins/MAD/MADDecoder.m index 2136f14ae..44914feb7 100644 --- a/Plugins/MAD/MADDecoder.m +++ b/Plugins/MAD/MADDecoder.m @@ -470,7 +470,7 @@ return ret; } -- (void)writeOutput { +- (BOOL)writeOutput { unsigned long startingSample = 0; unsigned long sampleCount = _synth.pcm.length; @@ -485,7 +485,7 @@ // Past the end of the file. if(totalFrames - _endPadding <= _framesDecoded) { // DLog(@"End of file. Not writing."); - return; + return YES; } // Clip this for the following calculation, so this doesn't underflow @@ -497,6 +497,11 @@ // DLog(@"End of file. %li", totalFrames - _endPadding - _framesDecoded); sampleCount = totalFrames - _endPadding - _framesDecoded + startingSample; } + } else { + // Past the end of the file. + if(totalFrames <= _framesDecoded) { + return YES; + } } // We haven't even gotten to the start yet @@ -504,7 +509,7 @@ // DLog(@"Skipping entire sample"); _framesDecoded += sampleCount; framesToSkip -= sampleCount; - return; + return NO; } framesToSkip = 0; @@ -536,6 +541,8 @@ // FILE *f = fopen("data.raw", "a"); // fwrite(_outputBuffer, channels * 2, _outputFrames, f); // fclose(f); + + return NO; } - (int)decodeMPEGFrame { @@ -687,8 +694,6 @@ } - (AudioChunk *)readAudio { - int framesRead = 0; - if(!_firstFrame) [self syncFormat]; @@ -712,7 +717,9 @@ else if(r == -1) // Unrecoverable error break; - [self writeOutput]; + if([self writeOutput]) { + return nil; + } // DLog(@"Wrote output"); [self syncFormat];