2006-01-20 15:34:02 +00:00
|
|
|
//
|
2006-04-02 15:44:08 +00:00
|
|
|
// BufferChain.m
|
2006-01-20 15:34:02 +00:00
|
|
|
// CogNew
|
|
|
|
//
|
2006-09-04 18:46:18 +00:00
|
|
|
// 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"
|
2007-03-02 01:36:52 +00:00
|
|
|
#import "AudioSource.h"
|
2007-02-24 20:36:27 +00:00
|
|
|
#import "CoreAudioUtils.h"
|
2006-01-20 15:34:02 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
@implementation BufferChain
|
|
|
|
|
|
|
|
- (id)initWithController:(id)c
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
2007-02-24 20:36:27 +00:00
|
|
|
controller = c;
|
|
|
|
streamURL = nil;
|
|
|
|
userInfo = nil;
|
2013-10-02 09:30:04 +00:00
|
|
|
rgInfo = nil;
|
2007-03-02 01:36:52 +00:00
|
|
|
|
2006-04-13 02:51:22 +00:00
|
|
|
inputNode = nil;
|
2007-10-03 20:23:14 +00:00
|
|
|
converterNode = nil;
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)buildChain
|
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
inputNode = nil;
|
|
|
|
converterNode = nil;
|
|
|
|
|
2006-04-13 02:51:22 +00:00
|
|
|
inputNode = [[InputNode alloc] initWithController:self previous:nil];
|
2007-10-03 20:23:14 +00:00
|
|
|
converterNode = [[ConverterNode alloc] initWithController:self previous:inputNode];
|
|
|
|
|
|
|
|
finalNode = converterNode;
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
2013-10-07 20:03:34 +00:00
|
|
|
- (BOOL)open:(NSURL *)url withOutputFormat:(AudioStreamBasicDescription)outputFormat withRGInfo:(NSDictionary *)rgi
|
2007-02-24 20:36:27 +00:00
|
|
|
{
|
2007-03-02 01:36:52 +00:00
|
|
|
[self setStreamURL:url];
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
[self buildChain];
|
2007-03-02 01:36:52 +00:00
|
|
|
|
|
|
|
id<CogSource> source = [AudioSource audioSourceForURL:url];
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Opening: %@", url);
|
2007-03-02 01:36:52 +00:00
|
|
|
if (![source open:url])
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Couldn't open source...");
|
2015-02-09 02:15:02 +00:00
|
|
|
url = [NSURL URLWithString:@"silence://1"];
|
|
|
|
source = [AudioSource audioSourceForURL:url];
|
|
|
|
if (![source open:url])
|
|
|
|
return NO;
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 18:12:15 +00:00
|
|
|
if (![inputNode openWithSource:source])
|
2006-04-13 02:51:22 +00:00
|
|
|
return NO;
|
2007-10-03 20:23:14 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
if (![converterNode setupWithInputFormat:propertiesToASBD([inputNode properties]) outputFormat:outputFormat])
|
|
|
|
return NO;
|
|
|
|
|
2013-10-07 20:03:34 +00:00
|
|
|
[self setRGInfo:rgi];
|
|
|
|
|
2007-10-12 02:55:59 +00:00
|
|
|
// return NO;
|
2007-11-24 20:16:27 +00:00
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
- (BOOL)openWithInput:(InputNode *)i withOutputFormat:(AudioStreamBasicDescription)outputFormat withRGInfo:(NSDictionary *)rgi
|
2007-10-11 02:08:29 +00:00
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"New buffer chain!");
|
2007-10-11 02:08:29 +00:00
|
|
|
[self buildChain];
|
|
|
|
|
|
|
|
if (![inputNode openWithDecoder:[i decoder]])
|
|
|
|
return NO;
|
2007-11-24 20:16:27 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Input Properties: %@", [inputNode properties]);
|
2007-10-11 02:08:29 +00:00
|
|
|
if (![converterNode setupWithInputFormat:propertiesToASBD([inputNode properties]) outputFormat:outputFormat])
|
|
|
|
return NO;
|
2013-10-11 12:03:55 +00:00
|
|
|
|
|
|
|
[self setRGInfo:rgi];
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
- (void)launchThreads
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Properties: %@", [inputNode properties]);
|
2007-10-11 02:08:29 +00:00
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
[inputNode launchThread];
|
2007-11-24 20:16:27 +00:00
|
|
|
[converterNode launchThread];
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
- (void)setUserInfo:(id)i
|
|
|
|
{
|
|
|
|
userInfo = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)userInfo
|
|
|
|
{
|
|
|
|
return userInfo;
|
|
|
|
}
|
|
|
|
|
2013-10-02 09:30:04 +00:00
|
|
|
- (void)setRGInfo:(NSDictionary *)rgi
|
|
|
|
{
|
|
|
|
rgInfo = rgi;
|
2013-10-07 10:59:04 +00:00
|
|
|
[converterNode setRGInfo:rgi];
|
2013-10-02 09:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)rgInfo
|
|
|
|
{
|
|
|
|
return rgInfo;
|
|
|
|
}
|
|
|
|
|
2006-04-02 15:44:08 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
2013-10-12 21:01:21 +00:00
|
|
|
[inputNode setShouldContinue:NO];
|
2013-10-12 20:59:34 +00:00
|
|
|
[[inputNode exitAtTheEndOfTheStream] signal];
|
|
|
|
[[inputNode semaphore] signal];
|
|
|
|
[[inputNode exitAtTheEndOfTheStream] wait]; // wait for decoder to be closed (see InputNode's -(void)process )
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Bufferchain dealloc");
|
2006-04-02 15:44:08 +00:00
|
|
|
}
|
|
|
|
|
2006-04-02 20:03:12 +00:00
|
|
|
- (void)seek:(double)time
|
|
|
|
{
|
2013-10-12 20:52:58 +00:00
|
|
|
long frame = (long) round(time * [[[inputNode properties] objectForKey:@"sampleRate"] floatValue]);
|
2007-11-24 20:16:27 +00:00
|
|
|
|
|
|
|
[inputNode seek:frame];
|
2006-04-02 20:03:12 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
- (BOOL)endOfInputReached
|
|
|
|
{
|
|
|
|
return [controller endOfInputReached:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)setTrack: (NSURL *)track
|
2006-04-13 02:51:22 +00:00
|
|
|
{
|
2007-10-11 02:08:29 +00:00
|
|
|
return [inputNode setTrack:track];
|
2006-04-13 02:51:22 +00:00
|
|
|
}
|
|
|
|
|
2007-10-12 02:55:59 +00:00
|
|
|
- (void)initialBufferFilled:(id)sender
|
2007-03-19 00:19:47 +00:00
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"INITIAL BUFFER FILLED");
|
2007-03-19 00:19:47 +00:00
|
|
|
[controller launchOutputThread];
|
|
|
|
}
|
|
|
|
|
2007-10-12 02:55:59 +00:00
|
|
|
- (void)inputFormatDidChange:(AudioStreamBasicDescription)format
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"FORMAT DID CHANGE!");
|
2007-10-12 02:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
- (InputNode *)inputNode
|
|
|
|
{
|
|
|
|
return inputNode;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
- (id)finalNode
|
|
|
|
{
|
|
|
|
return finalNode;
|
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
- (NSURL *)streamURL
|
2006-04-13 02:51:22 +00:00
|
|
|
{
|
2007-02-24 20:36:27 +00:00
|
|
|
return streamURL;
|
2006-04-13 02:51:22 +00:00
|
|
|
}
|
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
- (void)setStreamURL:(NSURL *)url
|
|
|
|
{
|
|
|
|
streamURL = url;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
- (void)setShouldContinue:(BOOL)s
|
|
|
|
{
|
|
|
|
[inputNode setShouldContinue:s];
|
2007-10-03 20:23:14 +00:00
|
|
|
[converterNode setShouldContinue:s];
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 20:52:58 +00:00
|
|
|
- (BOOL)isRunning
|
|
|
|
{
|
2013-10-12 21:01:21 +00:00
|
|
|
InputNode *node = [self inputNode];
|
|
|
|
if (nil != node && [node shouldContinue] && ![node endOfStream])
|
2013-10-12 20:52:58 +00:00
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2013-10-21 05:04:09 +00:00
|
|
|
- (id)controller
|
|
|
|
{
|
|
|
|
return controller;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
@end
|