Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/orxonox/sound/BaseSound.cc @ 10181

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

Reverted last commit (forgot to tick "Switch working copy to new branch"…)

  • Property svn:eol-style set to native
File size: 9.1 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 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
28
29#include "BaseSound.h"
30
31#include <cassert>
32#include <vector>
33#include <al.h>
34
35#include "util/Math.h"
36#include "core/CoreIncludes.h"
37#include "core/GameMode.h"
38#include "core/Resource.h"
39#include "core/XMLPort.h"
40#include "SoundBuffer.h"
41#include "SoundManager.h"
42
43namespace orxonox
44{
45    BaseSound::BaseSound()
46        : bPooling_(false)
47        , volume_(1.0)
48        , bLooping_(false)
49        , state_(Stopped)
50        , pitch_ (1.0)
51    {
52        RegisterRootObject(BaseSound);
53
54        // Initialise audioSource_ to a value that is not a source
55        // 0 is unfortunately not guaranteed to be no source ID.
56        this->audioSource_ = 123456789;
57        while (alIsSource(++this->audioSource_));
58    }
59
60    BaseSound::~BaseSound()
61    {
62        this->stop();
63        // Release buffer
64        if (this->soundBuffer_ != NULL)
65        {
66            assert(GameMode::playsSound());
67            SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_);
68        }
69    }
70
71    void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode)
72    {
73        XMLPortParam(BaseSound, "volume",  setVolume,  getVolume,  xmlelement, mode);
74        XMLPortParam(BaseSound, "looping", setLooping, getLooping, xmlelement, mode);
75        XMLPortParam(BaseSound, "pitch",   setPitch,   getPitch,   xmlelement, mode);
76        XMLPortParam(BaseSound, "source",  setSource,  getSource,  xmlelement, mode);
77    }
78
79    void BaseSound::doPlay()
80    {
81        this->state_ = Playing;
82        if (GameMode::playsSound() && this->getSourceState() != AL_PLAYING && this->soundBuffer_ != NULL)
83        {
84            if (!alIsSource(this->audioSource_))
85            {
86                this->audioSource_ = SoundManager::getInstance().getSoundSource(this);
87                if (!alIsSource(this->audioSource_))
88                    return;
89                this->initialiseSource();
90            }
91
92            alSourcePlay(this->audioSource_);
93            if (int error = alGetError())
94                COUT(2) << "Sound: Error playing sound: " << SoundManager::getALErrorString(error) << std::endl;
95        }
96    }
97
98    bool BaseSound::doStop()
99    {
100        this->state_ = Stopped;
101        if (alIsSource(this->audioSource_))
102        {
103            alSourceStop(this->audioSource_);
104            // Release buffer
105            alSourcei(this->audioSource_, AL_BUFFER, AL_NONE);
106            // Release source again
107            SoundManager::getInstance().releaseSoundSource(this->audioSource_);
108            // Get a no source ID
109            this->audioSource_ += 123455;
110            while (alIsSource(++this->audioSource_));
111
112            return true; // sound source destroyed - return true
113        }
114        return false; // nothing done - return false
115    }
116
117    void BaseSound::doPause()
118    {
119        if (this->isStopped())
120            return;
121        this->state_ = Paused;
122        if (alIsSource(this->audioSource_))
123            alSourcePause(this->audioSource_);
124    }
125
126    ALint BaseSound::getSourceState() const
127    {
128        if (alIsSource(this->audioSource_))
129        {
130            ALint state;
131            alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state);
132            return state;
133        }
134        else
135            return AL_INITIAL;
136    }
137
138    void BaseSound::initialiseSource()
139    {
140        this->updateVolume();
141        this->setPitch(this->getPitch());
142        this->setLooping(this->getLooping());
143        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
144        alSource3f(this->audioSource_, AL_VELOCITY,  0, 0, 0);
145        alSource3f(this->audioSource_, AL_DIRECTION, 0, 0, 0);
146        if (ALint error = alGetError())
147            COUT(2) << "Sound Warning: Setting source parameters to 0 failed: "
148                    << SoundManager::getALErrorString(error) << std::endl;
149        assert(this->soundBuffer_ != NULL);
150        alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer());
151        if (ALuint error = alGetError())
152            COUT(1) << "Sound Error: Could not set buffer \"" << this->source_ << "\": " << SoundManager::getALErrorString(error) << std::endl;
153    }
154
155    void BaseSound::setVolume(float vol)
156    {
157        this->volume_ = clamp(vol, 0.0f, 1.0f);
158        if (this->volume_ != vol)
159            COUT(2) << "Sound warning: volume out of range, clamping value." << std::endl;
160        this->updateVolume();
161    }
162
163    void BaseSound::updateVolume()
164    {
165        if (alIsSource(this->audioSource_))
166        {
167            float volume = this->volume_ * this->getRealVolume();
168            alSourcef(this->audioSource_, AL_GAIN, volume);
169            if (int error = alGetError())
170                COUT(2) << "Sound: Error setting volume to " << volume
171                        << ": " << SoundManager::getALErrorString(error) << std::endl;
172        }
173    }
174
175    void BaseSound::setLooping(bool val)
176    {
177        this->bLooping_ = val;
178        if (alIsSource(this->audioSource_))
179            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
180    }
181
182    void BaseSound::setPitch(float pitch)
183    {
184        if (pitch > 2 || pitch < 0.5f)
185        {
186            COUT(2) << "Sound warning: pitch out of range, cropping value." << std::endl;
187            pitch = pitch > 2.0f ? 2.0f : pitch;
188            pitch = pitch < 0.5f ? 0.5f : pitch;
189        }
190        this->pitch_ = pitch;
191        if (alIsSource(this->audioSource_))
192        {
193            alSourcef(this->audioSource_, AL_PITCH, pitch);
194            if (int error = alGetError())
195                COUT(2) << "Sound: Error setting pitch: " << SoundManager::getALErrorString(error) << std::endl;
196        }
197    }
198
199    void BaseSound::setSource(const std::string& source)
200    {
201        if (!GameMode::playsSound())
202        {
203            this->source_ = source;
204            return;
205        }
206
207        if (this->soundBuffer_ != NULL)
208        {
209            if (this->soundBuffer_->getFilename() == source)
210            {
211                assert(this->source_ == source_);
212                return;
213            }
214            // Stopping is imperative here!
215            if (alIsSource(this->audioSource_))
216            {
217                alSourceStop(this->audioSource_);
218                alSourcei(this->audioSource_, AL_BUFFER, AL_NONE);
219            }
220            SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_);
221            this->soundBuffer_.reset();
222        }
223
224        this->source_ = source;
225        // Don't load ""
226        if (source_.empty())
227            return;
228
229        // Get new sound buffer
230        this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(this->source_);
231        if (this->soundBuffer_ == NULL)
232            return;
233
234        if (alIsSource(this->audioSource_)) // already playing or paused
235        {
236            // Set new buffer
237            alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer());
238            if (ALuint error = alGetError())
239            {
240                COUT(1) << "Sound Error: Could not set buffer \"" << source << "\": " << SoundManager::getALErrorString(error) << std::endl;
241                return;
242            }
243
244            // Sound was already playing or paused because there was a source acquired
245            assert(this->isPlaying() || this->isPaused());
246            alSourcePlay(this->audioSource_);
247            if (int error = alGetError())
248                COUT(2) << "Sound: Error playing sound: " << SoundManager::getALErrorString(error) << std::endl;
249            if (this->isPaused())
250                alSourcePause(this->audioSource_);
251        }
252        else // No source acquired so far, but might be set to playing or paused
253        {
254            State state = static_cast<State>(this->state_); // save
255            if (this->isPlaying() || this->isPaused())
256                doPlay();
257            if (state == Paused)
258            {
259                this->state_ = Paused;
260                doPause();
261            }
262        }
263    }
264
265    void BaseSound::stateChanged()
266    {
267        switch (this->state_)
268        {
269            case Playing:
270                this->play();
271                break;
272            case Paused:
273                this->pause();
274                break;
275            case Stopped:
276            default:
277                this->stop();
278                break;
279        }
280    }
281}
Note: See TracBrowser for help on using the repository browser.