2006-01-20 12:34:02 -03:00
|
|
|
//
|
2006-04-02 11:44:08 -04:00
|
|
|
// BufferChain.m
|
2006-01-20 12:34:02 -03:00
|
|
|
// CogNew
|
|
|
|
//
|
2006-09-04 14:46:18 -04:00
|
|
|
// Created by Vincent Spader on 1/4/06.
|
|
|
|
// Copyright 2006 Vincent Spader. All rights reserved.
|
2006-01-20 12:34:02 -03:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "BufferChain.h"
|
2007-03-01 22:36:52 -03:00
|
|
|
#import "AudioSource.h"
|
2007-02-24 17:36:27 -03:00
|
|
|
#import "CoreAudioUtils.h"
|
2022-02-07 02:49:27 -03:00
|
|
|
#import "OutputNode.h"
|
2006-01-20 12:34:02 -03:00
|
|
|
|
2013-10-11 09:03:55 -03:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2006-01-20 12:34:02 -03:00
|
|
|
@implementation BufferChain
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (id)initWithController:(id)c {
|
2006-01-20 12:34:02 -03:00
|
|
|
self = [super init];
|
2022-02-07 02:49:27 -03:00
|
|
|
if(self) {
|
2007-02-24 17:36:27 -03:00
|
|
|
controller = c;
|
|
|
|
streamURL = nil;
|
|
|
|
userInfo = nil;
|
2022-02-07 02:49:27 -03:00
|
|
|
rgInfo = nil;
|
2007-03-01 22:36:52 -03:00
|
|
|
|
2006-04-12 22:51:22 -04:00
|
|
|
inputNode = nil;
|
2007-10-03 16:23:14 -04:00
|
|
|
converterNode = nil;
|
2006-01-20 12:34:02 -03:00
|
|
|
}
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2006-01-20 12:34:02 -03:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)buildChain {
|
|
|
|
inputNode = nil;
|
|
|
|
converterNode = nil;
|
|
|
|
|
2006-04-12 22:51:22 -04:00
|
|
|
inputNode = [[InputNode alloc] initWithController:self previous:nil];
|
2007-10-03 16:23:14 -04:00
|
|
|
converterNode = [[ConverterNode alloc] initWithController:self previous:inputNode];
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2007-10-03 16:23:14 -04:00
|
|
|
finalNode = converterNode;
|
2006-01-20 12:34:02 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)open:(NSURL *)url withOutputFormat:(AudioStreamBasicDescription)outputFormat withRGInfo:(NSDictionary *)rgi {
|
2007-03-01 22:36:52 -03:00
|
|
|
[self setStreamURL:url];
|
|
|
|
|
2006-01-20 12:34:02 -03:00
|
|
|
[self buildChain];
|
2022-02-07 02:49:27 -03:00
|
|
|
|
2007-03-01 22:36:52 -03:00
|
|
|
id<CogSource> source = [AudioSource audioSourceForURL:url];
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"Opening: %@", url);
|
2022-02-07 02:49:27 -03:00
|
|
|
if(!source || ![source open:url]) {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"Couldn't open source...");
|
2022-02-07 02:49:27 -03:00
|
|
|
url = [NSURL URLWithString:@"silence://10"];
|
|
|
|
source = [AudioSource audioSourceForURL:url];
|
|
|
|
if(![source open:url])
|
|
|
|
return NO;
|
2007-03-01 22:36:52 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
if(![inputNode openWithSource:source])
|
2006-04-12 22:51:22 -04:00
|
|
|
return NO;
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
NSDictionary *properties = [inputNode properties];
|
|
|
|
|
|
|
|
inputFormat = [inputNode nodeFormat];
|
|
|
|
|
|
|
|
outputFormat.mChannelsPerFrame = inputFormat.mChannelsPerFrame;
|
|
|
|
outputFormat.mBytesPerFrame = ((outputFormat.mBitsPerChannel + 7) / 8) * outputFormat.mChannelsPerFrame;
|
|
|
|
outputFormat.mBytesPerPacket = outputFormat.mBytesPerFrame * outputFormat.mFramesPerPacket;
|
|
|
|
|
|
|
|
if(![converterNode setupWithInputFormat:inputFormat outputFormat:outputFormat isLossless:[[properties valueForKey:@"encoding"] isEqualToString:@"lossless"]])
|
2007-11-24 17:16:27 -03:00
|
|
|
return NO;
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
[self setRGInfo:rgi];
|
2013-10-07 17:03:34 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
// return NO;
|
2007-11-24 17:16:27 -03:00
|
|
|
|
2006-01-20 12:34:02 -03:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)openWithInput:(InputNode *)i withOutputFormat:(AudioStreamBasicDescription)outputFormat withRGInfo:(NSDictionary *)rgi {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"New buffer chain!");
|
2007-10-10 22:08:29 -04:00
|
|
|
[self buildChain];
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
if(![inputNode openWithDecoder:[i decoder]])
|
2007-10-10 22:08:29 -04:00
|
|
|
return NO;
|
2022-02-07 02:49:27 -03:00
|
|
|
|
|
|
|
NSDictionary *properties = [inputNode properties];
|
|
|
|
|
|
|
|
inputFormat = [inputNode nodeFormat];
|
|
|
|
|
|
|
|
outputFormat.mChannelsPerFrame = inputFormat.mChannelsPerFrame;
|
|
|
|
outputFormat.mBytesPerFrame = ((outputFormat.mBitsPerChannel + 7) / 8) * outputFormat.mChannelsPerFrame;
|
|
|
|
outputFormat.mBytesPerPacket = outputFormat.mBytesPerFrame * outputFormat.mFramesPerPacket;
|
|
|
|
|
|
|
|
DLog(@"Input Properties: %@", properties);
|
|
|
|
if(![converterNode setupWithInputFormat:inputFormat outputFormat:outputFormat isLossless:[[properties objectForKey:@"encoding"] isEqualToString:@"lossless"]])
|
2007-10-10 22:08:29 -04:00
|
|
|
return NO;
|
2013-10-11 09:03:55 -03:00
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
[self setRGInfo:rgi];
|
|
|
|
|
2007-10-10 22:08:29 -04:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-12-26 07:01:02 -03:00
|
|
|
- (BOOL)openWithDecoder:(id<CogDecoder>)decoder
|
2022-02-07 02:49:27 -03:00
|
|
|
withOutputFormat:(AudioStreamBasicDescription)outputFormat
|
|
|
|
withRGInfo:(NSDictionary *)rgi;
|
2021-12-26 07:01:02 -03:00
|
|
|
{
|
2022-02-07 02:49:27 -03:00
|
|
|
DLog(@"New buffer chain!");
|
|
|
|
[self buildChain];
|
|
|
|
|
|
|
|
if(![inputNode openWithDecoder:decoder])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
NSDictionary *properties = [inputNode properties];
|
|
|
|
|
|
|
|
DLog(@"Input Properties: %@", properties);
|
|
|
|
|
|
|
|
inputFormat = [inputNode nodeFormat];
|
|
|
|
|
|
|
|
outputFormat.mChannelsPerFrame = inputFormat.mChannelsPerFrame;
|
|
|
|
outputFormat.mBytesPerFrame = ((outputFormat.mBitsPerChannel + 7) / 8) * outputFormat.mChannelsPerFrame;
|
|
|
|
outputFormat.mBytesPerPacket = outputFormat.mBytesPerFrame * outputFormat.mFramesPerPacket;
|
|
|
|
|
|
|
|
if(![converterNode setupWithInputFormat:inputFormat outputFormat:outputFormat isLossless:[[properties objectForKey:@"encoding"] isEqualToString:@"lossless"]])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
[self setRGInfo:rgi];
|
|
|
|
|
|
|
|
return YES;
|
2021-12-26 07:01:02 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)launchThreads {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"Properties: %@", [inputNode properties]);
|
2007-10-10 22:08:29 -04:00
|
|
|
|
2006-01-20 12:34:02 -03:00
|
|
|
[inputNode launchThread];
|
2007-11-24 17:16:27 -03:00
|
|
|
[converterNode launchThread];
|
2006-01-20 12:34:02 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)setUserInfo:(id)i {
|
2007-02-24 17:36:27 -03:00
|
|
|
userInfo = i;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (id)userInfo {
|
2007-02-24 17:36:27 -03:00
|
|
|
return userInfo;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)setRGInfo:(NSDictionary *)rgi {
|
|
|
|
rgInfo = rgi;
|
|
|
|
[converterNode setRGInfo:rgi];
|
2013-10-02 06:30:04 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (NSDictionary *)rgInfo {
|
|
|
|
return rgInfo;
|
2013-10-02 06:30:04 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)dealloc {
|
|
|
|
[inputNode setShouldContinue:NO];
|
|
|
|
[[inputNode exitAtTheEndOfTheStream] signal];
|
|
|
|
[[inputNode semaphore] signal];
|
|
|
|
[[inputNode exitAtTheEndOfTheStream] wait]; // wait for decoder to be closed (see InputNode's -(void)process )
|
2013-10-12 17:59:34 -03:00
|
|
|
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"Bufferchain dealloc");
|
2006-04-02 11:44:08 -04:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)seek:(double)time {
|
|
|
|
long frame = (long)round(time * [[[inputNode properties] objectForKey:@"sampleRate"] floatValue]);
|
2007-11-24 17:16:27 -03:00
|
|
|
|
|
|
|
[inputNode seek:frame];
|
2006-04-02 16:03:12 -04:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)endOfInputReached {
|
2007-10-10 22:08:29 -04:00
|
|
|
return [controller endOfInputReached:self];
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)setTrack:(NSURL *)track {
|
2007-10-10 22:08:29 -04:00
|
|
|
return [inputNode setTrack:track];
|
2006-04-12 22:51:22 -04:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)initialBufferFilled:(id)sender {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"INITIAL BUFFER FILLED");
|
2007-03-18 20:19:47 -04:00
|
|
|
[controller launchOutputThread];
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)inputFormatDidChange:(AudioStreamBasicDescription)format {
|
2013-10-11 09:03:55 -03:00
|
|
|
DLog(@"FORMAT DID CHANGE!");
|
2007-10-11 22:55:59 -04:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (InputNode *)inputNode {
|
2007-10-10 22:08:29 -04:00
|
|
|
return inputNode;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (id)finalNode {
|
2006-01-20 12:34:02 -03:00
|
|
|
return finalNode;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (NSURL *)streamURL {
|
2007-02-24 17:36:27 -03:00
|
|
|
return streamURL;
|
2006-04-12 22:51:22 -04:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)setStreamURL:(NSURL *)url {
|
2007-03-01 22:36:52 -03:00
|
|
|
streamURL = url;
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)setShouldContinue:(BOOL)s {
|
2006-01-20 12:34:02 -03:00
|
|
|
[inputNode setShouldContinue:s];
|
2007-10-03 16:23:14 -04:00
|
|
|
[converterNode setShouldContinue:s];
|
2006-01-20 12:34:02 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (BOOL)isRunning {
|
|
|
|
InputNode *node = [self inputNode];
|
|
|
|
if(nil != node && [node shouldContinue] && ![node endOfStream]) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
2013-10-12 17:52:58 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (id)controller {
|
|
|
|
return controller;
|
2013-10-21 02:04:09 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (ConverterNode *)converter {
|
|
|
|
return converterNode;
|
2021-12-26 04:41:45 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (AudioStreamBasicDescription)inputFormat {
|
|
|
|
return inputFormat;
|
2021-12-26 04:41:45 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (double)secondsBuffered {
|
|
|
|
double duration = 0.0;
|
|
|
|
OutputNode *outputNode = (OutputNode *)[controller output];
|
|
|
|
duration += [outputNode secondsBuffered];
|
|
|
|
|
|
|
|
Node *node = [self finalNode];
|
|
|
|
while(node) {
|
|
|
|
duration += [node secondsBuffered];
|
|
|
|
node = [node previousNode];
|
|
|
|
}
|
|
|
|
return duration;
|
2022-01-14 04:05:32 -03:00
|
|
|
}
|
|
|
|
|
2022-02-07 02:49:27 -03:00
|
|
|
- (void)sustainHDCD {
|
|
|
|
OutputNode *outputNode = (OutputNode *)[controller output];
|
|
|
|
[outputNode sustainHDCD];
|
|
|
|
[controller sustainHDCD];
|
2022-01-19 07:08:57 -03:00
|
|
|
}
|
|
|
|
|
2006-01-20 12:34:02 -03:00
|
|
|
@end
|