cog/Sound/SoundController.m

217 lines
3.7 KiB
Matlab
Raw Normal View History

2006-01-20 15:34:02 +00:00
//
// SoundController.m
2006-01-20 15:34:02 +00:00
// Cog
//
// Created by Vincent Spader on 8/7/05.
// Copyright 2005 Vincent Spader. All rights reserved.
2006-01-20 15:34:02 +00:00
//
#import "SoundController.h"
2006-01-29 14:57:48 +00:00
#import "Status.h"
2006-01-20 15:34:02 +00:00
@implementation SoundController
- (id)initWithDelegate:(id)d
{
DBLog(@"Initializing\n");
self = [super init];
if (self)
{
//things
output = NULL;
bufferChain = NULL;
2006-01-20 15:34:02 +00:00
chainQueue = [[NSMutableArray alloc] init];
delegate = d;
}
return self;
}
2006-04-13 02:51:22 +00:00
- (void)play:(PlaylistEntry *)pe
2006-01-20 15:34:02 +00:00
{
if (output)
{
[output release];
}
output = [[OutputNode alloc] initWithController:self previous:nil];
2006-01-20 15:34:02 +00:00
[output setup];
NSEnumerator *enumerator = [chainQueue objectEnumerator];
id anObject;
while (anObject = [enumerator nextObject])
{
[anObject setShouldContinue:NO];
}
[chainQueue removeAllObjects];
2006-04-13 02:51:22 +00:00
if (bufferChain)
{
[bufferChain setShouldContinue:NO];
2006-05-07 13:19:23 +00:00
[bufferChain release];
}
bufferChain = [[BufferChain alloc] initWithController:self];
2006-04-13 02:51:22 +00:00
while (![bufferChain open:pe])
{
[bufferChain release];
[self requestNextEntry:pe];
pe = nextEntry;
if (pe == nil)
{
return;
}
2006-01-20 15:34:02 +00:00
2006-04-13 02:51:22 +00:00
[self notifySongChanged:pe];
bufferChain = [[BufferChain alloc] initWithController:self];
}
2006-01-20 15:34:02 +00:00
[self setShouldContinue:YES];
DBLog(@"DETACHING THREADS");
[output launchThread];
[bufferChain launchThreads];
2006-01-29 14:57:48 +00:00
[self setPlaybackStatus:kCogStatusPlaying];
2006-01-20 15:34:02 +00:00
}
- (void)stop
{
//Set shouldoContinue to NO on allll things
[self setShouldContinue:NO];
2006-01-29 14:57:48 +00:00
[self setPlaybackStatus:kCogStatusStopped];
2006-01-20 15:34:02 +00:00
}
- (void)pause
{
[output pause];
2006-01-29 14:57:48 +00:00
[self setPlaybackStatus:kCogStatusPaused];
2006-01-20 15:34:02 +00:00
}
- (void)resume
{
[output resume];
2006-01-29 14:57:48 +00:00
[self setPlaybackStatus:kCogStatusPlaying];
2006-01-20 15:34:02 +00:00
}
- (void)seekToTime:(double)time
{
//Need to reset everything's buffers, and then seek?
2006-04-02 20:03:12 +00:00
/*HACK TO TEST HOW WELL THIS WOULD WORK*/
[bufferChain seek:time];
[output seek:time];
/*END HACK*/
2006-01-20 15:34:02 +00:00
}
- (void)setVolume:(double)v
{
[output setVolume:v];
}
2006-04-13 02:51:22 +00:00
- (void)setNextEntry:(PlaylistEntry *)pe
2006-01-20 15:34:02 +00:00
{
2006-04-13 02:51:22 +00:00
[pe retain];
NSLog(@"Releasing: %@", [pe display]);
2006-04-13 02:51:22 +00:00
[nextEntry release];
nextEntry = pe;
2006-01-20 15:34:02 +00:00
}
- (void)setShouldContinue:(BOOL)s
{
[bufferChain setShouldContinue:s];
[output setShouldContinue:s];
}
- (double)amountPlayed
{
return [output amountPlayed];
}
2006-01-20 15:34:02 +00:00
2006-04-13 02:51:22 +00:00
- (void)requestNextEntry:(PlaylistEntry *)pe
{
[delegate performSelectorOnMainThread:@selector(delegateRequestNextEntry:) withObject:pe waitUntilDone:YES];
}
2006-01-20 15:34:02 +00:00
2006-04-13 02:51:22 +00:00
- (void)notifySongChanged:(PlaylistEntry *)pe
{
[delegate performSelectorOnMainThread:@selector(delegateNotifySongChanged:) withObject:pe waitUntilDone:NO];
}
2006-01-20 15:34:02 +00:00
2006-04-13 02:51:22 +00:00
- (void)endOfInputReached:(id)sender
{
BufferChain *newChain = nil;
2006-01-20 15:34:02 +00:00
2006-04-13 02:51:22 +00:00
nextEntry = [sender playlistEntry];
[nextEntry retain];
2006-04-13 02:51:22 +00:00
do {
[newChain release];
[self requestNextEntry:nextEntry];
if (nextEntry == nil)
{
return;
}
newChain = [[BufferChain alloc] initWithController:self];
} while (![newChain open:nextEntry]);
2006-01-20 15:34:02 +00:00
[newChain setShouldContinue:YES];
[newChain launchThreads];
[chainQueue insertObject:newChain atIndex:[chainQueue count]];
[newChain release];
}
- (void)endOfInputPlayed
{
if ([chainQueue count] <= 0)
2006-01-29 14:57:48 +00:00
{
//End of playlist
2006-04-04 01:08:21 +00:00
DBLog(@"STOPPED");
[self stop];
2006-01-29 14:57:48 +00:00
2006-01-20 15:34:02 +00:00
return;
2006-01-29 14:57:48 +00:00
}
2006-01-20 15:34:02 +00:00
// NSLog(@"SWAPPING BUFFERS");
[bufferChain release];
2006-04-04 01:08:21 +00:00
DBLog(@"END OF INPUT PLAYED");
2006-01-20 15:34:02 +00:00
bufferChain = [chainQueue objectAtIndex:0];
[bufferChain retain];
[chainQueue removeObjectAtIndex:0];
2006-04-13 02:51:22 +00:00
[self notifySongChanged:[bufferChain playlistEntry]];
[output setEndOfStream:NO];
2006-01-20 15:34:02 +00:00
}
2006-01-29 14:57:48 +00:00
- (void)setPlaybackStatus:(int)s
{
2006-01-29 14:57:48 +00:00
[delegate performSelectorOnMainThread:@selector(delegateNotifyStatusUpdate:) withObject:[NSNumber numberWithInt:s] waitUntilDone:NO];
}
2006-01-20 15:34:02 +00:00
- (BufferChain *)bufferChain
{
return bufferChain;
}
- (OutputNode *) output
{
return output;
}
@end