[4597] | 1 | /* |
---|
[4504] | 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 | |
---|
[5386] | 19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND |
---|
[4504] | 20 | |
---|
| 21 | #include "sound_engine.h" |
---|
| 22 | |
---|
[4960] | 23 | #include "class_list.h" |
---|
[4504] | 24 | |
---|
| 25 | #include "p_node.h" |
---|
[7193] | 26 | #include "util/loading/resource_manager.h" |
---|
[4504] | 27 | #include "debug.h" |
---|
[7256] | 28 | #include "util/preferences.h" |
---|
[5427] | 29 | #include "globals.h" |
---|
[4504] | 30 | |
---|
| 31 | using namespace std; |
---|
| 32 | |
---|
[4597] | 33 | |
---|
[4504] | 34 | ////////////////// |
---|
| 35 | /* SOUND-ENGINE */ |
---|
| 36 | ////////////////// |
---|
| 37 | /** |
---|
[4836] | 38 | * standard constructor |
---|
[4504] | 39 | */ |
---|
[4597] | 40 | SoundEngine::SoundEngine () |
---|
[4504] | 41 | { |
---|
[4597] | 42 | this->setClassID(CL_SOUND_ENGINE, "SoundEngine"); |
---|
| 43 | this->setName("SoundEngine"); |
---|
| 44 | |
---|
[4504] | 45 | this->listener = NULL; |
---|
[4960] | 46 | this->bufferList = NULL; |
---|
| 47 | this->sourceList = NULL; |
---|
[5930] | 48 | |
---|
| 49 | this->device = NULL; |
---|
| 50 | this->context = NULL; |
---|
| 51 | |
---|
| 52 | this->maxSourceCount = 32; |
---|
[6829] | 53 | |
---|
| 54 | this->effectsVolume = .80; |
---|
| 55 | this->musicVolume = .75; |
---|
[4504] | 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
[4836] | 59 | * the singleton reference to this class |
---|
[4504] | 60 | */ |
---|
| 61 | SoundEngine* SoundEngine::singletonRef = NULL; |
---|
| 62 | |
---|
| 63 | /** |
---|
[4836] | 64 | * standard deconstructor |
---|
[5293] | 65 | */ |
---|
[4597] | 66 | SoundEngine::~SoundEngine () |
---|
[4504] | 67 | { |
---|
| 68 | // deleting all the SoundSources |
---|
[4960] | 69 | if(this->sourceList != NULL) |
---|
| 70 | { |
---|
[5779] | 71 | while (this->sourceList->size() > 0) |
---|
[7290] | 72 | delete static_cast<SoundSource*>(this->sourceList->front()); |
---|
[4960] | 73 | } |
---|
[4504] | 74 | |
---|
[5930] | 75 | while(!this->ALSources.empty()) |
---|
| 76 | { |
---|
[7291] | 77 | if (alIsSource(this->ALSources.top())) |
---|
| 78 | { |
---|
| 79 | alDeleteSources(1, &this->ALSources.top()); |
---|
| 80 | SoundEngine::checkError("Deleting Source", __LINE__); |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | PRINTF(1)("%d is not a Source\n", this->ALSources.top()); |
---|
| 84 | |
---|
[5930] | 85 | this->ALSources.pop(); |
---|
| 86 | } |
---|
| 87 | |
---|
[4504] | 88 | // deleting all the SoundBuffers |
---|
[4960] | 89 | if (this->bufferList != NULL) |
---|
| 90 | { |
---|
[5779] | 91 | while(this->bufferList->size() > 0) |
---|
[7290] | 92 | ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front())); |
---|
[4960] | 93 | } |
---|
[5293] | 94 | |
---|
[4504] | 95 | // removing openAL from AudioResource |
---|
[5473] | 96 | //! @todo this should be terminated through alc |
---|
| 97 | //alutExit(); |
---|
[5293] | 98 | |
---|
| 99 | SoundEngine::singletonRef = NULL; |
---|
[4504] | 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
[4985] | 103 | * loads the settings of the SoundEngine from an ini-file |
---|
| 104 | */ |
---|
[7256] | 105 | void SoundEngine::loadSettings() |
---|
[4985] | 106 | { |
---|
[7256] | 107 | MultiType channels = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_AUDIO_CHANNELS, "32"); |
---|
[7221] | 108 | this->maxSourceCount = channels.getInt(); |
---|
[4985] | 109 | |
---|
[7256] | 110 | MultiType effectsVolume = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_EFFECTS_VOLUME, "80"); |
---|
[7221] | 111 | this->effectsVolume = effectsVolume.getFloat()/100.0; |
---|
[6830] | 112 | |
---|
[7256] | 113 | MultiType musicVolume = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_MUSIC_VOLUME, "75"); |
---|
[7221] | 114 | this->musicVolume = musicVolume.getFloat()/100.0; |
---|
[4985] | 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
[4836] | 118 | * creates a new SoundSource. |
---|
| 119 | * @param fileName The Name to load the SoundBuffer from |
---|
| 120 | * @param sourceNode The sourceNode to bind this SoundSource to. |
---|
| 121 | * @returns The newly created SoundSource |
---|
[4504] | 122 | |
---|
| 123 | acctualy this is nothing more than a wrapper around the ResourceManager. |
---|
| 124 | */ |
---|
[7221] | 125 | SoundSource* SoundEngine::createSource(const std::string& fileName, PNode* sourceNode) |
---|
[4504] | 126 | { |
---|
[4885] | 127 | return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL)); |
---|
[4504] | 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
[4836] | 131 | * Sets the doppler values of openAL |
---|
| 132 | * @param dopplerFactor the extent of the doppler-effect |
---|
| 133 | * @param dopplerVelocity the Speed the sound travels |
---|
[4504] | 134 | */ |
---|
| 135 | void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity) |
---|
| 136 | { |
---|
| 137 | alDopplerFactor(dopplerFactor); |
---|
[6840] | 138 | this->checkError("Setting Doppler Factor", __LINE__); |
---|
| 139 | |
---|
[4504] | 140 | alDopplerVelocity(dopplerVelocity); |
---|
[6840] | 141 | this->checkError("Setting Doppler Velocity", __LINE__); |
---|
[4504] | 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
[5930] | 145 | void SoundEngine::popALSource(ALuint& source) |
---|
[4504] | 146 | { |
---|
[7284] | 147 | assert (source == 0); |
---|
| 148 | |
---|
| 149 | /// @TODO try to create more sources if needed |
---|
| 150 | if (!this->ALSources.empty()) |
---|
[5930] | 151 | { |
---|
[7284] | 152 | source = this->ALSources.top(); |
---|
| 153 | this->ALSources.pop(); |
---|
[7292] | 154 | printf("Retrieve Source %d\n", source); |
---|
[7284] | 155 | } |
---|
[4504] | 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | /** |
---|
[4836] | 160 | * updates all The positions, Directions and Velocities of all Sounds |
---|
[4504] | 161 | */ |
---|
[4746] | 162 | void SoundEngine::update() |
---|
[4504] | 163 | { |
---|
| 164 | // updating the Listeners Position |
---|
| 165 | if (likely(this->listener != NULL)) |
---|
[6846] | 166 | { |
---|
| 167 | alListener3f(AL_POSITION, |
---|
| 168 | this->listener->getAbsCoor().x, |
---|
| 169 | this->listener->getAbsCoor().y, |
---|
| 170 | this->listener->getAbsCoor().z); |
---|
| 171 | alListener3f(AL_VELOCITY, |
---|
| 172 | this->listener->getVelocity().x, |
---|
| 173 | this->listener->getVelocity().y, |
---|
| 174 | this->listener->getVelocity().z); |
---|
| 175 | Vector absDirV = this->listener->getAbsDirV(); |
---|
| 176 | ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z}; |
---|
| 177 | alListenerfv(AL_ORIENTATION, orientation); |
---|
[7291] | 178 | SoundEngine::checkError("SoundEngine::update() - Listener Error", __LINE__); |
---|
[6846] | 179 | } |
---|
[4504] | 180 | else |
---|
| 181 | PRINTF(2)("no listener defined\n"); |
---|
| 182 | |
---|
| 183 | // updating all the Sources positions |
---|
[5930] | 184 | if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL)) |
---|
[4960] | 185 | { |
---|
[5885] | 186 | list<BaseObject*>::const_iterator sourceIT; |
---|
[5779] | 187 | SoundSource* source; |
---|
| 188 | for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++) |
---|
[4504] | 189 | { |
---|
[5779] | 190 | source = static_cast<SoundSource*>(*sourceIT); |
---|
[5930] | 191 | if (source->isPlaying()) |
---|
[4504] | 192 | { |
---|
[6073] | 193 | int play = 0x000; |
---|
[5930] | 194 | alGetSourcei(source->getID(), AL_SOURCE_STATE, &play); |
---|
[7291] | 195 | if (DEBUG > 2) |
---|
| 196 | SoundEngine::checkError("SoundEngine::update() Play", __LINE__); |
---|
[5930] | 197 | if(play == AL_PLAYING) |
---|
| 198 | { |
---|
| 199 | if (likely(source->getNode() != NULL)) |
---|
| 200 | { |
---|
| 201 | alSource3f(source->getID(), AL_POSITION, |
---|
| 202 | source->getNode()->getAbsCoor().x, |
---|
| 203 | source->getNode()->getAbsCoor().y, |
---|
| 204 | source->getNode()->getAbsCoor().z); |
---|
[7291] | 205 | if (DEBUG > 2) |
---|
| 206 | SoundEngine::checkError("SoundEngine::update() Set Source Position", __LINE__); |
---|
[5930] | 207 | alSource3f(source->getID(), AL_VELOCITY, |
---|
| 208 | source->getNode()->getVelocity().x, |
---|
| 209 | source->getNode()->getVelocity().y, |
---|
| 210 | source->getNode()->getVelocity().z); |
---|
[7291] | 211 | if (DEBUG > 2) |
---|
| 212 | SoundEngine::checkError("SoundEngine::update() Set Source Velocity", __LINE__); |
---|
[5930] | 213 | } |
---|
| 214 | } |
---|
| 215 | else |
---|
| 216 | { |
---|
| 217 | source->stop(); |
---|
| 218 | } |
---|
[4504] | 219 | } |
---|
| 220 | } |
---|
[4960] | 221 | } |
---|
[7290] | 222 | SoundEngine::checkError("SoundEngine::update()", __LINE__); |
---|
[4504] | 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
[4836] | 226 | * Removes all the Buffers that are not anymore needed by any Sources |
---|
[4504] | 227 | */ |
---|
[4746] | 228 | void SoundEngine::flushUnusedBuffers() |
---|
[4504] | 229 | { |
---|
[5819] | 230 | /// FIXME |
---|
[6846] | 231 | /* if(this->sourceList && this->bufferList) |
---|
[4504] | 232 | { |
---|
[6846] | 233 | tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator(); |
---|
| 234 | SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement(); |
---|
| 235 | while (enumBuffer) |
---|
[4960] | 236 | { |
---|
[6846] | 237 | tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator(); |
---|
| 238 | SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement(); |
---|
| 239 | while (enumSource) |
---|
| 240 | { |
---|
| 241 | if (enumBuffer == enumSource->getBuffer()) |
---|
| 242 | break; |
---|
| 243 | enumSource = (SoundSource*)sourceIterator->nextElement(); |
---|
| 244 | } |
---|
| 245 | delete sourceIterator; |
---|
| 246 | if (enumSource == NULL) |
---|
| 247 | ResourceManager::getInstance()->unload(enumBuffer); |
---|
| 248 | enumBuffer = (SoundBuffer*)bufferIterator->nextElement(); |
---|
[4960] | 249 | } |
---|
[6846] | 250 | delete bufferIterator; |
---|
| 251 | }*/ /// FIXME |
---|
[4504] | 252 | } |
---|
| 253 | |
---|
| 254 | /** |
---|
[5293] | 255 | * flushes all the Buffers |
---|
| 256 | * deletes them from the BufferList, and also removes them via the ResourceManager. |
---|
| 257 | */ |
---|
[4746] | 258 | void SoundEngine::flushAllBuffers() |
---|
[4504] | 259 | { |
---|
[4960] | 260 | if (this->bufferList) |
---|
| 261 | { |
---|
[5779] | 262 | while (this->bufferList->size() > 0) |
---|
| 263 | ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front()), RP_LEVEL); |
---|
[4960] | 264 | } |
---|
[4504] | 265 | } |
---|
| 266 | |
---|
| 267 | /** |
---|
[5293] | 268 | * deletes all the Sources. |
---|
[4830] | 269 | */ |
---|
| 270 | void SoundEngine::flushAllSources() |
---|
| 271 | { |
---|
[4960] | 272 | if (this->sourceList) |
---|
[4830] | 273 | { |
---|
[5779] | 274 | while(this->sourceList->size() > 0) |
---|
| 275 | delete this->sourceList->front(); |
---|
[4830] | 276 | } |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | /** |
---|
[4836] | 280 | * initializes Audio in general |
---|
[4504] | 281 | */ |
---|
[4746] | 282 | bool SoundEngine::initAudio() |
---|
[4504] | 283 | { |
---|
[7284] | 284 | // ALenum result; |
---|
| 285 | // PRINTF(3)("Initialisazing openAL sound engine\n"); |
---|
| 286 | // const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); |
---|
| 287 | // const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER); |
---|
| 288 | // const char* devWalk = deviceList; |
---|
| 289 | // // if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE) |
---|
| 290 | // { // try out enumeration extension |
---|
| 291 | // PRINTF(3)("Enumeration-extension found\n"); |
---|
| 292 | // |
---|
| 293 | // PRINTF(3)("Default device: %s\n", defaultDevice); |
---|
| 294 | // do |
---|
| 295 | // { |
---|
| 296 | // PRINTF(3)("%s\n", devWalk); |
---|
| 297 | // devWalk += strlen(devWalk)+1; |
---|
| 298 | // } |
---|
| 299 | // while (devWalk[0] != '\0'); |
---|
| 300 | // } |
---|
[4504] | 301 | |
---|
[5819] | 302 | // INITIALIZING THE DEVICE: |
---|
[7284] | 303 | // char deviceName[] = |
---|
| 304 | // #ifdef __WIN32__ |
---|
| 305 | // "Direct3D"; |
---|
| 306 | // #else |
---|
| 307 | // "'( ( devices '( native null ) ) )"; |
---|
| 308 | // #endif |
---|
[5385] | 309 | |
---|
[6858] | 310 | this->device = alcOpenDevice(NULL); |
---|
[6856] | 311 | this->checkALCError("opening Device", __LINE__); |
---|
[5819] | 312 | |
---|
[6857] | 313 | PRINTF(4)("Audio-Specifier: %s\n", (const char*)alcGetString(this->device, ALC_DEVICE_SPECIFIER)); |
---|
| 314 | PRINTF(4)("Audio-Extensions: %s\n", (const char*)alcGetString(this->device, ALC_EXTENSIONS)); |
---|
[6849] | 315 | |
---|
[6856] | 316 | |
---|
[6836] | 317 | this->context = alcCreateContext(this->device, NULL); |
---|
[6856] | 318 | this->checkALCError("creating Context", __LINE__); |
---|
[6849] | 319 | |
---|
[6856] | 320 | alcMakeContextCurrent(this->context); |
---|
| 321 | this->checkALCError("making Context Current", __LINE__); |
---|
[6846] | 322 | // #endif |
---|
[5819] | 323 | |
---|
[4504] | 324 | this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY); |
---|
[6840] | 325 | this->allocateSources(this->maxSourceCount); |
---|
[7290] | 326 | this->checkError("Allocating Sources", __LINE__); |
---|
[4504] | 327 | } |
---|
| 328 | |
---|
[5917] | 329 | |
---|
[4504] | 330 | /** |
---|
[5917] | 331 | * Allocates openAL sources |
---|
| 332 | * @param count how many sources to allocate |
---|
| 333 | * @returns true on success, false if at least one source could not be allocated |
---|
| 334 | */ |
---|
| 335 | bool SoundEngine::allocateSources(unsigned int count) |
---|
| 336 | { |
---|
[6844] | 337 | unsigned int failCount = 0; |
---|
[5930] | 338 | for (unsigned int i = 0; i < count; i++) |
---|
[5917] | 339 | { |
---|
[6836] | 340 | ALuint source = 0; |
---|
[5917] | 341 | |
---|
[5930] | 342 | alGenSources(1, &source); |
---|
[6840] | 343 | this->checkError("allocate Source", __LINE__); |
---|
[6842] | 344 | if (!alIsSource(source)) |
---|
| 345 | { |
---|
[6844] | 346 | PRINTF(5)("not allocated Source\n"); |
---|
| 347 | failCount++; |
---|
[6842] | 348 | continue; |
---|
| 349 | } |
---|
[5917] | 350 | |
---|
[5930] | 351 | alSourcef (source, AL_PITCH, 1.0 ); |
---|
| 352 | alSourcef (source, AL_GAIN, this->getEffectsVolume() ); |
---|
| 353 | alSourcei (source, AL_LOOPING, AL_FALSE ); |
---|
| 354 | this->ALSources.push(source); |
---|
[5917] | 355 | } |
---|
[6844] | 356 | if (failCount == 0) |
---|
| 357 | return true; |
---|
| 358 | else |
---|
| 359 | { |
---|
| 360 | PRINTF(2)("Failed to allocate %d of %d SoundSources\n", failCount, count); |
---|
| 361 | } |
---|
[5917] | 362 | } |
---|
| 363 | |
---|
[7225] | 364 | bool SoundEngine::checkError(const std::string& error, unsigned int line) |
---|
[6836] | 365 | { |
---|
| 366 | ALenum errorCode; |
---|
| 367 | if ((errorCode = alGetError()) != AL_NO_ERROR) |
---|
| 368 | { |
---|
[7225] | 369 | PRINTF(1)("Error %s (line:%d): '%s'\n", error.c_str(), line, SoundEngine::getALErrorString(errorCode)); |
---|
[6836] | 370 | return false; |
---|
| 371 | } |
---|
| 372 | else |
---|
| 373 | return true; |
---|
| 374 | } |
---|
| 375 | |
---|
[7225] | 376 | bool SoundEngine::checkALCError(const std::string& error, unsigned int line) |
---|
[6849] | 377 | { |
---|
| 378 | ALenum errorCode; |
---|
| 379 | if ((errorCode = alcGetError(this->device)) != ALC_NO_ERROR) |
---|
| 380 | { |
---|
[7225] | 381 | PRINTF(1)("Error %s (line:%d): '%s'\n", error.c_str(), line, SoundEngine::getALCErrorString(errorCode)); |
---|
[6849] | 382 | return false; |
---|
| 383 | } |
---|
| 384 | else |
---|
| 385 | return true; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | |
---|
| 389 | |
---|
| 390 | void SoundEngine::listDevices() |
---|
| 391 | { |
---|
| 392 | printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER)); |
---|
| 393 | } |
---|
| 394 | |
---|
[5917] | 395 | /** |
---|
[4836] | 396 | * Transforms AL-errors into something readable |
---|
| 397 | * @param err The error found |
---|
[4504] | 398 | */ |
---|
[5930] | 399 | const char* SoundEngine::getALErrorString(ALenum err) |
---|
[4504] | 400 | { |
---|
| 401 | switch(err) |
---|
[6846] | 402 | { |
---|
[4504] | 403 | case AL_NO_ERROR: |
---|
[5930] | 404 | return ("AL_NO_ERROR"); |
---|
[4504] | 405 | case AL_INVALID_NAME: |
---|
[5930] | 406 | return ("AL_INVALID_NAME"); |
---|
[4504] | 407 | case AL_INVALID_ENUM: |
---|
[5930] | 408 | return ("AL_INVALID_ENUM"); |
---|
[4504] | 409 | case AL_INVALID_VALUE: |
---|
[5930] | 410 | return ("AL_INVALID_VALUE"); |
---|
[4504] | 411 | case AL_INVALID_OPERATION: |
---|
[5930] | 412 | return ("AL_INVALID_OPERATION"); |
---|
[4504] | 413 | case AL_OUT_OF_MEMORY: |
---|
[5930] | 414 | return ("AL_OUT_OF_MEMORY"); |
---|
[6846] | 415 | }; |
---|
[4504] | 416 | } |
---|
| 417 | |
---|
[4959] | 418 | |
---|
[6847] | 419 | const char* SoundEngine::getALCErrorString(ALenum err) |
---|
[4504] | 420 | { |
---|
| 421 | switch(err) |
---|
[7284] | 422 | { |
---|
[4504] | 423 | case ALC_NO_ERROR: |
---|
[6849] | 424 | return ("AL_NO_ERROR"); |
---|
[4504] | 425 | case ALC_INVALID_DEVICE: |
---|
[6849] | 426 | return ("ALC_INVALID_DEVICE"); |
---|
[4504] | 427 | case ALC_INVALID_CONTEXT: |
---|
[6849] | 428 | return("ALC_INVALID_CONTEXT"); |
---|
[4504] | 429 | case ALC_INVALID_ENUM: |
---|
[6849] | 430 | return("ALC_INVALID_ENUM"); |
---|
[4504] | 431 | case ALC_INVALID_VALUE: |
---|
[6849] | 432 | return ("ALC_INVALID_VALUE"); |
---|
[4504] | 433 | case ALC_OUT_OF_MEMORY: |
---|
[6849] | 434 | return("ALC_OUT_OF_MEMORY"); |
---|
[7284] | 435 | }; |
---|
[4504] | 436 | } |
---|