1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | |
---|
15 | code has been taken from http://www.devmaster.net/articles.php?catID=6 |
---|
16 | The code has been applied to our needs, and many things have been changed. |
---|
17 | */ |
---|
18 | |
---|
19 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
20 | |
---|
21 | #include "sound_engine.h" |
---|
22 | |
---|
23 | //#include <AL/alc.h> // maybe later |
---|
24 | #include "class_list.h" |
---|
25 | |
---|
26 | #include "p_node.h" |
---|
27 | #include "list.h" |
---|
28 | #include "resource_manager.h" |
---|
29 | #include "debug.h" |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | ////////////////// |
---|
35 | /* SOUND-BUFFER */ |
---|
36 | ////////////////// |
---|
37 | /** |
---|
38 | * Creates a Soundbuffer out of an inputfile |
---|
39 | * @param fileName The name of the File |
---|
40 | */ |
---|
41 | SoundBuffer::SoundBuffer(const char* fileName) |
---|
42 | { |
---|
43 | this->setClassID(CL_SOUND_BUFFER, "SoundBuffer"); |
---|
44 | this->setName(fileName); |
---|
45 | |
---|
46 | SoundEngine::getInstance()->addBuffer(this); |
---|
47 | |
---|
48 | ALenum format; |
---|
49 | ALvoid* data; |
---|
50 | ALsizei freq; |
---|
51 | |
---|
52 | ALenum result; |
---|
53 | |
---|
54 | // generate a Buffer |
---|
55 | alGenBuffers(1, &this->bufferID); |
---|
56 | if ((result = alGetError()) != AL_NO_ERROR) |
---|
57 | SoundEngine::PrintALErrorString(result); |
---|
58 | |
---|
59 | // read in the wav data |
---|
60 | /* according to http://www.edenwaith.com/products/pige/tutorials/openal.php the alutLoadWAVFile differs from platform to platform*/ |
---|
61 | #ifdef __APPLE__ |
---|
62 | alutLoadWAVFile(fileName, &format, &data, &this->size, &freq); |
---|
63 | #elifdef __WIN32__ |
---|
64 | alutLoadWAVFile(fileName, &format, &data, &size, &freq, &this->loop); |
---|
65 | #else |
---|
66 | alutLoadWAVFile((ALbyte*)fileName, &format, &data, &this->size, &freq, &this->loop); |
---|
67 | #endif |
---|
68 | if ((result = alGetError()) != AL_NO_ERROR) |
---|
69 | SoundEngine::PrintALErrorString(result); |
---|
70 | |
---|
71 | // send the loaded wav data to the buffer |
---|
72 | alBufferData(this->bufferID, format, data, this->size, freq); |
---|
73 | if ((result = alGetError()) != AL_NO_ERROR) |
---|
74 | SoundEngine::PrintALErrorString(result); |
---|
75 | |
---|
76 | // remove the wav data (redundant) |
---|
77 | alutUnloadWAV(format, data, this->size, freq); |
---|
78 | if ((result = alGetError()) != AL_NO_ERROR) |
---|
79 | SoundEngine::PrintALErrorString(result); |
---|
80 | } |
---|
81 | |
---|
82 | SoundBuffer::~SoundBuffer() |
---|
83 | { |
---|
84 | // SoundEngine::getInstance()->removeBuffer(this); |
---|
85 | alDeleteBuffers(1, &this->bufferID); |
---|
86 | } |
---|
87 | |
---|
88 | ////////////////// |
---|
89 | /* SOUND-SOURCE */ |
---|
90 | ////////////////// |
---|
91 | /** |
---|
92 | * creates a SoundSource at position sourceNode with the SoundBuffer buffer |
---|
93 | */ |
---|
94 | SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer) |
---|
95 | { |
---|
96 | this->setClassID(CL_SOUND_SOURCE, "SoundSource"); |
---|
97 | |
---|
98 | ALenum result; |
---|
99 | |
---|
100 | // adding the Source to the SourcesList of the SoundEngine |
---|
101 | SoundEngine::getInstance()->addSource(this); |
---|
102 | |
---|
103 | this->buffer = buffer; |
---|
104 | this->sourceNode = sourceNode; |
---|
105 | |
---|
106 | alGenSources(1, &this->sourceID); |
---|
107 | if ((result = alGetError()) != AL_NO_ERROR) |
---|
108 | SoundEngine::PrintALErrorString(result); |
---|
109 | if (this->buffer != NULL) |
---|
110 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
111 | alSourcef (this->sourceID, AL_PITCH, 1.0 ); |
---|
112 | alSourcef (this->sourceID, AL_GAIN, 1.0 ); |
---|
113 | alSourcei (sourceID, AL_LOOPING, AL_FALSE ); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * deletes a SoundSource |
---|
118 | */ |
---|
119 | SoundSource::~SoundSource() |
---|
120 | { |
---|
121 | //SoundEngine::getInstance()->removeSource(this); |
---|
122 | alDeleteSources(1, &this->sourceID); |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Plays back a SoundSource |
---|
127 | */ |
---|
128 | void SoundSource::play() |
---|
129 | { |
---|
130 | alSourcePlay(this->sourceID); |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Plays back buffer on this Source |
---|
135 | * @param buffer the buffer to play back on this Source |
---|
136 | */ |
---|
137 | void SoundSource::play(const SoundBuffer* buffer) |
---|
138 | { |
---|
139 | alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); |
---|
140 | alSourcePlay(this->sourceID); |
---|
141 | |
---|
142 | if (unlikely(this->buffer != NULL)) |
---|
143 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Stops playback of a SoundSource |
---|
148 | */ |
---|
149 | void SoundSource::stop() |
---|
150 | { |
---|
151 | alSourceStop(this->sourceID); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Pauses Playback of a SoundSource |
---|
156 | */ |
---|
157 | void SoundSource::pause() |
---|
158 | { |
---|
159 | alSourcePause(this->sourceID); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * Rewinds Playback of a SoundSource |
---|
164 | */ |
---|
165 | void SoundSource::rewind() |
---|
166 | { |
---|
167 | alSourceRewind(this->sourceID); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * sets the RolloffFactor of the Sound emitted from the SoundSource |
---|
172 | * @param rolloffFactor The Factor described |
---|
173 | |
---|
174 | this tells openAL how fast the Sounds decay outward from the Source |
---|
175 | */ |
---|
176 | void SoundSource::setRolloffFactor(ALfloat rolloffFactor) |
---|
177 | { |
---|
178 | alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | ////////////////// |
---|
184 | /* SOUND-ENGINE */ |
---|
185 | ////////////////// |
---|
186 | /** |
---|
187 | * standard constructor |
---|
188 | */ |
---|
189 | SoundEngine::SoundEngine () |
---|
190 | { |
---|
191 | this->setClassID(CL_SOUND_ENGINE, "SoundEngine"); |
---|
192 | this->setName("SoundEngine"); |
---|
193 | |
---|
194 | this->listener = NULL; |
---|
195 | this->bufferList = NULL; |
---|
196 | this->sourceList = NULL; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * the singleton reference to this class |
---|
201 | */ |
---|
202 | SoundEngine* SoundEngine::singletonRef = NULL; |
---|
203 | |
---|
204 | /** |
---|
205 | * standard deconstructor |
---|
206 | |
---|
207 | */ |
---|
208 | SoundEngine::~SoundEngine () |
---|
209 | { |
---|
210 | SoundEngine::singletonRef = NULL; |
---|
211 | |
---|
212 | // deleting all the SoundSources |
---|
213 | if(this->sourceList != NULL) |
---|
214 | { |
---|
215 | tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator(); |
---|
216 | SoundSource* enumSource = (SoundSource*)sourceIterator->nextElement(); |
---|
217 | while (enumSource) |
---|
218 | { |
---|
219 | delete enumSource; |
---|
220 | enumSource = (SoundSource*)sourceIterator->nextElement(); |
---|
221 | } |
---|
222 | delete sourceIterator; |
---|
223 | } |
---|
224 | |
---|
225 | // deleting all the SoundBuffers |
---|
226 | if (this->bufferList != NULL) |
---|
227 | { |
---|
228 | tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator(); |
---|
229 | SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->nextElement(); |
---|
230 | while (enumBuffer) |
---|
231 | { |
---|
232 | ResourceManager::getInstance()->unload(enumBuffer); |
---|
233 | enumBuffer = (SoundBuffer*)bufferIterator->nextElement(); |
---|
234 | } |
---|
235 | delete bufferIterator; |
---|
236 | } |
---|
237 | // removing openAL from AudioResource |
---|
238 | alutExit(); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * creates a new SoundSource. |
---|
243 | * @param fileName The Name to load the SoundBuffer from |
---|
244 | * @param sourceNode The sourceNode to bind this SoundSource to. |
---|
245 | * @returns The newly created SoundSource |
---|
246 | |
---|
247 | acctualy this is nothing more than a wrapper around the ResourceManager. |
---|
248 | */ |
---|
249 | SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode) |
---|
250 | { |
---|
251 | return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL)); |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * Sets the doppler values of openAL |
---|
256 | * @param dopplerFactor the extent of the doppler-effect |
---|
257 | * @param dopplerVelocity the Speed the sound travels |
---|
258 | */ |
---|
259 | void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity) |
---|
260 | { |
---|
261 | alDopplerFactor(dopplerFactor); |
---|
262 | alDopplerVelocity(dopplerVelocity); |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | /** |
---|
267 | * adds a SoundBuffer to the bufferList of the SoundEngine |
---|
268 | * @param buffer The buffer to add to the bufferList |
---|
269 | */ |
---|
270 | void SoundEngine::addBuffer(SoundBuffer* buffer) |
---|
271 | { |
---|
272 | if (unlikely(this->bufferList == NULL)) |
---|
273 | this->bufferList = ClassList::getList(CL_SOUND_BUFFER); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * removes a SoundBuffer from the bufferList of the SoundEngine |
---|
278 | * @param buffer The buffer to delete from the SoundEngine |
---|
279 | */ |
---|
280 | void SoundEngine::removeBuffer(SoundBuffer* buffer) |
---|
281 | { |
---|
282 | // look if there are any sources that have the buffer still loaded |
---|
283 | tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator(); |
---|
284 | SoundSource* enumSource = (SoundSource*)sourceIterator->nextElement(); |
---|
285 | while (enumSource) |
---|
286 | { |
---|
287 | if (buffer == enumSource->getBuffer()) |
---|
288 | delete enumSource; |
---|
289 | enumSource = (SoundSource*)sourceIterator->nextElement(); |
---|
290 | } |
---|
291 | delete sourceIterator; |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * adds a SoundSource to the sourceList of the SoundEngine |
---|
296 | * @param source The source to add to the sourceList |
---|
297 | */ |
---|
298 | void SoundEngine::addSource(SoundSource* source) |
---|
299 | { |
---|
300 | this->sourceList = ClassList::getList(CL_SOUND_SOURCE); |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * updates all The positions, Directions and Velocities of all Sounds |
---|
305 | */ |
---|
306 | void SoundEngine::update() |
---|
307 | { |
---|
308 | |
---|
309 | // updating the Listeners Position |
---|
310 | if (likely(this->listener != NULL)) |
---|
311 | { |
---|
312 | alListener3f(AL_POSITION, |
---|
313 | this->listener->getAbsCoor().x, |
---|
314 | this->listener->getAbsCoor().y, |
---|
315 | this->listener->getAbsCoor().z); |
---|
316 | alListener3f(AL_VELOCITY, |
---|
317 | this->listener->getVelocity().x, |
---|
318 | this->listener->getVelocity().y, |
---|
319 | this->listener->getVelocity().z); |
---|
320 | Vector absDirV = this->listener->getAbsDirV(); |
---|
321 | ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z}; |
---|
322 | alListenerfv(AL_ORIENTATION, orientation); |
---|
323 | } |
---|
324 | else |
---|
325 | PRINTF(2)("no listener defined\n"); |
---|
326 | |
---|
327 | // updating all the Sources positions |
---|
328 | if (likely(this->sourceList != NULL)) |
---|
329 | { |
---|
330 | tIterator<BaseObject>* iterator = this->sourceList->getIterator(); |
---|
331 | SoundSource* enumSource = (SoundSource*)iterator->nextElement(); |
---|
332 | while (enumSource) |
---|
333 | { |
---|
334 | if (likely(enumSource->getNode()!=NULL)) |
---|
335 | { |
---|
336 | alSource3f(enumSource->getID(), AL_POSITION, |
---|
337 | enumSource->getNode()->getAbsCoor().x, |
---|
338 | enumSource->getNode()->getAbsCoor().y, |
---|
339 | enumSource->getNode()->getAbsCoor().z); |
---|
340 | alSource3f(enumSource->getID(), AL_VELOCITY, |
---|
341 | enumSource->getNode()->getVelocity().x, |
---|
342 | enumSource->getNode()->getVelocity().y, |
---|
343 | enumSource->getNode()->getVelocity().z); |
---|
344 | } |
---|
345 | enumSource = (SoundSource*)iterator->nextElement(); |
---|
346 | } |
---|
347 | delete iterator; |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | /** |
---|
352 | * Removes all the Buffers that are not anymore needed by any Sources |
---|
353 | */ |
---|
354 | void SoundEngine::flushUnusedBuffers() |
---|
355 | { |
---|
356 | if(this->sourceList && this->bufferList) |
---|
357 | { |
---|
358 | tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator(); |
---|
359 | SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->nextElement(); |
---|
360 | while (enumBuffer) |
---|
361 | { |
---|
362 | tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator(); |
---|
363 | SoundSource* enumSource = (SoundSource*)sourceIterator->nextElement(); |
---|
364 | while (enumSource) |
---|
365 | { |
---|
366 | if (enumBuffer == enumSource->getBuffer()) |
---|
367 | break; |
---|
368 | enumSource = (SoundSource*)sourceIterator->nextElement(); |
---|
369 | } |
---|
370 | delete sourceIterator; |
---|
371 | if (enumSource == NULL) |
---|
372 | ResourceManager::getInstance()->unload(enumBuffer); |
---|
373 | enumBuffer = (SoundBuffer*)bufferIterator->nextElement(); |
---|
374 | } |
---|
375 | delete bufferIterator; |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | /** |
---|
380 | * SourceEngine::flushAllBuffers |
---|
381 | */ |
---|
382 | void SoundEngine::flushAllBuffers() |
---|
383 | { |
---|
384 | if (this->bufferList) |
---|
385 | { |
---|
386 | tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator(); |
---|
387 | SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->nextElement(); |
---|
388 | while (enumBuffer) |
---|
389 | { |
---|
390 | ResourceManager::getInstance()->unload(enumBuffer, RP_LEVEL); |
---|
391 | enumBuffer = (SoundBuffer*)bufferIterator->nextElement(); |
---|
392 | } |
---|
393 | delete bufferIterator; |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * SourceEngine::flushAllBuffers |
---|
399 | */ |
---|
400 | void SoundEngine::flushAllSources() |
---|
401 | { |
---|
402 | if (this->sourceList) |
---|
403 | { |
---|
404 | tIterator<BaseObject>* Iterator = this->sourceList->getIterator(); |
---|
405 | SoundSource* enumSource = (SoundSource*)Iterator->nextElement(); |
---|
406 | while (enumSource) |
---|
407 | { |
---|
408 | delete enumSource; |
---|
409 | enumSource = (SoundSource*)Iterator->nextElement(); |
---|
410 | } |
---|
411 | delete Iterator; |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | /** |
---|
416 | * initializes Audio in general |
---|
417 | */ |
---|
418 | bool SoundEngine::initAudio() |
---|
419 | { |
---|
420 | ALenum result; |
---|
421 | |
---|
422 | PRINTF(3)("Initialisazing openAL sound library\n"); |
---|
423 | |
---|
424 | alutInit(NULL, 0); |
---|
425 | if ((result = alGetError()) != AL_NO_ERROR) |
---|
426 | SoundEngine::PrintALErrorString(result); |
---|
427 | |
---|
428 | this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY); |
---|
429 | } |
---|
430 | |
---|
431 | /** |
---|
432 | * Transforms AL-errors into something readable |
---|
433 | * @param err The error found |
---|
434 | */ |
---|
435 | void SoundEngine::PrintALErrorString(ALenum err) |
---|
436 | { |
---|
437 | switch(err) |
---|
438 | { |
---|
439 | case AL_NO_ERROR: |
---|
440 | PRINTF(4)("AL_NO_ERROR\n"); |
---|
441 | break; |
---|
442 | |
---|
443 | case AL_INVALID_NAME: |
---|
444 | PRINTF(2)("AL_INVALID_NAME\n"); |
---|
445 | break; |
---|
446 | |
---|
447 | case AL_INVALID_ENUM: |
---|
448 | PRINTF(2)("AL_INVALID_ENUM\n"); |
---|
449 | break; |
---|
450 | |
---|
451 | case AL_INVALID_VALUE: |
---|
452 | PRINTF(2)("AL_INVALID_VALUE\n"); |
---|
453 | break; |
---|
454 | |
---|
455 | case AL_INVALID_OPERATION: |
---|
456 | PRINTF(2)("AL_INVALID_OPERATION\n"); |
---|
457 | break; |
---|
458 | |
---|
459 | case AL_OUT_OF_MEMORY: |
---|
460 | PRINTF(2)("AL_OUT_OF_MEMORY\n"); |
---|
461 | break; |
---|
462 | }; |
---|
463 | } |
---|
464 | |
---|
465 | void SoundEngine::listDevices() |
---|
466 | { |
---|
467 | |
---|
468 | printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER)); |
---|
469 | } |
---|
470 | |
---|
471 | /* |
---|
472 | void SoundEngine::PrintALCErrorString(ALenum err) |
---|
473 | { |
---|
474 | switch(err) |
---|
475 | { |
---|
476 | case ALC_NO_ERROR: |
---|
477 | PRINTF(4)("AL_NO_ERROR\n"); |
---|
478 | break; |
---|
479 | |
---|
480 | case ALC_INVALID_DEVICE: |
---|
481 | PRINTF(2)("ALC_INVALID_DEVICE\n"); |
---|
482 | break; |
---|
483 | |
---|
484 | case ALC_INVALID_CONTEXT: |
---|
485 | PRINTF(2)("ALC_INVALID_CONTEXT\n"); |
---|
486 | break; |
---|
487 | |
---|
488 | case ALC_INVALID_ENUM: |
---|
489 | PRINTF(2)("ALC_INVALID_ENUM\n"); |
---|
490 | break; |
---|
491 | |
---|
492 | case ALC_INVALID_VALUE: |
---|
493 | PRINTF(2)("ALC_INVALID_VALUE\n"); |
---|
494 | break; |
---|
495 | |
---|
496 | case ALC_OUT_OF_MEMORY: |
---|
497 | PRINTF(2)("ALC_OUT_OF_MEMORY\n"); |
---|
498 | break; |
---|
499 | }; |
---|
500 | } |
---|
501 | */ |
---|