cog/Application/PlaybackController.m

586 lines
12 KiB
Matlab
Raw Normal View History

2006-01-20 15:41:31 +00:00
#import "PlaybackController.h"
#import "PlaylistView.h"
#import <Foundation/NSTimer.h>
#import "CogAudio/Status.h"
#import "CogAudio/Helper.h"
2006-01-20 15:41:31 +00:00
#import "PlaylistController.h"
#import "PlaylistEntry.h"
2006-01-20 15:41:31 +00:00
@implementation PlaybackController
#define DEFAULT_SEEK 10
2006-01-20 15:41:31 +00:00
- (id)init
{
self = [super init];
if (self)
{
2007-02-26 05:26:48 +00:00
[self initDefaults];
audioPlayer = [[AudioPlayer alloc] init];
[audioPlayer setDelegate:self];
2006-01-20 15:41:31 +00:00
playbackStatus = kCogStatusStopped;
showTimeRemaining = NO;
scrobbler = [[AudioScrobbler alloc] init];
[GrowlApplicationBridge setGrowlDelegate:self];
2006-01-20 15:41:31 +00:00
}
return self;
}
2007-02-26 05:26:48 +00:00
- (void)initDefaults
{
NSDictionary *defaultsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], @"enableAudioScrobbler",
[NSNumber numberWithBool:NO], @"automaticallyLaunchLastFM",
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDictionary];
}
- (NSDictionary *) registrationDictionaryForGrowl
{
NSArray *notifications = [NSArray arrayWithObjects:@"Stream Changed", nil];
return [NSDictionary dictionaryWithObjectsAndKeys:
@"Cog", GROWL_APP_NAME,
notifications, GROWL_NOTIFICATIONS_ALL,
notifications, GROWL_NOTIFICATIONS_DEFAULT,
nil];
}
2006-01-20 15:41:31 +00:00
- (void)awakeFromNib
{
[volumeSlider setDoubleValue:logarithmicToLinear(100.0)];
[audioPlayer setVolume: 100];
[positionSlider setEnabled:NO];
2006-01-20 15:41:31 +00:00
}
2006-01-20 15:41:31 +00:00
- (IBAction)playPauseResume:(id)sender
{
if (playbackStatus == kCogStatusStopped)
[self play:self];
else
[self pauseResume:self];
}
- (IBAction)pauseResume:(id)sender
{
if (playbackStatus == kCogStatusPaused)
[self resume:self];
else
[self pause:self];
}
- (IBAction)pause:(id)sender
{
[audioPlayer pause];
playbackStatus = kCogStatusPaused;
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
[scrobbler pause];
}
2006-01-20 15:41:31 +00:00
}
- (IBAction)resume:(id)sender
{
[audioPlayer resume];
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
[scrobbler resume];
}
2006-01-20 15:41:31 +00:00
}
- (IBAction)stop:(id)sender
{
[audioPlayer stop];
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
[scrobbler stop];
}
2006-01-20 15:41:31 +00:00
}
//called by double-clicking on table
- (void)playEntryAtIndex:(int)i
{
PlaylistEntry *pe = [[playlistController arrangedObjects] objectAtIndex:i];
[self playEntry:pe];
}
2007-11-01 01:53:52 +00:00
- (IBAction)playbackButtonClick:(id)sender
{
int clickedSegment = [sender selectedSegment];
if (clickedSegment == 0) //Previous
{
[sender setSelected:YES forSegment:0];
[sender setSelected:YES forSegment:1];
[self prev:sender];
}
else if (clickedSegment == 1) //Play
{
[self playPauseResume:sender];
}
else if (clickedSegment == 2) //Next
{
[self next:sender];
}
}
2006-01-20 15:41:31 +00:00
- (IBAction)play:(id)sender
{
if ([playlistView selectedRow] == -1)
[playlistView selectRow:0 byExtendingSelection:NO];
[self playEntryAtIndex:[playlistView selectedRow]];
}
- (void)playEntry:(PlaylistEntry *)pe
2006-01-20 15:41:31 +00:00
{
2006-01-29 14:57:48 +00:00
if (playbackStatus != kCogStatusStopped)
[self stop:self];
[playlistController setCurrentEntry:pe];
[positionSlider setDoubleValue:0.0];
[self updateTimeField:0.0f];
2008-02-20 00:44:40 +00:00
[audioPlayer play:[pe URL] withUserInfo:pe];
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
[scrobbler start:pe];
}
[GrowlApplicationBridge notifyWithTitle:[pe title]
description:[pe artist]
notificationName:@"Stream Changed"
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
2006-01-20 15:41:31 +00:00
}
- (IBAction)next:(id)sender
{
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
[self stop:self];
[self playEntry:[playlistController currentEntry]];
2006-01-20 15:41:31 +00:00
}
- (IBAction)prev:(id)sender
{
if ([playlistController prev] == NO)
2006-01-20 15:41:31 +00:00
return;
[self stop:self];
[self playEntry:[playlistController currentEntry]];
2006-01-20 15:41:31 +00:00
}
- (IBAction)seek:(id)sender
{
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
[audioPlayer seekToTime:time];
2006-01-20 15:41:31 +00:00
[self updateTimeField:time];
}
- (IBAction)eventSeekForward:(id)sender
{
[self seekForward:DEFAULT_SEEK];
}
- (void)seekForward:(double)amount
{
double seekTo = [audioPlayer amountPlayed] + amount;
if (seekTo > (int)[positionSlider maxValue])
{
[self next:self];
}
else
{
[audioPlayer seekToTime:seekTo];
[self updateTimeField:seekTo];
[positionSlider setDoubleValue:seekTo];
}
}
- (IBAction)eventSeekBackward:(id)sender
{
[self seekBackward:DEFAULT_SEEK];
}
- (void)seekBackward:(double)amount
{
double seekTo = [audioPlayer amountPlayed] - amount;
if (seekTo < 0)
seekTo = 0;
[audioPlayer seekToTime:seekTo];
[self updateTimeField:seekTo];
[positionSlider setDoubleValue:seekTo];
}
2006-01-20 15:41:31 +00:00
- (void)changePlayButtonImage:(NSString *)name
{
2007-11-01 01:53:52 +00:00
NSImage *img = [NSImage imageNamed:name];
// [img retain];
2006-01-20 15:41:31 +00:00
if (img == nil)
{
2007-07-11 01:20:32 +00:00
NSLog(@"Error loading image!");
2006-01-20 15:41:31 +00:00
}
2007-11-01 01:53:52 +00:00
[playbackButtons setImage:img forSegment:1];
2006-01-20 15:41:31 +00:00
}
- (IBAction)changeVolume:(id)sender
{
double oneLog = logarithmicToLinear(100.0);
double distance = [sender frame].size.height*([sender doubleValue] - oneLog)/100.0;
if (fabs(distance) < 2.0)
2006-04-18 17:00:29 +00:00
{
[sender setDoubleValue:oneLog];
2006-04-18 17:00:29 +00:00
}
NSLog(@"VOLUME: %lf, %lf", [sender doubleValue], linearToLogarithmic([sender doubleValue]));
[audioPlayer setVolume:linearToLogarithmic([sender doubleValue])];
2006-01-20 15:41:31 +00:00
}
/* selector for NSTimer - gets passed the Timer object itself
and the appropriate userInfo, which in this case is an NSNumber
containing the current volume before we start fading. */
- (void)audioFadeDown:(NSTimer *)audioTimer
{
double volume = [audioPlayer volume];
double originalVolume = [[audioTimer userInfo] doubleValue];
double down = originalVolume/10;
NSLog(@"VOLUME IS %lf", volume);
if (volume > 0.0001) //YAY! Roundoff error!
{
[audioPlayer volumeDown:down];
}
else // volume is at 0 or below, we are ready to release the timer and move on
{
[audioPlayer pause];
[audioPlayer setVolume:originalVolume];
[volumeSlider setDoubleValue: logarithmicToLinear(originalVolume)];
[audioTimer invalidate];
}
}
- (void)audioFadeUp:(NSTimer *)audioTimer
{
double volume = [audioPlayer volume];
double originalVolume = [[audioTimer userInfo] doubleValue];
double up = originalVolume/10;
NSLog(@"VOLUME IS %lf", volume);
if (volume < originalVolume)
{
if ((volume + up) > originalVolume)
[audioPlayer volumeUp:(originalVolume - volume)];
else
[audioPlayer volumeUp:up];
}
else // volume is at 0 or below, we are ready to release the timer and move on
{
[volumeSlider setDoubleValue: logarithmicToLinear(originalVolume)];
[audioTimer invalidate];
}
}
- (IBAction)fade:(id)sender withTime:(double)time
{
// we can not allow multiple fade timers to be registered
if (playbackStatus == kCogStatusFading)
return;
NSNumber *originalVolume = [NSNumber numberWithDouble: [audioPlayer volume]];
NSTimer *fadeTimer;
if (playbackStatus == kCogStatusPlaying)
fadeTimer = [NSTimer scheduledTimerWithTimeInterval:time
target:self
selector:@selector(audioFadeDown:)
userInfo:originalVolume
repeats:YES];
else
{
[audioPlayer setVolume:0];
fadeTimer = [NSTimer scheduledTimerWithTimeInterval:time
target:self
selector:@selector(audioFadeUp:)
userInfo:originalVolume
repeats:YES];
[self pauseResume:self];
}
playbackStatus = kCogStatusFading;
}
- (IBAction)skipToNextAlbum:(id)sender
{
BOOL found = NO;
NSNumber *index = (NSNumber *)[[playlistController currentEntry] index];
2008-02-20 00:12:25 +00:00
NSString *origAlbum = [[playlistController currentEntry] album];
int playlistLength = [[playlistController arrangedObjects] count] - 1;
int i = [index intValue] + 1;
NSString *curAlbum;
PlaylistEntry *pe;
while (!found)
{
pe = [[playlistController arrangedObjects] objectAtIndex:i];
curAlbum = [pe album];
// check for untagged files, and just play the first untagged one
// we come across
if (curAlbum == nil)
break;
if (![curAlbum caseInsensitiveCompare:origAlbum])
{
i++;
if (i > playlistLength)
return;
continue;
}
else
{
found = YES;
}
}
[self playEntryAtIndex:i];
}
- (IBAction)skipToPreviousAlbum:(id)sender
{
BOOL found = NO;
BOOL foundAlbum = NO;
NSNumber *index = (NSNumber *)[[playlistController currentEntry] index];
2008-02-20 00:12:25 +00:00
NSString *origAlbum = [[playlistController currentEntry] album];
NSString *curAlbum;
int i = [index intValue] - 1;
if (i <= 0)
return;
PlaylistEntry *pe;
while (!found)
{
pe = [[playlistController arrangedObjects] objectAtIndex:i];
curAlbum = [pe album];
if (curAlbum == nil)
break;
if (![curAlbum caseInsensitiveCompare:origAlbum])
{
i--;
if (i == 0) // first entry in playlist
if (foundAlbum == YES)
break;
else
return;
continue;
}
else
{
if (foundAlbum == NO)
{
foundAlbum = YES;
// now we need to move up to the first song in the album, so we'll
// go till we either find index 0, or the first song in the album
2008-02-20 00:12:25 +00:00
origAlbum = [[[playlistController arrangedObjects] objectAtIndex:i] album];
i--;
}
else
found = YES; // terminate loop
}
}
if ((foundAlbum == YES) && i != 0)
i++;
[self playEntryAtIndex:i];
}
- (IBAction)volumeDown:(id)sender
{
double newVolume;
newVolume = [audioPlayer volumeDown:DEFAULT_VOLUME_DOWN];
[volumeSlider setDoubleValue:logarithmicToLinear(newVolume)];
}
- (IBAction)volumeUp:(id)sender
{
double newVolume;
newVolume = [audioPlayer volumeUp:DEFAULT_VOLUME_UP];
[volumeSlider setDoubleValue:logarithmicToLinear(newVolume)];
}
2006-01-20 15:41:31 +00:00
- (void)updateTimeField:(double)pos
{
NSString *text;
if (showTimeRemaining == NO)
{
int sec = (int)(pos);
2006-01-20 15:41:31 +00:00
text = [NSString stringWithFormat:NSLocalizedString(@"TimeElapsed", @""), sec/60, sec%60];
}
else
{
int sec = (int)(([positionSlider maxValue] - pos));
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]];
}
- (void)audioPlayer:(AudioPlayer *)player requestNextStream:(id)userInfo
2006-01-20 15:41:31 +00:00
{
PlaylistEntry *curEntry = (PlaylistEntry *)userInfo;
2008-02-19 23:49:51 +00:00
PlaylistEntry *pe = [playlistController getNextEntry:curEntry];
2006-05-12 18:12:31 +00:00
2008-02-20 00:44:40 +00:00
[player setNextStream:[pe URL] withUserInfo:pe];
2006-01-20 15:41:31 +00:00
}
- (void)audioPlayer:(AudioPlayer *)player streamChanged:(id)userInfo
2006-01-20 15:41:31 +00:00
{
PlaylistEntry *pe = (PlaylistEntry *)userInfo;
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
if([[NSUserDefaults standardUserDefaults] boolForKey:@"enableAudioScrobbler"]) {
[scrobbler start:pe];
}
[GrowlApplicationBridge notifyWithTitle:[pe title]
description:[pe artist]
notificationName:@"Stream Changed"
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
2006-01-20 15:41:31 +00:00
}
- (void)updatePosition:(id)sender
2006-01-20 15:41:31 +00:00
{
double pos = [audioPlayer amountPlayed];
2006-01-20 15:41:31 +00:00
if ([positionSlider tracking] == NO)
{
[positionSlider setDoubleValue:pos];
[self updateTimeField:pos];
}
}
- (void)audioPlayer:(AudioPlayer *)player statusChanged:(id)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)
{
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"];
}
2007-06-03 17:09:07 +00:00
if (status == kCogStatusStopped) {
[positionSlider setEnabled:NO];
}
else {
[positionSlider setEnabled:[[[playlistController currentEntry] seekable] boolValue]];
}
2006-01-20 15:41:31 +00:00
playbackStatus = status;
}
-(BOOL)validateMenuItem:(NSMenuItem*)menuItem
{
SEL action = [menuItem action];
if (action == @selector(seekBackward:) && (playbackStatus == kCogStatusStopped))
return NO;
if (action == @selector(seekForward:) && (playbackStatus == kCogStatusStopped))
return NO;
if (action == @selector(stop:) && (playbackStatus == kCogStatusStopped))
return NO;
return YES;
}
2006-01-20 15:41:31 +00:00
@end