Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound5/src/orxonox/sound/SoundManager.cc @ 6931

Last change on this file since 6931 was 6767, checked in by youngk, 14 years ago

Added descriptions and missing documentation to the classes I edited last semester. Thanks!

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