-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.m
724 lines (617 loc) · 18.3 KB
/
Player.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/* vim: set ft=objc ts=4 sw=4 expandtab nowrap: */
/*
* Player.m
*
* Copyright (c) 1999 - 2003, 2015
*
* Author: ACKyugo <[email protected]>
* Andreas Schik <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#import <Cynthiune/Output.h>
#import <AppKit/NSImageView.h>
#include "Player.h"
#include "TrackList.h"
#include "BundleManager.h"
#include "GeneralView.h"
#include "SliderCell.h"
#define AUDIOCD_PLAYING 0
#define AUDIOCD_PAUSED 1
#define AUDIOCD_STOPPED 2
static BOOL mustReadTOC = NO;
static Player *sharedPlayer = nil;
@interface Player (Private)
- (BOOL) loadBundles;
- (void) ensureOutput;
- (BOOL) reInitOutputIfNeeded;
- (void) playLoopIteration;
- (void) setCurrentTrack: (int) current;
#ifdef MUSICBRAINZ
- (void) displayCoverArt;
#endif
@end
@implementation Player (Private)
- (BOOL) loadBundles
{
int i;
NSString *path;
NSArray *searchPaths;
BundleManager *bundleManager;
// try to load the AudioCD bundle
searchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask|NSLocalDomainMask|NSSystemDomainMask, YES);
for (i = 0; i < [searchPaths count]; i++) {
NSBundle *bundle;
NSString *bundlePath = [NSString stringWithFormat:
@"%@/Bundles/AudioCD.bundle",
[searchPaths objectAtIndex: i]];
bundle = [NSBundle bundleWithPath: bundlePath];
if (bundle) {
audiocdClass = [bundle principalClass];
if (audiocdClass) {
break;
}
}
} // for (i = 0; i < [searchPaths count]; i++)
if (!audiocdClass) {
NSRunAlertPanel(@"CDPlayer",
_(@"Couldn't find AudioCD bundle."),
_(@"Exit"), nil, nil);
exit(-1);
}
bundleManager = [BundleManager bundleManager];
[bundleManager loadBundles];
path = [[NSUserDefaults standardUserDefaults] stringForKey: @"Device"];
drive = [[audiocdClass alloc] initWithHandler: self];
if (nil != volume) {
[drive setVolumeLevel: [volume floatValue]];
}
[drive startPollingWithPreferredDevice: path];
return YES;
}
- (BOOL) reInitOutputIfNeeded
{
// CDDA is always 2 ch with 44100 Hz smaple rate
if (![output prepareDeviceWithChannels: 2
andRate: 44100
withEndianness: LittleEndian]) {
NSLog (@"error preparing output for 2 channels at a rate of 44100");
return NO;
}
if (NO == [output openDevice]) {
NSLog (@"error opening output device");
return NO;
}
return YES;
}
- (void) ensureOutput
{
GeneralView *gv;
Class outputClass;
gv = [GeneralView singleInstance];
outputClass = [gv preferredOutputClass];
if (output && [output class] != outputClass) {
[output closeDevice];
[output release];
output = nil;
}
if (!output) {
outputIsThreaded = [outputClass isThreaded];
output = [outputClass new];
[output setParentPlayer: self];
if (![self reInitOutputIfNeeded]) {
[output release];
output = nil;
}
}
}
- (void) playLoopIteration
{
unsigned char buffer[DEFAULT_BUFFER_SIZE];
int size;
size = [drive readNextChunk: buffer withSize: DEFAULT_BUFFER_SIZE];
if (size > 0) {
NSData *streamChunk = [NSData dataWithBytes: buffer length: size];
[output playChunk: streamChunk];
} else {
// If no data can be read, we assume that playing has stopped
if (doRepeat) {
[self setCurrentTrack: 1];
} else {
[self stop: self];
}
}
}
- (void) setCurrentTrack: (int) current
{
BOOL restart = NO;
if (currentState == AUDIOCD_PLAYING) {
restart = YES;
[self pause: self];
}
if((currentState == AUDIOCD_PLAYING) ||
(currentState == AUDIOCD_PAUSED)) {
[drive seek: current];
currentTrack = current;
}
if (YES == restart) {
[self play: self];
}
}
#ifdef MUSICBRAINZ
- (void) displayCoverArt
{
NSImage *image = [[TrackList sharedTrackList] getCoverArtFromCache];
if (nil == image) {
[window setMiniwindowImage: [NSApp applicationIconImage]];
NSString *path = [[NSBundle mainBundle] pathForResource: @"disc" ofType: @"tiff"];
image = [[[NSImage alloc] initWithContentsOfFile: path] autorelease];
} else {
NSImage *imgCopy = [image copy];
[imgCopy setSize: NSMakeSize(48,48)];
[window setMiniwindowImage: imgCopy];
RELEASE(imgCopy);
}
[coverArt setImage: image];
}
#endif
@end
@implementation Player
- init
{
[self initWithNibName: @"Player"];
return self;
}
- (id) initWithNibName: (NSString *) nibName;
{
if (sharedPlayer) {
[self dealloc];
} else {
self = [super init];
if (![NSBundle loadNibNamed: nibName owner: self]) {
NSLog (@"Could not load nib \"%@\".", nibName);
} else {
sharedPlayer = self;
currentTrack = 1;
drive = nil;
autoPlay = NO;
currentState = AUDIOCD_STOPPED;
output = nil;
outputIsThreaded = NO;
closingThread = NO;
togglePlayButton = NO;
togglePauseButton = NO;
// we must already create the (hidden) track list
[TrackList sharedTrackList];
if (![self loadBundles]) {
[self release];
return nil;
}
timer = [NSTimer scheduledTimerWithTimeInterval: 1
target: self
selector: @selector(timer:)
userInfo: self
repeats: YES];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(playTrack:)
name: @"PlayTrack"
object: nil];
[window makeKeyAndOrderFront: self];
[window setFrameAutosaveName: @"CDPlayerWindow"];
[window setFrameUsingName: @"CDPlayerWindow"];
}
}
return sharedPlayer;
}
- (void) awakeFromNib
{
// For some reason creating the volume slider within the Gorm
// bundle did not work. Thus we set up the volume control, here.
NSRect frame = NSMakeRect(26, 10, 280, 16);
volume = [[NSSlider alloc] initWithFrame: frame];
NSCell *cell = [SliderCell new];
[cell setBordered: NO];
[cell setBezeled: NO];
[volume setCell: cell];
[volume setEnabled: YES];
[volume setMinValue: 0.0f];
[volume setMaxValue: 1.0f];
[volume setFloatValue: 0.75f];
[volume setContinuous: NO];
[volume setTarget: self];
[volume setAction: @selector(setVolume:)];
if (nil != drive) {
[drive setVolumeLevel: [volume floatValue]];
}
[volume setToolTip: [NSString stringWithFormat: @"Volume: %f", [volume floatValue]]];
[[window contentView] addSubview: volume];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self
name: @"PlayTrack"
object: nil];
[drive release];
[timer invalidate];
[timer release];
[super dealloc];
}
/**
* This method is called from threaded outputs only.
*/
- (int) readNextChunk: (unsigned char *) buffer
withSize: (unsigned int) bufferSize
{
int inputSize;
if (!closingThread) {
inputSize = [drive readNextChunk: buffer withSize: bufferSize];
if (inputSize <= 0) {
inputSize = 0;
// If no data can be read, we assume that playing has stopped
if (doRepeat) {
[self setCurrentTrack: 1];
} else {
[self stop: self];
}
}
} else {
inputSize = 0;
}
return inputSize;
}
- (void) chunkFinishedPlaying
{
if (currentState == AUDIOCD_PLAYING) {
[self playLoopIteration];
}
}
//
//
//
- (void) timer: (id) timer
{
int min, sec;
// This must be done in any case. If the user removes the CD,
// we must read the TOC and afterwards clear the display.
if (mustReadTOC) {
mustReadTOC = NO;
[[TrackList sharedTrackList] setTOC: [drive readTOC]];
[cdLabel setStringValue: [[TrackList sharedTrackList] cdTitle]];
#ifdef MUSICBRAINZ
[self displayCoverArt];
#endif
}
// is a disc in the drive?
if ([drive cdPresent] == NO) {
if (AUDIOCD_STOPPED != currentState) {
currentState = AUDIOCD_STOPPED;
[[TrackList sharedTrackList] setPlaysTrack: -1 andNotify: NO];
}
return;
}
if (autoPlay) {
autoPlay = NO;
currentTrack = 1;
min = sec = 0;
[self play: self];
return;
}
currentTrack = 1;
min = sec = 0;
if (currentState == AUDIOCD_PLAYING) {
currentTrack = [drive currentTrack];
min = [drive currentMin];
sec = [drive currentSec];
}
if (currentState != AUDIOCD_PAUSED) {
if (currentState == AUDIOCD_PLAYING) {
NSString *time = [NSString stringWithFormat: @"%02d:%02d", min, sec];
[timeLabel setStringValue: time];
[trackLabel setStringValue: [[TrackList sharedTrackList] trackTitle: currentTrack]];
} else {
[trackLabel setStringValue: @""];
[timeLabel setStringValue: @""];
}
[[TrackList sharedTrackList] setPlaysTrack: currentTrack andNotify: (currentState == AUDIOCD_PLAYING)];
}
if (YES == togglePlayButton) {
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = nil;
NSImage *image;
if (currentState == AUDIOCD_STOPPED) {
path = [bundle pathForResource: @"stop_on" ofType: @"tiff"];
} else {
path = [bundle pathForResource: @"stop" ofType: @"tiff"];
}
image = [[[NSImage alloc] initWithContentsOfFile: path] autorelease];
[stop setImage: image];
if (currentState == AUDIOCD_STOPPED) {
path = [bundle pathForResource: @"play" ofType: @"tiff"];
} else {
path = [bundle pathForResource: @"play_on" ofType: @"tiff"];
}
image = [[[NSImage alloc] initWithContentsOfFile: path] autorelease];
[play setImage: image];
togglePlayButton = NO;
}
if (YES == togglePauseButton) {
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = nil;
NSImage *image;
if (currentState == AUDIOCD_PAUSED) {
path = [bundle pathForResource: @"pause_on" ofType: @"tiff"];
} else {
path = [bundle pathForResource: @"pause" ofType: @"tiff"];
}
image = [[[NSImage alloc] initWithContentsOfFile: path] autorelease];
[pause setImage: image];
if (currentState == AUDIOCD_PLAYING) {
path = [bundle pathForResource: @"play_on" ofType: @"tiff"];
} else {
path = [bundle pathForResource: @"play" ofType: @"tiff"];
}
image = [[[NSImage alloc] initWithContentsOfFile: path] autorelease];
[play setImage: image];
togglePauseButton = NO;
}
}
//
//
//
- (void) showTrackList: (id)sender
{
[[TrackList sharedTrackList] activate];
}
- (void) playTrack: (NSNotification *) not
{
int nextTrack = [[[not userInfo] objectForKey: @"Track"] intValue];
currentTrack = nextTrack;
if (currentState == AUDIOCD_PLAYING) {
[self pause: self];
}
[drive seek: currentTrack];
[self play: self];
}
- (void) play: (id) sender
{
if([drive cdPresent] == NO) {
return;
}
[self ensureOutput];
// Here, we either have an opened and initialized output
// device or we have nothing
if (nil == output) {
return;
}
if (AUDIOCD_PLAYING != currentState) {
if (AUDIOCD_STOPPED == currentState) {
[drive seek: currentTrack];
}
[drive start];
if (outputIsThreaded) {
closingThread = NO;
[output startThread];
} else {
[self playLoopIteration];
}
togglePlayButton = YES;
togglePauseButton = YES;
currentState = AUDIOCD_PLAYING;
}
}
- (void) pause: (id) sender
{
if ([drive cdPresent] == NO) {
return;
}
if (currentState == AUDIOCD_PLAYING) {
if (outputIsThreaded) {
closingThread = YES;
[output stopThread];
}
[drive stop];
currentState = AUDIOCD_PAUSED;
} else if (currentState == AUDIOCD_PAUSED) {
[self play: self];
}
togglePauseButton = YES;
}
- (void) stop: (id) sender
{
if (AUDIOCD_PLAYING == currentState) {
if (outputIsThreaded) {
closingThread = YES;
[output stopThread];
}
[drive stop];
}
currentState = AUDIOCD_STOPPED;
currentTrack = 1;
togglePlayButton = YES;
togglePauseButton = YES;
}
- (void) eject: (id) sender
{
if([drive cdPresent] == NO) {
return;
}
[drive eject];
}
- (void) repeat: (id) sender
{
doRepeat = !doRepeat;
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = nil;
NSImage *image;
if (doRepeat) {
path = [bundle pathForResource: @"repeat_on" ofType: @"tiff"];
} else {
path = [bundle pathForResource: @"repeat" ofType: @"tiff"];
}
image = [[[NSImage alloc] initWithContentsOfFile: path] autorelease];
[repeat setImage: image];
}
- (void) setVolume: (id) sender
{
[drive setVolumeLevel: [volume floatValue]];
[volume setToolTip: [NSString stringWithFormat: @"Volume: %f", [volume floatValue]]];
}
- (void) next: (id) sender
{
int current = currentTrack;
if([drive cdPresent] == NO) {
return;
}
current++;
if(current > [drive totalTrack]) {
current = 1;
}
[self setCurrentTrack: current];
}
- (void) prev: (id) sender
{
int current = currentTrack;
if([drive cdPresent] == NO) {
return;
}
// We jump back only if we are not playing at the moment
// or if we are at the very beginning of a playing track.
// The latter condition allows to jump back to the beginning
// of the current track before jumping back one more track.
if ((currentState == AUDIOCD_STOPPED) ||
([drive currentSec] == 0 && [drive currentMin] == 0)) {
current--;
if(current < 1) {
current = [drive totalTrack];
}
}
[self setCurrentTrack: current];
}
//
//
//
//
- (BOOL) audioCD: (id) sender error: (int) no message: (NSString *) msg
{
// FIXME: Take appropriate action when an error occurs!!
// NSRunAlertPanel(@"CDPlayer",
// msg,
// _(@"OK"), nil, nil);
NSLog(@"CDPlayer error: %@", msg);
return YES;
}
- (void) audioCDChanged: (id) sender
{
mustReadTOC = YES;
}
- (BOOL) windowShouldClose: (id) sender
{
[[NSApplication sharedApplication ] terminate: self];
return YES;
}
//
// services methods
//
- (void) getTOC: (NSPasteboard *) pboard
userData: (NSString *) userData
error: (NSString **) error
{
TrackList *tl = [TrackList sharedTrackList];
int i, rows = [tl numberOfTracksInTOC];
NSMutableArray *array;
/*
* If we don't have any rows, there is probably no CD.
*/
if (rows == 0) {
*error = _(@"No Audio CD found.");
return;
}
array = [[NSMutableArray alloc] init];
for (i = 0; i < rows; i++) {
[array addObject: [NSNumber numberWithInt: i]];
}
/*
* This is a small hack, but we can clean this up later.
*/
if (![tl tableView: nil writeRows: array toPasteboard: pboard]) {
*error = _(@"Could not write TOC to pasteboard.");
} else {
}
RELEASE(array);
}
- (void) playCD: (NSPasteboard *) pboard
userData: (NSString *) userData
error: (NSString **) error
{
NSArray *types = [pboard types];
// If CD is currently playing, we reject the request
if(currentState == AUDIOCD_PLAYING) {
*error = _(@"Player.alreadyPlaying");
return;
}
/*
* Do we have at least one valid pasteboard type?
*/
if (![types containsObject: NSFilenamesPboardType] &&
![types containsObject: NSStringPboardType]) {
*error = _(@"Player.noValidPboardType");
return;
}
/*
* Try to add as much as possible, i.e. even if one pasteboard
* type fails try the other one (if it exists in the pasteboard).
*/
if ([types containsObject: NSFilenamesPboardType] ||
[types containsObject: NSStringPboardType]) {
// Get the device name from the pboard
NSString *device = nil;
NSArray *devices = [pboard propertyListForType: NSFilenamesPboardType];
if (devices != nil) {
if ([devices count] != 1) {
*error = _(@"Player.tooManyFileNames");
return;
}
device = [devices objectAtIndex: 0];
} else {
device = [pboard propertyListForType: NSStringPboardType];
}
if (device == nil) {
*error = _(@"Player.noDeviceNameFound");
return;
}
[self playCD: device];
}
}
- (void) playCD: (NSString *) device
{
if (device == nil) {
return;
}
autoPlay = YES;
[drive stopPolling];
[drive startPollingWithPreferredDevice: device];
}
//
// class methods
//
+ (Player *) sharedPlayer
{
if (nil == sharedPlayer) {
sharedPlayer = [[Player alloc] init];
}
return sharedPlayer;
}
@end