33@interface OOSoundChannel (Private)
35- (BOOL) enqueueBuffer:(
OOSound *)sound;
46 if ((
self = [super init]))
49 OOAL(alGenSources(1,&_source));
50 if ((error = alGetError()) != AL_NO_ERROR)
59 OOAL(alSourcei(_source, AL_SOURCE_RELATIVE, AL_TRUE));
60 OOAL(alSource3f(_source, AL_POSITION, 0.0f, 0.0f, 0.0f));
70 OOAL(alDeleteSources(1, &_source));
80 OOAL(alGetSourcei(_source,AL_SOURCE_STATE,&check));
81 if (check == AL_STOPPED)
85 else if ([_sound soundIncomplete])
87 OOLog(
@"sound.buffer",
@"Incomplete, trying next for %@", [_sound name]);
88 [
self getNextSoundBuffer];
92 OOLog(
@"sound.buffer",
@"Looping, trying restart for %@", [_sound name]);
95 [
self getNextSoundBuffer];
107 _lastBuffer = _buffer;
108 [
self enqueueBuffer:_sound];
115 OOAL(alGetSourcei(_source, AL_BUFFERS_PROCESSED, &processed));
120 OOAL(alSourceUnqueueBuffers(_source, 1, &buffer));
121 assert(buffer == _lastBuffer);
122 OOAL(alDeleteBuffers(1,&_lastBuffer));
124 _lastBuffer = _buffer;
125 [
self enqueueBuffer:_sound];
131- (void) setDelegate:(
id)delegate
133 _delegate = delegate;
149- (void) setPosition:(Vector) vector
151 OOAL(alSource3f(_source, AL_POSITION, vector.x, vector.y, vector.z));
155- (void) setGain:(
float) gain
157 OOAL(alSourcef(_source, AL_GAIN, gain));
161- (BOOL) playSound:(
OOSound *)sound looped:(BOOL)loop
163 if (sound ==
nil)
return NO;
165 if (_sound !=
nil) [
self stop];
170 if ([
self enqueueBuffer:sound])
172 _sound = [sound retain];
186 OOAL(alSourceStop(_source));
187 OOAL(alSourcei(_source, AL_BUFFER, AL_NONE));
196 OOAL(alGetSourcei(_source, AL_BUFFERS_QUEUED, &queued));
201 OOAL(alSourceUnqueueBuffers(_source, 1, &buffer));
204 OOAL(alDeleteBuffers(1,&_buffer));
208 OOAL(alDeleteBuffers(1,&_lastBuffer));
216 if (
nil != _delegate && [_delegate respondsToSelector:
@selector(channel:didFinishPlayingSound:)])
218 [_delegate channel:self didFinishPlayingSound:sound];
224- (BOOL) enqueueBuffer:(
OOSound *)sound
229 OOAL(alSourceQueueBuffers(_source, 1, &_buffer));
231 if ((error = alGetError()) != AL_NO_ERROR)
233 OOLog(
@"ov.debug",
@"Error %u queueing buffers (_source: %u (%p), _buffer: %u (%p))",
234 error, _source, &_source, _buffer, &_buffer);
238 OOAL(alGetSourcei(_source,AL_SOURCE_STATE,&playing));
239 if (playing != AL_PLAYING)
241 OOAL(alSourcePlay(_source));
242 if ((error = alGetError()) != AL_NO_ERROR)
244 OOLog(
@"ov.debug",
@"Error %d playing source",error);
#define OOLog(class, format,...)
static NSString *const kOOLogSoundInitError
void getNextSoundBuffer()