Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6222 was 6222, checked in by youngk, 16 years ago

Cleaned up the code a little

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