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 | * Kevin Young |
---|
25 | * Reto Grieder |
---|
26 | * Co-authors: |
---|
27 | * ... |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | #include "SoundManager.h" |
---|
32 | |
---|
33 | #if defined(__APPLE__) |
---|
34 | #include <ALUT/alut.h> |
---|
35 | #else |
---|
36 | #include <AL/alut.h> |
---|
37 | #endif |
---|
38 | #include <utility> |
---|
39 | #include <loki/ScopeGuard.h> |
---|
40 | |
---|
41 | #include "util/Exception.h" |
---|
42 | #include "util/Math.h" |
---|
43 | #include "util/Clock.h" |
---|
44 | #include "util/ScopedSingletonManager.h" |
---|
45 | #include "core/ConfigValueIncludes.h" |
---|
46 | #include "core/CoreIncludes.h" |
---|
47 | #include "core/GameMode.h" |
---|
48 | #include "core/Resource.h" |
---|
49 | #include "SoundBuffer.h" |
---|
50 | #include "BaseSound.h" |
---|
51 | #include "AmbientSound.h" |
---|
52 | #include "WorldSound.h" |
---|
53 | |
---|
54 | namespace orxonox |
---|
55 | { |
---|
56 | ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); |
---|
57 | |
---|
58 | std::string SoundManager::getALErrorString(ALenum code) |
---|
59 | { |
---|
60 | switch (code) |
---|
61 | { |
---|
62 | case AL_NO_ERROR: return "No error"; |
---|
63 | case AL_INVALID_NAME: return "Invalid AL parameter name"; |
---|
64 | case AL_INVALID_ENUM: return "Invalid AL enum"; |
---|
65 | case AL_INVALID_VALUE: return "Invalid AL value"; |
---|
66 | case AL_INVALID_OPERATION: return "Invalid AL operation"; |
---|
67 | case AL_OUT_OF_MEMORY: return "AL reports out of memory"; |
---|
68 | default: return "Unknown AL error"; |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | SoundManager::SoundManager() |
---|
73 | : effectsPoolSize_(0) |
---|
74 | { |
---|
75 | RegisterRootObject(SoundManager); |
---|
76 | |
---|
77 | // Clear error messages (might be problematic on some systems) |
---|
78 | alGetError(); |
---|
79 | alutGetError(); |
---|
80 | |
---|
81 | // See whether we even want to load |
---|
82 | bool bDisableSound_ = false; |
---|
83 | SetConfigValue(bDisableSound_, false); |
---|
84 | if (bDisableSound_) |
---|
85 | ThrowException(InitialisationAborted, "Sound: Not loading at all"); |
---|
86 | if (!alutInitWithoutContext(NULL, NULL)) |
---|
87 | ThrowException(InitialisationFailed, "Sound Error: ALUT initialisation failed: " << alutGetErrorString(alutGetError())); |
---|
88 | Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); |
---|
89 | |
---|
90 | /* |
---|
91 | // Get list of available sound devices and display them |
---|
92 | const char* devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER); |
---|
93 | char* device = new char[strlen(devices)+1]; |
---|
94 | strcpy(device, devices); |
---|
95 | std::string renderDevice; |
---|
96 | SetConfigValue(renderDevice, std::string(device)).description("Sound device used for rendering"); |
---|
97 | COUT(4) << "Sound: Available devices: "; |
---|
98 | while (true) |
---|
99 | { |
---|
100 | this->deviceNames_.push_back(devices); |
---|
101 | COUT(4) << '"' << devices << "\", "; |
---|
102 | devices += strlen(devices) + 1; |
---|
103 | if (*devices == '\0') |
---|
104 | break; |
---|
105 | } |
---|
106 | COUT(4) << std::endl; |
---|
107 | |
---|
108 | // Open the selected device |
---|
109 | COUT(3) << "Sound: Opening device \"" << renderDevice << '\' << std::endl; |
---|
110 | this->device_ = alcOpenDevice(renderDevice.c_str()); |
---|
111 | */ |
---|
112 | this->device_ = alcOpenDevice(NULL); |
---|
113 | if (this->device_ == NULL) |
---|
114 | { |
---|
115 | COUT(1) << "Sound: Could not open sound device. Have you installed OpenAL?" << std::endl; |
---|
116 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
117 | COUT(1) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; |
---|
118 | #endif |
---|
119 | ThrowException(InitialisationFailed, "Sound Error: Could not open sound device."); |
---|
120 | } |
---|
121 | Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); |
---|
122 | |
---|
123 | // Create sound context and make it the currently used one |
---|
124 | this->context_ = alcCreateContext(this->device_, NULL); |
---|
125 | if (this->context_ == NULL) |
---|
126 | ThrowException(InitialisationFailed, "Sound Error: Could not create ALC context"); |
---|
127 | Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); |
---|
128 | if (!alcMakeContextCurrent(this->context_)) |
---|
129 | ThrowException(InitialisationFailed, "Sound Error: Could not use ALC context"); |
---|
130 | |
---|
131 | GameMode::setPlaysSound(true); |
---|
132 | Loki::ScopeGuard resetPlaysSoundGuard = Loki::MakeGuard(&GameMode::setPlaysSound, false); |
---|
133 | |
---|
134 | // Get some information about the sound |
---|
135 | if (const char* version = alGetString(AL_VERSION)) |
---|
136 | COUT(4) << "Sound: --- OpenAL Version: " << version << std::endl; |
---|
137 | if (const char* vendor = alGetString(AL_VENDOR)) |
---|
138 | COUT(4) << "Sound: --- OpenAL Vendor : " << vendor << std::endl; |
---|
139 | if (const char* types = alutGetMIMETypes(ALUT_LOADER_BUFFER)) |
---|
140 | COUT(4) << "Sound: --- Supported MIME Types: " << types << std::endl; |
---|
141 | else |
---|
142 | COUT(2) << "Sound Warning: MIME Type retrieval failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
143 | |
---|
144 | this->mute_[SoundType::All] = 1.0f; |
---|
145 | this->mute_[SoundType::Music] = 1.0f; |
---|
146 | this->mute_[SoundType::Effects] = 1.0f; |
---|
147 | |
---|
148 | this->setConfigValues(); |
---|
149 | |
---|
150 | // Try to get at least one source |
---|
151 | ALuint source; |
---|
152 | alGenSources(1, &source); |
---|
153 | if (!alGetError() && alIsSource(source)) |
---|
154 | this->availableSoundSources_.push_back(source); |
---|
155 | else |
---|
156 | ThrowException(InitialisationFailed, "Sound Error: Could not create even a single source"); |
---|
157 | // Create a few initial sources |
---|
158 | this->createSoundSources(this->minSources_ - 1); |
---|
159 | |
---|
160 | // Disarm guards |
---|
161 | alutExitGuard.Dismiss(); |
---|
162 | closeDeviceGuard.Dismiss(); |
---|
163 | desroyContextGuard.Dismiss(); |
---|
164 | resetPlaysSoundGuard.Dismiss(); |
---|
165 | |
---|
166 | COUT(4) << "Sound: Initialisation complete" << std::endl; |
---|
167 | } |
---|
168 | |
---|
169 | SoundManager::~SoundManager() |
---|
170 | { |
---|
171 | // Erase fade lists because of the smart pointers |
---|
172 | this->fadeInList_.clear(); |
---|
173 | this->fadeOutList_.clear(); |
---|
174 | |
---|
175 | // If there are still used buffers around, well, that's just very bad... |
---|
176 | if (this->soundBuffers_.size() != this->effectsPool_.size()) |
---|
177 | COUT(1) << "Sound Error: Some sound buffers are still in use but OpenAL is about to shut down. Fix this!" << std::endl; |
---|
178 | // Empty buffer pool and buffer list |
---|
179 | this->effectsPool_.clear(); |
---|
180 | this->soundBuffers_.clear(); |
---|
181 | |
---|
182 | // There should not be any sources in use anymore |
---|
183 | if (!this->usedSoundSources_.empty()) |
---|
184 | COUT(1) << "Sound Error: Some sound sources are still in use but OpenAL is about to shut down. Fix this!" << std::endl; |
---|
185 | while (!this->availableSoundSources_.empty()) |
---|
186 | { |
---|
187 | alDeleteSources(1, &this->availableSoundSources_.back()); |
---|
188 | this->availableSoundSources_.pop_back(); |
---|
189 | } |
---|
190 | |
---|
191 | GameMode::setPlaysSound(false); |
---|
192 | |
---|
193 | // Relieve context to destroy it |
---|
194 | if (!alcMakeContextCurrent(NULL)) |
---|
195 | COUT(1) << "Sound Error: Could not unset ALC context" << std::endl; |
---|
196 | alcDestroyContext(this->context_); |
---|
197 | if (ALCenum error = alcGetError(this->device_)) |
---|
198 | { |
---|
199 | if (error == AL_INVALID_OPERATION) |
---|
200 | COUT(1) << "Sound Error: Could not destroy ALC context because it is the current one" << std::endl; |
---|
201 | else |
---|
202 | COUT(1) << "Sound Error: Could not destroy ALC context because it is invalid" << std::endl; |
---|
203 | } |
---|
204 | #ifdef AL_VERSION_1_1 |
---|
205 | if (!alcCloseDevice(this->device_)) |
---|
206 | COUT(1) << "Sound Error: Could not destroy ALC device. This might be because there are still buffers in use!" << std::endl; |
---|
207 | #else |
---|
208 | alcCloseDevice(this->device_); |
---|
209 | #endif |
---|
210 | if (!alutExit()) |
---|
211 | COUT(1) << "Sound Error: Closing ALUT failed: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
212 | } |
---|
213 | |
---|
214 | void SoundManager::setConfigValues() |
---|
215 | { |
---|
216 | SetConfigValue(crossFadeStep_, 0.2f) |
---|
217 | .description("Determines how fast sounds should fade, per second.") |
---|
218 | .callback(this, &SoundManager::checkFadeStepValidity); |
---|
219 | |
---|
220 | SetConfigValueAlias(volume_[SoundType::All], "soundVolume_", 1.0f) |
---|
221 | .description("Defines the overall volume.") |
---|
222 | .callback(this, &SoundManager::checkSoundVolumeValidity); |
---|
223 | SetConfigValueAlias(volume_[SoundType::Music], "ambientVolume_", 1.0f) |
---|
224 | .description("Defines the ambient volume.") |
---|
225 | .callback(this, &SoundManager::checkAmbientVolumeValidity); |
---|
226 | SetConfigValueAlias(volume_[SoundType::Effects], "effectsVolume_", 1.0f) |
---|
227 | .description("Defines the effects volume.") |
---|
228 | .callback(this, &SoundManager::checkEffectsVolumeValidity); |
---|
229 | |
---|
230 | SetConfigValue(minSources_, 16) |
---|
231 | .description("Minimum number of sources being generated (if possible)"); |
---|
232 | SetConfigValue(maxSources_, 1024) |
---|
233 | .description("Maximum number of sources to be made available"); |
---|
234 | } |
---|
235 | |
---|
236 | void SoundManager::preUpdate(const Clock& time) |
---|
237 | { |
---|
238 | this->processCrossFading(time.getDeltaTime()); |
---|
239 | |
---|
240 | // Check whether a sound object has stopped playing |
---|
241 | for (unsigned int i = 0; i < this->usedSoundSources_.size(); ++i) |
---|
242 | { |
---|
243 | ALint state; |
---|
244 | alGetSourcei(this->usedSoundSources_[i].first, AL_SOURCE_STATE, &state); |
---|
245 | if (state == AL_STOPPED) |
---|
246 | { |
---|
247 | this->usedSoundSources_[i].second->stop(); |
---|
248 | --i; |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | void SoundManager::checkFadeStepValidity() |
---|
254 | { |
---|
255 | if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 ) |
---|
256 | { |
---|
257 | COUT(2) << "Sound warning: fade step out of range, ignoring change." << std::endl; |
---|
258 | ResetConfigValue(crossFadeStep_); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | void SoundManager::checkVolumeValidity(SoundType::Value type) |
---|
263 | { |
---|
264 | float clampedVolume = clamp(this->volume_[type], 0.0f, 1.0f); |
---|
265 | if (clampedVolume != this->volume_[type]) |
---|
266 | COUT(2) << "Sound warning: Volume setting (" << type << ") out of range, clamping." << std::endl; |
---|
267 | this->updateVolume(type); |
---|
268 | } |
---|
269 | |
---|
270 | void SoundManager::setVolume(float vol, SoundType::Value type) |
---|
271 | { |
---|
272 | if (type < 0 || type > SoundType::Effects) |
---|
273 | return; |
---|
274 | this->volume_[type] = vol; |
---|
275 | this->checkVolumeValidity(type); |
---|
276 | } |
---|
277 | |
---|
278 | float SoundManager::getVolume(SoundType::Value type) |
---|
279 | { |
---|
280 | if (type < 0 || type > SoundType::Effects) |
---|
281 | return 0.0f; |
---|
282 | return this->volume_[type]; |
---|
283 | } |
---|
284 | |
---|
285 | float SoundManager::getRealVolume(SoundType::Value type) |
---|
286 | { |
---|
287 | if (type != SoundType::Music && type != SoundType::Effects) |
---|
288 | return 0.0f; |
---|
289 | return this->volume_[SoundType::All] * this->mute_[SoundType::All] * this->volume_[type] * this->mute_[type]; |
---|
290 | } |
---|
291 | |
---|
292 | void SoundManager::updateVolume(SoundType::Value type) |
---|
293 | { |
---|
294 | switch(type) |
---|
295 | { |
---|
296 | case SoundType::All: |
---|
297 | for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it) |
---|
298 | (*it)->updateVolume(); |
---|
299 | break; |
---|
300 | case SoundType::Music: |
---|
301 | for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it) |
---|
302 | (*it)->updateVolume(); |
---|
303 | break; |
---|
304 | case SoundType::Effects: |
---|
305 | for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it) |
---|
306 | (*it)->updateVolume(); |
---|
307 | break; |
---|
308 | default: |
---|
309 | assert(false); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | void SoundManager::toggleMute(SoundType::Value type) |
---|
314 | { |
---|
315 | if (type < 0 || type > SoundType::Effects) |
---|
316 | return; |
---|
317 | this->mute_[type] = (this->mute_[type] == 0) ? 1.0f : 0.0f; |
---|
318 | this->updateVolume(type); |
---|
319 | } |
---|
320 | |
---|
321 | bool SoundManager::getMute(SoundType::Value type) |
---|
322 | { |
---|
323 | if (type < 0 || type > SoundType::Effects) |
---|
324 | return true; |
---|
325 | return (this->mute_[type] == 0); |
---|
326 | } |
---|
327 | |
---|
328 | void SoundManager::setListenerPosition(const Vector3& position) |
---|
329 | { |
---|
330 | alListener3f(AL_POSITION, position.x, position.y, position.z); |
---|
331 | ALenum error = alGetError(); |
---|
332 | if (error == AL_INVALID_VALUE) |
---|
333 | COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; |
---|
334 | } |
---|
335 | |
---|
336 | void SoundManager::setListenerOrientation(const Quaternion& orientation) |
---|
337 | { |
---|
338 | // update listener orientation |
---|
339 | const Vector3& direction = -orientation.zAxis(); |
---|
340 | const Vector3& up = orientation.yAxis(); |
---|
341 | |
---|
342 | ALfloat orient[6] = { direction.x, direction.y, direction.z, up.x, up.y, up.z }; |
---|
343 | |
---|
344 | alListenerfv(AL_ORIENTATION, orient); |
---|
345 | ALenum error = alGetError(); |
---|
346 | if (error == AL_INVALID_VALUE) |
---|
347 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
---|
348 | } |
---|
349 | |
---|
350 | void SoundManager::registerAmbientSound(AmbientSound* newAmbient) |
---|
351 | { |
---|
352 | if (newAmbient != NULL) |
---|
353 | { |
---|
354 | for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
355 | { |
---|
356 | if (it->first == newAmbient) |
---|
357 | { |
---|
358 | COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl; |
---|
359 | return; |
---|
360 | } |
---|
361 | } |
---|
362 | |
---|
363 | if (!this->ambientSounds_.empty()) |
---|
364 | { |
---|
365 | this->fadeOut(ambientSounds_.front().first); |
---|
366 | } |
---|
367 | this->ambientSounds_.push_front(std::make_pair(newAmbient, false)); |
---|
368 | newAmbient->doPlay(); |
---|
369 | this->fadeIn(newAmbient); |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient) |
---|
374 | { |
---|
375 | if (oldAmbient == NULL || ambientSounds_.empty()) |
---|
376 | return; |
---|
377 | |
---|
378 | if (this->ambientSounds_.front().first == oldAmbient) |
---|
379 | { |
---|
380 | this->fadeOut(oldAmbient); |
---|
381 | this->ambientSounds_.pop_front(); |
---|
382 | if (!this->ambientSounds_.empty()) |
---|
383 | { |
---|
384 | if (!this->ambientSounds_.front().second) // Not paused before |
---|
385 | { |
---|
386 | this->ambientSounds_.front().first->doPlay(); |
---|
387 | } |
---|
388 | this->fadeIn(this->ambientSounds_.front().first); |
---|
389 | } |
---|
390 | } |
---|
391 | else |
---|
392 | { |
---|
393 | for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
394 | { |
---|
395 | if (it->first == oldAmbient) |
---|
396 | { |
---|
397 | this->fadeOut(oldAmbient); |
---|
398 | this->ambientSounds_.erase(it); |
---|
399 | break; |
---|
400 | } |
---|
401 | } |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | void SoundManager::pauseAmbientSound(AmbientSound* ambient) |
---|
406 | { |
---|
407 | if (ambient != NULL) |
---|
408 | { |
---|
409 | for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it) |
---|
410 | { |
---|
411 | if (it->first == ambient) |
---|
412 | { |
---|
413 | it->second = true; |
---|
414 | this->fadeOut(it->first); |
---|
415 | return; |
---|
416 | } |
---|
417 | } |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | void SoundManager::fadeIn(const SmartPtr<AmbientSound>& sound) |
---|
422 | { |
---|
423 | // If we're already fading out --> remove that |
---|
424 | for (std::list<SmartPtr<AmbientSound> >::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++) |
---|
425 | { |
---|
426 | if (*it == sound) |
---|
427 | { |
---|
428 | this->fadeOutList_.erase(it); |
---|
429 | break; |
---|
430 | } |
---|
431 | } |
---|
432 | // No duplicate entries |
---|
433 | if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end()) |
---|
434 | this->fadeInList_.push_back(sound); |
---|
435 | } |
---|
436 | |
---|
437 | void SoundManager::fadeOut(const SmartPtr<AmbientSound>& sound) |
---|
438 | { |
---|
439 | // If we're already fading in --> remove that |
---|
440 | for (std::list<SmartPtr<AmbientSound> >::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++) |
---|
441 | { |
---|
442 | if (*it == sound) |
---|
443 | { |
---|
444 | this->fadeInList_.erase(it); |
---|
445 | break; |
---|
446 | } |
---|
447 | } |
---|
448 | // No duplicate entries |
---|
449 | if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end()) |
---|
450 | this->fadeOutList_.push_back(sound); |
---|
451 | } |
---|
452 | |
---|
453 | void SoundManager::processCrossFading(float dt) |
---|
454 | { |
---|
455 | |
---|
456 | // Hacky solution to the fade delay while loading a level. |
---|
457 | if(dt > 0.2) |
---|
458 | { |
---|
459 | return; |
---|
460 | } |
---|
461 | |
---|
462 | // FADE IN |
---|
463 | for (std::list<SmartPtr<AmbientSound> >::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); ) |
---|
464 | { |
---|
465 | if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f) |
---|
466 | { |
---|
467 | (*it)->setVolume(1.0f); |
---|
468 | this->fadeInList_.erase(it++); |
---|
469 | } |
---|
470 | else |
---|
471 | { |
---|
472 | (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt); |
---|
473 | ++it; |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | // FADE OUT |
---|
478 | for (std::list<SmartPtr<AmbientSound> >::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); ) |
---|
479 | { |
---|
480 | if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f) |
---|
481 | { |
---|
482 | (*it)->setVolume(0.0f); |
---|
483 | |
---|
484 | // If sound is in the ambient list --> pause |
---|
485 | for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2) |
---|
486 | { |
---|
487 | if (it2->first == *it) |
---|
488 | { |
---|
489 | (*it)->doPause(); |
---|
490 | break; |
---|
491 | } |
---|
492 | } |
---|
493 | // If not pause (by loop above for instance) --> stop |
---|
494 | if (!(*it)->isPaused()) |
---|
495 | (*it)->doStop(); |
---|
496 | |
---|
497 | this->fadeOutList_.erase(it++); |
---|
498 | } |
---|
499 | else |
---|
500 | { |
---|
501 | (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt); |
---|
502 | ++it; |
---|
503 | } |
---|
504 | } |
---|
505 | } |
---|
506 | |
---|
507 | shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(const std::string& filename) |
---|
508 | { |
---|
509 | shared_ptr<SoundBuffer> buffer; |
---|
510 | // Check active or pooled buffers |
---|
511 | SoundBufferMap::const_iterator it = this->soundBuffers_.find(filename); |
---|
512 | if (it != this->soundBuffers_.end()) |
---|
513 | { |
---|
514 | buffer = it->second; |
---|
515 | |
---|
516 | // Remove from effects pool if not active used before |
---|
517 | if (buffer->poolIterator_ != this->effectsPool_.end()) |
---|
518 | { |
---|
519 | this->effectsPoolSize_ -= buffer->getSize(); |
---|
520 | this->effectsPool_.erase(buffer->poolIterator_); |
---|
521 | buffer->poolIterator_ = this->effectsPool_.end(); |
---|
522 | } |
---|
523 | } |
---|
524 | else |
---|
525 | { |
---|
526 | try |
---|
527 | { |
---|
528 | buffer.reset(new SoundBuffer(filename, this->effectsPool_.end())); |
---|
529 | } |
---|
530 | catch (const std::exception& ex) |
---|
531 | { |
---|
532 | COUT(1) << ex.what() << std::endl; |
---|
533 | return buffer; |
---|
534 | } |
---|
535 | this->soundBuffers_[filename] = buffer; |
---|
536 | } |
---|
537 | return buffer; |
---|
538 | } |
---|
539 | |
---|
540 | void SoundManager::releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer) |
---|
541 | { |
---|
542 | // Check if others are still using the buffer |
---|
543 | if (buffer.use_count() != 2) |
---|
544 | return; |
---|
545 | SoundBufferMap::iterator it = this->soundBuffers_.find(buffer->getFilename()); |
---|
546 | if (it != this->soundBuffers_.end()) |
---|
547 | { |
---|
548 | if (bPoolBuffer) |
---|
549 | { |
---|
550 | // Pool already too large? |
---|
551 | while (this->effectsPoolSize_ + it->second->getSize() > this->maxEffectsPoolSize_s && !this->effectsPool_.empty()) |
---|
552 | { |
---|
553 | shared_ptr<SoundBuffer> bufferDel = this->effectsPool_.back(); |
---|
554 | this->effectsPoolSize_ -= bufferDel->getSize(); |
---|
555 | bufferDel->poolIterator_ = this->effectsPool_.end(); |
---|
556 | this->effectsPool_.pop_back(); |
---|
557 | // Remove from buffer map too |
---|
558 | SoundBufferMap::iterator itDel = this->soundBuffers_.find(bufferDel->getFilename()); |
---|
559 | if (itDel != this->soundBuffers_.end()) |
---|
560 | this->soundBuffers_.erase(itDel); |
---|
561 | } |
---|
562 | // Put buffer into the pool |
---|
563 | this->effectsPoolSize_ += it->second->getSize(); |
---|
564 | this->effectsPool_.push_front(it->second); |
---|
565 | it->second->poolIterator_ = this->effectsPool_.begin(); |
---|
566 | } |
---|
567 | else |
---|
568 | this->soundBuffers_.erase(it); |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | ALuint SoundManager::getSoundSource(BaseSound* object) |
---|
573 | { |
---|
574 | if (!this->availableSoundSources_.empty()) |
---|
575 | { |
---|
576 | ALuint source = this->availableSoundSources_.back(); |
---|
577 | this->availableSoundSources_.pop_back(); |
---|
578 | this->usedSoundSources_.push_back(std::make_pair(source, object)); |
---|
579 | return source; |
---|
580 | } |
---|
581 | else |
---|
582 | { |
---|
583 | if (this->usedSoundSources_.size() < this->maxSources_) |
---|
584 | { |
---|
585 | ALuint source; |
---|
586 | alGenSources(1, &source); |
---|
587 | // Try to create new sources (50% more, but at least one) |
---|
588 | if (alIsSource(source) && !alGetError()) |
---|
589 | { |
---|
590 | this->usedSoundSources_.push_back(std::make_pair(source, object)); |
---|
591 | return source; |
---|
592 | } |
---|
593 | } |
---|
594 | // Return no source ID |
---|
595 | ALuint source = 123456789; |
---|
596 | while (alIsSource(++source)); |
---|
597 | return source; |
---|
598 | } |
---|
599 | } |
---|
600 | |
---|
601 | void SoundManager::releaseSoundSource(ALuint source) |
---|
602 | { |
---|
603 | #ifndef NDEBUG |
---|
604 | for (std::vector<ALuint>::const_iterator it = this->availableSoundSources_.begin(); it != this->availableSoundSources_.end(); ++it) |
---|
605 | assert((*it) != source); |
---|
606 | #endif |
---|
607 | this->availableSoundSources_.push_back(source); |
---|
608 | for (std::vector<std::pair<ALuint, BaseSound*> >::iterator it = this->usedSoundSources_.begin(); |
---|
609 | it != this->usedSoundSources_.end(); ++it) |
---|
610 | { |
---|
611 | if (it->first == source) |
---|
612 | { |
---|
613 | this->usedSoundSources_.erase(it); |
---|
614 | break; |
---|
615 | } |
---|
616 | } |
---|
617 | int used = std::max((unsigned int)(this->usedSoundSources_.size()), this->minSources_); |
---|
618 | // Subtract those we added in the statement above trough std::max |
---|
619 | int available = (int)this->availableSoundSources_.size() - (used - (int)this->usedSoundSources_.size()); |
---|
620 | // Delete sources again to free resources if appropriate (more than 50% more available than used) |
---|
621 | int toDelete = available - used / 2; |
---|
622 | while (toDelete-- > 0) |
---|
623 | { |
---|
624 | alDeleteSources(1, &this->availableSoundSources_.back()); |
---|
625 | if (alGetError()) |
---|
626 | COUT(1) << "Sound Error: Failed to delete a source --> lost forever" << std::endl; |
---|
627 | this->availableSoundSources_.pop_back(); |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | unsigned int SoundManager::createSoundSources(unsigned int n) |
---|
632 | { |
---|
633 | unsigned int count = this->availableSoundSources_.size() + this->usedSoundSources_.size(); |
---|
634 | while (count < this->maxSources_ && count <= n) |
---|
635 | { |
---|
636 | ALuint source; |
---|
637 | alGenSources(1, &source); |
---|
638 | if (alIsSource(source) && !alGetError()) |
---|
639 | this->availableSoundSources_.push_back(source); |
---|
640 | else |
---|
641 | break; |
---|
642 | ++count; |
---|
643 | } |
---|
644 | return count - this->availableSoundSources_.size() - this->usedSoundSources_.size(); |
---|
645 | } |
---|
646 | } |
---|