cog/Audio/Chain/InputNode.m

137 lines
2.6 KiB
Matlab
Raw Normal View History

2006-01-20 15:34:02 +00:00
//
// InputNode.m
2006-01-20 15:34:02 +00:00
// Cog
//
// Created by Vincent Spader on 8/2/05.
// Copyright 2005 Vincent Spader. All rights reserved.
2006-01-20 15:34:02 +00:00
//
#import "InputNode.h"
#import "BufferChain.h"
#import "Plugin.h"
#import "CoreAudioUtils.h"
2006-01-20 15:34:02 +00:00
@implementation InputNode
- (BOOL)openURL:(NSURL *)url withSource:(id<CogSource>)source outputFormat:(AudioStreamBasicDescription)of
2006-01-20 15:34:02 +00:00
{
outputFormat = of;
decoder = [AudioDecoder audioDecoderForURL:url];
[decoder retain];
[self registerObservers];
if (decoder == nil)
2006-04-13 02:51:22 +00:00
return NO;
if (![decoder open:source])
{
NSLog(@"Couldn't open decoder...");
return NO;
}
shouldContinue = YES;
2006-04-02 20:03:12 +00:00
shouldSeek = NO;
2006-04-13 02:51:22 +00:00
return YES;
2006-01-20 15:34:02 +00:00
}
- (void)registerObservers
{
[decoder addObserver:self
forKeyPath:@"properties"
options:(NSKeyValueObservingOptionNew)
context:NULL];
[decoder addObserver:self
forKeyPath:@"metadata"
options:(NSKeyValueObservingOptionNew)
context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqual:@"properties"]) {
//Setup converter!
//Inform something of properties change
}
else if ([keyPath isEqual:@"metadata"]) {
//Inform something of metadata change
}
}
2006-01-20 15:34:02 +00:00
- (void)process
{
int amountRead = 0, amountInBuffer = 0;
2007-03-03 21:13:25 +00:00
void *inputBuffer = malloc(CHUNK_SIZE);
2006-01-20 15:34:02 +00:00
while ([self shouldContinue] == YES && [self endOfStream] == NO)
2006-01-20 15:34:02 +00:00
{
2006-04-02 20:03:12 +00:00
if (shouldSeek == YES)
{
NSLog(@"SEEKING!");
[decoder seekToTime:seekTime];
NSLog(@"Har");
2006-04-02 20:03:12 +00:00
shouldSeek = NO;
NSLog(@"Seeked! Resetting Buffer");
2007-05-26 22:13:11 +00:00
[self resetBuffer];
NSLog(@"Reset buffer!");
2007-05-26 22:13:11 +00:00
initialBufferFilled = NO;
2006-04-02 20:03:12 +00:00
}
if (amountInBuffer < CHUNK_SIZE) {
amountRead = [decoder fillBuffer:((char *)inputBuffer) + amountInBuffer ofSize:CHUNK_SIZE - amountInBuffer];
amountInBuffer += amountRead;
2006-04-02 20:03:12 +00:00
if (amountRead <= 0)
{
if (initialBufferFilled == NO) {
[controller initialBufferFilled];
}
endOfStream = YES;
[controller endOfInputReached];
break; //eof
2007-05-16 23:07:00 +00:00
}
[self writeData:inputBuffer amount:amountInBuffer];
amountInBuffer = 0;
2006-01-20 15:34:02 +00:00
}
}
[decoder close];
2006-05-07 13:19:23 +00:00
2007-03-03 21:13:25 +00:00
free(inputBuffer);
2006-01-20 15:34:02 +00:00
}
2006-04-02 20:03:12 +00:00
- (void)seek:(double)time
{
seekTime = time;
shouldSeek = YES;
NSLog(@"Should seek!");
2007-05-26 22:13:11 +00:00
[semaphore signal];
2006-04-02 20:03:12 +00:00
}
- (void)dealloc
{
[decoder removeObserver:self forKeyPath:@"properties"];
[decoder removeObserver:self forKeyPath:@"metadata"];
[decoder release];
[super dealloc];
}
- (NSDictionary *) properties
2006-01-20 15:34:02 +00:00
{
return [decoder properties];
2006-01-20 15:34:02 +00:00
}
@end