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