Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/sound/SoundManager.cc @ 6298

Last change on this file since 6298 was 6298, checked in by scheusso, 14 years ago

hackfix with config values/sound device

  • Property svn:eol-style set to native
File size: 20.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 <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/GameMode.h"
42#include "core/ScopedSingletonManager.h"
43#include "core/Resource.h"
44#include "SoundBuffer.h"
45#include "BaseSound.h"
46#include "AmbientSound.h"
47#include "WorldSound.h"
48
49namespace orxonox
50{
51    ManageScopedSingleton(SoundManager, ScopeID::Graphics, true);
52
53    SoundManager::SoundManager()
54        : effectsPoolSize_(0)
55    {
56        RegisterRootObject(SoundManager);
57
58        if (!alutInitWithoutContext(NULL, NULL))
59            ThrowException(InitialisationFailed, "Sound Error: ALUT initialisation failed: " << alutGetErrorString(alutGetError()));
60        Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
61
62        // Get list of available sound devices and display them
63/*        const char* devices = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
64        char* device = new char[strlen(devices)+1];
65        strcpy(device, devices);
66        std::string renderDevice;
67//        SetConfigValue(renderDevice, std::string(device)).description("Sound device used for rendering");
68        COUT(4) << "Sound: Available devices: ";
69        while (true)
70        {
71            this->deviceNames_.push_back(devices);
72            COUT(4) << "\"" << devices << "\", ";
73            devices += strlen(devices) + 1;
74            if (*devices == '\0')
75                break;
76        }
77        COUT(4) << std::endl;
78
79        // Open the selected device
80        COUT(3) << "Sound: Opening device \"" << renderDevice << "\"" << std::endl;
81        this->device_ = alcOpenDevice(renderDevice.c_str());*/
82        this->device_ = alcOpenDevice(NULL);
83        if (this->device_ == NULL)
84        {
85            COUT(1) << "Sound: Could not open sound device. Have you installed OpenAL?" << std::endl;
86#ifdef ORXONOX_PLATFORM_WINDOWS
87            COUT(1) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl;
88#endif
89            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device.");
90        }
91        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
92
93        // Create sound context and make it the currently used one
94        this->context_ = alcCreateContext(this->device_, NULL);
95        if (this->context_ == NULL)
96            ThrowException(InitialisationFailed, "Sound Error: Could not create ALC context");
97        Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_);
98        if (!alcMakeContextCurrent(this->context_))
99            ThrowException(InitialisationFailed, "Sound Error: Could not use ALC context");
100
101        GameMode::setPlaysSound(true);
102
103        // Get some information about the sound
104        if (const char* version = alGetString(AL_VERSION))
105            COUT(4) << "Sound: --- OpenAL Version: " << version << std::endl;
106        if (const char* vendor = alGetString(AL_VENDOR))
107            COUT(4) << "Sound: --- OpenAL Vendor : " << vendor << std::endl;
108        if (const char* types = alutGetMIMETypes(ALUT_LOADER_BUFFER))
109            COUT(4) << "Sound: --- Supported MIME Types: " << types << std::endl;
110        else
111            COUT(2) << "Sound Warning: MIME Type retrieval failed: " << alutGetErrorString(alutGetError()) << std::endl;
112
113        // Disarm guards
114        alutExitGuard.Dismiss();
115        closeDeviceGuard.Dismiss();
116        desroyContextGuard.Dismiss();
117       
118        this->setVolumeInternal(1.0, SoundType::none);
119        this->setVolumeInternal(1.0, SoundType::ambient);
120        this->setVolumeInternal(1.0, SoundType::effects);
121       
122        this->mute_[SoundType::none] = false;
123        this->mute_[SoundType::ambient] = false;
124        this->mute_[SoundType::effects] = false;
125
126        this->setConfigValues();
127
128        COUT(4) << "Sound: Initialisation complete" << std::endl;
129    }
130
131    SoundManager::~SoundManager()
132    {
133        GameMode::setPlaysSound(false);
134
135        // Relieve context to destroy it
136        if (!alcMakeContextCurrent(NULL))
137            COUT(1) << "Sound Error: Could not unset ALC context" << std::endl;
138        alcDestroyContext(this->context_);
139        if (ALCenum error = alcGetError(this->device_))
140        {
141            if (error == AL_INVALID_OPERATION)
142                COUT(1) << "Sound Error: Could not destroy ALC context because it is the current one" << std::endl;
143            else
144                COUT(1) << "Sound Error: Could not destroy ALC context because it is invalid" << std::endl;
145        }
146#ifdef AL_VERSION_1_1
147        if (!alcCloseDevice(this->device_))
148            COUT(1) << "Sound Error: Could not destroy ALC device. This might be because there are still buffers in use!" << std::endl;
149#else
150        alcCloseDevice(this->device_);
151#endif
152        if (!alutExit())
153            COUT(1) << "Sound Error: Closing ALUT failed: " << alutGetErrorString(alutGetError()) << std::endl;
154    }
155
156    void SoundManager::preUpdate(const Clock& time)
157    {
158        this->processCrossFading(time.getDeltaTime());
159    }
160
161    void SoundManager::setConfigValues()
162    {
163        SetConfigValue(crossFadeStep_, 0.2f)
164            .description("Determines how fast sounds should fade, per second.")
165            .callback(this, &SoundManager::checkFadeStepValidity);
166           
167        SetConfigValue(soundVolume_, 1.0f)
168            .description("Defines the overall volume.")
169            .callback(this, &SoundManager::checkSoundVolumeValidity);
170           
171        SetConfigValue(ambientVolume_, 1.0f)
172            .description("Defines the ambient volume.")
173            .callback(this, &SoundManager::checkAmbientVolumeValidity);
174           
175        SetConfigValue(effectsVolume_, 1.0f)
176            .description("Defines the effects volume.")
177            .callback(this, &SoundManager::checkEffectsVolumeValidity);
178    }
179
180    std::string SoundManager::getALErrorString(ALenum code)
181    {
182        switch (code)
183        {
184        case AL_NO_ERROR:          return "No error";
185        case AL_INVALID_NAME:      return "Invalid AL parameter name";
186        case AL_INVALID_ENUM:      return "Invalid AL enum";
187        case AL_INVALID_VALUE:     return "Invalid AL value";
188        case AL_INVALID_OPERATION: return "Invalid AL operation";
189        case AL_OUT_OF_MEMORY:     return "AL reports out of memory";
190        default:                   return "Unknown AL error";
191        }
192    }
193
194    void SoundManager::checkFadeStepValidity()
195    {
196        if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 )
197        {
198            COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl;
199            ResetConfigValue(crossFadeStep_);
200        }
201        COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl;
202        return;
203    }
204   
205    bool SoundManager::checkVolumeValidity(SoundType::Value type)
206    {
207        bool valid = true;
208       
209        if(this->getVolumeInternal(type) < 0.0 || this->getVolumeInternal(type) > 1.0)
210        {
211            COUT(2) << "Sound warning: Sound volume out of range, ignoring change." << std::endl;
212            valid = false;
213        }
214       
215        this->updateVolume(type);
216        COUT(4) << "SoundManager: volume set to " << this->getVolumeInternal(type) << std::endl;
217        return valid;
218    }
219   
220    void SoundManager::checkSoundVolumeValidity()
221    {
222        if(!checkVolumeValidity(SoundType::none))
223        {
224            ResetConfigValue(soundVolume_);
225        }
226    }
227   
228    void SoundManager::checkAmbientVolumeValidity()
229    {
230        if(!checkVolumeValidity(SoundType::ambient))
231        {
232            ResetConfigValue(ambientVolume_);
233        }
234    }
235   
236    void SoundManager::checkEffectsVolumeValidity()
237    {
238        if(!checkVolumeValidity(SoundType::effects))
239        {
240            ResetConfigValue(effectsVolume_);
241        }
242    }
243
244    void SoundManager::setListenerPosition(const Vector3& position)
245    {
246        alListener3f(AL_POSITION, position.x, position.y, position.z);
247        ALenum error = alGetError();
248        if (error == AL_INVALID_VALUE)
249            COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl;
250    }
251
252    void SoundManager::setListenerOrientation(const Quaternion& orientation)
253    {
254        // update listener orientation
255        const Vector3& direction = -orientation.zAxis();
256        const Vector3& up = orientation.yAxis();
257
258        ALfloat orient[6] = { direction.x, direction.y, direction.z, up.x, up.y, up.z };
259
260        alListenerfv(AL_ORIENTATION, orient);
261        ALenum error = alGetError();
262        if (error == AL_INVALID_VALUE)
263            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
264    }
265
266    void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
267    {
268        if (newAmbient != NULL)
269        {
270            for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
271            {
272                if (it->first == newAmbient)
273                {
274                    COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl;
275                    return;
276                }
277            }
278
279            if (!this->ambientSounds_.empty()) 
280            {
281                this->fadeOut(ambientSounds_.front().first);
282            }
283            this->ambientSounds_.push_front(std::make_pair(newAmbient, false));
284            newAmbient->doPlay();
285            this->fadeIn(newAmbient);
286        }
287    }
288
289    void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient)
290    {
291        if (oldAmbient == NULL || ambientSounds_.empty())
292        {
293            return;
294        }
295        if (this->ambientSounds_.front().first == oldAmbient) 
296        {
297            this->fadeOut(oldAmbient);
298            this->ambientSounds_.pop_front();
299            if (!this->ambientSounds_.empty())
300            {
301                if (!this->ambientSounds_.front().second) // Not paused before
302                {
303                    this->ambientSounds_.front().first->doPlay();
304                }
305                this->fadeIn(this->ambientSounds_.front().first);
306            }
307        }
308        else
309        {
310            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
311            {
312                if (it->first == oldAmbient)
313                {
314                    this->fadeOut(oldAmbient);
315                    this->ambientSounds_.erase(it);
316                    break;
317                }
318            }
319        }
320    }
321
322    void SoundManager::pauseAmbientSound(AmbientSound* ambient)
323    {
324        if (ambient != NULL)
325        {
326            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
327            {
328                if (it->first == ambient)
329                {
330                    it->second = true;
331                    this->fadeOut(it->first);
332                    return;
333                }
334            }
335        }
336    }
337   
338   
339    void SoundManager::setVolume(float vol, SoundType::Value type)
340    {
341        vol = this->checkVolumeRange(vol);
342       
343        this->setVolumeInternal(vol, type);
344       
345        this->updateVolume(type);
346    }
347   
348    float SoundManager::checkVolumeRange(float vol)
349    {
350        if(vol < 0.0 || vol > 1.0)
351        {
352            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
353            vol = vol > 1 ? 1 : vol;
354            vol = vol < 0 ? 0 : vol;
355        }
356       
357        return vol;
358    }
359   
360    void SoundManager::updateVolume(SoundType::Value type)
361    {
362        switch(type)
363        {
364            case SoundType::none:
365                for (ObjectList<BaseSound>::iterator it = ObjectList<BaseSound>::begin(); it != ObjectList<BaseSound>::end(); ++it)
366                {
367                    (*it)->updateVolume();
368                }
369                break;
370            case SoundType::ambient:
371                for (ObjectList<AmbientSound>::iterator it = ObjectList<AmbientSound>::begin(); it != ObjectList<AmbientSound>::end(); ++it)
372                {
373                    (*it)->updateVolume();
374                }
375                break;
376            case SoundType::effects:
377                for (ObjectList<WorldSound>::iterator it = ObjectList<WorldSound>::begin(); it != ObjectList<WorldSound>::end(); ++it)
378                {
379                    (*it)->updateVolume();
380                }
381                break;
382            default:
383                COUT(2) << "Invalid SoundType in SoundManager::updateVolume() - Not updating!" << std::endl;
384        }
385    }
386   
387    void SoundManager::setVolumeInternal(float vol, SoundType::Value type)
388    {
389        switch(type)
390        {
391            case SoundType::none:
392                this->soundVolume_ = vol;
393                break;
394            case SoundType::ambient:
395                this->ambientVolume_ = vol;
396                break;
397            case SoundType::effects:
398                this->effectsVolume_ = vol;
399                break;
400            default:
401                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Not setting any volume!" << std::endl;
402        }
403    }
404   
405    float SoundManager::getVolumeInternal(SoundType::Value type)
406    {
407        switch(type)
408        {
409            case SoundType::none:
410                return this->soundVolume_;
411            case SoundType::ambient:
412                return this->ambientVolume_;
413            case SoundType::effects:
414                return this->effectsVolume_;
415            default:
416                COUT(2) << "Invalid SoundType in SoundManager::setVolumeInternal() - Returning 0.0!" << std::endl;
417                return 0.0;
418        }
419    }
420   
421    float SoundManager::getVolume(SoundType::Value type) 
422    {
423        if(this->mute_[SoundType::none] || this->mute_[type])
424            return 0.0;
425       
426        if(type == SoundType::none)
427            return this->getVolumeInternal(type);
428       
429        return this->getVolumeInternal(SoundType::none)*this->getVolumeInternal(type);
430    }
431   
432    void SoundManager::toggleMute(SoundType::Value type)
433    {
434        bool mute = !this->mute_[type];
435        this->mute_[type] = mute;
436       
437        this->updateVolume(type);
438    }
439   
440    bool SoundManager::getMute(SoundType::Value type)
441    {
442        return this->mute_[type];
443    }
444   
445
446    void SoundManager::fadeIn(AmbientSound* sound)
447    {
448        // If we're already fading out --> remove that
449        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
450        {
451            if (*it == sound)
452            {
453                this->fadeOutList_.erase(it);
454                break;
455            }
456        }
457        // No duplicate entries
458        if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end())
459            this->fadeInList_.push_back(sound);
460    }
461
462    void SoundManager::fadeOut(AmbientSound* sound)
463    {
464        // If we're already fading in --> remove that
465        for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
466        {
467            if (*it == sound)
468            {
469                this->fadeInList_.erase(it);
470                break;
471            }
472        }
473        // No duplicate entries
474        if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end())
475            this->fadeOutList_.push_back(sound);
476    }
477
478    void SoundManager::processCrossFading(float dt)
479    {
480       
481        // Hacky solution to the fade delay while loading a level.
482        if(dt > 0.2)
483        {
484            return;
485        }
486       
487        // FADE IN
488        for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); )
489        {
490            if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f)
491            {
492                (*it)->setVolume(1.0f);
493                this->fadeInList_.erase(it++);
494            }
495            else
496            {
497                (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt);
498                ++it;
499            }
500        }
501
502        // FADE OUT
503        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); )
504        {
505            if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f)
506            {
507                (*it)->setVolume(0.0f);
508
509                // If sound is in the ambient list --> pause
510                for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2)
511                {
512                    if (it2->first == *it)
513                    {
514                        (*it)->doPause();
515                        break;
516                    }
517                }
518                // If not pause (by loop above for instance) --> stop
519                if (!(*it)->isPaused())
520                    (*it)->doStop();
521
522                this->fadeOutList_.erase(it++);
523            }
524            else
525            {
526                (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt);
527                ++it;
528            }
529        }
530    }
531
532    shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(const std::string& filename)
533    {
534        shared_ptr<SoundBuffer> buffer;
535        // Check active or pooled buffers
536        SoundBufferMap::const_iterator it = this->soundBuffers_.find(filename);
537        if (it != this->soundBuffers_.end())
538        {
539            buffer = it->second;
540
541            // Remove from effects pool if not active used before
542            if (buffer->poolIterator_ != this->effectsPool_.end())
543            {
544                this->effectsPoolSize_ -= buffer->getSize();
545                this->effectsPool_.erase(buffer->poolIterator_);
546                buffer->poolIterator_ = this->effectsPool_.end();
547            }
548        }
549        else
550        {
551            try
552            {
553                buffer.reset(new SoundBuffer(filename));
554                buffer->poolIterator_ = this->effectsPool_.end();
555            }
556            catch (...)
557            {
558                COUT(1) << Exception::handleMessage() << std::endl;
559                return buffer;
560            }
561            this->soundBuffers_[filename] = buffer;
562        }
563        return buffer;
564    }
565
566    void SoundManager::releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer)
567    {
568        // Check if others are still using the buffer
569        if (buffer.use_count() != 2)
570            return;
571        SoundBufferMap::iterator it = this->soundBuffers_.find(buffer->getFilename());
572        if (it != this->soundBuffers_.end())
573        {
574            if (bPoolBuffer)
575            {
576                // Pool already too large?
577                while (this->effectsPoolSize_ + it->second->getSize() > this->maxEffectsPoolSize_s && !this->effectsPool_.empty())
578                {
579                    shared_ptr<SoundBuffer> bufferDel = this->effectsPool_.back();
580                    this->effectsPoolSize_ -= bufferDel->getSize();
581                    bufferDel->poolIterator_ = this->effectsPool_.end();
582                    this->effectsPool_.pop_back();
583                    // Remove from buffer map too
584                    SoundBufferMap::iterator itDel = this->soundBuffers_.find(bufferDel->getFilename());
585                    if (itDel != this->soundBuffers_.end())
586                        this->soundBuffers_.erase(itDel);
587                }
588                // Put buffer into the pool
589                this->effectsPoolSize_ += it->second->getSize();
590                this->effectsPool_.push_front(it->second);
591                it->second->poolIterator_ = this->effectsPool_.begin();
592            }
593            else
594                this->soundBuffers_.erase(it);
595        }
596    }
597}
Note: See TracBrowser for help on using the repository browser.