Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/sound/SoundManager.cc @ 8037

Last change on this file since 8037 was 7858, checked in by landauf, 14 years ago

removed some spaces

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