From aafb021a80d04228ebb07e6cb715a25bb6912c46 Mon Sep 17 00:00:00 2001 From: vspader Date: Sat, 3 Mar 2007 21:13:25 +0000 Subject: [PATCH] Gapless playback fixes. --- Audio/Chain/Converter.h | 8 ++----- Audio/Chain/Converter.m | 47 ++++++++++++++++++++--------------------- Audio/Chain/InputNode.m | 7 ++++-- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Audio/Chain/Converter.h b/Audio/Chain/Converter.h index a551683b6..34d691e5c 100644 --- a/Audio/Chain/Converter.h +++ b/Audio/Chain/Converter.h @@ -15,28 +15,24 @@ { AudioConverterRef converter; - void *inputBuffer; void *outputBuffer; //Temporary for callback use + void *inputBuffer; int inputBufferSize; //end int outputSize; - int maxInputSize; AudioStreamBasicDescription inputFormat; AudioStreamBasicDescription outputFormat; } - (void *)outputBuffer; -- (void *)inputBuffer; - (void)setupWithInputFormat:(AudioStreamBasicDescription)inputFormat outputFormat:(AudioStreamBasicDescription)outputFormat; - (void)cleanUp; -- (int)convert:(int)amount; - -- (int)maxInputSize; +- (int)convert:(void *)input amount:(int)inputSize; @end diff --git a/Audio/Chain/Converter.m b/Audio/Chain/Converter.m index 9accba3eb..a3202507c 100644 --- a/Audio/Chain/Converter.m +++ b/Audio/Chain/Converter.m @@ -35,15 +35,27 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber Converter *converter = (Converter *)inUserData; OSStatus err = noErr; - ioData->mBuffers[0].mData = converter->inputBuffer; - ioData->mBuffers[0].mDataByteSize = converter->inputBufferSize; - ioData->mBuffers[0].mNumberChannels = (converter->inputFormat.mChannelsPerFrame); - ioData->mNumberBuffers = 1; + if (converter->inputBufferSize > 0) { + ioData->mBuffers[0].mData = converter->inputBuffer; + ioData->mBuffers[0].mDataByteSize = converter->inputBufferSize; + ioData->mBuffers[0].mNumberChannels = (converter->inputFormat.mChannelsPerFrame); + ioData->mNumberBuffers = 1; + + *ioNumberDataPackets = converter->inputBufferSize / converter->inputFormat.mBytesPerFrame; + + converter->inputBufferSize = 0; + } + else { + NSLog(@"RETURNING 0"); + ioData->mBuffers[0].mData = NULL; + ioData->mBuffers[0].mDataByteSize = 0; + *ioNumberDataPackets = 0; + } return err; } -- (int)convert:(int)inputSize +- (int)convert:(void *)input amount:(int)inputSize { AudioBufferList ioData; UInt32 ioNumberFrames; @@ -56,12 +68,14 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber ioData.mBuffers[0].mNumberChannels = outputFormat.mChannelsPerFrame; ioData.mNumberBuffers = 1; + inputBuffer = input; inputBufferSize = inputSize; err = AudioConverterFillComplexBuffer(converter, ACInputProc, self, &ioNumberFrames, &ioData, NULL); if (err == kAudioConverterErr_InvalidInputSize) //It returns insz at EOS at times...so run it again to make sure all data is converted { - return [self convert:inputSize]; + NSLog(@"WOAH NELLY THIS SHOULDNT BE A-HAPPENIN"); +// return [self convert:input amount:inputSize]; } return ioData.mBuffers[0].mDataByteSize; @@ -94,20 +108,16 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber } outputSize = CHUNK_SIZE; - maxInputSize = outputSize; - UInt32 dataSize = sizeof(maxInputSize); + UInt32 dataSize = sizeof(outputSize); AudioConverterGetProperty(converter, - kAudioConverterPropertyCalculateInputBufferSize, + kAudioConverterPropertyCalculateOutputBufferSize, &dataSize, - (void*)&maxInputSize); + (void*)&outputSize); if (outputBuffer) free(outputBuffer); outputBuffer = malloc(outputSize); - if (inputBuffer) - free(inputBuffer); - inputBuffer = malloc(maxInputSize); NSLog(@"Converter setup!"); @@ -115,25 +125,14 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber PrintStreamDesc(&outf); } -- (int)maxInputSize -{ - return maxInputSize; -} - (void *)outputBuffer { return outputBuffer; } -- (void *)inputBuffer -{ - return inputBuffer; -} - - (void)cleanUp { - if (inputBuffer) - free(inputBuffer); if (outputBuffer) free(outputBuffer); diff --git a/Audio/Chain/InputNode.m b/Audio/Chain/InputNode.m index 4c576075d..70d437cc7 100644 --- a/Audio/Chain/InputNode.m +++ b/Audio/Chain/InputNode.m @@ -81,6 +81,7 @@ - (void)process { int amountRead, amountConverted; + void *inputBuffer = malloc(CHUNK_SIZE); NSLog(@"Playing file: %i", self); @@ -93,9 +94,9 @@ shouldSeek = NO; } - amountRead = [decoder fillBuffer:[converter inputBuffer] ofSize:[converter maxInputSize]]; + amountRead = [decoder fillBuffer:inputBuffer ofSize:CHUNK_SIZE]; - amountConverted = [converter convert:amountRead]; //Convert fills in converter buffer, til the next call + amountConverted = [converter convert:inputBuffer amount:amountRead]; //Convert fills in converter buffer, til the next call if (amountConverted <= 0) { endOfStream = YES; @@ -110,6 +111,8 @@ [decoder close]; [converter cleanUp]; + free(inputBuffer); + NSLog(@"CLOSED: %i", self); }