diff --git a/PlaybackController.m b/PlaybackController.m index 714ea26b4..1e828835b 100644 --- a/PlaybackController.m +++ b/PlaybackController.m @@ -129,7 +129,7 @@ double time; time = [positionSlider doubleValue]; - if ([sender tracking] == NO) // check if user stopped sliding before playing audio +// if ([sender tracking] == NO) // check if user stopped sliding before playing audio [soundController seekToTime:time]; [self updateTimeField:time]; diff --git a/Sound/BufferChain.h b/Sound/BufferChain.h index 12159c199..0446b1589 100644 --- a/Sound/BufferChain.h +++ b/Sound/BufferChain.h @@ -26,6 +26,7 @@ - (id)initWithController:(id)c; - (void)buildChain; - (BOOL)open:(const char *)filename; +- (void)seek:(double)time; - (void)launchThreads; diff --git a/Sound/BufferChain.m b/Sound/BufferChain.m index c0a36e1dc..e8e6a7c1e 100644 --- a/Sound/BufferChain.m +++ b/Sound/BufferChain.m @@ -59,6 +59,13 @@ [super dealloc]; } +- (void)seek:(double)time +{ + [inputNode seek:time]; + [inputNode resetBuffer]; + [converterNode resetBuffer]; +} + - (id)finalNode { diff --git a/Sound/ConverterNode.m b/Sound/ConverterNode.m index 143fea662..447aaee18 100644 --- a/Sound/ConverterNode.m +++ b/Sound/ConverterNode.m @@ -185,8 +185,8 @@ static OSStatus ACInputProc(AudioConverterRef inAudioConverter, UInt32* ioNumber { return [self convert:dest amount:amount]; } - if (err != noErr) - NSLog(@"Converter error: %i", err); +// if (err != noErr) +// NSLog(@"Converter error: %i", err); return ioData.mBuffers[0].mDataByteSize; /* diff --git a/Sound/InputNode.h b/Sound/InputNode.h index 5586754b7..87632ba35 100644 --- a/Sound/InputNode.h +++ b/Sound/InputNode.h @@ -19,9 +19,14 @@ AudioStreamBasicDescription format; SoundFile *soundFile; + + BOOL shouldSeek; + double seekTime; } - (void)process; - (AudioStreamBasicDescription) format; +- (void)seek:(double)time; + @end diff --git a/Sound/InputNode.m b/Sound/InputNode.m index 8bf757467..d0249071f 100644 --- a/Sound/InputNode.m +++ b/Sound/InputNode.m @@ -18,33 +18,47 @@ [soundFile getFormat:&format]; shouldContinue = YES; + shouldSeek = NO; } - (void)process { const int chunk_size = CHUNK_SIZE; - char buf[chunk_size]; + char *buf; int amountRead; DBLog(@"Playing file.\n"); + buf = malloc(chunk_size); while ([self shouldContinue] == YES && [self endOfStream] == NO) { + if (shouldSeek == YES) + { + [soundFile seekToTime:seekTime]; + shouldSeek = NO; + } + amountRead = [soundFile fillBuffer:buf ofSize: chunk_size]; if (amountRead <= 0) { endOfStream = YES; NSLog(@"END OF INPUT WAS REACHED"); [controller endOfInputReached]; - [soundFile close]; - return; //eof + break; //eof } [self writeData:buf amount:amountRead]; } + free(buf); [soundFile close]; } +- (void)seek:(double)time +{ + seekTime = time; + shouldSeek = YES; +} + - (AudioStreamBasicDescription) format { return format; diff --git a/Sound/Node.h b/Sound/Node.h index 01434c041..a41490050 100644 --- a/Sound/Node.h +++ b/Sound/Node.h @@ -16,6 +16,8 @@ @interface Node : NSObject { VirtualRingBuffer *buffer; Semaphore *semaphore; + NSLock *readLock; + NSLock *writeLock; id previousNode; id controller; @@ -33,12 +35,16 @@ - (void)launchThread; +- (NSLock *)readLock; +- (NSLock *)writeLock; + - (id)previousNode; - (BOOL)shouldContinue; - (void)setShouldContinue:(BOOL)s; - (VirtualRingBuffer *)buffer; +- (void)resetBuffer; //WARNING! DANGER WILL ROBINSON! - (Semaphore *)semaphore; diff --git a/Sound/Node.m b/Sound/Node.m index 71e7f4f53..643c05ea5 100644 --- a/Sound/Node.m +++ b/Sound/Node.m @@ -17,6 +17,8 @@ { buffer = [[VirtualRingBuffer alloc] initWithLength:BUFFER_SIZE]; semaphore = [[Semaphore alloc] init]; + readLock = [[NSLock alloc] init]; + writeLock = [[NSLock alloc] init]; controller = c; previousNode = p; @@ -33,6 +35,7 @@ int amountToCopy, availOutput; int amountLeft = amount; + [writeLock lock]; while (shouldContinue == YES && amountLeft > 0) { availOutput = [buffer lengthAvailableToWriteReturningPointer:&writePtr]; @@ -56,6 +59,7 @@ amountLeft -= amountToCopy; } } + [writeLock unlock]; return (amount - amountLeft); } @@ -68,15 +72,17 @@ - (void)threadEntry:(id)arg { + [self retain]; + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; DBLog(@"In thread entry"); - [self retain]; [self process]; - [self release]; [pool release]; + + [self release]; } - (int)readData:(void *)ptr amount:(int)amount @@ -85,6 +91,7 @@ int amountToCopy; int availInput; + [readLock lock]; availInput = [[previousNode buffer] lengthAvailableToReadReturningPointer:&readPtr]; if (availInput <= amount && [previousNode endOfStream] == YES) @@ -110,6 +117,7 @@ [[previousNode semaphore] signal]; } + [readLock unlock]; return amountToCopy; } @@ -140,6 +148,27 @@ return buffer; } +- (void)resetBuffer +{ + [readLock lock]; + [writeLock lock]; + + [buffer empty]; + + [writeLock unlock]; + [readLock unlock]; +} + +- (NSLock *)readLock +{ + return readLock; +} + +- (NSLock *)writeLock +{ + return writeLock; +} + - (Semaphore *)semaphore { return semaphore; diff --git a/Sound/OutputNode.h b/Sound/OutputNode.h index 50c96b318..8c8e98b46 100644 --- a/Sound/OutputNode.h +++ b/Sound/OutputNode.h @@ -29,6 +29,7 @@ - (void)setup; - (void)process; - (void)close; +- (void)seek:(double)time; - (int)readData:(void *)ptr amount:(int)amount; diff --git a/Sound/OutputNode.m b/Sound/OutputNode.m index 3e8a94734..fd00c76b5 100644 --- a/Sound/OutputNode.m +++ b/Sound/OutputNode.m @@ -20,6 +20,12 @@ [output setup]; } +- (void)seek:(double)time +{ + amountPlayed = time*format.mBytesPerFrame*(format.mSampleRate/1000.0); + +} + - (void)process { [output start]; diff --git a/Sound/SoundController.m b/Sound/SoundController.m index cee7db894..ab9da4635 100644 --- a/Sound/SoundController.m +++ b/Sound/SoundController.m @@ -90,6 +90,12 @@ - (void)seekToTime:(double)time { //Need to reset everything's buffers, and then seek? + /*HACK TO TEST HOW WELL THIS WOULD WORK*/ + [bufferChain seek:time]; + [output seek:time]; + + + /*END HACK*/ } - (void)setVolume:(double)v diff --git a/Sound/SoundFile/VorbisFile.m b/Sound/SoundFile/VorbisFile.m index 996d68213..f09eb0374 100644 --- a/Sound/SoundFile/VorbisFile.m +++ b/Sound/SoundFile/VorbisFile.m @@ -65,6 +65,10 @@ return total; } +- (void)dealloc +{ + NSLog(@"DEALLOCATING VORBIS"); +} - (void)close { ov_clear(&vorbisRef);