Line data Source code
1 0 : /* 2 : 3 : OOALSound.m 4 : 5 : OOALSound - OpenAL sound implementation for Oolite. 6 : 7 : Permission is hereby granted, free of charge, to any person obtaining a copy 8 : of this software and associated documentation files (the "Software"), to deal 9 : in the Software without restriction, including without limitation the rights 10 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 : copies of the Software, and to permit persons to whom the Software is 12 : furnished to do so, subject to the following conditions: 13 : 14 : The above copyright notice and this permission notice shall be included in all 15 : copies or substantial portions of the Software. 16 : 17 : THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 : SOFTWARE. 24 : 25 : */ 26 : 27 : #import "OOALSound.h" 28 : #import "OOLogging.h" 29 : #import "OOCollectionExtractors.h" 30 : #import "OOMaths.h" 31 : #import "OOALSoundDecoder.h" 32 : #import "OOOpenALController.h" 33 : #import "OOALBufferedSound.h" 34 : #import "OOALStreamedSound.h" 35 : #import "OOALSoundMixer.h" 36 : 37 0 : #define KEY_VOLUME_CONTROL @"volume_control" 38 : 39 0 : static const size_t kMaxBufferedSoundSize = 1 << 20; // 1 MB 40 : 41 0 : static BOOL sIsSetUp = NO; 42 0 : static BOOL sIsSoundOK = NO; 43 : 44 : @implementation OOSound 45 : 46 : + (BOOL) setUp 47 : { 48 : if (!sIsSetUp) 49 : { 50 : sIsSetUp = YES; 51 : OOOpenALController* controller = [OOOpenALController sharedController]; 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 : 71 : OOOpenALController *controller = [OOOpenALController sharedController]; 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 : 85 : OOOpenALController *controller = [OOOpenALController sharedController]; 86 : return [controller masterVolume]; 87 : } 88 : 89 : 90 0 : - (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 0 : - (id)initWithDecoder:(OOALSoundDecoder *)inDecoder 136 : { 137 : [self release]; 138 : return nil; 139 : } 140 : 141 : 142 : - (NSString *)name 143 : { 144 : OOLogGenericSubclassResponsibility(); 145 : return @""; 146 : } 147 : 148 : 149 : + (void) update 150 : { 151 : OOSoundMixer * mixer = [OOSoundMixer sharedMixer]; 152 : if( sIsSoundOK && mixer) 153 : [mixer update]; 154 : } 155 : 156 : + (BOOL) isSoundOK 157 : { 158 : return sIsSoundOK; 159 : } 160 : 161 : 162 : - (ALuint) soundBuffer 163 : { 164 : OOLogGenericSubclassResponsibility(); 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