Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOALSound.m
Go to the documentation of this file.
1/*
2
3OOALSound.m
4
5OOALSound - OpenAL sound implementation for Oolite.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#import "OOALSound.h"
28#import "OOLogging.h"
30#import "OOMaths.h"
31#import "OOALSoundDecoder.h"
33#import "OOALBufferedSound.h"
34#import "OOALStreamedSound.h"
35#import "OOALSoundMixer.h"
36
37#define KEY_VOLUME_CONTROL @"volume_control"
38
39static const size_t kMaxBufferedSoundSize = 1 << 20; // 1 MB
40
41static BOOL sIsSetUp = NO;
42static BOOL sIsSoundOK = NO;
43
44@implementation OOSound
45
46+ (BOOL) setUp
47{
48 if (!sIsSetUp)
49 {
50 sIsSetUp = YES;
52 if (controller != nil)
53 {
54 sIsSoundOK = YES;
55 float volume = [[NSUserDefaults standardUserDefaults] oo_floatForKey:KEY_VOLUME_CONTROL defaultValue:0.5];
56 [self setMasterVolume:volume];
57 }
58 }
59
60 return sIsSoundOK;
61}
62
63
64+ (void) setMasterVolume:(float) fraction
65{
66 if (!sIsSetUp && ![self setUp])
67 return;
68
69 fraction = OOClamp_0_1_f(fraction);
70
72 if (fraction != [controller masterVolume])
73 {
74 [controller setMasterVolume:fraction];
75 [[NSUserDefaults standardUserDefaults] setFloat:[controller masterVolume] forKey:KEY_VOLUME_CONTROL];
76 }
77}
78
79
80+ (float) masterVolume
81{
82 if (!sIsSetUp && ![self setUp] )
83 return 0.0;
84
86 return [controller masterVolume];
87}
88
89
90- (id) init
91{
92 if (!sIsSetUp) [OOSound setUp];
93 return [super init];
94}
95
96
97- (id) initWithContentsOfFile:(NSString *)path
98{
99 if (!sIsSoundOK) return nil;
100
101 [self release];
102 if (!sIsSetUp && ![OOSound setUp]) return nil;
103
104 OOALSoundDecoder *decoder;
105
106 decoder = [[OOALSoundDecoder alloc] initWithPath:path];
107 if (nil == decoder) return nil;
108
109 if ([decoder sizeAsBuffer] <= kMaxBufferedSoundSize)
110 {
111 self = [[OOALBufferedSound alloc] initWithDecoder:decoder];
112 }
113 else
114 {
115 self = [[OOALStreamedSound alloc] initWithDecoder:decoder];
116 }
117 [decoder release];
118
119 if (nil != self)
120 {
121 #ifndef NDEBUG
122 OOLog(kOOLogSoundLoadingSuccess, @"Loaded sound %@", path);
123 #endif
124 }
125 else
126 {
127 OOLog(kOOLogSoundLoadingError, @"Failed to load sound \"%@\"", path);
128 }
129
130 return self;
131
132
133}
134
135- (id)initWithDecoder:(OOALSoundDecoder *)inDecoder
136{
137 [self release];
138 return nil;
139}
140
141
142- (NSString *)name
143{
145 return @"";
146}
147
148
149+ (void) update
150{
152 if( sIsSoundOK && mixer)
153 [mixer update];
154}
155
156+ (BOOL) isSoundOK
157{
158 return sIsSoundOK;
159}
160
161
162- (ALuint) soundBuffer
163{
165 return 0;
166}
167
168
169- (BOOL) soundIncomplete
170{
171 return NO;
172}
173
174
175- (void) rewind
176{
177 // doesn't need to do anything on seekable FDs
178}
179
180@end
static BOOL sIsSetUp
Definition OOALSound.m:41
static const size_t kMaxBufferedSoundSize
Definition OOALSound.m:39
static BOOL sIsSoundOK
Definition OOALSound.m:42
#define OOLogGenericSubclassResponsibility()
Definition OOLogging.h:129
#define OOLog(class, format,...)
Definition OOLogging.h:88
static NSString *const kOOLogSoundLoadingSuccess
static NSString *const kOOLogSoundLoadingError
return nil
void setMasterVolume:(ALfloat fraction)
OOOpenALController * sharedController()
BOOL soundIncomplete()
Definition OOALSound.m:169
id init()
Definition OOALSound.m:90
BOOL isSoundOK()
Definition OOALSound.m:156
void setMasterVolume:(float fraction)
Definition OOALSound.m:64
float masterVolume()
Definition OOALSound.m:80
BOOL setUp()
Definition OOALSound.m:46
void update()
Definition OOALSound.m:149
void rewind()
Definition OOALSound.m:175
ALuint soundBuffer()
Definition OOALSound.m:162
NSString * name()
Definition OOALSound.m:142