Oolite
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 "OOALBufferedSound.h"
29#import "OOALSoundDecoder.h"
30#import "OOALSoundMixer.h"
31#import "OOALStreamedSound.h"
33#import "OOLogging.h"
34#import "OOMaths.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 sIsSetUp = YES;
51 if (controller != nil) {
52 sIsSoundOK = YES;
53 float volume = [[NSUserDefaults standardUserDefaults] oo_floatForKey:KEY_VOLUME_CONTROL defaultValue:0.5];
54 [self setMasterVolume:volume];
55 }
56 }
57
58 return sIsSoundOK;
59}
60
61+ (void)setMasterVolume:(float)fraction
62{
63 if (!sIsSetUp && ![self setUp])
64 return;
65
66 fraction = OOClamp_0_1_f(fraction);
67
69 if (fraction != [controller masterVolume]) {
70 [controller setMasterVolume:fraction];
71 [[NSUserDefaults standardUserDefaults] setFloat:[controller masterVolume] forKey:KEY_VOLUME_CONTROL];
72 }
73}
74
76{
77 if (!sIsSetUp && ![self setUp])
78 return 0.0;
79
81 return [controller masterVolume];
82}
83
84- (id)init
85{
86 if (!sIsSetUp)
87 [OOSound setUp];
88 return [super init];
89}
90
91- (id)initWithContentsOfFile:(NSString*)path
92{
93 if (!sIsSoundOK)
94 return nil;
95
96 [self release];
97 if (!sIsSetUp && ![OOSound setUp])
98 return nil;
99
100 OOALSoundDecoder* decoder;
101
102 decoder = [[OOALSoundDecoder alloc] initWithPath:path];
103 if (nil == decoder)
104 return nil;
105
106 if ([decoder sizeAsBuffer] <= kMaxBufferedSoundSize) {
107 self = [[OOALBufferedSound alloc] initWithDecoder:decoder];
108 } else {
109 self = [[OOALStreamedSound alloc] initWithDecoder:decoder];
110 }
111 [decoder release];
112
113 if (nil != self) {
114#ifndef NDEBUG
115 OOLog(kOOLogSoundLoadingSuccess, @"Loaded sound %@", path);
116#endif
117 } else {
118 OOLog(kOOLogSoundLoadingError, @"Failed to load sound \"%@\"", path);
119 }
120
121 return self;
122}
123
124- (id)initWithDecoder:(OOALSoundDecoder*)inDecoder
125{
126 [self release];
127 return nil;
128}
129
130- (NSString*)name
131{
133 return @"";
134}
135
136+ (void)update
137{
139 if (sIsSoundOK && mixer)
140 [mixer update];
141}
142
144{
145 return sIsSoundOK;
146}
147
148- (ALuint)soundBuffer
149{
151 return 0;
152}
153
155{
156 return NO;
157}
158
159- (void)rewind
160{
161 // doesn't need to do anything on seekable FDs
162}
163
164@end
static BOOL sIsSetUp
Definition OOALSound.m:41
static const size_t kMaxBufferedSoundSize
Definition OOALSound.m:39
static BOOL sIsSoundOK
Definition OOALSound.m:42
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
#define OOLogGenericSubclassResponsibility()
Definition OOLogging.h:141
#define OOLog(class, format,...)
Definition OOLogging.h:84
static NSString *const kOOLogSoundLoadingSuccess
static NSString *const kOOLogSoundLoadingError
return nil
void setMasterVolume:(ALfloat fraction)
OOOpenALController * sharedController()
BOOL soundIncomplete()
Definition OOALSound.m:154
id init()
Definition OOALSound.m:84
BOOL isSoundOK()
Definition OOALSound.m:143
void setMasterVolume:(float fraction)
Definition OOALSound.m:61
float masterVolume()
Definition OOALSound.m:75
BOOL setUp()
Definition OOALSound.m:46
void update()
Definition OOALSound.m:136
void rewind()
Definition OOALSound.m:159
ALuint soundBuffer()
Definition OOALSound.m:148
NSString * name()
Definition OOALSound.m:130