Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Clear openAL error before starting.

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