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