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, 16 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
RevLine 
[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
[6117]24 *       Kevin Young
[3060]25 *   Co-authors:
26 *      ...
27 *
28 */
29
[3196]30#include "SoundManager.h"
31
[3060]32#include <AL/alut.h>
[6117]33#include <utility>
[3060]34
[5929]35#include "util/Exception.h"
[3196]36#include "util/Math.h"
[5929]37#include "util/ScopeGuard.h"
[6117]38#include "util/StringUtils.h"
39#include "util/Clock.h"
[5929]40#include "core/GameMode.h"
41#include "core/ScopedSingletonManager.h"
[6117]42#include "core/ConfigValueIncludes.h"
43#include "BaseSound.h"
44#include "AmbientSound.h"
[6184]45#include "WorldSound.h"
[3060]46
47namespace orxonox
48{
[5929]49    ManageScopedSingleton(SoundManager, ScopeID::Graphics, true);
[3060]50
51    SoundManager::SoundManager()
52    {
[6117]53        RegisterRootObject(SoundManager);
54
55        if (!alutInitWithoutContext(NULL, NULL))
56            ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
[5929]57        Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
58
[6117]59        COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl;
[5929]60        this->device_ = alcOpenDevice(NULL);
61        if (this->device_ == NULL)
[3060]62        {
[6117]63            COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl;
[5929]64#ifdef ORXONOX_PLATFORM_WINDOWS
[6117]65            COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl;
[5929]66#endif
[6117]67            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device.");
[3060]68        }
[5929]69        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
70
[6117]71        COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl;
[5929]72        this->context_ = alcCreateContext(this->device_, NULL);
73        if (this->context_ == NULL)
[6117]74            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context");
[5929]75        Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_);
76
77        if (alcMakeContextCurrent(this->context_) == AL_TRUE)
[6117]78            COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl;
[5929]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)
[6117]84            COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;
[3060]85        else
[6117]86            COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl;
[3060]87
[5929]88        GameMode::setPlaysSound(true);
89        // Disarm guards
90        alutExitGuard.Dismiss();
91        closeDeviceGuard.Dismiss();
92        desroyContextGuard.Dismiss();
[6184]93       
[6186]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;
[6117]101
102        this->setConfigValues();
[3060]103    }
104
105    SoundManager::~SoundManager()
106    {
[5929]107        GameMode::setPlaysSound(false);
[3060]108        alcDestroyContext(this->context_);
[3280]109        alcCloseDevice(this->device_);
[3060]110        alutExit();
111    }
112
[6183]113    void SoundManager::preUpdate(const Clock& time)
[6117]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);
[6184]123           
[6186]124        SetConfigValue(soundVolume_, 1.0f)
[6184]125            .description("Defines the overall volume.")
[6186]126            .callback(this, &SoundManager::checkSoundVolumeValidity);
[6184]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);
[6117]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    }
[6184]147   
[6186]148    bool SoundManager::checkVolumeValidity(SoundType::Value type)
[6184]149    {
[6186]150        bool valid = true;
151       
152        if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0)
[6184]153        {
154            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
[6186]155            valid = false;
[6184]156        }
157       
[6186]158        this->updateVolume(type);
159        COUT(3) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl;
160        return valid;
[6184]161    }
162   
[6186]163    void SoundManager::checkSoundVolumeValidity()
164    {
[6197]165        if(!checkVolumeValidity(SoundType::none))
[6186]166        {
167            ResetConfigValue(soundVolume_);
168        }
169    }
170   
[6184]171    void SoundManager::checkAmbientVolumeValidity()
172    {
[6197]173        if(!checkVolumeValidity(SoundType::ambient))
[6184]174        {
175            ResetConfigValue(ambientVolume_);
176        }
177    }
178   
179    void SoundManager::checkEffectsVolumeValidity()
180    {
[6197]181        if(!checkVolumeValidity(SoundType::effects))
[6184]182        {
183            ResetConfigValue(effectsVolume_);
184        }
185    }
[6117]186
[5929]187    void SoundManager::setListenerPosition(const Vector3& position)
[3060]188    {
[5929]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;
[3060]193    }
194
[5929]195    void SoundManager::setListenerOrientation(const Quaternion& orientation)
[3060]196    {
[5929]197        // update listener orientation
198        Vector3 up = orientation.xAxis(); // just a wild guess
199        Vector3 at = orientation.zAxis();
[3060]200
[5929]201        ALfloat orient[6] = { at.x, at.y, at.z,
202                              up.x, up.y, up.z };
[3060]203
[5929]204        alListenerfv(AL_POSITION, orient);
[3060]205        ALenum error = alGetError();
[5929]206        if (error == AL_INVALID_VALUE)
[3060]207            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
208    }
[6117]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    }
[6184]281   
[6186]282   
283    void SoundManager::setVolume(float vol, SoundType::Value type)
[6184]284    {
[6186]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)
[6184]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       
[6186]301        return vol;
[6184]302    }
303   
[6186]304    void SoundManager::updateVolume(SoundType::Value type)
[6184]305    {
[6186]306        switch(type)
[6184]307        {
[6186]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;
[6184]328        }
329    }
330   
[6186]331    void SoundManager::setVolumeInternal(float vol, SoundType::Value type)
[6184]332    {
[6186]333        switch(type)
[6184]334        {
[6186]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;
[6184]346        }
347    }
348   
[6186]349    float SoundManager::getVolumeInternal(SoundType::Value type)
[6184]350    {
[6186]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        }
[6184]363    }
364   
[6186]365    float SoundManager::getVolume(SoundType::Value type) 
[6184]366    {
[6186]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);
[6184]374    }
375   
[6186]376    void SoundManager::toggleMute(SoundType::Value type)
[6184]377    {
[6186]378        bool mute = !this->mute_[type];
379        this->mute_[type] = mute;
380       
381        this->updateVolume(type);
[6184]382    }
[6186]383   
384    bool SoundManager::getMute(SoundType::Value type)
385    {
386        return this->mute_[type];
387    }
388   
[6117]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
[6138]432        for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); )
[6117]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
[6138]447        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); )
[6117]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    }
[3060]475}
Note: See TracBrowser for help on using the repository browser.