Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ois_update/src/orxonox/sound/SoundManager.cc @ 7591

Last change on this file since 7591 was 7591, checked in by youngk, 14 years ago

Adjusted inclusion of ALUT header files for mac and edited the PackageConfigOSX script.

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