Oolite
Loading...
Searching...
No Matches
OOMusicController.m
Go to the documentation of this file.
1/*
2
3OOMusicController.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOMusicController.h"
28#import "OOSound.h"
29#import "ResourceManager.h"
30
31static id sSingleton = nil;
32
33@interface OOMusicController (Private)
34
35- (void)playiTunesPlaylist:(NSString*)playlistName;
36- (void)pauseiTunes;
37
38@end
39
40// Values for _special
41enum {
47};
48
49@implementation OOMusicController
50
52{
53 if (sSingleton == nil) {
54 sSingleton = [[self alloc] init];
55 }
56
57 return sSingleton;
58}
59
60- (id)init
61{
62 self = [super init];
63 if (self != nil) {
64 NSString* modeString = [[NSUserDefaults standardUserDefaults] stringForKey:@"music mode"];
65 if ([modeString isEqualToString:@"off"])
67 else if ([modeString isEqualToString:@"iTunes"])
69 else
71
72 // Handle unlikely case of taking prefs from iTunes-enabled system to other.
75
76 [self setMissionMusic:@"OoliteTheme.ogg"];
77 }
78
79 return self;
80}
81
82- (void)playMusicNamed:(NSString*)name loop:(BOOL)loop
83{
84 [self playMusicNamed:name loop:loop gain:OO_DEFAULT_SOUNDSOURCE_GAIN];
85}
86
87- (void)playMusicNamed:(NSString*)name loop:(BOOL)loop gain:(float)gain
88{
89 if ([self isPlaying] && [name isEqual:[self playingMusic]])
90 return;
91
92 if (_mode == kOOMusicOn || (_mode == kOOMusicITunes && [name isEqualToString:@"OoliteTheme.ogg"])) {
93 OOMusic* music = [ResourceManager ooMusicNamed:name inFolder:@"Music"];
94 if (music != nil) {
95 [_current stop];
96
97 [music setMusicGain:OOClamp_0_1_f(gain)];
98 [music playLooped:loop];
99
100 [_current release];
101 _current = [music retain];
102 }
103 }
104}
105
107{
109 [self playMusicNamed:@"OoliteTheme.ogg" loop:YES];
110}
111
113{
115
116 if (_mode == kOOMusicITunes) {
117 [self playiTunesPlaylist:@"Oolite-Docking"];
118 } else {
119 [self playMusicNamed:@"BlueDanube.ogg" loop:YES];
120 }
121}
122
124{
126
127 if (_mode == kOOMusicITunes) {
128 [self playiTunesPlaylist:@"Oolite-Docked"];
129 } else {
130 [self playMusicNamed:@"OoliteDocked.ogg" loop:NO];
131 }
132}
133
134- (void)setMissionMusic:(NSString*)missionMusicName
135{
136 [_missionMusic autorelease];
138}
139
141{
142 if (_missionMusic != nil) {
144 [self playMusicNamed:_missionMusic loop:NO];
145 }
146}
147
148// Stop without switching iTunes to in-flight music.
149- (void)justStop
150{
151 [_current stop];
152 [_current release];
153 _current = nil;
155}
156
157- (void)stop
158{
159 [self justStop];
160
161 if (_mode == kOOMusicITunes) {
162 [self playiTunesPlaylist:@"Oolite-Inflight"];
163 }
164}
165
166- (void)stopMusicNamed:(NSString*)name
167{
168 if ([name isEqual:[self playingMusic]])
169 [self stop];
170}
171
173{
174 if (_special == kSpecialTheme) {
175 [self justStop];
176 [self playDockedMusic];
177 }
178}
179
181{
183 [self stop];
184}
185
187{
189 [self stop];
190}
191
193{
194 if (_mode != kOOMusicOn)
195 return;
196
197 if (![self isPlaying])
198 [self playDockingMusic];
199 else if (_special == kSpecialDocking)
200 [self stop];
201}
202
207
208- (NSString*)playingMusic
209{
210 return [_current name];
211}
212
214{
215 return [_current isPlaying];
216}
217
219{
220 return _mode;
221}
222
223- (void)setMode:(OOMusicMode)mode
224{
225 if (mode <= kOOMusicModeMax && _mode != mode) {
226 if (_mode == kOOMusicITunes)
227 [self pauseiTunes];
228 _mode = mode;
229
230 if (_mode == kOOMusicOff)
231 [self stop];
232 else
233 switch (_special) {
234 case kSpecialNone:
235 [self stop];
236 break;
237
238 case kSpecialTheme:
239 [self playThemeMusic];
240 break;
241
242 case kSpecialDocked:
243 [self playDockedMusic];
244 break;
245
246 case kSpecialDocking:
247 [self playDockingMusic];
248 break;
249
250 case kSpecialMission:
251 [self playMissionMusic];
252 break;
253 }
254
255 NSString* modeString = nil;
256 switch (_mode) {
257 case kOOMusicOff:
258 modeString = @"off";
259 break;
260 case kOOMusicOn:
261 modeString = @"on";
262 break;
263 case kOOMusicITunes:
264 modeString = @"iTunes";
265 break;
266 }
267 [[NSUserDefaults standardUserDefaults] setObject:modeString forKey:@"music mode"];
268 }
269}
270
271@end
272
273@implementation OOMusicController (Singleton)
274
275/* Canonical singleton boilerplate.
276 See Cocoa Fundamentals Guide: Creating a Singleton Instance.
277 See also +sharedController above.
278
279 NOTE: assumes single-threaded access.
280*/
281
282+ (id)allocWithZone:(NSZone*)inZone
283{
284 if (sSingleton == nil) {
285 sSingleton = [super allocWithZone:inZone];
286 return sSingleton;
287 }
288 return nil;
289}
290
291- (id)copyWithZone:(NSZone*)inZone
292{
293 return self;
294}
295
297{
298 return self;
299}
300
301- (NSUInteger)retainCount
302{
303 return UINT_MAX;
304}
305
306- (void)release
307{
308}
309
311{
312 return self;
313}
314
315@end
316
317@implementation OOMusicController (Private)
318
319#if OOLITE_MAC_OS_X
320- (void)playiTunesPlaylist:(NSString*)playlistName
321{
322 NSString* ootunesScriptString =
323 [NSString stringWithFormat:
324 @"with timeout of 1 second\n"
325 " tell application \"iTunes\"\n"
326 " copy playlist \"%@\" to thePlaylist\n"
327 " if thePlaylist exists then\n"
328 " play some track of thePlaylist\n"
329 " end if\n"
330 " end tell\n"
331 "end timeout",
332 playlistName];
333
334 NSAppleScript* ootunesScript = [[[NSAppleScript alloc] initWithSource:ootunesScriptString] autorelease];
335 NSDictionary* errDict = nil;
336
337 [ootunesScript executeAndReturnError:&errDict];
338 if (errDict)
339 OOLog(@"sound.music.iTunesIntegration.failed", @"ootunes returned :%@", errDict);
340}
341
343{
344 NSString* ootunesScriptString = [NSString stringWithFormat:@"try\nignoring application responses\ntell application \"iTunes\" to pause\nend ignoring\nend try"];
345 NSAppleScript* ootunesScript = [[NSAppleScript alloc] initWithSource:ootunesScriptString];
346 NSDictionary* errDict = nil;
347 [ootunesScript executeAndReturnError:&errDict];
348 if (errDict)
349 OOLog(@"sound.music.iTunesIntegration.failed", @"ootunes returned :%@", errDict);
350 [ootunesScript release];
351}
352#else
353- (void)playiTunesPlaylist:(NSString*)playlistName
354{
355}
356- (void)pauseiTunes
357{
358}
359#endif
360
361@end
#define OOLog(class, format,...)
Definition OOLogging.h:84
OOMusicMode
@ kOOMusicOn
@ kOOMusicITunes
@ kOOMusicOff
@ kOOMusicModeMax
@ kSpecialTheme
@ kSpecialDocked
@ kSpecialMission
@ kSpecialNone
@ kSpecialDocking
static OONullTexture * sSingleton
return nil
OOMusicController * sharedController()
void playiTunesPlaylist:(NSString *playlistName)
void playMusicNamed:loop:(NSString *name,[loop] BOOL loop)
void playMusicNamed:loop:gain:(NSString *name,[loop] BOOL loop,[gain] float gain)
OOSoundSource * soundSource()
void setMissionMusic:(NSString *missionMusicName)
BOOL isPlaying()
Definition OOALMusic.m:103
void setMusicGain:(float newValue)
Definition OOALMusic.m:69
OOSoundSource * musicSoundSource()
Definition OOALMusic.m:98
void playLooped:(BOOL looped)
Definition OOALMusic.m:83
NSString * name()
Definition OOALMusic.m:64
void stop()
Definition OOALMusic.m:108
OOMusic * ooMusicNamed:inFolder:(NSString *fileName,[inFolder] NSString *folderName)