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

#include <OOALSoundChannel.h>

+ Inheritance diagram for OOSoundChannel:
+ Collaboration diagram for OOSoundChannel:

Instance Methods

(void) - update
 
(void) - setDelegate:
 
(OOSoundChannel *) - next
 
(void) - setNext:
 
(void) - setPosition:
 
(void) - setGain:
 
(BOOL) - playSound:looped:
 
(void) - stop
 
(OOSound *) - sound
 
(id) - init [implementation]
 
(void) - dealloc [implementation]
 
(void) - getNextSoundBuffer [implementation]
 
(void) - hasStopped [implementation]
 
(BOOL) - enqueueBuffer: [implementation]
 

Protected Attributes

OOSoundChannel_next
 
id _delegate
 
OOSound_sound
 
ALuint _buffer
 
ALuint _lastBuffer
 
BOOL _bigSound
 
ALuint _source
 
BOOL _playing
 
BOOL _loop
 

Detailed Description

Definition at line 40 of file OOALSoundChannel.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Definition at line 1 of file OOALSoundChannel.m.

68{
69 [self hasStopped]; // make sure buffers are dequeued and deleted
70 OOAL(alDeleteSources(1, &_source));
71 [super dealloc];
72}
#define OOAL(cmd)
Definition OOOpenAL.h:40

◆ enqueueBuffer:

- (BOOL) enqueueBuffer: (OOSound *) sound
implementation

Provided by category OOSoundChannel(Private).

Definition at line 1 of file OOALSoundChannel.m.

224 :(OOSound *)sound
225{
226 // get sound data
227 _buffer = [sound soundBuffer];
228 // bind sound data to buffer
229 OOAL(alSourceQueueBuffers(_source, 1, &_buffer));
230 ALuint error;
231 if ((error = alGetError()) != AL_NO_ERROR)
232 {
233 OOLog(@"ov.debug", @"Error %u queueing buffers (_source: %u (%p), _buffer: %u (%p))",
234 error, _source, &_source, _buffer, &_buffer);
235 return NO;
236 }
237 ALint playing = 0;
238 OOAL(alGetSourcei(_source,AL_SOURCE_STATE,&playing));
239 if (playing != AL_PLAYING)
240 {
241 OOAL(alSourcePlay(_source));
242 if ((error = alGetError()) != AL_NO_ERROR)
243 {
244 OOLog(@"ov.debug",@"Error %d playing source",error);
245 return NO;
246 }
247 }
248 return YES;
249}
#define OOLog(class, format,...)
Definition OOLogging.h:88
ALuint soundBuffer()
Definition OOALSound.m:162

◆ getNextSoundBuffer

- (void) getNextSoundBuffer
implementation

Provided by category OOSoundChannel(Private).

Definition at line 1 of file OOALSoundChannel.m.

102{
103 if (!_bigSound)
104 {
105 // we've only loaded one buffer so far
106 _bigSound = YES;
108 [self enqueueBuffer:_sound];
109 }
110 else
111 {
112 // _lastBuffer has something in it, so only queue up
113 // another one if we've finished with that
114 ALint processed = 0;
115 OOAL(alGetSourcei(_source, AL_BUFFERS_PROCESSED, &processed));
116 if (processed > 0) // slot free
117 {
118 // dequeue and delete lastBuffer
119 ALuint buffer;
120 OOAL(alSourceUnqueueBuffers(_source, 1, &buffer));
121 assert(buffer == _lastBuffer);
122 OOAL(alDeleteBuffers(1,&_lastBuffer));
123 // shuffle along, and grab the next bit
125 [self enqueueBuffer:_sound];
126 }
127 }
128}

◆ hasStopped

- (void) hasStopped
implementation

Provided by category OOSoundChannel(Private).

Definition at line 1 of file OOALSoundChannel.m.

194{
195 ALint queued;
196 OOAL(alGetSourcei(_source, AL_BUFFERS_QUEUED, &queued));
197
198 while (queued--)
199 {
200 ALuint buffer;
201 OOAL(alSourceUnqueueBuffers(_source, 1, &buffer));
202 }
203
204 OOAL(alDeleteBuffers(1,&_buffer));
205 if (_bigSound)
206 {
207 // then we have two buffers to cleanup
208 OOAL(alDeleteBuffers(1,&_lastBuffer));
209 }
210 _bigSound = NO;
211
212
214 _sound = nil;
215
216 if (nil != _delegate && [_delegate respondsToSelector:@selector(channel:didFinishPlayingSound:)])
217 {
218 [_delegate channel:self didFinishPlayingSound:sound];
219 }
220 [sound release];
221}
return nil

◆ init

- (id) init
implementation

Definition at line 1 of file OOALSoundChannel.m.

