Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6203 was 6203, checked in by rgrieder, 14 years ago

Added buffer buffering: Sounds are only loaded into one buffer and then reused.

  • Property svn:eol-style set to native
File size: 16.4 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/ConfigValueIncludes.h"
41#include "core/GameMode.h"
42#include "core/ScopedSingletonManager.h"
43#include "core/Resource.h"
44#include "SoundBuffer.h"
45#include "BaseSound.h"
46#include "AmbientSound.h"
47#include "WorldSound.h"
48
49namespace orxonox
50{
51    ManageScopedSingleton(SoundManager, ScopeID::Graphics, true);
52
53    SoundManager::SoundManager()
54    {
55        RegisterRootObject(SoundManager);
56
57        if (!alutInitWithoutContext(NULL, NULL))
58            ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
59        Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
60
61        COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl;
62        this->device_ = alcOpenDevice(NULL);
63        if (this->device_ == NULL)
64        {
65            COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl;
66#ifdef ORXONOX_PLATFORM_WINDOWS
67            COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl;
68#endif
69            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device.");
70        }
71        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
72
73        COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl;
74        this->context_ = alcCreateContext(this->device_, NULL);
75        if (this->context_ == NULL)
76            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context");
77        Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_);
78
79        if (alcMakeContextCurrent(this->context_) == AL_TRUE)
80            COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl;
81
82        COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl;
83
84        const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER);
85        if (str == NULL)
86            COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;
87        else
88            COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl;
89
90        GameMode::setPlaysSound(true);
91        // Disarm guards
92        alutExitGuard.Dismiss();
93        closeDeviceGuard.Dismiss();
94        desroyContextGuard.Dismiss();
95       
96        this->setVolumeInternal(1.0, SoundType::none);
97        this->setVolumeInternal(1.0, SoundType::ambient);
98        this->setVolumeInternal(1.0, SoundType::effects);
99       
100        this->mute_[SoundType::none] = false;
101        this->mute_[SoundType::ambient] = false;
102        this->mute_[SoundType::effects] = false;
103
104        this->setConfigValues();
105    }
106
107    SoundManager::~SoundManager()
108    {
109        GameMode::setPlaysSound(false);
110        alcDestroyContext(this->context_);
111        alcCloseDevice(this->device_);
112        alutExit();
113    }
114
115    void SoundManager::preUpdate(const Clock& time)
116    {
117        this->processCrossFading(time.getDeltaTime());
118    }
119
120    void SoundManager::setConfigValues()
121    {
122        SetConfigValue(crossFadeStep_, 0.2f)
123            .description("Determines how fast sounds should fade, per second.")
124            .callback(this, &SoundManager::checkFadeStepValidity);
125           
126        SetConfigValue(soundVolume_, 1.0f)
127            .description("Defines the overall volume.")
128            .callback(this, &SoundManager::checkSoundVolumeValidity);
129           
130        SetConfigValue(ambientVolume_, 1.0f)
131            .description("Defines the ambient volume.")
132            .callback(this, &SoundManager::checkAmbientVolumeValidity);
133           
134        SetConfigValue(effectsVolume_, 1.0f)
135            .description("Defines the effects volume.")
136            .callback(this, &SoundManager::checkEffectsVolumeValidity);
137    }
138
139    void SoundManager::checkFadeStepValidity()
140    {
141        if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 )
142        {
143            COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl;
144            ResetConfigValue(crossFadeStep_);
145        }
146        COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl;
147        return;
148    }
149   
150    bool SoundManager::checkVolumeValidity(SoundType::Value type)
151    {
152        bool valid = true;
153       
154        if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0)
155        {
156            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
157            valid = false;
158        }
159       
160        this->updateVolume(type);
161        COUT(3) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl;
162        return valid;
163    }
164   
165    void SoundManager::checkSoundVolumeValidity()
166    {
167        if(!checkVolumeValidity(SoundType::none))
168        {
169            ResetConfigValue(soundVolume_);
170        }
171    }
172   
173    void SoundManager::checkAmbientVolumeValidity()
174    {
175        if(!checkVolumeValidity(SoundType::ambient))
176        {
177            ResetConfigValue(ambientVolume_);
178        }
179    }
180   
181    void SoundManager::checkEffectsVolumeValidity()
182    {
183        if(!checkVolumeValidity(SoundType::effects))
184        {
185            ResetConfigValue(effectsVolume_);
186        }
187    }
188
189    void SoundManager::setListenerPosition(const Vector3& position)
190    {
191        alListener3f(AL_POSITION, position.x, position.y, position.z);
192        ALenum error = alGetError();
193        if (error == AL_INVALID_VALUE)
194            COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl;
195    }
196
197    void SoundManager::setListenerOrientation(const Quaternion& orientation)
198    {
199        // update listener orientation
200        Vector3 up = orientation.xAxis(); // just a wild guess
201        Vector3 at = orientation.zAxis();
202
203        ALfloat orient[6] = { at.x, at.y, at.z,
204                              up.x, up.y, up.z };
205
206        alListenerfv(AL_POSITION, orient);
207        ALenum error = alGetError();
208        if (error == AL_INVALID_VALUE)
209            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
210    }
211
212    void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
213    {
214        if (newAmbient != NULL)
215        {
216            for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
217            {
218                if (it->first == newAmbient)
219                {
220                    COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl;
221                    return;
222                }
223            }
224
225            if (!this->ambientSounds_.empty()) 
226            {
227                this->fadeOut(ambientSounds_.front().first);
228            }
229            this->ambientSounds_.push_front(std::make_pair(newAmbient, false));
230            newAmbient->doPlay();
231            this->fadeIn(newAmbient);
232        }
233    }
234
235    void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient)
236    {
237        if (oldAmbient == NULL || ambientSounds_.empty())
238        {
239            return;
240        }
241        if (this->ambientSounds_.front().first == oldAmbient) 
242        {
243            this->fadeOut(oldAmbient);
244            this->ambientSounds_.pop_front();
245            if (!this->ambientSounds_.empty())
246            {
247                if (!this->ambientSounds_.front().second) // Not paused before
248                {
249                    this->ambientSounds_.front().first->doPlay();
250                }
251                this->fadeIn(this->ambientSounds_.front().first);
252            }
253        }
254        else
255        {
256            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
257            {
258                if (it->first == oldAmbient)
259                {
260                    this->fadeOut(oldAmbient);
261                    this->ambientSounds_.erase(it);
262                    break;
263                }
264            }
265        }
266    }
267
268    void SoundManager::pauseAmbientSound(AmbientSound* ambient)
269    {
270        if (ambient != NULL)
271        {
272            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
273            {
274                if (it->first == ambient)
275                {
276                    it->second = true;
277                    this->fadeOut(it->first);
278                    return;
279                }
280            }
281        }
282    }
283   
284   
285    void SoundManager::setVolume(float vol, SoundType::Value type)
286    {
287        vol = this->checkVolumeRange(vol);
288       
289        this->setVolumeInternal(vol, type);
290       
291        this->updateVolume(type);
292    }
293   
294    float SoundManager::checkVolumeRange(float vol)
295    {
296        if(vol < 0.0 || vol > 1.0)
297        {
298            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
299            vol = vol > 1 ? 1 : vol;
300            vol = vol < 0 ? 0 : vol;
301        }
302       
303        return vol;
304    }
305   
306    void SoundManager::updateVolume(SoundType::Value type)
307    {
308        switch(type)
309        {
310            case SoundType::none:
311                for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it)
312                {
313                    (*it)->updateVolume();
314                }
315                break;
316            case SoundType::ambient:
317                for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it)
318                {
319                    (*it)->updateVolume();
320                }
321                break;
322            case SoundType::effects:
323                for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it)
324                {
325                    (*it)->updateVolume();
326                }
327                break;
328            default:
329                COUT(2) << "Invalid SoundType in SoundManager::updateVolume() - Not updating!" << std::endl;
330        }
331    }
332   
333    void SoundManager::setVolumeInternal(float vol, SoundType::Value type)
334    {
335        switch(type)
336        {
337            case SoundType::none:
338                this->soundVolume_ = vol;
339                break;
340            case SoundType::ambient:
341                this->ambientVolume_ = vol;
342                break;
343            case SoundType::effects:
344                this->effectsVolume_ = vol;
345                break;
346            default:
347                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Not setting any volume!" << std::endl;
348        }
349    }
350   
351    float SoundManager::getVolumeInternal(SoundType::Value type)
352    {
353        switch(type)
354        {
355            case SoundType::none:
356                return this->soundVolume_;
357            case SoundType::ambient:
358                return this->ambientVolume_;
359            case SoundType::effects:
360                return this->effectsVolume_;
361            default:
362                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Returning 0.0!" << std::endl;
363                return 0.0;
364        }
365    }
366   
367    float SoundManager::getVolume(SoundType::Value type) 
368    {
369        if(this->mute_[SoundType::none] || this->mute_[type])
370            return 0.0;
371       
372        if(type == SoundType::none)
373            return this->getVolumeInternal(type);
374       
375        return this->getVolumeInternal(SoundType::none)*this->getVolumeInternal(type);
376    }
377   
378    void SoundManager::toggleMute(SoundType::Value type)
379    {
380        bool mute = !this->mute_[type];
381        this->mute_[type] = mute;
382       
383        this->updateVolume(type);
384    }
385   
386    bool SoundManager::getMute(SoundType::Value type)
387    {
388        return this->mute_[type];
389    }
390   
391
392    void SoundManager::fadeIn(AmbientSound* sound)
393    {
394        // If we're already fading out --> remove that
395        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
396        {
397            if (*it == sound)
398            {
399                this->fadeOutList_.erase(it);
400                break;
401            }
402        }
403        // No duplicate entries
404        if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end())
405            this->fadeInList_.push_back(sound);
406    }
407
408    void SoundManager::fadeOut(AmbientSound* sound)
409    {
410        // If we're already fading in --> remove that
411        for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
412        {
413            if (*it == sound)
414            {
415                this->fadeInList_.erase(it);
416                break;
417            }
418        }
419        // No duplicate entries
420        if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end())
421            this->fadeOutList_.push_back(sound);
422    }
423
424    void SoundManager::processCrossFading(float dt)
425    {
426       
427        // Hacky solution to the fade delay while loading a level.
428        if(dt > 0.2)
429        {
430            return;
431        }
432       
433        // FADE IN
434        for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); )
435        {
436            if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f)
437            {
438                (*it)->setVolume(1.0f);
439                this->fadeInList_.erase(it++);
440            }
441            else
442            {
443                (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt);
444                ++it;
445            }
446        }
447
448        // FADE OUT
449        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); )
450        {
451            if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f)
452            {
453                (*it)->setVolume(0.0f);
454
455                // If sound is in the ambient list --> pause
456                for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2)
457                {
458                    if (it2->first == *it)
459                    {
460                        (*it)->doPause();
461                        break;
462                    }
463                }
464                // If not pause (by loop above for instance) --> stop
465                if (!(*it)->isPaused())
466                    (*it)->doStop();
467
468                this->fadeOutList_.erase(it++);
469            }
470            else
471            {
472                (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt);
473                ++it;
474            }
475        }
476    }
477
478    shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(shared_ptr<ResourceInfo> fileInfo)
479    {
480        std::map<std::string, weak_ptr<SoundBuffer> >::const_iterator it
481            = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
482        if (it != this->soundBuffers_.end())
483            return it->second.lock();
484        else
485        {
486            shared_ptr<SoundBuffer> buffer(new SoundBuffer(fileInfo));
487            this->soundBuffers_[fileInfo->group + '/' + fileInfo->filename] = buffer;
488            return buffer;
489        }
490    }
491
492    void SoundManager::removeBuffer(shared_ptr<ResourceInfo> fileInfo)
493    {
494        std::map<std::string, weak_ptr<SoundBuffer> >::const_iterator it
495            = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
496        if (it == this->soundBuffers_.end())
497            this->soundBuffers_.erase(it);
498    }
499}
Note: See TracBrowser for help on using the repository browser.