Oolite
Loading...
Searching...
No Matches
OOALSoundChannel.m
Go to the documentation of this file.
1/*
2
3OOALSoundChannel.m
4
5OOALSound - OpenAL sound implementation for Oolite.
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 "OOALSoundChannel.h"
29#import "OOALSound.h"
30#import "OOLogging.h"
31#import "OOMaths.h"
32
33@interface OOSoundChannel (Private)
34
35- (BOOL)enqueueBuffer:(OOSound*)sound;
36- (void)hasStopped;
37- (void)getNextSoundBuffer;
38
39@end
40
41@implementation OOSoundChannel
42
43- (id)init
44{
45 if ((self = [super init])) {
46 ALuint error;
47 OOAL(alGenSources(1, &_source));
48 if ((error = alGetError()) != AL_NO_ERROR) {
49 OOLog(kOOLogSoundInitError, @"%@", @"Could not create OpenAL source");
50 [self release];
51 self = nil;
52 } else {
53 // sources are all relative to listener, defaulting to zero vector
54 OOAL(alSourcei(_source, AL_SOURCE_RELATIVE, AL_TRUE));
55 OOAL(alSource3f(_source, AL_POSITION, 0.0f, 0.0f, 0.0f));
56 }
57 }
58 return self;
59}
60
61- (void)dealloc
62{
63 [self hasStopped]; // make sure buffers are dequeued and deleted
64 OOAL(alDeleteSources(1, &_source));
65 [super dealloc];
66}
67
68- (void)update
69{
70 // Check if we've reached the end of a sound.
71 if (_sound != nil) {
72 ALint check;
73 OOAL(alGetSourcei(_source, AL_SOURCE_STATE, &check));
74 if (check == AL_STOPPED) {
75 [self hasStopped];
76 } else if ([_sound soundIncomplete]) // streaming and not finished loading
77 {
78 OOLog(@"sound.buffer", @"Incomplete, trying next for %@", [_sound name]);
79 [self getNextSoundBuffer];
80 } else if (_loop) {
81 OOLog(@"sound.buffer", @"Looping, trying restart for %@", [_sound name]);
82 // sound is complete, but needs to be looped, so start it again
83 [_sound rewind];
84 [self getNextSoundBuffer];
85 }
86 }
87}
88
90{
91 if (!_bigSound) {
92 // we've only loaded one buffer so far
93 _bigSound = YES;
94 _lastBuffer = _buffer;
95 [self enqueueBuffer:_sound];
96 } else {
97 // _lastBuffer has something in it, so only queue up
98 // another one if we've finished with that
99 ALint processed = 0;
100 OOAL(alGetSourcei(_source, AL_BUFFERS_PROCESSED, &processed));
101 if (processed > 0) // slot free
102 {
103 // dequeue and delete lastBuffer
104 ALuint buffer;
105 OOAL(alSourceUnqueueBuffers(_source, 1, &buffer));
106 assert(buffer == _lastBuffer);
107 OOAL(alDeleteBuffers(1, &_lastBuffer));
108 // shuffle along, and grab the next bit
109 _lastBuffer = _buffer;
110 [self enqueueBuffer:_sound];
111 }
112 }
113}
114
115- (void)setDelegate:(id)delegate
116{
117 _delegate = delegate;
118}
119
121{
122 return _next;
123}
124
125- (void)setNext:(OOSoundChannel*)next
126{
127 _next = next;
128}
129
130- (void)setPosition:(Vector)vector
131{
132 OOAL(alSource3f(_source, AL_POSITION, vector.x, vector.y, vector.z));
133}
134
135- (void)setGain:(float)gain
136{
137 OOAL(alSourcef(_source, AL_GAIN, gain));
138}
139
140- (BOOL)playSound:(OOSound*)sound looped:(BOOL)loop
141{
142 if (sound == nil)
143 return NO;
144
145 if (_sound != nil)
146 [self stop];
147
148 _loop = loop;
149 _bigSound = NO;
150 [sound rewind];
151 if ([self enqueueBuffer:sound]) {
152 _sound = [sound retain];
153 return YES;
154 } else {
155 return NO;
156 }
157}
158
159- (void)stop
160{
161 if (_sound != nil) {
162 OOAL(alSourceStop(_source));
163 OOAL(alSourcei(_source, AL_BUFFER, AL_NONE));
164 [self hasStopped];
165 }
166}
167
169{
170 ALint queued;
171 OOAL(alGetSourcei(_source, AL_BUFFERS_QUEUED, &queued));
172
173 while (queued--) {
174 ALuint buffer;
175 OOAL(alSourceUnqueueBuffers(_source, 1, &buffer));
176 }
177
178 OOAL(alDeleteBuffers(1, &_buffer));
179 if (_bigSound) {
180 // then we have two buffers to cleanup
181 OOAL(alDeleteBuffers(1, &_lastBuffer));
182 }
183 _bigSound = NO;
184
185 OOSound* sound = _sound;
186 _sound = nil;
187
188 if (nil != _delegate && [_delegate respondsToSelector:@selector(channel:didFinishPlayingSound:)]) {
189 [_delegate channel:self didFinishPlayingSound:sound];
190 }
191 [sound release];
192}
193
194- (BOOL)enqueueBuffer:(OOSound*)sound
195{
196 // get sound data
197 _buffer = [sound soundBuffer];
198 // bind sound data to buffer
199 OOAL(alSourceQueueBuffers(_source, 1, &_buffer));
200 ALuint error;
201 if ((error = alGetError()) != AL_NO_ERROR) {
202 OOLog(@"ov.debug", @"Error %u queueing buffers (_source: %u (%p), _buffer: %u (%p))",
203 error, _source, &_source, _buffer, &_buffer);
204 return NO;
205 }
206 ALint playing = 0;
207 OOAL(alGetSourcei(_source, AL_SOURCE_STATE, &playing));
208 if (playing != AL_PLAYING) {
209 OOAL(alSourcePlay(_source));
210 if ((error = alGetError()) != AL_NO_ERROR) {
211 OOLog(@"ov.debug", @"Error %d playing source", error);
212 return NO;
213 }
214 }
215 return YES;
216}
217
219{
220 return _sound;
221}
222
223@end
#define OOLog(class, format,...)
Definition OOLogging.h:84
static NSString *const kOOLogSoundInitError
#define OOAL(cmd)
Definition OOOpenAL.h:40
return nil
OOSoundChannel * next()
BOOL enqueueBuffer:(OOSound *sound)
OOSoundChannel * _next
void rewind()
Definition OOALSound.m:159
ALuint soundBuffer()
Definition OOALSound.m:148
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34