Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOSoundSourcePool Class Reference

#include <OOSoundSourcePool.h>

+ Inheritance diagram for OOSoundSourcePool:
+ Collaboration diagram for OOSoundSourcePool:

Instance Methods

(id) - initWithCount:minRepeatTime:
 
(void) - playSoundWithKey:priority:expiryTime:overlap:position:
 
(void) - playSoundWithKey:priority:expiryTime:
 
(void) - playSoundWithKey:priority:
 
(void) - playSoundWithKey:priority:position:
 
(void) - playSoundWithKey:position:
 
(void) - playSoundWithKey:
 
(void) - playSoundWithKey:overlap:
 
(void) - playSoundWithKey:overlap:position:
 
(void) - dealloc [implementation]
 
(uint8_t) - selectSlotForPriority: [implementation]
 

Class Methods

(instancetype) + poolWithCount:minRepeatTime:
 

Private Attributes

struct OOSoundSourcePoolElement_sources
 
uint8_t _count
 
uint8_t _latest
 
uint8_t _reserved
 
OOTimeDelta _minRepeat
 
OOTimeAbsolute _nextRepeat
 
NSString * _lastKey
 

Detailed Description

Definition at line 40 of file OOSoundSourcePool.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Definition at line 43 of file OOSoundSourcePool.m.

88{
89 uint8_t i;
90
91 for (i = 0; i != _count; i++)
92 {
93 [_sources[i].source release];
94 }
95 free(_sources);
96
97 [_lastKey release];
98
99 [super dealloc];
100}
struct OOSoundSourcePoolElement * _sources

◆ initWithCount:minRepeatTime:

- (id) initWithCount: (uint8_t) count
minRepeatTime: (OOTimeDelta) minRepeat 

Definition at line 43 of file OOSoundSourcePool.m.

62 :(uint8_t)count minRepeatTime:(OOTimeDelta)minRepeat
63{
64 if ((self = [super init]))
65 {
66 // Sanity-check count
67 if (count == 0) count = 1;
68 if (count == kNoSlot) --count;
69 _count = count;
71
72 if (minRepeat < 0.0) minRepeat = 0.0;
73 _minRepeat = minRepeat;
74
75 // Create source pool
76 _sources = calloc(sizeof(PoolElement), count);
77 if (_sources == NULL)
78 {
79 [self release];
80 self = nil;
81 }
82 }
83 return self;
84}
unsigned count
return nil
@ kNoSlot
double OOTimeDelta
Definition OOTypes.h:224

◆ playSoundWithKey:

- (void) playSoundWithKey: (NSString *) key

Definition at line 43 of file OOSoundSourcePool.m.

191 :(NSString *)key
192{
193 [self playSoundWithKey:key priority:1.0];
194}

◆ playSoundWithKey:overlap:

- (void) playSoundWithKey: (NSString *) key
overlap: (BOOL) overlap 

Definition at line 43 of file OOSoundSourcePool.m.

203 :(NSString *)key overlap:(BOOL)overlap
204{
205 [self playSoundWithKey:key
206 priority:1.0
207 expiryTime:0.5
208 overlap:overlap
209 position:kZeroVector];
210}

◆ playSoundWithKey:overlap:position:

- (void) playSoundWithKey: (NSString *) key
overlap: (BOOL) overlap
position: (Vector) position 

Definition at line 43 of file OOSoundSourcePool.m.

213 :(NSString *)key overlap:(BOOL)overlap position:(Vector)position
214{
215 [self playSoundWithKey:key
216 priority:1.0
217 expiryTime:0.5
218 overlap:overlap
219 position:position];
220}

◆ playSoundWithKey:position:

- (void) playSoundWithKey: (NSString *) key
position: (Vector) position 

Definition at line 43 of file OOSoundSourcePool.m.

197 :(NSString *)key position:(Vector)position
198{
199 [self playSoundWithKey:key priority:1.0 position:position];
200}

◆ playSoundWithKey:priority:

- (void) playSoundWithKey: (NSString *) key
priority: (float) priority 

Definition at line 43 of file OOSoundSourcePool.m.

182 :(NSString *)key
183 priority:(float)priority
184{
185 [self playSoundWithKey:key
186 priority:priority
187 expiryTime:0.5 + randf() * 0.1];
188}

◆ playSoundWithKey:priority:expiryTime:

- (void) playSoundWithKey: (NSString *) key
priority: (float) priority
expiryTime: (OOTimeDelta) expiryTime 

Definition at line 43 of file OOSoundSourcePool.m.

158 :(NSString *)key
159 priority:(float)priority
160 expiryTime:(OOTimeDelta)expiryTime
161{
162 [self playSoundWithKey:key
163 priority:priority
164 expiryTime:expiryTime
165 overlap:YES
166 position:kZeroVector];
167}

◆ playSoundWithKey:priority:expiryTime:overlap:position:

- (void) playSoundWithKey: (NSString *) key
priority: (float) priority
expiryTime: (OOTimeDelta) expiryTime
overlap: (BOOL) overlap
position: (Vector) position 

Definition at line 43 of file OOSoundSourcePool.m.

