Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed weapon sound source problem

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