cog/Audio/Chain/Node.m

225 lines
3.6 KiB
Matlab
Raw Normal View History

2006-01-20 15:34:02 +00:00
//
// Node.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 "Node.h"
#import "Logging.h"
2021-01-27 02:12:07 +00:00
#import "BufferChain.h"
2006-01-20 15:34:02 +00:00
@implementation Node
- (id)initWithController:(id)c previous:(id)p
{
self = [super init];
if (self)
{
buffer = [[VirtualRingBuffer alloc] initWithLength:BUFFER_SIZE];
semaphore = [[Semaphore alloc] init];
2006-04-02 20:03:12 +00:00
readLock = [[NSLock alloc] init];
writeLock = [[NSLock alloc] init];
2006-01-20 15:34:02 +00:00
initialBufferFilled = NO;
controller = c;
endOfStream = NO;
shouldContinue = YES;
[self setPreviousNode:p];
2006-01-20 15:34:02 +00:00
}
return self;
}
- (int)writeData:(void *)ptr amount:(int)amount
{
void *writePtr;
int amountToCopy, availOutput;
int amountLeft = amount;
2006-04-02 20:03:12 +00:00
[writeLock lock];
while (shouldContinue == YES && amountLeft > 0)
2006-01-20 15:34:02 +00:00
{
availOutput = [buffer lengthAvailableToWriteReturningPointer:&writePtr];
if (availOutput == 0) {
if (initialBufferFilled == NO) {
initialBufferFilled = YES;
if ([controller respondsToSelector:@selector(initialBufferFilled:)])
[controller performSelector:@selector(initialBufferFilled:) withObject:self];
}
}
if (availOutput == 0 || shouldReset)
{
[writeLock unlock];
2006-01-20 15:34:02 +00:00
[semaphore wait];
2006-04-13 02:55:42 +00:00
[writeLock lock];
}
else
{
amountToCopy = availOutput;
if (amountToCopy > amountLeft)
amountToCopy = amountLeft;
2006-01-20 15:34:02 +00:00
memcpy(writePtr, &((char *)ptr)[amount - amountLeft], amountToCopy);
if (amountToCopy > 0)
2006-01-20 15:34:02 +00:00
{
[buffer didWriteLength:amountToCopy];
2006-01-20 15:34:02 +00:00
}
amountLeft -= amountToCopy;
2006-01-20 15:34:02 +00:00
}
}
2006-04-02 20:03:12 +00:00
[writeLock unlock];
2006-01-20 15:34:02 +00:00
return (amount - amountLeft);
}
//Should be overwriten by subclass.
- (void)process
{
}
- (void)threadEntry:(id)arg
{
@autoreleasepool {
[self process];
}
2006-01-20 15:34:02 +00:00
}
- (int)readData:(void *)ptr amount:(int)amount
{
void *readPtr;
int amountToCopy;
int availInput;
2006-04-02 20:03:12 +00:00
[readLock lock];
2006-01-20 15:34:02 +00:00
availInput = [[previousNode buffer] lengthAvailableToReadReturningPointer:&readPtr];
if (availInput < amount && [previousNode endOfStream] == YES)
{
// [previousNode release];
//If it is the outputNode, [soundController newInputChain];
//else
endOfStream = YES;
}
2007-07-11 01:20:32 +00:00
/* if (availInput <= 0) {
DLog(@"BUFFER RAN DRY!");
}
else if (availInput < amount) {
DLog(@"BUFFER IN DANGER");
}
2007-07-11 01:20:32 +00:00
*/
if ([previousNode shouldReset] == YES) {
[writeLock lock];
[buffer empty];
shouldReset = YES;
[previousNode setShouldReset: NO];
[writeLock unlock];
[[previousNode semaphore] signal];
}
2006-01-20 15:34:02 +00:00
amountToCopy = availInput;
if (amountToCopy > amount)
2006-01-20 15:34:02 +00:00
{
amountToCopy = amount;
}
memcpy(ptr, readPtr, amountToCopy);
if (amountToCopy > 0)
{
[[previousNode buffer] didReadLength:amountToCopy];
2006-01-20 15:34:02 +00:00
[[previousNode semaphore] signal];
}
2006-04-02 20:03:12 +00:00
[readLock unlock];
2006-01-20 15:34:02 +00:00
return amountToCopy;
}
- (void)launchThread
{
[NSThread detachNewThreadSelector:@selector(threadEntry:) toTarget:self withObject:nil];
}
- (void)setPreviousNode:(id)p
{
previousNode = p;
}
2006-01-20 15:34:02 +00:00
- (id)previousNode
{
return previousNode;
}
- (BOOL)shouldContinue
{
return shouldContinue;
}
- (void)setShouldContinue:(BOOL)s
{
shouldContinue = s;
}
- (VirtualRingBuffer *)buffer
{
return buffer;
}
2006-04-02 20:03:12 +00:00
- (void)resetBuffer
{
shouldReset = YES; //Will reset on next write.
if (previousNode == nil) {
[readLock lock];
[buffer empty];
[readLock unlock];
}
2006-04-02 20:03:12 +00:00
}
- (NSLock *)readLock
{
return readLock;
}
- (NSLock *)writeLock
{
return writeLock;
}
2006-01-20 15:34:02 +00:00
- (Semaphore *)semaphore
{
return semaphore;
}
- (BOOL)endOfStream
2006-01-20 15:34:02 +00:00
{
return endOfStream;
2006-01-20 15:34:02 +00:00
}
- (void)setEndOfStream:(BOOL)e
2006-01-20 15:34:02 +00:00
{
endOfStream = e;
2006-01-20 15:34:02 +00:00
}
- (void)setShouldReset:(BOOL)s
{
shouldReset = s;
}
- (BOOL)shouldReset
{
return shouldReset;
}
2006-01-20 15:34:02 +00:00
@end