From 88d26d3461f4147f3d954a9e50a144c9ef46e672 Mon Sep 17 00:00:00 2001 From: vspader Date: Sat, 26 May 2007 22:13:11 +0000 Subject: [PATCH] Fixed some seeking bugs. --- Audio/AudioPlayer.m | 3 +-- Audio/Chain/BufferChain.m | 13 +------------ Audio/Chain/Converter.h | 2 ++ Audio/Chain/Converter.m | 8 +++++++- Audio/Chain/InputNode.m | 6 +++++- Audio/Chain/Node.m | 6 ++++++ Audio/Chain/OutputNode.m | 9 +++------ Audio/Output/OutputCoreAudio.m | 9 ++++++--- 8 files changed, 31 insertions(+), 25 deletions(-) diff --git a/Audio/AudioPlayer.m b/Audio/AudioPlayer.m index 58dcc744c..a0c6a584e 100644 --- a/Audio/AudioPlayer.m +++ b/Audio/AudioPlayer.m @@ -127,9 +127,8 @@ { //Need to reset everything's buffers, and then seek? /*HACK TO TEST HOW WELL THIS WOULD WORK*/ - [bufferChain seek:time]; [output seek:time]; - + [bufferChain seek:time]; /*END HACK*/ } diff --git a/Audio/Chain/BufferChain.m b/Audio/Chain/BufferChain.m index 922f575bb..b3a92dbd6 100644 --- a/Audio/Chain/BufferChain.m +++ b/Audio/Chain/BufferChain.m @@ -87,19 +87,7 @@ - (void)seek:(double)time { - [[inputNode readLock] lock]; - [[inputNode writeLock] lock]; - - //Signal so its waiting when we unlock - [[inputNode semaphore] signal]; - - NSLog(@"Reset input buffer!"); - [inputNode resetBuffer]; - NSLog(@"Seeking in bufferchain!"); [inputNode seek:time]; - - [[inputNode writeLock] unlock]; - [[inputNode readLock] unlock]; } - (void)endOfInputReached @@ -109,6 +97,7 @@ - (void)initialBufferFilled { + NSLog(@"Filled initial buffer!"); [controller launchOutputThread]; } diff --git a/Audio/Chain/Converter.h b/Audio/Chain/Converter.h index c72e0f2ad..c23053529 100644 --- a/Audio/Chain/Converter.h +++ b/Audio/Chain/Converter.h @@ -36,6 +36,8 @@ - (void)setupWithInputFormat:(AudioStreamBasicDescription)inputFormat outputFormat:(AudioStreamBasicDescription)outputFormat; - (void)cleanUp; +- (void)reset; + //Returns the amount actually read from input - (int)convert:(void *)input amount:(int)inputSize; diff --git a/Audio/Chain/Converter.m b/Audio/Chain/Converter.m index 5e3f9f573..2350cf412 100644 --- a/Audio/Chain/Converter.m +++ b/Audio/Chain/Converter.m @@ -64,6 +64,12 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber return err; } +- (void)reset +{ + NSLog(@"RESETTING"); + AudioConverterReset(converter); +} + - (int)convert:(void *)input amount:(int)inputSize { AudioBufferList ioData; @@ -89,7 +95,7 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber err = AudioConverterFillComplexBuffer(converter, ACInputProc, self, &ioNumberFrames, &ioData, NULL); if (err != noErr || needsReset) //It returns insz at EOS at times...so run it again to make sure all data is converted { - AudioConverterReset(converter); + [self reset]; } outputBufferSize = ioData.mBuffers[0].mDataByteSize; diff --git a/Audio/Chain/InputNode.m b/Audio/Chain/InputNode.m index 44c6c8ccc..4ca49c60c 100644 --- a/Audio/Chain/InputNode.m +++ b/Audio/Chain/InputNode.m @@ -92,6 +92,9 @@ NSLog(@"Actually seeking"); [decoder seekToTime:seekTime]; shouldSeek = NO; + + [self resetBuffer]; + initialBufferFilled = NO; } if (amountInBuffer < CHUNK_SIZE) { @@ -133,7 +136,8 @@ NSLog(@"SEEKING IN INPUTNODE"); seekTime = time; shouldSeek = YES; - initialBufferFilled = NO; + [self resetBuffer]; + [semaphore signal]; } - (void)dealloc diff --git a/Audio/Chain/Node.m b/Audio/Chain/Node.m index dc25a3c14..5814e963e 100644 --- a/Audio/Chain/Node.m +++ b/Audio/Chain/Node.m @@ -166,7 +166,13 @@ - (void)resetBuffer { + [readLock lock]; + [writeLock lock]; + [buffer empty]; + + [writeLock unlock]; + [readLock unlock]; } - (NSLock *)readLock diff --git a/Audio/Chain/OutputNode.m b/Audio/Chain/OutputNode.m index 22dca1160..98eb23aa5 100644 --- a/Audio/Chain/OutputNode.m +++ b/Audio/Chain/OutputNode.m @@ -24,8 +24,9 @@ - (void)seek:(double)time { + NSLog(@"SEEKING!"); [output pause]; - [semaphore signal]; + amountPlayed = time*format.mBytesPerFrame*(format.mSampleRate/1000.0); } @@ -52,15 +53,11 @@ previousNode = [[controller bufferChain] finalNode]; n = [super readData:ptr amount:amount]; -// NSLog(@"N: %i %i", n, endOfStream); if (endOfStream == YES) { - DBLog(@"End of stream reached: %i", endOfStream); - + NSLog(@"End of stream reached!"); amountPlayed = 0; [controller endOfInputPlayed]; //Updates shouldContinue appropriately? - DBLog(@"End of stream reached: %i", endOfStream); -// return (n + [self readData:ptr amount:(amount-n)]); } if (n == 0) { diff --git a/Audio/Output/OutputCoreAudio.m b/Audio/Output/OutputCoreAudio.m index e2598a011..a02facd0f 100644 --- a/Audio/Output/OutputCoreAudio.m +++ b/Audio/Output/OutputCoreAudio.m @@ -44,17 +44,20 @@ static OSStatus Sound_Renderer(void *inRefCon, AudioUnitRenderActionFlags *ioAc amountToRead = inNumberFrames*(output->deviceFormat.mBytesPerPacket); amountRead = [output->outputController readData:(readPointer) amount:amountToRead]; -// NSLog(@"Amount read: %i %i", amountRead, [output->outputController endOfStream]); + if ((amountRead < amountToRead) && [output->outputController endOfStream] == NO) //Try one more time! for track changes! { + NSLog(@"READING AGAIN! %i/%i", amountRead, amountToRead); int amountRead2; //Use this since return type of readdata isnt known...may want to fix then can do a simple += to readdata amountRead2 = [output->outputController readData:(readPointer+amountRead) amount:amountToRead-amountRead]; amountRead += amountRead2; } -// NSLog(@"Amount read: %i", amountRead); ioData->mBuffers[0].mDataByteSize = amountRead; - + ioData->mBuffers[0].mNumberChannels = output->deviceFormat.mChannelsPerFrame; + +// NSLog(@"Amount read for output: (%i) %i %i/%i", ioData->mNumberBuffers, ioData->mBuffers[0].mNumberChannels, amountRead, amountToRead); + return err; }