[3060] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Erwin 'vaiursch' Herrsche |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[3196] | 29 | #include "SoundManager.h" |
---|
| 30 | |
---|
[3060] | 31 | #include <AL/alut.h> |
---|
| 32 | |
---|
[5929] | 33 | #include "util/Exception.h" |
---|
[3196] | 34 | #include "util/Math.h" |
---|
[5929] | 35 | #include "util/ScopeGuard.h" |
---|
[6031] | 36 | #include "util/StringUtils.h" |
---|
[6046] | 37 | #include "util/Clock.h" |
---|
[5929] | 38 | #include "core/GameMode.h" |
---|
| 39 | #include "core/ScopedSingletonManager.h" |
---|
[6031] | 40 | #include "core/Resource.h" |
---|
[6046] | 41 | #include "core/ConfigValueIncludes.h" |
---|
[5982] | 42 | #include "BaseSound.h" |
---|
[6031] | 43 | #include "MoodManager.h" |
---|
[6046] | 44 | #include "AmbientSound.h" |
---|
[3060] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[3370] | 48 | SoundManager* SoundManager::singletonPtr_s = NULL; |
---|
[5929] | 49 | ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); |
---|
[3060] | 50 | |
---|
| 51 | SoundManager::SoundManager() |
---|
| 52 | { |
---|
[6046] | 53 | RegisterRootObject(SoundManager); |
---|
| 54 | |
---|
[5929] | 55 | if (!alutInitWithoutContext(NULL,NULL)) |
---|
[5946] | 56 | ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError())); |
---|
[5929] | 57 | Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); |
---|
| 58 | |
---|
[5946] | 59 | COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl; |
---|
[5929] | 60 | this->device_ = alcOpenDevice(NULL); |
---|
| 61 | if (this->device_ == NULL) |
---|
[3060] | 62 | { |
---|
[5946] | 63 | COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl; |
---|
[5929] | 64 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[5946] | 65 | COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; |
---|
[5929] | 66 | #endif |
---|
[5946] | 67 | ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device."); |
---|
[3060] | 68 | } |
---|
[5929] | 69 | Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); |
---|
| 70 | |
---|
[5946] | 71 | COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl; |
---|
[5929] | 72 | this->context_ = alcCreateContext(this->device_, NULL); |
---|
| 73 | if (this->context_ == NULL) |
---|
[5946] | 74 | ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context"); |
---|
[5929] | 75 | Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); |
---|
| 76 | |
---|
| 77 | if (alcMakeContextCurrent(this->context_) == AL_TRUE) |
---|
[5946] | 78 | COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl; |
---|
[5929] | 79 | |
---|
| 80 | COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl; |
---|
| 81 | |
---|
| 82 | const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER); |
---|
| 83 | if (str == NULL) |
---|
[5946] | 84 | COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[3060] | 85 | else |
---|
[5946] | 86 | COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl; |
---|
[3060] | 87 | |
---|
[5929] | 88 | GameMode::setPlaysSound(true); |
---|
| 89 | // Disarm guards |
---|
| 90 | alutExitGuard.Dismiss(); |
---|
| 91 | closeDeviceGuard.Dismiss(); |
---|
| 92 | desroyContextGuard.Dismiss(); |
---|
[6046] | 93 | |
---|
| 94 | this->setConfigValues(); |
---|
[3060] | 95 | } |
---|
| 96 | |
---|
| 97 | SoundManager::~SoundManager() |
---|
| 98 | { |
---|
[5929] | 99 | GameMode::setPlaysSound(false); |
---|
[3060] | 100 | alcDestroyContext(this->context_); |
---|
[3280] | 101 | alcCloseDevice(this->device_); |
---|
[3060] | 102 | alutExit(); |
---|
| 103 | } |
---|
| 104 | |
---|
[6046] | 105 | void SoundManager::update(const Clock &time) |
---|
| 106 | { |
---|
| 107 | this->fadeInAmbientSound(time.getDeltaTime()); |
---|
| 108 | this->fadeOutAmbientSound(time.getDeltaTime()); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | void SoundManager::setConfigValues() |
---|
| 112 | { |
---|
| 113 | SetConfigValue(fadeStep_, 0.2f) |
---|
| 114 | .description("Determines how fast sounds should fade, per second.") |
---|
| 115 | .callback(this, &SoundManager::checkFadeStepValidity); |
---|
| 116 | } |
---|
| 117 | |
---|
[5929] | 118 | void SoundManager::setListenerPosition(const Vector3& position) |
---|
[3060] | 119 | { |
---|
[5929] | 120 | alListener3f(AL_POSITION, position.x, position.y, position.z); |
---|
| 121 | ALenum error = alGetError(); |
---|
| 122 | if (error == AL_INVALID_VALUE) |
---|
| 123 | COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; |
---|
[3060] | 124 | } |
---|
| 125 | |
---|
[5929] | 126 | void SoundManager::setListenerOrientation(const Quaternion& orientation) |
---|
[3060] | 127 | { |
---|
[5929] | 128 | // update listener orientation |
---|
| 129 | Vector3 up = orientation.xAxis(); // just a wild guess |
---|
| 130 | Vector3 at = orientation.zAxis(); |
---|
[3060] | 131 | |
---|
[5929] | 132 | ALfloat orient[6] = { at.x, at.y, at.z, |
---|
| 133 | up.x, up.y, up.z }; |
---|
[3060] | 134 | |
---|
[5929] | 135 | alListenerfv(AL_POSITION, orient); |
---|
[3060] | 136 | ALenum error = alGetError(); |
---|
[5929] | 137 | if (error == AL_INVALID_VALUE) |
---|
[3060] | 138 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
---|
| 139 | } |
---|
[5982] | 140 | |
---|
[6046] | 141 | void SoundManager::registerAmbientSound(AmbientSound* newAmbient) |
---|
[5982] | 142 | { |
---|
[6046] | 143 | if(newAmbient != NULL) |
---|
[5982] | 144 | { |
---|
[6046] | 145 | if (!(this->ambientSounds_.empty())) |
---|
| 146 | { |
---|
| 147 | this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0)); |
---|
| 148 | } |
---|
| 149 | this->fadeInList_.push_front(std::make_pair(newAmbient, 0.0)); |
---|
| 150 | this->ambientSounds_.push_front(newAmbient); |
---|
[5982] | 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
[6046] | 154 | void SoundManager::unregisterAmbientSound(AmbientSound* currentAmbient) |
---|
[5982] | 155 | { |
---|
[6031] | 156 | if(currentAmbient == NULL || ambientSounds_.empty()) |
---|
| 157 | { |
---|
| 158 | return; |
---|
| 159 | } |
---|
[5982] | 160 | if(this->ambientSounds_.front() == currentAmbient) |
---|
| 161 | { |
---|
[6046] | 162 | this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0)); |
---|
[5982] | 163 | this->ambientSounds_.pop_front(); |
---|
[6031] | 164 | if(!(this->ambientSounds_.empty())) |
---|
| 165 | { |
---|
[6046] | 166 | this->fadeInList_.push_front(std::make_pair(this->ambientSounds_.front(), 0.0)); |
---|
[6031] | 167 | } |
---|
[5982] | 168 | } |
---|
| 169 | else |
---|
| 170 | { |
---|
[6046] | 171 | for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++) |
---|
[5982] | 172 | { |
---|
| 173 | if(*it == currentAmbient) |
---|
| 174 | { |
---|
[6046] | 175 | currentAmbient->doStop(); |
---|
[5982] | 176 | this->ambientSounds_.erase(it); |
---|
| 177 | break; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | } |
---|
[6031] | 182 | |
---|
| 183 | // Get the current mood and return the full path string to the requested sound. |
---|
| 184 | const std::string& SoundManager::getAmbientPath(const std::string& source) |
---|
| 185 | { |
---|
[6046] | 186 | lastReqPath_ = "ambient/" + MoodManager::getInstance().getMood() + "/" + source; |
---|
| 187 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath_); |
---|
[6031] | 188 | if(fileInfo == NULL) |
---|
| 189 | { |
---|
| 190 | return BLANKSTRING; |
---|
| 191 | } |
---|
[6046] | 192 | return lastReqPath_; |
---|
[6031] | 193 | } |
---|
[6046] | 194 | |
---|
| 195 | void SoundManager::fadeInAmbientSound(float dt) |
---|
| 196 | { |
---|
| 197 | if(!(this->fadeInList_.empty())) |
---|
| 198 | { |
---|
| 199 | for(std::list<std::pair<AmbientSound*, float> >::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it++) |
---|
| 200 | { |
---|
| 201 | it->second += fadeStep_ * dt; |
---|
| 202 | alSourcef(it->first->getALAudioSource(), AL_GAIN, it->second); |
---|
| 203 | } |
---|
| 204 | if(this->fadeInList_.back().second >= 1) |
---|
| 205 | { |
---|
| 206 | this->fadeInList_.pop_back(); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void SoundManager::fadeOutAmbientSound(float dt) |
---|
| 212 | { |
---|
| 213 | if(!(this->fadeInList_.empty())) |
---|
| 214 | { |
---|
| 215 | for(std::list<std::pair<AmbientSound*, float> >::iterator it= this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++) |
---|
| 216 | { |
---|
| 217 | it->second -= fadeStep_ * dt; |
---|
| 218 | alSourcef(it->first->getALAudioSource(), AL_GAIN, it->second); |
---|
| 219 | } |
---|
| 220 | if(this->fadeOutList_.back().second <= 0) |
---|
| 221 | { |
---|
| 222 | bool pauseTest = false; |
---|
| 223 | |
---|
| 224 | for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++) |
---|
| 225 | { |
---|
| 226 | if(*it == this->fadeOutList_.back().first) |
---|
| 227 | { |
---|
| 228 | pauseTest = true; |
---|
| 229 | break; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | if(pauseTest) |
---|
| 233 | { |
---|
| 234 | this->fadeOutList_.back().first->pause(); |
---|
| 235 | } |
---|
| 236 | else |
---|
| 237 | { |
---|
| 238 | this->fadeOutList_.back().first->doStop(); |
---|
| 239 | } |
---|
| 240 | this->fadeOutList_.pop_back(); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | void SoundManager::checkFadeStepValidity() |
---|
| 246 | { |
---|
| 247 | if(fadeStep_ <= 0.0 || fadeStep_ >= 1.0 ) |
---|
| 248 | { |
---|
| 249 | ResetConfigValue(fadeStep_); |
---|
| 250 | } |
---|
| 251 | COUT(0) << "SoundManager: fade step now set to " << fadeStep_ << std::endl; |
---|
| 252 | return; |
---|
| 253 | } |
---|
[3060] | 254 | } |
---|