Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added buffer buffering: Sounds are only loaded into one buffer and then reused.

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