Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib2/src/orxonox/sound/SoundManager.cc @ 8284

Last change on this file since 8284 was 8284, checked in by rgrieder, 13 years ago

Merged revisions 7978 - 8096 from kicklib to kicklib2.

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