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