[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; |
---|
[4504] | 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
[4836] | 52 | * the singleton reference to this class |
---|
[4504] | 53 | */ |
---|
| 54 | SoundEngine* SoundEngine::singletonRef = NULL; |
---|
| 55 | |
---|
| 56 | /** |
---|
[4836] | 57 | * standard deconstructor |
---|
[5293] | 58 | */ |
---|
[4597] | 59 | SoundEngine::~SoundEngine () |
---|
[4504] | 60 | { |
---|
| 61 | // deleting all the SoundSources |
---|
[4960] | 62 | if(this->sourceList != NULL) |
---|
| 63 | { |
---|
[5779] | 64 | while (this->sourceList->size() > 0) |
---|
| 65 | delete dynamic_cast<SoundSource*>(this->sourceList->front()); |
---|
[4960] | 66 | } |
---|
[4504] | 67 | |
---|
| 68 | // deleting all the SoundBuffers |
---|
[4960] | 69 | if (this->bufferList != NULL) |
---|
| 70 | { |
---|
[5779] | 71 | while(this->bufferList->size() > 0) |
---|
| 72 | ResourceManager::getInstance()->unload(dynamic_cast<SoundBuffer*>(this->bufferList->front())); |
---|
[4960] | 73 | } |
---|
[5293] | 74 | |
---|
[4504] | 75 | // removing openAL from AudioResource |
---|
[5473] | 76 | //! @todo this should be terminated through alc |
---|
| 77 | //alutExit(); |
---|
[5293] | 78 | |
---|
| 79 | SoundEngine::singletonRef = NULL; |
---|
[4504] | 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
[4985] | 83 | * loads the settings of the SoundEngine from an ini-file |
---|
| 84 | * @param iniParser the IniParser of the inifile |
---|
| 85 | */ |
---|
| 86 | void SoundEngine::loadSettings(IniParser* iniParser) |
---|
| 87 | { |
---|
| 88 | const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80"); |
---|
| 89 | this->musicVolume = atof(musicVolume)/100.0; |
---|
| 90 | |
---|
| 91 | const char* effectsVolume = iniParser->getVar(CONFIG_NAME_EFFECTS_VOLUME, CONFIG_SECTION_AUDIO, "80"); |
---|
| 92 | this->effectsVolume = atof(effectsVolume)/100.0; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
[4836] | 96 | * creates a new SoundSource. |
---|
| 97 | * @param fileName The Name to load the SoundBuffer from |
---|
| 98 | * @param sourceNode The sourceNode to bind this SoundSource to. |
---|
| 99 | * @returns The newly created SoundSource |
---|
[4504] | 100 | |
---|
| 101 | acctualy this is nothing more than a wrapper around the ResourceManager. |
---|
| 102 | */ |
---|
| 103 | SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode) |
---|
| 104 | { |
---|
[4885] | 105 | return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL)); |
---|
[4504] | 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
[4836] | 109 | * Sets the doppler values of openAL |
---|
| 110 | * @param dopplerFactor the extent of the doppler-effect |
---|
| 111 | * @param dopplerVelocity the Speed the sound travels |
---|
[4504] | 112 | */ |
---|
| 113 | void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity) |
---|
| 114 | { |
---|
| 115 | alDopplerFactor(dopplerFactor); |
---|
| 116 | alDopplerVelocity(dopplerVelocity); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | /** |
---|
[4836] | 121 | * adds a SoundBuffer to the bufferList of the SoundEngine |
---|
| 122 | * @param buffer The buffer to add to the bufferList |
---|
[4504] | 123 | */ |
---|
| 124 | void SoundEngine::addBuffer(SoundBuffer* buffer) |
---|
| 125 | { |
---|
[4960] | 126 | if (unlikely(this->bufferList == NULL)) |
---|
| 127 | this->bufferList = ClassList::getList(CL_SOUND_BUFFER); |
---|
[4504] | 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
[4836] | 131 | * removes a SoundBuffer from the bufferList of the SoundEngine |
---|
| 132 | * @param buffer The buffer to delete from the SoundEngine |
---|
[4504] | 133 | */ |
---|
| 134 | void SoundEngine::removeBuffer(SoundBuffer* buffer) |
---|
| 135 | { |
---|
| 136 | // look if there are any sources that have the buffer still loaded |
---|
[5779] | 137 | if (this->sourceList != NULL) |
---|
| 138 | { |
---|
[5885] | 139 | list<BaseObject*>::const_iterator source; |
---|
[5779] | 140 | for (source = this->sourceList->begin(); source != this->sourceList->end(); source++) |
---|
[4504] | 141 | { |
---|
[5779] | 142 | if (buffer == static_cast<SoundSource*>(*source)->getBuffer()) |
---|
| 143 | delete (*source); |
---|
[4504] | 144 | } |
---|
[5779] | 145 | } |
---|
[4504] | 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
[5293] | 149 | * adds a SoundSource to the sourceList of the SoundEngine |
---|
[4836] | 150 | * @param source The source to add to the sourceList |
---|
[4504] | 151 | */ |
---|
| 152 | void SoundEngine::addSource(SoundSource* source) |
---|
| 153 | { |
---|
[4960] | 154 | this->sourceList = ClassList::getList(CL_SOUND_SOURCE); |
---|
[4504] | 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
[4836] | 158 | * updates all The positions, Directions and Velocities of all Sounds |
---|
[4504] | 159 | */ |
---|
[4746] | 160 | void SoundEngine::update() |
---|
[4504] | 161 | { |
---|
| 162 | |
---|
| 163 | // updating the Listeners Position |
---|
| 164 | if (likely(this->listener != NULL)) |
---|
| 165 | { |
---|
| 166 | alListener3f(AL_POSITION, |
---|
[4597] | 167 | this->listener->getAbsCoor().x, |
---|
| 168 | this->listener->getAbsCoor().y, |
---|
| 169 | this->listener->getAbsCoor().z); |
---|
[4504] | 170 | alListener3f(AL_VELOCITY, |
---|
[4597] | 171 | this->listener->getVelocity().x, |
---|
| 172 | this->listener->getVelocity().y, |
---|
| 173 | this->listener->getVelocity().z); |
---|
[4504] | 174 | Vector absDirV = this->listener->getAbsDirV(); |
---|
| 175 | ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z}; |
---|
| 176 | alListenerfv(AL_ORIENTATION, orientation); |
---|
| 177 | } |
---|
| 178 | else |
---|
| 179 | PRINTF(2)("no listener defined\n"); |
---|
| 180 | |
---|
| 181 | // updating all the Sources positions |
---|
[4960] | 182 | if (likely(this->sourceList != NULL)) |
---|
| 183 | { |
---|
[5885] | 184 | list<BaseObject*>::const_iterator sourceIT; |
---|
[5779] | 185 | SoundSource* source; |
---|
| 186 | for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++) |
---|
[4504] | 187 | { |
---|
[5779] | 188 | source = static_cast<SoundSource*>(*sourceIT); |
---|
| 189 | if (likely(source->getNode() != NULL)) |
---|
[4504] | 190 | { |
---|
[5779] | 191 | alSource3f(source->getID(), AL_POSITION, |
---|
| 192 | source->getNode()->getAbsCoor().x, |
---|
| 193 | source->getNode()->getAbsCoor().y, |
---|
| 194 | source->getNode()->getAbsCoor().z); |
---|
| 195 | alSource3f(source->getID(), AL_VELOCITY, |
---|
| 196 | source->getNode()->getVelocity().x, |
---|
| 197 | source->getNode()->getVelocity().y, |
---|
| 198 | source->getNode()->getVelocity().z); |
---|
[4504] | 199 | } |
---|
| 200 | } |
---|
[4960] | 201 | } |
---|
[4504] | 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
[4836] | 205 | * Removes all the Buffers that are not anymore needed by any Sources |
---|
[4504] | 206 | */ |
---|
[4746] | 207 | void SoundEngine::flushUnusedBuffers() |
---|
[4504] | 208 | { |
---|
[5819] | 209 | /// FIXME |
---|
[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 | |
---|
[5819] | 282 | // INITIALIZING THE DEVICE: |
---|
[5856] | 283 | ALubyte deviceName[] = |
---|
[5819] | 284 | #ifdef __WIN32__ |
---|
| 285 | "native"; |
---|
| 286 | #else |
---|
| 287 | "'( ( devices '( native arts null ) ) )"; |
---|
| 288 | #endif |
---|
| 289 | // |
---|
| 290 | this->device = alcOpenDevice(deviceName); |
---|
[5385] | 291 | |
---|
[5819] | 292 | this->context = alcCreateContext(this->device, NULL); |
---|
| 293 | |
---|
| 294 | alcMakeContextCurrent(this->context); |
---|
| 295 | |
---|
| 296 | |
---|
[4504] | 297 | if ((result = alGetError()) != AL_NO_ERROR) |
---|
| 298 | SoundEngine::PrintALErrorString(result); |
---|
| 299 | |
---|
| 300 | this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | /** |
---|
[4836] | 304 | * Transforms AL-errors into something readable |
---|
| 305 | * @param err The error found |
---|
[4504] | 306 | */ |
---|
| 307 | void SoundEngine::PrintALErrorString(ALenum err) |
---|
| 308 | { |
---|
| 309 | switch(err) |
---|
| 310 | { |
---|
| 311 | case AL_NO_ERROR: |
---|
| 312 | PRINTF(4)("AL_NO_ERROR\n"); |
---|
| 313 | break; |
---|
[4597] | 314 | |
---|
[4504] | 315 | case AL_INVALID_NAME: |
---|
| 316 | PRINTF(2)("AL_INVALID_NAME\n"); |
---|
| 317 | break; |
---|
| 318 | |
---|
| 319 | case AL_INVALID_ENUM: |
---|
| 320 | PRINTF(2)("AL_INVALID_ENUM\n"); |
---|
| 321 | break; |
---|
| 322 | |
---|
| 323 | case AL_INVALID_VALUE: |
---|
| 324 | PRINTF(2)("AL_INVALID_VALUE\n"); |
---|
| 325 | break; |
---|
| 326 | |
---|
| 327 | case AL_INVALID_OPERATION: |
---|
| 328 | PRINTF(2)("AL_INVALID_OPERATION\n"); |
---|
| 329 | break; |
---|
| 330 | |
---|
| 331 | case AL_OUT_OF_MEMORY: |
---|
| 332 | PRINTF(2)("AL_OUT_OF_MEMORY\n"); |
---|
| 333 | break; |
---|
| 334 | }; |
---|
| 335 | } |
---|
| 336 | |
---|
[4959] | 337 | void SoundEngine::listDevices() |
---|
| 338 | { |
---|
| 339 | |
---|
| 340 | printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER)); |
---|
| 341 | } |
---|
| 342 | |
---|
[4504] | 343 | /* |
---|
| 344 | void SoundEngine::PrintALCErrorString(ALenum err) |
---|
| 345 | { |
---|
| 346 | switch(err) |
---|
| 347 | { |
---|
| 348 | case ALC_NO_ERROR: |
---|
| 349 | PRINTF(4)("AL_NO_ERROR\n"); |
---|
| 350 | break; |
---|
| 351 | |
---|
| 352 | case ALC_INVALID_DEVICE: |
---|
| 353 | PRINTF(2)("ALC_INVALID_DEVICE\n"); |
---|
| 354 | break; |
---|
| 355 | |
---|
| 356 | case ALC_INVALID_CONTEXT: |
---|
| 357 | PRINTF(2)("ALC_INVALID_CONTEXT\n"); |
---|
| 358 | break; |
---|
| 359 | |
---|
| 360 | case ALC_INVALID_ENUM: |
---|
| 361 | PRINTF(2)("ALC_INVALID_ENUM\n"); |
---|
| 362 | break; |
---|
| 363 | |
---|
| 364 | case ALC_INVALID_VALUE: |
---|
| 365 | PRINTF(2)("ALC_INVALID_VALUE\n"); |
---|
| 366 | break; |
---|
| 367 | |
---|
| 368 | case ALC_OUT_OF_MEMORY: |
---|
| 369 | PRINTF(2)("ALC_OUT_OF_MEMORY\n"); |
---|
| 370 | break; |
---|
| 371 | }; |
---|
| 372 | } |
---|
| 373 | */ |
---|