cog/Audio/Chain/BufferChain.m

203 lines
3.6 KiB
Matlab
Raw Normal View History

2006-01-20 15:34:02 +00:00
//
// BufferChain.m
2006-01-20 15:34:02 +00:00
// CogNew
//
// Created by Vincent Spader on 1/4/06.
// Copyright 2006 Vincent Spader. All rights reserved.
2006-01-20 15:34:02 +00:00
//
#import "BufferChain.h"
#import "OutputNode.h"
#import "AudioSource.h"
#import "CoreAudioUtils.h"
2006-01-20 15:34:02 +00:00
#import "Logging.h"
2006-01-20 15:34:02 +00:00
@implementation BufferChain
- (id)initWithController:(id)c
{
self = [super init];
if (self)
{
controller = c;
streamURL = nil;
userInfo = nil;
rgInfo = nil;
2006-04-13 02:51:22 +00:00
inputNode = nil;
converterNode = nil;
2006-01-20 15:34:02 +00:00
}
return self;
}
- (void)buildChain
{
inputNode = nil;
converterNode = nil;
2006-04-13 02:51:22 +00:00
inputNode = [[InputNode alloc] initWithController:self previous:nil];
converterNode = [[ConverterNode alloc] initWithController:self previous:inputNode];
finalNode = converterNode;
2006-01-20 15:34:02 +00:00
}
- (BOOL)open:(NSURL *)url withOutputFormat:(AudioStreamBasicDescription)outputFormat withRGInfo:(NSDictionary *)rgi
{
[self setStreamURL:url];
2006-01-20 15:34:02 +00:00
[self buildChain];
id<CogSource> source = [AudioSource audioSourceForURL:url];
DLog(@"Opening: %@", url);
if (![source open:url])
{
DLog(@"Couldn't open source...");
url = [NSURL URLWithString:@"silence://1"];
source = [AudioSource audioSourceForURL:url];
if (![source open:url])
return NO;
}
2007-10-14 18:12:15 +00:00
if (![inputNode openWithSource:source])
2006-04-13 02:51:22 +00:00
return NO;
if (![converterNode setupWithInputFormat:propertiesToASBD([inputNode properties]) outputFormat:outputFormat])
return NO;
[self setRGInfo:rgi];
// return NO;
2006-01-20 15:34:02 +00:00
return YES;
}
- (BOOL)openWithInput:(InputNode *)i withOutputFormat:(AudioStreamBasicDescription)outputFormat withRGInfo:(NSDictionary *)rgi
{
DLog(@"New buffer chain!");
[self buildChain];
if (![inputNode openWithDecoder:[i decoder]])
return NO;
DLog(@"Input Properties: %@", [inputNode properties]);
if (![converterNode setupWithInputFormat:propertiesToASBD([inputNode properties]) outputFormat:outputFormat])
return NO;
[self setRGInfo:rgi];
return YES;
}
2006-01-20 15:34:02 +00:00
- (void)launchThreads
{
DLog(@"Properties: %@", [inputNode properties]);
2006-01-20 15:34:02 +00:00
[inputNode launchThread];
[converterNode launchThread];
2006-01-20 15:34:02 +00:00
}
- (void)setUserInfo:(id)i
{
userInfo = i;
}
- (id)userInfo
{
return userInfo;
}
- (void)setRGInfo:(NSDictionary *)rgi
{
rgInfo = rgi;
[converterNode setRGInfo:rgi];
}
- (NSDictionary *)rgInfo
{
return rgInfo;
}
- (void)dealloc
{
2013-10-12 21:01:21 +00:00
[inputNode setShouldContinue:NO];
[[inputNode exitAtTheEndOfTheStream] signal];
[[inputNode semaphore] signal];
[[inputNode exitAtTheEndOfTheStream] wait]; // wait for decoder to be closed (see InputNode's -(void)process )
DLog(@"Bufferchain dealloc");
}
2006-04-02 20:03:12 +00:00
- (void)seek:(double)time
{
long frame = (long) round(time * [[[inputNode properties] objectForKey:@"sampleRate"] floatValue]);
[inputNode seek:frame];
2006-04-02 20:03:12 +00:00
}
- (BOOL)endOfInputReached
{
return [controller endOfInputReached:self];
}
- (BOOL)setTrack: (NSURL *)track
2006-04-13 02:51:22 +00:00
{
return [inputNode setTrack:track];
2006-04-13 02:51:22 +00:00
}
- (void)initialBufferFilled:(id)sender
{
DLog(@"INITIAL BUFFER FILLED");
[controller launchOutputThread];
}
- (void)inputFormatDidChange:(AudioStreamBasicDescription)format
{
DLog(@"FORMAT DID CHANGE!");
}
- (InputNode *)inputNode
{
return inputNode;
}
2006-01-20 15:34:02 +00:00
- (id)finalNode
{
return finalNode;
}
- (NSURL *)streamURL
2006-04-13 02:51:22 +00:00
{
return streamURL;
2006-04-13 02:51:22 +00:00
}
- (void)setStreamURL:(NSURL *)url
{
streamURL = url;
}
2006-01-20 15:34:02 +00:00
- (void)setShouldContinue:(BOOL)s
{
[inputNode setShouldContinue:s];
[converterNode setShouldContinue:s];
2006-01-20 15:34:02 +00:00
}
- (BOOL)isRunning
{
2013-10-12 21:01:21 +00:00
InputNode *node = [self inputNode];
if (nil != node && [node shouldContinue] && ![node endOfStream])
{
return YES;
}
return NO;
}
- (id)controller
{
return controller;
}
2006-01-20 15:34:02 +00:00
@end