Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/sound/BaseSound.cc @ 7358

Last change on this file since 7358 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

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