Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8521 was 8521, checked in by youngk, 13 years ago

Reverting changes from last commit concerning debug output.

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