Oolite
Loading...
Searching...
No Matches
OOSoundSource.m
Go to the documentation of this file.
1/*
2
3OOSoundSource.m
4
5
6Copyright (C) 2006-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 "OOLogging.h"
29#import "OOMaths.h"
30#import "OOSoundInternal.h"
31
32static NSMutableSet* sPlayingSoundSources;
33
34@implementation OOSoundSource
35
36+ (instancetype)sourceWithSound:(OOSound*)inSound
37{
38 return [[[self alloc] initWithSound:inSound] autorelease];
39}
40
41- (id)init
42{
43 self = [super init];
44 if (!self)
45 return nil;
46
47 _positional = NO;
49 _gain = OO_DEFAULT_SOUNDSOURCE_GAIN;
50 return self;
51}
52
53- (id)initWithSound:(OOSound*)inSound
54{
55 self = [self init];
56 if (!self)
57 return nil;
58
59 [self setSound:inSound];
60
61 return self;
62}
63
64- (void)dealloc
65{
66 [self stop];
67 [_sound autorelease];
68
69 [super dealloc];
70}
71
73{
74 if ([self isPlaying]) {
75 return [NSString stringWithFormat:@"sound=%@, loop=%s, repeatCount=%u, playing on channel %@", _sound, [self loop] ? "YES" : "NO", [self repeatCount], _channel];
76 } else {
77 return [NSString stringWithFormat:@"sound=%@, loop=%s, repeatCount=%u, not playing", _sound, [self loop] ? "YES" : "NO", [self repeatCount]];
78 }
79}
80
82{
83 return _sound;
84}
85
86- (void)setSound:(OOSound*)sound
87{
88 if (_sound != sound) {
89 [self stop];
90 [_sound autorelease];
91 _sound = [sound retain];
92 }
93}
94
95- (BOOL)loop
96{
97 return _loop;
98}
99
100- (void)setLoop:(BOOL)loop
101{
102 _loop = !!loop;
103}
104
105- (uint8_t)repeatCount
106{
107 return _repeatCount ? _repeatCount : 1;
108}
109
110- (void)setRepeatCount:(uint8_t)count
111{
113}
114
116{
117 return _channel != nil;
118}
119
120- (void)play
121{
122 if ([self sound] == nil)
123 return;
124
126
127 if (_channel)
128 [self stop];
129
131 if (nil != _channel) {
133 [_channel setDelegate:self];
134 [_channel setPosition:_position];
135 [_channel setGain:_gain];
136 [_channel playSound:[self sound] looped:[self loop]];
137 [self retain];
138 }
139
141 sPlayingSoundSources = [[NSMutableSet alloc] init];
142 }
143 [sPlayingSoundSources addObject:self];
144
146}
147
149{
150 if (![self isPlaying])
151 [self play];
152 else
154}
155
156- (void)stop
157{
159
160 if (nil != _channel) {
161 [_channel setDelegate:[self class]];
162 [_channel stop];
163 _channel = nil;
164
165 [sPlayingSoundSources removeObject:self];
166 [self release];
167 }
168
170}
171
172+ (void)stopAll
173{
174 /* We're not allowed to mutate sPlayingSoundSources during iteration. The
175 normal solution would be to copy the set, but since we know it will
176 end up empty we may as well use the original set and let a new one be
177 set up lazily.
178 */
179 NSMutableSet* playing = sPlayingSoundSources;
181
182 [playing makeObjectsPerformSelector:@selector(stop)];
183 [playing release];
184}
185
186- (void)playSound:(OOSound*)sound
187{
188 [self playSound:sound repeatCount:_repeatCount];
189}
190
191- (void)playSound:(OOSound*)sound repeatCount:(uint8_t)count
192{
193 [self stop];
194 [self setSound:sound];
195 [self setRepeatCount:count];
196 [self play];
197}
198
199- (void)playOrRepeatSound:(OOSound*)sound
200{
201 if (_sound != sound)
202 [self playSound:sound];
203 else
204 [self playOrRepeat];
205}
206
207- (void)setPositional:(BOOL)inPositional
208{
209 if (inPositional) {
210 _positional = YES;
211 } else {
212 /* OpenAL doesn't easily do non-positional sounds beyond the
213 * stereo/mono distinction, but setting the position to the
214 * zero vector is probably close enough */
215 _positional = NO;
216 [self setPosition:kZeroVector];
217 }
218}
219
221{
222 return _positional;
223}
224
225- (void)setPosition:(Vector)inPosition
226{
227 _position = inPosition;
228 if (inPosition.x != 0.0 || inPosition.y != 0.0 || inPosition.z != 0.0) {
229 _positional = YES;
230 }
231 if (_channel) {
232 [_channel setPosition:_position];
233 }
234}
235
237{
238 return _position;
239}
240
241- (void)setGain:(float)gain
242{
243 _gain = gain;
244 if (_channel) {
245 [_channel setGain:_gain];
246 }
247}
248
249- (float)gain
250{
251 return _gain;
252}
253
254/* Following not yet implemented */
255- (void)setVelocity:(Vector)inVelocity
256{
257}
258
259- (void)setOrientation:(Vector)inOrientation
260{
261}
262
263- (void)setConeAngle:(float)inAngle
264{
265}
266
267- (void)setGainInsideCone:(float)inInside outsideCone:(float)inOutside
268{
269}
270
271- (void)positionRelativeTo:(OOSoundReferencePoint*)inPoint
272{
273}
274
275// OOSoundChannelDelegate
276- (void)channel:(OOSoundChannel*)channel didFinishPlayingSound:(OOSound*)sound
277{
278 assert(_channel == channel);
279
281
282 if (--_remainingCount) {
283 [_channel playSound:[self sound] looped:NO];
284 } else {
285 [_channel setDelegate:nil];
287 _channel = nil;
288 [self release];
289 }
291}
292
293+ (void)channel:(OOSoundChannel*)inChannel didFinishPlayingSound:(OOSound*)inSound
294{
295 // This delegate is used for a stopped source
297}
298
299@end
#define EXPECT_NOT(x)
unsigned count
return nil
#define OOSoundReleaseLock()
#define OOSoundAcquireLock()
static NSMutableSet * sPlayingSoundSources
const Vector kZeroVector
Definition OOVector.m:27
BOOL playSound:looped:(OOSound *sound,[looped] BOOL loop)
void setGain:(float gain)
void setDelegate:(id delegate)
void setPosition:(Vector vector)
void pushChannel:(OOSoundChannel *channel)
OOSoundChannel * popChannel()
void playSound:repeatCount:(OOSound *inSound,[repeatCount] uint8_t inCount)
uint8_t repeatCount()
OOSound * _sound
OOSoundChannel * _channel
void setRepeatCount:(uint8_t inCount)
NSString * descriptionComponents()
void playSound:(OOSound *inSound)
void setPosition:(Vector inPosition)
void setSound:(OOSound *inSound)
uint8_t _remainingCount
OOSound * sound()
uint8_t _repeatCount
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34