Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/sound5/src/orxonox/sound/BaseSound.cc @ 10255

Last change on this file since 10255 was 6726, checked in by erwin, 15 years ago

Some more atempts to fix maybe it's platform specific…

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