Line data Source code
1 0 : /* 2 : 3 : OOALSoundMixer.m 4 : 5 : OOALSound - OpenAL sound implementation for Oolite. 6 : Copyright (C) 2006-2013 Jens Ayton 7 : 8 : Permission is hereby granted, free of charge, to any person obtaining a copy 9 : of this software and associated documentation files (the "Software"), to deal 10 : in the Software without restriction, including without limitation the rights 11 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 : copies of the Software, and to permit persons to whom the Software is 13 : furnished to do so, subject to the following conditions: 14 : 15 : The above copyright notice and this permission notice shall be included in all 16 : copies or substantial portions of the Software. 17 : 18 : THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 : SOFTWARE. 25 : 26 : */ 27 : 28 : #include <assert.h> 29 : 30 : #import "OOALSoundMixer.h" 31 : #import "OOCocoa.h" 32 : #import "OOALSound.h" 33 : #import "OOALSoundChannel.h" 34 : 35 0 : static OOSoundMixer *sSingleton = nil; 36 : 37 : 38 : @implementation OOSoundMixer 39 : 40 : + (id) sharedMixer 41 : { 42 : if (nil == sSingleton) 43 : { 44 : [[self alloc] init]; 45 : } 46 : return sSingleton; 47 : } 48 : 49 : 50 0 : - (id) init 51 : { 52 : BOOL OK = YES; 53 : uint32_t idx = 0, count = kMixerGeneralChannels; 54 : OOSoundChannel *channel; 55 : 56 : if (!(self = [super init])) return nil; 57 : if (![OOSound setUp]) OK = NO; 58 : 59 : if (OK) 60 : { 61 : // Allocate channels 62 : do 63 : { 64 : channel = [[OOSoundChannel alloc] init]; 65 : if (nil != channel) 66 : { 67 : _channels[idx++] = channel; 68 : [self pushChannel:channel]; 69 : } 70 : } while (--count); 71 : } 72 : 73 : if (!OK) 74 : { 75 : [super release]; 76 : // static analyser complains about this next line; probably nothing - CIM 77 : self = nil; 78 : } 79 : else 80 : { 81 : sSingleton = self; 82 : } 83 : 84 : return sSingleton; 85 : } 86 : 87 : 88 : // only to be called at app shutdown by OOOpenALController::shutdown 89 0 : - (void) shutdown 90 : { 91 : uint32_t i; 92 : for (i = 0; i < kMixerGeneralChannels; ++i) 93 : { 94 : DESTROY(_channels[i]); 95 : } 96 : } 97 : 98 : 99 : - (void) update 100 : { 101 : uint32_t i; 102 : for (i = 0; i < kMixerGeneralChannels; ++i) 103 : { 104 : [_channels[i] update]; 105 : } 106 : } 107 : 108 : 109 : - (OOSoundChannel *) popChannel 110 : { 111 : OOSoundChannel *channel = _freeList; 112 : _freeList = [channel next]; 113 : [channel setNext:nil]; 114 : 115 : return channel; 116 : } 117 : 118 : 119 : - (void) pushChannel:(OOSoundChannel *)channel 120 : { 121 : assert(channel != nil); 122 : 123 : [channel setNext:_freeList]; 124 : _freeList = channel; 125 : } 126 : 127 : @end 128 : 129 : 130 : @implementation OOSoundMixer (Singleton) 131 : 132 : /* Canonical singleton boilerplate. 133 : See Cocoa Fundamentals Guide: Creating a Singleton Instance. 134 : See also +sharedMixer above. 135 : 136 : NOTE: assumes single-threaded access. 137 : */ 138 : 139 0 : + (id)allocWithZone:(NSZone *)inZone 140 : { 141 : if (sSingleton == nil) 142 : { 143 : sSingleton = [super allocWithZone:inZone]; 144 : return sSingleton; 145 : } 146 : return nil; 147 : } 148 : 149 : 150 0 : - (id)copyWithZone:(NSZone *)inZone 151 : { 152 : return self; 153 : } 154 : 155 : 156 0 : - (id)retain 157 : { 158 : return self; 159 : } 160 : 161 : 162 0 : - (NSUInteger)retainCount 163 : { 164 : return UINT_MAX; 165 : } 166 : 167 : 168 0 : - (void)release 169 : {} 170 : 171 : 172 0 : - (id)autorelease 173 : { 174 : return self; 175 : } 176 : 177 : @end