45{
46 if ((self = [super init]))
47 {
48 ALuint error;
49 OOAL(alGenSources(1,&_source));
50 if ((error = alGetError()) != AL_NO_ERROR)
51 {
52 OOLog(kOOLogSoundInitError, @"%@", @"Could not create OpenAL source");
53 [self release];
54 self = nil;
55 }
56 else
57 {
58 // sources are all relative to listener, defaulting to zero vector
59 OOAL(alSourcei(_source, AL_SOURCE_RELATIVE, AL_TRUE));
60 OOAL(alSource3f(_source, AL_POSITION, 0.0f, 0.0f, 0.0f));
61 }
62 }
63 return self;
64}
static NSString *const kOOLogSoundInitError

◆ next

- (OOSoundChannel *) next

Definition at line 1 of file OOALSoundChannel.m.

138{
139 return _next;
140}
OOSoundChannel * _next

◆ playSound:looped:

- (BOOL) playSound: (OOSound *) sound
looped: (BOOL) loop 

Definition at line 1 of file OOALSoundChannel.m.

161 :(OOSound *)sound looped:(BOOL)loop
162{
163 if (sound == nil) return NO;
164
165 if (_sound != nil) [self stop];
166
167 _loop = loop;
168 _bigSound = NO;
169 [sound rewind];
170 if ([self enqueueBuffer:sound])
171 {
172 _sound = [sound retain];
173 return YES;
174 }
175 else
176 {
177 return NO;
178 }
179}
void rewind()
Definition OOALSound.m:175

◆ setDelegate:

- (void) setDelegate: (id) delegate

Definition at line 1 of file OOALSoundChannel.m.

131 :(id)delegate
132{
133 _delegate = delegate;
134}

◆ setGain:

- (void) setGain: (float) gain

Definition at line 1 of file OOALSoundChannel.m.

155 :(float) gain
156{
157 OOAL(alSourcef(_source, AL_GAIN, gain));
158}

◆ setNext:

- (void) setNext: (OOSoundChannel *) next

Definition at line 1 of file OOALSoundChannel.m.

144{
145 _next = next;
146}
OOSoundChannel * next()

◆ setPosition:

- (void) setPosition: (Vector) vector

Definition at line 1 of file OOALSoundChannel.m.

149 :(Vector) vector
150{
151 OOAL(alSource3f(_source, AL_POSITION, vector.x, vector.y, vector.z));
152}

◆ sound

- (OOSound *) sound

Definition at line 1 of file OOALSoundChannel.m.

254{
255 return _sound;
256}

◆ stop

- (void) stop

Definition at line 1 of file OOALSoundChannel.m.

183{
184 if (_sound != nil)
185 {
186 OOAL(alSourceStop(_source));
187 OOAL(alSourcei(_source, AL_BUFFER, AL_NONE));
188 [self hasStopped];
189 }
190}

◆ update

- (void) update

Definition at line 1 of file OOALSoundChannel.m.

75{
76 // Check if we've reached the end of a sound.
77 if (_sound != nil)
78 {
79 ALint check;
80 OOAL(alGetSourcei(_source,AL_SOURCE_STATE,&check));
81 if (check == AL_STOPPED)
82 {
83 [self hasStopped];
84 }
85 else if ([_sound soundIncomplete]) // streaming and not finished loading
86 {
87 OOLog(@"sound.buffer", @"Incomplete, trying next for %@", [_sound name]);
88 [self getNextSoundBuffer];
89 }
90 else if (_loop)
91 {
92 OOLog(@"sound.buffer", @"Looping, trying restart for %@", [_sound name]);
93 // sound is complete, but needs to be looped, so start it again
94 [_sound rewind];
95 [self getNextSoundBuffer];
96 }
97 }
98}

Member Data Documentation

◆ _bigSound

- (BOOL) _bigSound
protected

Definition at line 47 of file OOALSoundChannel.h.

◆ _buffer

- (ALuint) _buffer
protected

Definition at line 45 of file OOALSoundChannel.h.

◆ _delegate

- (id) _delegate
protected

Definition at line 43 of file OOALSoundChannel.h.

◆ _lastBuffer

- (ALuint) _lastBuffer
protected

Definition at line 46 of file OOALSoundChannel.h.

◆ _loop

- (BOOL) _loop
protected

Definition at line 50 of file OOALSoundChannel.h.

◆ _next

- (OOSoundChannel*) _next
protected

Definition at line 42 of file OOALSoundChannel.h.

◆ _playing

- (BOOL) _playing
protected

Definition at line 49 of file OOALSoundChannel.h.

◆ _sound

- (OOSound*) _sound
protected

Definition at line 44 of file OOALSoundChannel.h.

◆ _source

- (ALuint) _source
protected

Definition at line 48 of file OOALSoundChannel.h.


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