Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound6/src/orxonox/sound/SoundManager.cc @ 8662

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

Merged sound5 into sound6 branch.

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