Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/sound/SoundManager.cc @ 6197

Last change on this file since 6197 was 6197, checked in by dafrick, 14 years ago

Small adjustments, better code re-use, added getConfig() function to get values of config values.

  • Property svn:eol-style set to native
File size: 15.5 KB
Line 
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 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "SoundManager.h"
31
32#include <AL/alut.h>
33#include <utility>
34
35#include "util/Exception.h"
36#include "util/Math.h"
37#include "util/ScopeGuard.h"
38#include "util/StringUtils.h"
39#include "util/Clock.h"
40#include "core/GameMode.h"
41#include "core/ScopedSingletonManager.h"
42#include "core/ConfigValueIncludes.h"
43#include "BaseSound.h"
44#include "AmbientSound.h"
45#include "WorldSound.h"
46
47namespace orxonox
48{
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->setVolumeInternal(1.0, SoundType::none);
95        this->setVolumeInternal(1.0, SoundType::ambient);
96        this->setVolumeInternal(1.0, SoundType::effects);
97       
98        this->mute_[SoundType::none] = false;
99        this->mute_[SoundType::ambient] = false;
100        this->mute_[SoundType::effects] = false;
101
102        this->setConfigValues();
103    }
104
105    SoundManager::~SoundManager()
106    {
107        GameMode::setPlaysSound(false);
108        alcDestroyContext(this->context_);
109        alcCloseDevice(this->device_);
110        alutExit();
111    }
112
113    void SoundManager::preUpdate(const Clock& time)
114    {
115        this->processCrossFading(time.getDeltaTime());
116    }
117
118    void SoundManager::setConfigValues()
119    {
120        SetConfigValue(crossFadeStep_, 0.2f)
121            .description("Determines how fast sounds should fade, per second.")
122            .callback(this, &SoundManager::checkFadeStepValidity);
123           
124        SetConfigValue(soundVolume_, 1.0f)
125            .description("Defines the overall volume.")
126            .callback(this, &SoundManager::checkSoundVolumeValidity);
127           
128        SetConfigValue(ambientVolume_, 1.0f)
129            .description("Defines the ambient volume.")
130            .callback(this, &SoundManager::checkAmbientVolumeValidity);
131           
132        SetConfigValue(effectsVolume_, 1.0f)
133            .description("Defines the effects volume.")
134            .callback(this, &SoundManager::checkEffectsVolumeValidity);
135    }
136
137    void SoundManager::checkFadeStepValidity()
138    {
139        if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 )
140        {
141            COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl;
142            ResetConfigValue(crossFadeStep_);
143        }
144        COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl;
145        return;
146    }
147   
148    bool SoundManager::checkVolumeValidity(SoundType::Value type)
149    {
150        bool valid = true;
151       
152        if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0)
153        {
154            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
155            valid = false;
156        }
157       
158        this->updateVolume(type);
159        COUT(3) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl;
160        return valid;
161    }
162   
163    void SoundManager::checkSoundVolumeValidity()
164    {
165        if(!checkVolumeValidity(SoundType::none))
166        {
167            ResetConfigValue(soundVolume_);
168        }
169    }
170   
171    void SoundManager::checkAmbientVolumeValidity()
172    {
173        if(!checkVolumeValidity(SoundType::ambient))
174        {
175            ResetConfigValue(ambientVolume_);
176        }
177    }
178   
179    void SoundManager::checkEffectsVolumeValidity()
180    {
181        if(!checkVolumeValidity(SoundType::effects))
182        {
183            ResetConfigValue(effectsVolume_);
184        }
185    }
186
187    void SoundManager::setListenerPosition(const Vector3& position)
188    {
189        alListener3f(AL_POSITION, position.x, position.y, position.z);
190        ALenum error = alGetError();
191        if (error == AL_INVALID_VALUE)
192            COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl;
193    }
194
195    void SoundManager::setListenerOrientation(const Quaternion& orientation)
196    {
197        // update listener orientation
198        Vector3 up = orientation.xAxis(); // just a wild guess
199        Vector3 at = orientation.zAxis();
200
201        ALfloat orient[6] = { at.x, at.y, at.z,
202                              up.x, up.y, up.z };
203
204        alListenerfv(AL_POSITION, orient);
205        ALenum error = alGetError();
206        if (error == AL_INVALID_VALUE)
207            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
208    }
209
210    void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
211    {
212        if (newAmbient != NULL)
213        {
214            for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
215            {
216                if (it->first == newAmbient)
217                {
218                    COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl;
219                    return;
220                }
221            }
222
223            if (!this->ambientSounds_.empty()) 
224            {
225                this->fadeOut(ambientSounds_.front().first);
226            }
227            this->ambientSounds_.push_front(std::make_pair(newAmbient, false));
228            newAmbient->doPlay();
229            this->fadeIn(newAmbient);
230        }
231    }
232
233    void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient)
234    {
235        if (oldAmbient == NULL || ambientSounds_.empty())
236        {
237            return;
238        }
239        if (this->ambientSounds_.front().first == oldAmbient) 
240        {
241            this->fadeOut(oldAmbient);
242            this->ambientSounds_.pop_front();
243            if (!this->ambientSounds_.empty())
244            {
245                if (!this->ambientSounds_.front().second) // Not paused before
246                {
247                    this->ambientSounds_.front().first->doPlay();
248                }
249                this->fadeIn(this->ambientSounds_.front().first);
250            }
251        }
252        else
253        {
254            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
255            {
256                if (it->first == oldAmbient)
257                {
258                    this->fadeOut(oldAmbient);
259                    this->ambientSounds_.erase(it);
260                    break;
261                }
262            }
263        }
264    }
265
266    void SoundManager::pauseAmbientSound(AmbientSound* ambient)
267    {
268        if (ambient != NULL)
269        {
270            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
271            {
272                if (it->first == ambient)
273                {
274                    it->second = true;
275                    this->fadeOut(it->first);
276                    return;
277                }
278            }
279        }
280    }
281   
282   
283    void SoundManager::setVolume(float vol, SoundType::Value type)
284    {
285        vol = this->checkVolumeRange(vol);
286       
287        this->setVolumeInternal(vol, type);
288       
289        this->updateVolume(type);
290    }
291   
292    float SoundManager::checkVolumeRange(float vol)
293    {
294        if(vol < 0.0 || vol > 1.0)
295        {
296            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
297            vol = vol > 1 ? 1 : vol;
298            vol = vol < 0 ? 0 : vol;
299        }
300       
301        return vol;
302    }
303   
304    void SoundManager::updateVolume(SoundType::Value type)
305    {
306        switch(type)
307        {
308            case SoundType::none:
309                for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it)
310                {
311                    (*it)->updateVolume();
312                }
313                break;
314            case SoundType::ambient:
315                for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it)
316                {
317                    (*it)->updateVolume();
318                }
319                break;
320            case SoundType::effects:
321                for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it)
322                {
323                    (*it)->updateVolume();
324                }
325                break;
326            default:
327                COUT(2) << "Invalid SoundType in SoundManager::updateVolume() - Not updating!" << std::endl;
328        }
329    }
330   
331    void SoundManager::setVolumeInternal(float vol, SoundType::Value type)
332    {
333        switch(type)
334        {
335            case SoundType::none:
336                this->soundVolume_ = vol;
337                break;
338            case SoundType::ambient:
339                this->ambientVolume_ = vol;
340                break;
341            case SoundType::effects:
342                this->effectsVolume_ = vol;
343                break;
344            default:
345                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Not setting any volume!" << std::endl;
346        }
347    }
348   
349    float SoundManager::getVolumeInternal(SoundType::Value type)
350    {
351        switch(type)
352        {
353            case SoundType::none:
354                return this->soundVolume_;
355            case SoundType::ambient:
356                return this->ambientVolume_;
357            case SoundType::effects:
358                return this->effectsVolume_;
359            default:
360                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Returning 0.0!" << std::endl;
361                return 0.0;
362        }
363    }
364   
365    float SoundManager::getVolume(SoundType::Value type) 
366    {
367        if(this->mute_[SoundType::none] || this->mute_[type])
368            return 0.0;
369       
370        if(type == SoundType::none)
371            return this->getVolumeInternal(type);
372       
373        return this->getVolumeInternal(SoundType::none)*this->getVolumeInternal(type);
374    }
375   
376    void SoundManager::toggleMute(SoundType::Value type)
377    {
378        bool mute = !this->mute_[type];
379        this->mute_[type] = mute;
380       
381        this->updateVolume(type);
382    }
383   
384    bool SoundManager::getMute(SoundType::Value type)
385    {
386        return this->mute_[type];
387    }
388   
389
390    void SoundManager::fadeIn(AmbientSound* sound)
391    {
392        // If we're already fading out --> remove that
393        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
394        {
395            if (*it == sound)
396            {
397                this->fadeOutList_.erase(it);
398                break;
399            }
400        }
401        // No duplicate entries
402        if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end())
403            this->fadeInList_.push_back(sound);
404    }
405
406    void SoundManager::fadeOut(AmbientSound* sound)
407    {
408        // If we're already fading in --> remove that
409        for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
410        {
411            if (*it == sound)
412            {
413                this->fadeInList_.erase(it);
414                break;
415            }
416        }
417        // No duplicate entries
418        if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end())
419            this->fadeOutList_.push_back(sound);
420    }
421
422    void SoundManager::processCrossFading(float dt)
423    {
424       
425        // Hacky solution to the fade delay while loading a level.
426        if(dt > 0.2)
427        {
428            return;
429        }
430       
431        // FADE IN
432        for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); )
433        {
434            if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f)
435            {
436                (*it)->setVolume(1.0f);
437                this->fadeInList_.erase(it++);
438            }
439            else
440            {
441                (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt);
442                ++it;
443            }
444        }
445
446        // FADE OUT
447        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); )
448        {
449            if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f)
450            {
451                (*it)->setVolume(0.0f);
452
453                // If sound is in the ambient list --> pause
454                for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2)
455                {
456                    if (it2->first == *it)
457                    {
458                        (*it)->doPause();
459                        break;
460                    }
461                }
462                // If not pause (by loop above for instance) --> stop
463                if (!(*it)->isPaused())
464                    (*it)->doStop();
465
466                this->fadeOutList_.erase(it++);
467            }
468            else
469            {
470                (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt);
471                ++it;
472            }
473        }
474    }
475}
Note: See TracBrowser for help on using the repository browser.