Audio Chunks now have full timestamp accounting, including DSP playback speed ratio for the one DSP that can change play ratio, Rubber Band. Inputs which support looping and actually reporting the absolute play position now do so. Signed-off-by: Christopher Snowhill <kode54@gmail.com>
301 lines
5.7 KiB
Objective-C
301 lines
5.7 KiB
Objective-C
//
|
|
// Node.m
|
|
// CogNew
|
|
//
|
|
// Created by Vincent Spader on 1/4/06.
|
|
// Copyright 2006 Vincent Spader. All rights reserved.
|
|
//
|
|
|
|
#import "Node.h"
|
|
|
|
#import "BufferChain.h"
|
|
#import "Logging.h"
|
|
|
|
#import "OutputCoreAudio.h"
|
|
|
|
#import <pthread.h>
|
|
|
|
#import <mach/mach_time.h>
|
|
|
|
@implementation Node
|
|
|
|
- (id)initWithController:(id)c previous:(id)p {
|
|
self = [super init];
|
|
if(self) {
|
|
buffer = [[ChunkList alloc] initWithMaximumDuration:10.0];
|
|
semaphore = [[Semaphore alloc] init];
|
|
|
|
accessLock = [[NSLock alloc] init];
|
|
|
|
initialBufferFilled = NO;
|
|
|
|
controller = c;
|
|
endOfStream = NO;
|
|
shouldContinue = YES;
|
|
|
|
nodeChannelConfig = 0;
|
|
nodeLossless = NO;
|
|
|
|
durationPrebuffer = 2.0;
|
|
|
|
[self setPreviousNode:p];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (AudioStreamBasicDescription)nodeFormat {
|
|
return nodeFormat;
|
|
}
|
|
|
|
- (uint32_t)nodeChannelConfig {
|
|
return nodeChannelConfig;
|
|
}
|
|
|
|
- (BOOL)nodeLossless {
|
|
return nodeLossless;
|
|
}
|
|
|
|
- (void)writeData:(const void *)ptr amount:(size_t)amount {
|
|
[accessLock lock];
|
|
|
|
AudioChunk *chunk = [[AudioChunk alloc] init];
|
|
[chunk setFormat:nodeFormat];
|
|
if(nodeChannelConfig) {
|
|
[chunk setChannelConfig:nodeChannelConfig];
|
|
}
|
|
[chunk setLossless:nodeLossless];
|
|
[chunk assignSamples:ptr frameCount:amount / nodeFormat.mBytesPerPacket];
|
|
|
|
double durationList = [buffer listDuration];
|
|
double durationLeft = [buffer maxDuration] - durationList;
|
|
|
|
if(shouldContinue == YES && durationList >= durationPrebuffer) {
|
|
if(initialBufferFilled == NO) {
|
|
initialBufferFilled = YES;
|
|
if([controller respondsToSelector:@selector(initialBufferFilled:)])
|
|
[controller performSelector:@selector(initialBufferFilled:) withObject:self];
|
|
}
|
|
}
|
|
|
|
while(shouldContinue == YES && durationLeft <= 0.0) {
|
|
if(durationLeft <= 0.0 || shouldReset) {
|
|
[accessLock unlock];
|
|
[semaphore timedWait:500];
|
|
[accessLock lock];
|
|
}
|
|
|
|
durationLeft = [buffer maxDuration] - [buffer listDuration];
|
|
}
|
|
|
|
[buffer addChunk:chunk];
|
|
|
|
[accessLock unlock];
|
|
}
|
|
|
|
- (void)writeChunk:(AudioChunk *)chunk {
|
|
[accessLock lock];
|
|
|
|
double durationList = [buffer listDuration];
|
|
double durationLeft = [buffer maxDuration] - durationList;
|
|
|
|
if(shouldContinue == YES && durationList >= durationPrebuffer) {
|
|
if(initialBufferFilled == NO) {
|
|
initialBufferFilled = YES;
|
|
if([controller respondsToSelector:@selector(initialBufferFilled:)])
|
|
[controller performSelector:@selector(initialBufferFilled:) withObject:self];
|
|
}
|
|
}
|
|
|
|
while(shouldContinue == YES && durationLeft <= 0.0) {
|
|
if(previousNode && [previousNode shouldContinue] == NO) {
|
|
shouldContinue = NO;
|
|
break;
|
|
}
|
|
|
|
if(durationLeft <= 0.0 || shouldReset) {
|
|
[accessLock unlock];
|
|
[semaphore timedWait:500];
|
|
[accessLock lock];
|
|
}
|
|
|
|
durationLeft = [buffer maxDuration] - [buffer listDuration];
|
|
}
|
|
|
|
[buffer addChunk:chunk];
|
|
|
|
[accessLock unlock];
|
|
}
|
|
|
|
// Should be overwriten by subclass.
|
|
- (void)process {
|
|
}
|
|
|
|
- (void)threadEntry:(id)arg {
|
|
@autoreleasepool {
|
|
[self process];
|
|
}
|
|
}
|
|
|
|
- (BOOL)peekFormat:(nonnull AudioStreamBasicDescription *)format channelConfig:(nonnull uint32_t *)config {
|
|
[accessLock lock];
|
|
|
|
if([[previousNode buffer] isEmpty] && [previousNode endOfStream] == YES) {
|
|
endOfStream = YES;
|
|
[accessLock unlock];
|
|
return NO;
|
|
}
|
|
|
|
BOOL ret = [[previousNode buffer] peekFormat:format channelConfig:config];
|
|
|
|
[accessLock unlock];
|
|
|
|
return ret;
|
|
}
|
|
|
|
- (BOOL)peekTimestamp:(double *_Nonnull)timestamp timeRatio:(double *_Nonnull)timeRatio {
|
|
[accessLock lock];
|
|
|
|
if([[previousNode buffer] isEmpty] && [previousNode endOfStream] == YES) {
|
|
endOfStream = YES;
|
|
[accessLock unlock];
|
|
return NO;
|
|
}
|
|
|
|
BOOL ret = [[previousNode buffer] peekTimestamp:timestamp timeRatio:timeRatio];
|
|
|
|
[accessLock unlock];
|
|
|
|
return ret;
|
|
}
|
|
|
|
- (AudioChunk *)readChunk:(size_t)maxFrames {
|
|
[accessLock lock];
|
|
|
|
if([[previousNode buffer] isEmpty] && [previousNode endOfStream] == YES) {
|
|
endOfStream = YES;
|
|
[accessLock unlock];
|
|
return [[AudioChunk alloc] init];
|
|
}
|
|
|
|
if([previousNode shouldReset] == YES) {
|
|
@autoreleasepool {
|
|
[buffer reset];
|
|
}
|
|
|
|
shouldReset = YES;
|
|
[previousNode setShouldReset:NO];
|
|
|
|
[[previousNode semaphore] signal];
|
|
}
|
|
|
|
AudioChunk *ret;
|
|
|
|
@autoreleasepool {
|
|
ret = [[previousNode buffer] removeSamples:maxFrames];
|
|
}
|
|
|
|
[accessLock unlock];
|
|
|
|
if([ret frameCount]) {
|
|
[[previousNode semaphore] signal];
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
- (AudioChunk *)readChunkAsFloat32:(size_t)maxFrames {
|
|
[accessLock lock];
|
|
|
|
if([[previousNode buffer] isEmpty] && [previousNode endOfStream] == YES) {
|
|
endOfStream = YES;
|
|
[accessLock unlock];
|
|
return [[AudioChunk alloc] init];
|
|
}
|
|
|
|
if([previousNode shouldReset] == YES) {
|
|
@autoreleasepool {
|
|
[buffer reset];
|
|
}
|
|
|
|
shouldReset = YES;
|
|
[previousNode setShouldReset:NO];
|
|
|
|
[[previousNode semaphore] signal];
|
|
}
|
|
|
|
AudioChunk *ret;
|
|
|
|
@autoreleasepool {
|
|
ret = [[previousNode buffer] removeSamplesAsFloat32:maxFrames];
|
|
}
|
|
|
|
[accessLock unlock];
|
|
|
|
if([ret frameCount]) {
|
|
[[previousNode semaphore] signal];
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
- (void)launchThread {
|
|
[NSThread detachNewThreadSelector:@selector(threadEntry:) toTarget:self withObject:nil];
|
|
}
|
|
|
|
- (void)setPreviousNode:(id)p {
|
|
previousNode = p;
|
|
}
|
|
|
|
- (id)previousNode {
|
|
return previousNode;
|
|
}
|
|
|
|
- (BOOL)shouldContinue {
|
|
return shouldContinue;
|
|
}
|
|
|
|
- (void)setShouldContinue:(BOOL)s {
|
|
shouldContinue = s;
|
|
}
|
|
|
|
- (ChunkList *)buffer {
|
|
return buffer;
|
|
}
|
|
|
|
- (void)resetBuffer {
|
|
shouldReset = YES; // Will reset on next write.
|
|
if(previousNode == nil) {
|
|
@autoreleasepool {
|
|
[accessLock lock];
|
|
[buffer reset];
|
|
[accessLock unlock];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (Semaphore *)semaphore {
|
|
return semaphore;
|
|
}
|
|
|
|
- (BOOL)endOfStream {
|
|
return endOfStream;
|
|
}
|
|
|
|
- (void)setEndOfStream:(BOOL)e {
|
|
endOfStream = e;
|
|
}
|
|
|
|
- (void)setShouldReset:(BOOL)s {
|
|
shouldReset = s;
|
|
}
|
|
- (BOOL)shouldReset {
|
|
return shouldReset;
|
|
}
|
|
|
|
// Buffering nodes should implement this
|
|
- (double)secondsBuffered {
|
|
return 0.0;
|
|
}
|
|
|
|
@end
|