Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/sound/BaseSound.cc @ 6371

Last change on this file since 6371 was 6370, checked in by rgrieder, 15 years ago

Little cleanup in sound stuff and MoodManager.
Caution: Renamed SoundTypes to {All, Music, Effects}!

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