Oolite
Loading...
Searching...
No Matches
OOSoundSourcePool.m
Go to the documentation of this file.
1/*
2
3OOSoundSourcePool.m
4
5
6Copyright (C) 2008-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28#import "OOSoundSourcePool.h"
29#import "OOSound.h"
30#import "Universe.h"
31
32enum {
33 kNoSlot = UINT8_MAX
34};
35
41
42@interface OOSoundSourcePool (Private)
43
44- (uint8_t)selectSlotForPriority:(float)priority;
45
46@end
47
48@implementation OOSoundSourcePool
49
50+ (instancetype)poolWithCount:(uint8_t)count minRepeatTime:(OOTimeDelta)minRepeat
51{
52 return [[[self alloc] initWithCount:count minRepeatTime:minRepeat] autorelease];
53}
54
55- (id)initWithCount:(uint8_t)count minRepeatTime:(OOTimeDelta)minRepeat
56{
57 if ((self = [super init])) {
58 // Sanity-check count
59 if (count == 0)
60 count = 1;
61 if (count == kNoSlot)
62 --count;
63 _count = count;
65
66 if (minRepeat < 0.0)
67 minRepeat = 0.0;
68 _minRepeat = minRepeat;
69
70 // Create source pool
71 _sources = calloc(sizeof(PoolElement), count);
72 if (_sources == NULL) {
73 [self release];
74 self = nil;
75 }
76 }
77 return self;
78}
79
80- (void)dealloc
81{
82 uint8_t i;
83
84 for (i = 0; i != _count; i++) {
85 [_sources[i].source release];
86 }
87 free(_sources);
88
89 [_lastKey release];
90
91 [super dealloc];
92}
93
94- (void)playSoundWithKey:(NSString*)key
95 priority:(float)priority
96 expiryTime:(OOTimeDelta)expiryTime
97 overlap:(BOOL)overlap
98 position:(Vector)position
99{
100 uint8_t slot;
101 OOTimeAbsolute now, absExpiryTime;
102 PoolElement* element = NULL;
103 OOSound* sound = NULL;
104
105 // Convert expiry time to absolute
106 now = [UNIVERSE getTime];
107 absExpiryTime = expiryTime + now;
108
109 // Avoid repeats if required
110 if (now < _nextRepeat && [key isEqualToString:_lastKey])
111 return;
112 if (!overlap && _reserved != kNoSlot && [_sources[_reserved].source isPlaying])
113 return;
114
115 // Look for a slot in the source list to use
116 slot = [self selectSlotForPriority:priority];
117 if (slot == kNoSlot)
118 return;
119 element = &_sources[slot];
120
121 // Load sound
122 sound = [OOSound soundWithCustomSoundKey:key];
123 if (sound == nil)
124 return;
125
126 // Stop playing sound or set up sound source as appropriate
127 if (element->source != nil)
128 [element->source stop];
129 else {
130 element->source = [[OOSoundSource alloc] init];
131 if (element->source == nil)
132 return;
133 }
134 if (slot == _reserved)
135 _reserved = kNoSlot; // _reserved has finished playing!
136 if (!overlap)
137 _reserved = slot;
138
139 // Play and store metadata
140 [element->source setPosition:position];
141 [element->source playSound:sound];
142 element->expiryTime = absExpiryTime;
143 element->priority = priority;
144 if (_minRepeat > 0.0) {
145 _nextRepeat = now + _minRepeat;
146 [_lastKey release];
147 _lastKey = [key copy];
148 }
149
150 // Set staring search location for next slot lookup
151 _latest = slot;
152}
153
154- (void)playSoundWithKey:(NSString*)key
155 priority:(float)priority
156 expiryTime:(OOTimeDelta)expiryTime
157{
158 [self playSoundWithKey:key
159 priority:priority
160 expiryTime:expiryTime
161 overlap:YES
162 position:kZeroVector];
163}
164
165- (void)playSoundWithKey:(NSString*)key
166 priority:(float)priority
167 position:(Vector)position
168{
169 [self playSoundWithKey:key
170 priority:priority
171 expiryTime:0.5 + randf() * 0.1
172 overlap:YES
173 position:position];
174}
175
176- (void)playSoundWithKey:(NSString*)key
177 priority:(float)priority
178{
179 [self playSoundWithKey:key
180 priority:priority
181 expiryTime:0.5 + randf() * 0.1];
182}
183
184- (void)playSoundWithKey:(NSString*)key
185{
186 [self playSoundWithKey:key priority:1.0];
187}
188
189- (void)playSoundWithKey:(NSString*)key position:(Vector)position
190{
191 [self playSoundWithKey:key priority:1.0 position:position];
192}
193
194- (void)playSoundWithKey:(NSString*)key overlap:(BOOL)overlap
195{
196 [self playSoundWithKey:key
197 priority:1.0
198 expiryTime:0.5
199 overlap:overlap
200 position:kZeroVector];
201}
202
203- (void)playSoundWithKey:(NSString*)key overlap:(BOOL)overlap position:(Vector)position
204{
205 [self playSoundWithKey:key
206 priority:1.0
207 expiryTime:0.5
208 overlap:overlap
209 position:position];
210}
211
212@end
213
214@implementation OOSoundSourcePool (Private)
215
216- (uint8_t)selectSlotForPriority:(float)priority
217{
218 uint8_t curr, count, expiredLower = kNoSlot, unexpiredLower = kNoSlot, expiredEqual = kNoSlot;
219 PoolElement* element = NULL;
220 OOTimeAbsolute now = [UNIVERSE getTime];
221
222#define NEXT(x) (((x) + 1) % _count)
223
224 curr = _latest;
225 count = _count;
226 do {
227 curr = NEXT(curr);
228 element = &_sources[curr];
229
230 if (element->source == nil || ![element->source isPlaying])
231 return curr; // Best type of slot: empty
232 else if (element->priority < priority) {
233 if (element->expiryTime <= now)
234 expiredLower = curr; // Second-best type: expired lower-priority
235 else if (curr != _reserved)
236 unexpiredLower = curr; // Third-best type: unexpired lower-priority
237 } else if (element->priority == priority && element->expiryTime <= now) {
238 expiredEqual = curr; // Fourth-best type: expired equal-priority.
239 }
240 } while (--count);
241
242 if (expiredLower != kNoSlot)
243 return expiredLower;
244 if (unexpiredLower != kNoSlot)
245 return unexpiredLower;
246 return expiredEqual; // Will be kNoSlot if none found
247}
248
249@end
unsigned count
return nil
@ kNoSlot
#define NEXT(x)
struct OOSoundSourcePoolElement PoolElement
double OOTimeDelta
Definition OOTypes.h:193
double OOTimeAbsolute
Definition OOTypes.h:192
uint8_t selectSlotForPriority:(float priority)
void playSoundWithKey:priority:expiryTime:overlap:position:(NSString *key,[priority] float priority,[expiryTime] OOTimeDelta expiryTime,[overlap] BOOL overlap,[position] Vector position)
void playSoundWithKey:priority:position:(NSString *key,[priority] float priority,[position] Vector position)
void playSoundWithKey:priority:expiryTime:(NSString *key,[priority] float priority,[expiryTime] OOTimeDelta expiryTime)
struct OOSoundSourcePoolElement * _sources
OOTimeAbsolute _nextRepeat
void playSoundWithKey:priority:(NSString *key,[priority] float priority)
id soundWithCustomSoundKey:(NSString *key)
Definition Universe.m:9950