Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6256 was 6255, checked in by rgrieder, 16 years ago

Temporarily resolved problem of sounds only playing once.

  • Property svn:eol-style set to native
File size: 6.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
[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()
[5899]45        : audioSource_(0)
[6254]46        , bPooling_(false)
[6203]47        , volume_(1.0)
[5896]48        , bLoop_(false)
[6117]49        , state_(Stopped)
[6222]50        , pitch_ (1.0)
[3060]51    {
[5896]52        RegisterRootObject(BaseSound);
[6117]53
54        if (GameMode::playsSound())
55        {
56            alGenSources(1, &this->audioSource_);
57            assert(this->audioSource_ != 0);
58        }
[3060]59    }
60
[5896]61    BaseSound::~BaseSound()
[3060]62    {
[5899]63        this->setSource("");
[6117]64        if (GameMode::playsSound())
65            alDeleteSources(1, &this->audioSource_);
[3060]66    }
67
[6117]68    void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode)
69    {
70        XMLPortParam(BaseSound, "volume", setVolume,  getVolume,  xmlelement, mode);
71        XMLPortParam(BaseSound, "loop",   setLooping, getLooping, xmlelement, mode);
[6188]72        XMLPortParam(BaseSound, "play",   setPlaying, isPlaying,  xmlelement, mode);
[6117]73        XMLPortParam(BaseSound, "source", setSource,  getSource,  xmlelement, mode);
74    }
75
[5896]76    void BaseSound::play()
[3060]77    {
[6255]78        this->state_ = Playing;
79        if (GameMode::playsSound() && this->getSourceState() != AL_PLAYING)
[5896]80        {
[5899]81            alSourcePlay(this->audioSource_);
[3108]82
[6203]83            if (int error = alGetError())
84                COUT(2) << "Sound: Error playing sound: " << error << std::endl;
[3060]85        }
86    }
87
[5896]88    void BaseSound::stop()
89    {
[6117]90        this->state_ = Stopped;
91        if (GameMode::playsSound())
[5899]92            alSourceStop(this->audioSource_);
[3060]93    }
94
[5896]95    void BaseSound::pause()
96    {
[6117]97        if (this->isStopped())
98            return;
99        this->state_ = Paused;
100        if (GameMode::playsSound())
[5899]101            alSourcePause(this->audioSource_);
[3060]102    }
103
[6255]104    ALint BaseSound::getSourceState() const
105    {
106        if (GameMode::playsSound())
107        {
108            ALint state;
109            alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state);
110            return state;
111        }
112        else
113            return AL_INITIAL;
114    }
115
[6117]116    void BaseSound::setVolume(float vol)
[5896]117    {
[6117]118        if (vol > 1 || vol < 0)
119        {
120            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
121            vol = vol > 1 ? 1 : vol;
122            vol = vol < 0 ? 0 : vol;
123        }
124        this->volume_ = vol;
[6186]125       
126        this->updateVolume();
[3060]127    }
[6184]128   
[6186]129    float BaseSound::getVolumeGain()
[6184]130    {
[6186]131        return SoundManager::getInstance().getVolume(SoundType::none);
[6184]132    }
133   
[6186]134    void BaseSound::updateVolume(void)
[6184]135    {
[6186]136        if (alIsSource(this->audioSource_))
[6204]137        {
[6186]138            alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain());
[6204]139            if (int error = alGetError())
140                COUT(2) << "Sound: Error setting volume: " << error << std::endl;
141        }
[6184]142    }
[3060]143
[6117]144    void BaseSound::setLooping(bool val)
[5896]145    {
[6117]146        this->bLoop_ = val;
147        if (GameMode::playsSound())
148            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
[3060]149    }
150
[6202]151    void BaseSound::setPitch(float pitch)
152    {
153        if (pitch > 2 || pitch < 0.5)
154        {
155            COUT(2) << "Sound warning: pitch out of range, cropping value." << std::endl;
156            pitch = pitch > 2 ? 2 : pitch;
157            pitch = pitch < 0.5 ? 0.5 : pitch;
158        }       
159        this->pitch_ = pitch;
160        if (GameMode::playsSound())
[6204]161        {
162            if (int error = alGetError())
163                COUT(2) << "Sound: Error setting pitch: " << error << std::endl;
[6205]164            alSourcef(this->audioSource_, AL_PITCH, pitch);
[6204]165        }
[6202]166    }
167
[5899]168    void BaseSound::setSource(const std::string& source)
[5896]169    {
[6117]170        if (!GameMode::playsSound() || source == this->source_) 
171        {
172            this->source_ = source;
[5896]173            return;
[6117]174        }
[5896]175
[6203]176        if (this->soundBuffer_ != NULL)
[3060]177        {
[6117]178            alSourceStop(this->audioSource_);
179            // Unload old sound first
[5899]180            alSourcei(this->audioSource_, AL_BUFFER, 0);
[6254]181            SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_);
[6203]182            this->soundBuffer_.reset();
[3060]183        }
184
[6117]185        this->source_ = source;
[6232]186        if (source_.empty())
[6117]187            return;
188
[5695]189        // Get DataStream from the resources
[5899]190        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source);
[5896]191        if (fileInfo == NULL)
192        {
[6117]193            COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl;
[5896]194            return;
[5695]195        }
196
[6203]197        this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(fileInfo);
198        if (this->soundBuffer_ == NULL)
199            return;
[5695]200
[6203]201        alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer());
[5896]202        if (alGetError() != AL_NO_ERROR)
203        {
[5899]204            COUT(2) << "Sound: OpenAL: Error loading sample file: " << source << std::endl;
[5896]205            return;
[3060]206        }
[5896]207
[5899]208        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
[6186]209        this->updateVolume();
[6202]210        this->setPitch(this->getPitch());
[6204]211        this->setLooping(getLooping());
[6117]212        if (this->isPlaying() || this->isPaused())
[6234]213        {
214            alSourcePlay(this->audioSource_);
215            if (int error = alGetError())
216                COUT(2) << "Sound: Error playing sound: " << error << std::endl;
217        }
[6117]218        if (this->isPaused())
[6234]219            alSourcePause(this->audioSource_);
[3060]220    }
[6117]221}
Note: See TracBrowser for help on using the repository browser.