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