Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8895 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

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