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