Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound3/src/orxonox/sound/SoundManager.cc @ 6071

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

Improved synchronisability of the sound classes (not yet Synchronisable though!).

  • Property svn:eol-style set to native
File size: 10.2 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 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "SoundManager.h"
31
32#include <AL/alut.h>
33#include <utility>
34
35#include "util/Exception.h"
36#include "util/Math.h"
37#include "util/ScopeGuard.h"
38#include "util/StringUtils.h"
39#include "util/Clock.h"
40#include "core/GameMode.h"
41#include "core/ScopedSingletonManager.h"
42#include "core/ConfigValueIncludes.h"
43#include "BaseSound.h"
44#include "AmbientSound.h"
45
46namespace orxonox
47{
48    SoundManager* SoundManager::singletonPtr_s = NULL;
49    ManageScopedSingleton(SoundManager, ScopeID::Graphics, true);
50
51    SoundManager::SoundManager()
52    {
53        RegisterRootObject(SoundManager);
54
55        if (!alutInitWithoutContext(NULL, NULL))
56            ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
57        Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
58
59        COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl;
60        this->device_ = alcOpenDevice(NULL);
61        if (this->device_ == NULL)
62        {
63            COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl;
64#ifdef ORXONOX_PLATFORM_WINDOWS
65            COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl;
66#endif
67            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device.");
68        }
69        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
70
71        COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl;
72        this->context_ = alcCreateContext(this->device_, NULL);
73        if (this->context_ == NULL)
74            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context");
75        Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_);
76
77        if (alcMakeContextCurrent(this->context_) == AL_TRUE)
78            COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl;
79
80        COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl;
81
82        const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER);
83        if (str == NULL)
84            COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;
85        else
86            COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl;
87
88        GameMode::setPlaysSound(true);
89        // Disarm guards
90        alutExitGuard.Dismiss();
91        closeDeviceGuard.Dismiss();
92        desroyContextGuard.Dismiss();
93
94        this->setConfigValues();
95    }
96
97    SoundManager::~SoundManager()
98    {
99        GameMode::setPlaysSound(false);
100        alcDestroyContext(this->context_);
101        alcCloseDevice(this->device_);
102        alutExit();
103    }
104
105    void SoundManager::update(const Clock& time)
106    {
107        this->processCrossFading(time.getDeltaTime());
108    }
109
110    void SoundManager::setConfigValues()
111    {
112        SetConfigValue(crossFadeStep_, 0.2f)
113            .description("Determines how fast sounds should fade, per second.")
114            .callback(this, &SoundManager::checkFadeStepValidity);
115    }
116
117    void SoundManager::checkFadeStepValidity()
118    {
119        if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 )
120        {
121            COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl;
122            ResetConfigValue(crossFadeStep_);
123        }
124        COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl;
125        return;
126    }
127
128    void SoundManager::setListenerPosition(const Vector3& position)
129    {
130        alListener3f(AL_POSITION, position.x, position.y, position.z);
131        ALenum error = alGetError();
132        if (error == AL_INVALID_VALUE)
133            COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl;
134    }
135
136    void SoundManager::setListenerOrientation(const Quaternion& orientation)
137    {
138        // update listener orientation
139        Vector3 up = orientation.xAxis(); // just a wild guess
140        Vector3 at = orientation.zAxis();
141
142        ALfloat orient[6] = { at.x, at.y, at.z,
143                              up.x, up.y, up.z };
144
145        alListenerfv(AL_POSITION, orient);
146        ALenum error = alGetError();
147        if (error == AL_INVALID_VALUE)
148            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
149    }
150
151    void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
152    {
153        if (newAmbient != NULL)
154        {
155            for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
156            {
157                if (it->first == newAmbient)
158                {
159                    COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl;
160                    return;
161                }
162            }
163
164            if (!this->ambientSounds_.empty()) 
165            {
166                this->fadeOut(ambientSounds_.front().first);
167            }
168            this->ambientSounds_.push_front(std::make_pair(newAmbient, false));
169            newAmbient->doPlay();
170            this->fadeIn(newAmbient);
171        }
172    }
173
174    void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient)
175    {
176        if (oldAmbient == NULL || ambientSounds_.empty())
177        {
178            return;
179        }
180        if (this->ambientSounds_.front().first == oldAmbient) 
181        {
182            this->fadeOut(oldAmbient);
183            this->ambientSounds_.pop_front();
184            if (!this->ambientSounds_.empty())
185            {
186                if (!this->ambientSounds_.front().second) // Not paused before
187                {
188                    this->ambientSounds_.front().first->doPlay();
189                }
190                this->fadeIn(this->ambientSounds_.front().first);
191            }
192        }
193        else
194        {
195            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
196            {
197                if (it->first == oldAmbient)
198                {
199                    this->fadeOut(oldAmbient);
200                    this->ambientSounds_.erase(it);
201                    break;
202                }
203            }
204        }
205    }
206
207    void SoundManager::pauseAmbientSound(AmbientSound* ambient)
208    {
209        if (ambient != NULL)
210        {
211            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
212            {
213                if (it->first == ambient)
214                {
215                    it->second = true;
216                    this->fadeOut(it->first);
217                    return;
218                }
219            }
220        }
221    }
222
223    void SoundManager::fadeIn(AmbientSound* sound)
224    {
225        // If we're already fading out --> remove that
226        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
227        {
228            if (*it == sound)
229            {
230                this->fadeOutList_.erase(it);
231                break;
232            }
233        }
234        // No duplicate entries
235        if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end())
236            this->fadeInList_.push_back(sound);
237    }
238
239    void SoundManager::fadeOut(AmbientSound* sound)
240    {
241        // If we're already fading in --> remove that
242        for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
243        {
244            if (*it == sound)
245            {
246                this->fadeInList_.erase(it);
247                break;
248            }
249        }
250        // No duplicate entries
251        if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end())
252            this->fadeOutList_.push_back(sound);
253    }
254
255    void SoundManager::processCrossFading(float dt)
256    {
257        // FADE IN
258        for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it)
259        {
260            if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f)
261            {
262                (*it)->setVolume(1.0f);
263                this->fadeInList_.erase(it++);
264            }
265            else
266            {
267                (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt);
268                ++it;
269            }
270        }
271
272        // FADE OUT
273        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it)
274        {
275            if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f)
276            {
277                (*it)->setVolume(0.0f);
278
279                // If sound is in the ambient list --> pause
280                for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2)
281                {
282                    if (it2->first == *it)
283                    {
284                        (*it)->doPause();
285                        break;
286                    }
287                }
288                // If not pause (by loop above for instance) --> stop
289                if (!(*it)->isPaused())
290                    (*it)->doStop();
291
292                this->fadeOutList_.erase(it++);
293            }
294            else
295            {
296                (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt);
297                ++it;
298            }
299        }
300    }
301}
Note: See TracBrowser for help on using the repository browser.