cog/PlaybackController.m

289 lines
5.6 KiB
Matlab
Raw Normal View History

2006-01-20 15:41:31 +00:00
#import "PlaybackController.h"
#import "PlaylistView.h"
#import "DBLog.h"
#import "Status.h"
@implementation PlaybackController
- (id)init
{
self = [super init];
if (self)
{
soundController = [[SoundController alloc] initWithDelegate:self];
playbackStatus = kCogStatusStopped;
showTimeRemaining = NO;
}
return self;
}
- (void)awakeFromNib
{
2006-04-04 01:08:21 +00:00
currentVolume = 100.0;
2006-05-13 13:37:32 +00:00
[volumeSlider setDoubleValue:pow(10.0, log10(0.5)/4.0)*[volumeSlider maxValue]];
2006-01-20 15:41:31 +00:00
}
- (IBAction)playPauseResume:(id)sender
{
2006-04-04 01:08:21 +00:00
DBLog(@"PLAYING");
2006-01-20 15:41:31 +00:00
if (playbackStatus == kCogStatusStopped)
[self play:self];
else
[self pauseResume:self];
}
- (IBAction)pauseResume:(id)sender
{
// DBLog(@"Pause/Resume Sent!");
if (playbackStatus == kCogStatusPaused)
[self resume:self];
else
[self pause:self];
}
- (IBAction)pause:(id)sender
{
// DBLog(@"Pause Sent!");
[soundController pause];
}
- (IBAction)resume:(id)sender
{
// DBLog(@"Resume Sent!");
[soundController resume];
}
- (IBAction)stop:(id)sender
{
// DBLog(@"Stop Sent!");
[soundController stop];
}
//called by double-clicking on table
- (void)playEntryAtIndex:(int)i
{
PlaylistEntry *pe = [[playlistController arrangedObjects] objectAtIndex:i];
[self playEntry:pe];
}
- (IBAction)play:(id)sender
{
if ([playlistView selectedRow] == -1)
[playlistView selectRow:0 byExtendingSelection:NO];
[self playEntryAtIndex:[playlistView selectedRow]];
}
- (void)playEntry:(PlaylistEntry *)pe;
{
// DBLog(@"PlayEntry: %@ Sent!", [pe filename]);
2006-01-29 14:57:48 +00:00
if (playbackStatus != kCogStatusStopped)
[self stop:self];
[playlistController setCurrentEntry:pe];
[positionSlider setDoubleValue:0.0f];
[self updateTimeField:0.0f];
2006-04-13 02:51:22 +00:00
[soundController play:pe];
2006-04-04 01:08:21 +00:00
[soundController setVolume:currentVolume];
2006-01-20 15:41:31 +00:00
}
- (IBAction)next:(id)sender
{
2006-04-04 01:08:21 +00:00
DBLog(@"CALLING: %i %i", playbackStatus, kCogStatusStopped);
2006-01-29 14:57:48 +00:00
if ([playlistController next] == NO)
2006-01-20 15:41:31 +00:00
return;
2006-01-29 14:57:48 +00:00
if (playbackStatus != kCogStatusStopped)
{
2006-04-04 01:08:21 +00:00
DBLog(@"STOPPING");
2006-01-29 14:57:48 +00:00
[self stop:self];
[self playEntry:[playlistController currentEntry]];
}
2006-01-20 15:41:31 +00:00
}
- (IBAction)prev:(id)sender
{
2006-04-04 01:08:21 +00:00
DBLog(@"CALLING");
2006-01-20 15:41:31 +00:00
if ([playlistController prev] == nil)
return;
2006-01-29 14:57:48 +00:00
if (playbackStatus != kCogStatusStopped)
{
[self stop:self];
[self playEntry:[playlistController currentEntry]];
}
2006-01-20 15:41:31 +00:00
}
- (IBAction)seek:(id)sender
{
// DBLog(@"SEEKING?");
double time;
time = [positionSlider doubleValue];
2006-04-18 17:00:29 +00:00
2006-04-04 01:08:21 +00:00
if ([sender tracking] == NO) // check if user stopped sliding before playing audio
2006-01-20 15:41:31 +00:00
[soundController seekToTime:time];
[self updateTimeField:time];
}
- (void)changePlayButtonImage:(NSString *)name
{
NSImage *img = [NSImage imageNamed:[name stringByAppendingString:@"_gray"]];
NSImage *alt = [NSImage imageNamed:[name stringByAppendingString:@"_blue"]];
[img retain];
[alt retain];
if (img == nil)
{
DBLog(@"NIL IMAGE!!!");
}
if (alt == nil)
{
DBLog(@"NIL ALT");
}
[playButton setImage:img];
[playButton setAlternateImage:alt];
}
- (IBAction)changeVolume:(id)sender
{
2006-05-13 13:37:32 +00:00
float percent;
2006-04-18 17:00:29 +00:00
2006-05-13 13:37:32 +00:00
//Approximated log
percent = (float)[sender doubleValue]/[sender maxValue];
percent = percent * percent * percent * percent;
//gravitates at the 100% mark
float v = [sender frame].size.width - ([sender frame].size.width*(percent*[sender maxValue])/100.0);
2006-04-18 17:00:29 +00:00
if (fabs(v) < 10.0)
{
2006-05-13 13:37:32 +00:00
percent = 0.5;
v = pow(10.0, log10(percent)/4.0);
[sender setDoubleValue:v*[sender maxValue]];
2006-04-18 17:00:29 +00:00
}
2006-05-13 13:37:32 +00:00
currentVolume = percent * [sender maxValue];
2006-04-04 01:08:21 +00:00
[soundController setVolume:currentVolume];
2006-01-20 15:41:31 +00:00
}
- (void)updateTimeField:(double)pos
{
NSString *text;
if (showTimeRemaining == NO)
{
int sec = (int)(pos/1000.0);
text = [NSString stringWithFormat:NSLocalizedString(@"TimeElapsed", @""), sec/60, sec%60];
}
else
{
int sec = (int)(([positionSlider maxValue] - pos)/1000.0);
2006-05-13 16:50:52 +00:00
if (sec < 0)
sec = 0;
2006-01-20 15:41:31 +00:00
text = [NSString stringWithFormat:NSLocalizedString(@"TimeRemaining", @""), sec/60, sec%60];
}
[timeField setStringValue:text];
}
- (IBAction)toggleShowTimeRemaining:(id)sender
{
showTimeRemaining = !showTimeRemaining;
[self updateTimeField:[positionSlider doubleValue]];
}
2006-04-13 02:51:22 +00:00
- (void)delegateRequestNextEntry:(PlaylistEntry *)curEntry
2006-01-20 15:41:31 +00:00
{
PlaylistEntry *pe;
2006-05-12 18:12:31 +00:00
if ([playlistController shuffle] == YES)
{
pe = [playlistController entryAtIndex:[curEntry shuffleIndex]+1];
}
else
{
pe = [playlistController entryAtIndex:[curEntry index]+1];
}
2006-01-20 15:41:31 +00:00
if (pe == nil)
2006-04-13 02:51:22 +00:00
[soundController setNextEntry:nil];
2006-01-20 15:41:31 +00:00
else
{
2006-04-04 01:08:21 +00:00
DBLog(@"NEXT SONG: %@", [pe filename]);
2006-04-13 02:51:22 +00:00
[soundController setNextEntry:pe];
}
2006-01-20 15:41:31 +00:00
}
2006-04-13 02:51:22 +00:00
- (void)delegateNotifySongChanged:(PlaylistEntry *)pe
2006-01-20 15:41:31 +00:00
{
2006-04-13 02:51:22 +00:00
[playlistController setCurrentEntry:pe];
2006-01-20 15:41:31 +00:00
[positionSlider setDoubleValue:0.0f];
2006-01-20 15:41:31 +00:00
[self updateTimeField:0.0f];
2006-01-20 15:41:31 +00:00
}
- (void)delegateNotifyBitrateUpdate:(float)bitrate
{
// [bitrateField setIntValue:bitrate];
}
- (void)updatePosition:(id)sender
2006-01-20 15:41:31 +00:00
{
double pos = [soundController amountPlayed];
2006-01-20 15:41:31 +00:00
if ([positionSlider tracking] == NO)
{
// DBLog(@"Received pos update: %f", pos);
[positionSlider setDoubleValue:pos];
[self updateTimeField:pos];
}
}
2006-01-29 14:57:48 +00:00
- (void)delegateNotifyStatusUpdate:(NSNumber *)s
2006-01-20 15:41:31 +00:00
{
2006-01-29 14:57:48 +00:00
int status = [s intValue];
2006-01-20 15:41:31 +00:00
if (status == kCogStatusStopped || status == kCogStatusPaused)
{
2006-04-04 01:08:21 +00:00
DBLog(@"INVALIDATING");
if (positionTimer)
{
[positionTimer invalidate];
positionTimer = NULL;
}
if (status == kCogStatusStopped)
{
[positionSlider setDoubleValue:0.0f];
[self updateTimeField:0.0f];
}
2006-01-20 15:41:31 +00:00
//Show play image
[self changePlayButtonImage:@"play"];
}
else if (status == kCogStatusPlaying)
{
if (!positionTimer)
positionTimer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(updatePosition:) userInfo:nil repeats:YES];
2006-01-20 15:41:31 +00:00
//Show pause
[self changePlayButtonImage:@"pause"];
}
playbackStatus = status;
}
@end