| [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" |
|---|
| [5929] | 37 | #include "core/GameMode.h" |
|---|
| 38 | #include "core/ScopedSingletonManager.h" |
|---|
| [6031] | 39 | #include "core/Resource.h" |
|---|
| [5982] | 40 | #include "BaseSound.h" |
|---|
| [6031] | 41 | #include "MoodManager.h" |
|---|
| [3060] | 42 | |
|---|
| 43 | namespace orxonox |
|---|
| 44 | { |
|---|
| [3370] | 45 | SoundManager* SoundManager::singletonPtr_s = NULL; |
|---|
| [5929] | 46 | ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); |
|---|
| [3060] | 47 | |
|---|
| 48 | SoundManager::SoundManager() |
|---|
| 49 | { |
|---|
| [5929] | 50 | if (!alutInitWithoutContext(NULL,NULL)) |
|---|
| [5946] | 51 | ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError())); |
|---|
| [5929] | 52 | Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); |
|---|
| 53 | |
|---|
| [5946] | 54 | COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl; |
|---|
| [5929] | 55 | this->device_ = alcOpenDevice(NULL); |
|---|
| 56 | if (this->device_ == NULL) |
|---|
| [3060] | 57 | { |
|---|
| [5946] | 58 | COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl; |
|---|
| [5929] | 59 | #ifdef ORXONOX_PLATFORM_WINDOWS |
|---|
| [5946] | 60 | COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; |
|---|
| [5929] | 61 | #endif |
|---|
| [5946] | 62 | ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device."); |
|---|
| [3060] | 63 | } |
|---|
| [5929] | 64 | Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); |
|---|
| 65 | |
|---|
| [5946] | 66 | COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl; |
|---|
| [5929] | 67 | this->context_ = alcCreateContext(this->device_, NULL); |
|---|
| 68 | if (this->context_ == NULL) |
|---|
| [5946] | 69 | ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context"); |
|---|
| [5929] | 70 | Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); |
|---|
| 71 | |
|---|
| 72 | if (alcMakeContextCurrent(this->context_) == AL_TRUE) |
|---|
| [5946] | 73 | COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl; |
|---|
| [5929] | 74 | |
|---|
| 75 | COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl; |
|---|
| 76 | |
|---|
| 77 | const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER); |
|---|
| 78 | if (str == NULL) |
|---|
| [5946] | 79 | COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl; |
|---|
| [3060] | 80 | else |
|---|
| [5946] | 81 | COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl; |
|---|
| [3060] | 82 | |
|---|
| [5929] | 83 | GameMode::setPlaysSound(true); |
|---|
| 84 | // Disarm guards |
|---|
| 85 | alutExitGuard.Dismiss(); |
|---|
| 86 | closeDeviceGuard.Dismiss(); |
|---|
| 87 | desroyContextGuard.Dismiss(); |
|---|
| [3060] | 88 | } |
|---|
| 89 | |
|---|
| 90 | SoundManager::~SoundManager() |
|---|
| 91 | { |
|---|
| [5929] | 92 | GameMode::setPlaysSound(false); |
|---|
| [3060] | 93 | alcDestroyContext(this->context_); |
|---|
| [3280] | 94 | alcCloseDevice(this->device_); |
|---|
| [3060] | 95 | alutExit(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| [5929] | 98 | void SoundManager::setListenerPosition(const Vector3& position) |
|---|
| [3060] | 99 | { |
|---|
| [5929] | 100 | alListener3f(AL_POSITION, position.x, position.y, position.z); |
|---|
| 101 | ALenum error = alGetError(); |
|---|
| 102 | if (error == AL_INVALID_VALUE) |
|---|
| 103 | COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; |
|---|
| [3060] | 104 | } |
|---|
| 105 | |
|---|
| [5929] | 106 | void SoundManager::setListenerOrientation(const Quaternion& orientation) |
|---|
| [3060] | 107 | { |
|---|
| [5929] | 108 | // update listener orientation |
|---|
| 109 | Vector3 up = orientation.xAxis(); // just a wild guess |
|---|
| 110 | Vector3 at = orientation.zAxis(); |
|---|
| [3060] | 111 | |
|---|
| [5929] | 112 | ALfloat orient[6] = { at.x, at.y, at.z, |
|---|
| 113 | up.x, up.y, up.z }; |
|---|
| [3060] | 114 | |
|---|
| [5929] | 115 | alListenerfv(AL_POSITION, orient); |
|---|
| [3060] | 116 | ALenum error = alGetError(); |
|---|
| [5929] | 117 | if (error == AL_INVALID_VALUE) |
|---|
| [3060] | 118 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
|---|
| 119 | } |
|---|
| [5982] | 120 | |
|---|
| 121 | void SoundManager::registerAmbientSound(BaseSound* newAmbient) |
|---|
| 122 | { |
|---|
| 123 | if (!(this->ambientSounds_.empty())) |
|---|
| 124 | { |
|---|
| 125 | this->ambientSounds_.front()->pause(); |
|---|
| 126 | } |
|---|
| 127 | this->ambientSounds_.push_front(newAmbient); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | void SoundManager::unregisterAmbientSound(BaseSound* currentAmbient) |
|---|
| 131 | { |
|---|
| [6031] | 132 | if(currentAmbient == NULL || ambientSounds_.empty()) |
|---|
| 133 | { |
|---|
| 134 | return; |
|---|
| 135 | } |
|---|
| [5982] | 136 | if(this->ambientSounds_.front() == currentAmbient) |
|---|
| 137 | { |
|---|
| 138 | this->ambientSounds_.pop_front(); |
|---|
| [6031] | 139 | if(!(this->ambientSounds_.empty())) |
|---|
| 140 | { |
|---|
| 141 | this->ambientSounds_.front()->replay(); |
|---|
| 142 | } |
|---|
| [5982] | 143 | } |
|---|
| 144 | else |
|---|
| 145 | { |
|---|
| 146 | for(std::list<BaseSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++) |
|---|
| 147 | { |
|---|
| 148 | if(*it == currentAmbient) |
|---|
| 149 | { |
|---|
| 150 | this->ambientSounds_.erase(it); |
|---|
| 151 | break; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| [6031] | 156 | |
|---|
| 157 | // Get the current mood and return the full path string to the requested sound. |
|---|
| 158 | const std::string& SoundManager::getAmbientPath(const std::string& source) |
|---|
| 159 | { |
|---|
| 160 | lastReqPath = "ambient/" + MoodManager::getInstance().getMood() + "/" + source; |
|---|
| 161 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath); |
|---|
| 162 | if(fileInfo == NULL) |
|---|
| 163 | { |
|---|
| 164 | return BLANKSTRING; |
|---|
| 165 | } |
|---|
| 166 | return lastReqPath; |
|---|
| 167 | } |
|---|
| [3060] | 168 | } |
|---|