103 :(NSString *)key
104 priority:(float)priority
105 expiryTime:(OOTimeDelta)expiryTime
106 overlap:(BOOL)overlap
107 position:(Vector)position
108{
109 uint8_t slot;
110 OOTimeAbsolute now, absExpiryTime;
111 PoolElement *element = NULL;
112 OOSound *sound = NULL;
113
114 // Convert expiry time to absolute
115 now = [UNIVERSE getTime];
116 absExpiryTime = expiryTime + now;
117
118 // Avoid repeats if required
119 if (now < _nextRepeat && [key isEqualToString:_lastKey]) return;
120 if (!overlap && _reserved != kNoSlot && [_sources[_reserved].source isPlaying]) return;
121
122 // Look for a slot in the source list to use
123 slot = [self selectSlotForPriority:priority];
124 if (slot == kNoSlot) return;
125 element = &_sources[slot];
126
127 // Load sound
128 sound = [OOSound soundWithCustomSoundKey:key];
129 if (sound == nil) return;
130
131 // Stop playing sound or set up sound source as appropriate
132 if (element->source != nil) [element->source stop];
133 else
134 {
135 element->source = [[OOSoundSource alloc] init];
136 if (element->source == nil) return;
137 }
138 if (slot == _reserved) _reserved = kNoSlot; // _reserved has finished playing!
139 if (!overlap) _reserved = slot;
140
141 // Play and store metadata
142 [element->source setPosition:position];
143 [element->source playSound:sound];
144 element->expiryTime = absExpiryTime;
145 element->priority = priority;
146 if (_minRepeat > 0.0)
147 {
148 _nextRepeat = now + _minRepeat;
149 [_lastKey release];
150 _lastKey = [key copy];
151 }
152
153 // Set staring search location for next slot lookup
154 _latest = slot;
155}
double OOTimeAbsolute
Definition OOTypes.h:223
OOTimeAbsolute _nextRepeat
id soundWithCustomSoundKey:(NSString *key)
Definition Universe.m:11032

◆ playSoundWithKey:priority:position:

- (void) playSoundWithKey: (NSString *) key
priority: (float) priority
position: (Vector) position 

Definition at line 43 of file OOSoundSourcePool.m.

170 :(NSString *)key
171 priority:(float)priority
172 position:(Vector)position
173{
174 [self playSoundWithKey:key
175 priority:priority
176 expiryTime:0.5 + randf() * 0.1
177 overlap:YES
178 position:position];
179}

◆ poolWithCount:minRepeatTime:

+ (instancetype) poolWithCount: (uint8_t) count
minRepeatTime: (OOTimeDelta) minRepeat 

Definition at line 43 of file OOSoundSourcePool.m.

56 :(uint8_t)count minRepeatTime:(OOTimeDelta)minRepeat
57{
58 return [[[self alloc] initWithCount:count minRepeatTime:minRepeat] autorelease];
59}

◆ selectSlotForPriority:

- (uint8_t) selectSlotForPriority: (float) priority
implementation

Provided by category OOSoundSourcePool(Private).

Definition at line 43 of file OOSoundSourcePool.m.

228 :(float)priority
229{
230 uint8_t curr, count, expiredLower = kNoSlot, unexpiredLower = kNoSlot, expiredEqual = kNoSlot;
231 PoolElement *element = NULL;
232 OOTimeAbsolute now = [UNIVERSE getTime];
233
234#define NEXT(x) (((x) + 1) % _count)
235
236 curr = _latest;
237 count = _count;
238 do
239 {
240 curr = NEXT(curr);
241 element = &_sources[curr];
242
243 if (element->source == nil || ![element->source isPlaying]) return curr; // Best type of slot: empty
244 else if (element->priority < priority)
245 {
246 if (element->expiryTime <= now) expiredLower = curr; // Second-best type: expired lower-priority
247 else if (curr != _reserved) unexpiredLower = curr; // Third-best type: unexpired lower-priority
248 }
249 else if (element->priority == priority && element->expiryTime <= now)
250 {
251 expiredEqual = curr; // Fourth-best type: expired equal-priority.
252 }
253 } while (--count);
254
255 if (expiredLower != kNoSlot) return expiredLower;
256 if (unexpiredLower != kNoSlot) return unexpiredLower;
257 return expiredEqual; // Will be kNoSlot if none found
258}
#define NEXT(x)

Member Data Documentation

◆ _count

- (uint8_t) _count
private

Definition at line 44 of file OOSoundSourcePool.h.

◆ _lastKey

- (NSString*) _lastKey
private

Definition at line 49 of file OOSoundSourcePool.h.

◆ _latest

- (uint8_t) _latest
private

Definition at line 45 of file OOSoundSourcePool.h.

◆ _minRepeat

- (OOTimeDelta) _minRepeat
private

Definition at line 47 of file OOSoundSourcePool.h.

◆ _nextRepeat

- (OOTimeAbsolute) _nextRepeat
private

Definition at line 48 of file OOSoundSourcePool.h.

◆ _reserved

- (uint8_t) _reserved
private

Definition at line 46 of file OOSoundSourcePool.h.

◆ _sources

- (struct OOSoundSourcePoolElement*) _sources
private

Definition at line 43 of file OOSoundSourcePool.h.


The documentation for this class was generated from the following files: