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