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 | |
---|
29 | #include "SoundManager.h" |
---|
30 | |
---|
31 | #include <AL/alut.h> |
---|
32 | |
---|
33 | #include "util/Exception.h" |
---|
34 | #include "util/Math.h" |
---|
35 | #include "util/ScopeGuard.h" |
---|
36 | #include "util/StringUtils.h" |
---|
37 | #include "util/Clock.h" |
---|
38 | #include "core/GameMode.h" |
---|
39 | #include "core/ScopedSingletonManager.h" |
---|
40 | #include "core/Resource.h" |
---|
41 | #include "core/ConfigValueIncludes.h" |
---|
42 | #include "BaseSound.h" |
---|
43 | #include "MoodManager.h" |
---|
44 | #include "AmbientSound.h" |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | SoundManager* SoundManager::singletonPtr_s = NULL; |
---|
49 | ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); |
---|
50 | |
---|
51 | SoundManager::SoundManager() |
---|
52 | { |
---|
53 | RegisterRootObject(SoundManager); |
---|
54 | |
---|
55 | if (!alutInitWithoutContext(NULL,NULL)) |
---|
56 | ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError())); |
---|
57 | Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); |
---|
58 | |
---|
59 | COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl; |
---|
60 | this->device_ = alcOpenDevice(NULL); |
---|
61 | if (this->device_ == NULL) |
---|
62 | { |
---|
63 | COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl; |
---|
64 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
65 | COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; |
---|
66 | #endif |
---|
67 | ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device."); |
---|
68 | } |
---|
69 | Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); |
---|
70 | |
---|
71 | COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl; |
---|
72 | this->context_ = alcCreateContext(this->device_, NULL); |
---|
73 | if (this->context_ == NULL) |
---|
74 | ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context"); |
---|
75 | Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); |
---|
76 | |
---|
77 | if (alcMakeContextCurrent(this->context_) == AL_TRUE) |
---|
78 | COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl; |
---|
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) |
---|
84 | COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
85 | else |
---|
86 | COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl; |
---|
87 | |
---|
88 | GameMode::setPlaysSound(true); |
---|
89 | // Disarm guards |
---|
90 | alutExitGuard.Dismiss(); |
---|
91 | closeDeviceGuard.Dismiss(); |
---|
92 | desroyContextGuard.Dismiss(); |
---|
93 | |
---|
94 | this->setConfigValues(); |
---|
95 | } |
---|
96 | |
---|
97 | SoundManager::~SoundManager() |
---|
98 | { |
---|
99 | GameMode::setPlaysSound(false); |
---|
100 | alcDestroyContext(this->context_); |
---|
101 | alcCloseDevice(this->device_); |
---|
102 | alutExit(); |
---|
103 | } |
---|
104 | |
---|
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 | |
---|
118 | void SoundManager::setListenerPosition(const Vector3& position) |
---|
119 | { |
---|
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; |
---|
124 | } |
---|
125 | |
---|
126 | void SoundManager::setListenerOrientation(const Quaternion& orientation) |
---|
127 | { |
---|
128 | // update listener orientation |
---|
129 | Vector3 up = orientation.xAxis(); // just a wild guess |
---|
130 | Vector3 at = orientation.zAxis(); |
---|
131 | |
---|
132 | ALfloat orient[6] = { at.x, at.y, at.z, |
---|
133 | up.x, up.y, up.z }; |
---|
134 | |
---|
135 | alListenerfv(AL_POSITION, orient); |
---|
136 | ALenum error = alGetError(); |
---|
137 | if (error == AL_INVALID_VALUE) |
---|
138 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
---|
139 | } |
---|
140 | |
---|
141 | void SoundManager::registerAmbientSound(AmbientSound* newAmbient) |
---|
142 | { |
---|
143 | if(newAmbient != NULL) |
---|
144 | { |
---|
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); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | void SoundManager::unregisterAmbientSound(AmbientSound* currentAmbient) |
---|
155 | { |
---|
156 | if(currentAmbient == NULL || ambientSounds_.empty()) |
---|
157 | { |
---|
158 | return; |
---|
159 | } |
---|
160 | if(this->ambientSounds_.front() == currentAmbient) |
---|
161 | { |
---|
162 | this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0)); |
---|
163 | this->ambientSounds_.pop_front(); |
---|
164 | if(!(this->ambientSounds_.empty())) |
---|
165 | { |
---|
166 | this->fadeInList_.push_front(std::make_pair(this->ambientSounds_.front(), 0.0)); |
---|
167 | } |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++) |
---|
172 | { |
---|
173 | if(*it == currentAmbient) |
---|
174 | { |
---|
175 | currentAmbient->doStop(); |
---|
176 | this->ambientSounds_.erase(it); |
---|
177 | break; |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | } |
---|
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 | { |
---|
186 | lastReqPath_ = "ambient/" + MoodManager::getInstance().getMood() + "/" + source; |
---|
187 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath_); |
---|
188 | if(fileInfo == NULL) |
---|
189 | { |
---|
190 | return BLANKSTRING; |
---|
191 | } |
---|
192 | return lastReqPath_; |
---|
193 | } |
---|
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 | } |
---|
254 | } |
---|