cog/Sound/SoundController.m

186 lines
3.2 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 Zaphod Beeblebrox on 8/7/05.
// Copyright 2005 __MyCompanyName__. All rights reserved.
//
#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;
}
- (void)play:(NSString *)filename
{
DBLog(@"OPENING FILE: %s\n", filename);
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];
if (bufferChain)
{
[bufferChain setShouldContinue:NO];
[bufferChain release];
}
bufferChain = [[BufferChain alloc] initWithController:self];
2006-01-20 15:34:02 +00:00
[bufferChain open:filename];
[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];
}
- (void)setNextSong:(NSString *)s
{
//Need to lock things, and set it...this may be calling from any threads...also, if its nil then that signals end of playlist
[s retain];
[nextSong release];
nextSong = s;
}
- (void)setShouldContinue:(BOOL)s
{
[bufferChain setShouldContinue:s];
[output setShouldContinue:s];
}
- (double)amountPlayed
{
return [output amountPlayed];
}
2006-01-20 15:34:02 +00:00
- (void)endOfInputReached
{
[delegate delegateRequestNextSong:[chainQueue count]];
2006-04-04 01:08:21 +00:00
DBLog(@"END OF INPUT REACHED");
2006-01-20 15:34:02 +00:00
if (nextSong == nil)
return;
BufferChain *newChain = [[BufferChain alloc] initWithController:self];
[newChain open:nextSong];
[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];
[delegate delegateNotifySongChanged];
[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