47@interface OOSoundSourcePool (Private)
49- (uint8_t) selectSlotForPriority:(
float)priority;
56+ (instancetype) poolWithCount:(uint8_t)count minRepeatTime:(
OOTimeDelta)minRepeat
58 return [[[
self alloc] initWithCount:count minRepeatTime:minRepeat] autorelease];
62- (id) initWithCount:(uint8_t)count minRepeatTime:(
OOTimeDelta)minRepeat
64 if ((
self = [super init]))
72 if (minRepeat < 0.0) minRepeat = 0.0;
73 _minRepeat = minRepeat;
91 for (i = 0; i != _count; i++)
93 [_sources[i].source release];
103- (void) playSoundWithKey:(NSString *)key
104 priority:(
float)priority
106 overlap:(BOOL)overlap
107 position:(Vector)position
115 now = [UNIVERSE getTime];
116 absExpiryTime = expiryTime + now;
119 if (now < _nextRepeat && [key isEqualToString:_lastKey])
return;
120 if (!overlap && _reserved !=
kNoSlot && [_sources[_reserved].source isPlaying])
return;
123 slot = [
self selectSlotForPriority:priority];
125 element = &_sources[slot];
129 if (sound ==
nil)
return;
132 if (element->
source !=
nil) [element->source stop];
138 if (slot == _reserved) _reserved =
kNoSlot;
139 if (!overlap) _reserved = slot;
142 [element->source setPosition:position];
143 [element->source playSound:sound];
146 if (_minRepeat > 0.0)
148 _nextRepeat = now + _minRepeat;
150 _lastKey = [key copy];
158- (void) playSoundWithKey:(NSString *)key
159 priority:(
float)priority
162 [
self playSoundWithKey:key
164 expiryTime:expiryTime
166 position:kZeroVector];
170- (void) playSoundWithKey:(NSString *)key
171 priority:(
float)priority
172 position:(Vector)position
174 [
self playSoundWithKey:key
176 expiryTime:0.5 + randf() * 0.1
182- (void) playSoundWithKey:(NSString *)key
183 priority:(
float)priority
185 [
self playSoundWithKey:key
187 expiryTime:0.5 + randf() * 0.1];
191- (void) playSoundWithKey:(NSString *)key
193 [
self playSoundWithKey:key priority:1.0];
197- (void) playSoundWithKey:(NSString *)key position:(Vector)position
199 [
self playSoundWithKey:key priority:1.0 position:position];
203- (void) playSoundWithKey:(NSString *)key overlap:(BOOL)overlap
205 [
self playSoundWithKey:key
209 position:kZeroVector];
213- (void) playSoundWithKey:(NSString *)key overlap:(BOOL)overlap position:(Vector)position
215 [
self playSoundWithKey:key
226@implementation OOSoundSourcePool (Private)
228- (uint8_t) selectSlotForPriority:(
float)priority
234#define NEXT(x) (((x) + 1) % _count)
241 element = &_sources[curr];
243 if (element->
source ==
nil || ![element->
source isPlaying])
return curr;
244 else if (element->
priority < priority)
246 if (element->
expiryTime <= now) expiredLower = curr;
247 else if (curr != _reserved) unexpiredLower = curr;
255 if (expiredLower !=
kNoSlot)
return expiredLower;
256 if (unexpiredLower !=
kNoSlot)
return unexpiredLower;