Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6310 was 6307, checked in by scheusso, 15 years ago

Approach to make sounds synchronisable (not yet working)

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
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:
23 *      Erwin 'vaiursch' Herrsche
24 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
28
29#include "BaseSound.h"
30
31#include <cassert>
32#include <vector>
33#include <al.h>
34
35#include "core/CoreIncludes.h"
36#include "core/GameMode.h"
37#include "core/Resource.h"
38#include "core/XMLPort.h"
39#include "SoundBuffer.h"
40#include "SoundManager.h"
41
42namespace orxonox
43{
44    BaseSound::BaseSound()
45        : audioSource_(0)
46        , bPooling_(false)
47        , volume_(1.0)
48        , bLooping_(false)
49        , state_(Stopped)
50        , pitch_ (1.0)
51    {
52        RegisterRootObject(BaseSound);
53
54        if (GameMode::playsSound())
55        {
56            alGenSources(1, &this->audioSource_);
57            if (!alIsSource(this->audioSource_))
58                COUT(1) << "Sound: Source generation failed: " << SoundManager::getALErrorString(alGetError()) << std::endl;
59
60            if (alIsSource(this->audioSource_))
61            {
62                alSourcei(this->audioSource_, AL_REFERENCE_DISTANCE, 20);
63                alSourcei(this->audioSource_, AL_MAX_DISTANCE, 10000);
64            }
65        }
66    }
67
68    BaseSound::~BaseSound()
69    {
70        this->setSource(std::string());
71        if (GameMode::playsSound() && alIsSource(this->audioSource_))
72            alDeleteSources(1, &this->audioSource_);
73    }
74
75    void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode)
76    {
77        XMLPortParam(BaseSound, "volume", setVolume,  getVolume,  xmlelement, mode);
78        XMLPortParam(BaseSound, "loop",   setLooping, getLooping, xmlelement, mode);
79        XMLPortParam(BaseSound, "play",   setPlaying, isPlaying,  xmlelement, mode);
80        XMLPortParam(BaseSound, "source", setSource,  getSource,  xmlelement, mode);
81    }
82
83    void BaseSound::play()
84    {
85        this->state_ = Playing;
86        if (GameMode::playsSound() && alIsSource(this->audioSource_) && this->getSourceState() != AL_PLAYING)
87        {
88            alSourcePlay(this->audioSource_);
89
90            if (int error = alGetError())
91                COUT(2) << "Sound: Error playing sound: " << error << std::endl;
92        }
93    }
94
95    void BaseSound::stop()
96    {
97        this->state_ = Stopped;
98        if (GameMode::playsSound() && alIsSource(this->audioSource_))
99            alSourceStop(this->audioSource_);
100    }
101
102    void BaseSound::pause()
103    {
104        if (this->isStopped())
105            return;
106        this->state_ = Paused;
107        if (GameMode::playsSound() && alIsSource(this->audioSource_))
108            alSourcePause(this->audioSource_);
109    }
110
111    ALint BaseSound::getSourceState() const
112    {
113        if (GameMode::playsSound() && alIsSource(this->audioSource_))
114        {
115            ALint state;
116            alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state);
117            return state;
118        }
119        else
120            return AL_INITIAL;
121    }
122
123    void BaseSound::setVolume(float vol)
124    {
125        if (vol > 1 || vol < 0)
126        {
127            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
128            vol = vol > 1 ? 1 : vol;
129            vol = vol < 0 ? 0 : vol;
130        }
131        this->volume_ = vol;
132       
133        this->updateVolume();
134    }
135   
136    float BaseSound::getVolumeGain()
137    {
138        return SoundManager::getInstance().getVolume(SoundType::none);
139    }
140   
141    void BaseSound::updateVolume(void)
142    {
143        if (alIsSource(this->audioSource_))
144        {
145            alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain());
146            if (int error = alGetError())
147                COUT(2) << "Sound: Error setting volume: " << error << std::endl;
148        }
149    }
150
151    void BaseSound::setLooping(bool val)
152    {
153        this->bLooping_ = val;
154        if (GameMode::playsSound() && alIsSource(this->audioSource_))
155            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
156    }
157
158    void BaseSound::setPitch(float pitch)
159    {
160        if (pitch > 2 || pitch < 0.5)
161        {
162            COUT(2) << "Sound warning: pitch out of range, cropping value." << std::endl;
163            pitch = pitch > 2 ? 2 : pitch;
164            pitch = pitch < 0.5 ? 0.5 : pitch;
165        }       
166        this->pitch_ = pitch;
167        if (GameMode::playsSound() && alIsSource(this->audioSource_))
168        {
169            if (int error = alGetError())
170                COUT(2) << "Sound: Error setting pitch: " << error << std::endl;
171            alSourcef(this->audioSource_, AL_PITCH, pitch);
172        }
173    }
174
175    void BaseSound::setSource(const std::string& source)
176    {
177        if (!GameMode::playsSound() || source == this->source_) 
178        {
179            this->source_ = source;
180            return;
181        }
182
183        if (this->soundBuffer_ != NULL)
184        {
185            if (alIsSource(this->audioSource_))
186            {
187                alSourceStop(this->audioSource_);
188                // Unload old buffer first
189                alSourcei(this->audioSource_, AL_BUFFER, 0);
190            }
191            SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_);
192            this->soundBuffer_.reset();
193        }
194
195        this->source_ = source;
196        if (source_.empty() || !alIsSource(this->audioSource_))
197            return;
198
199        this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(this->source_);
200        if (this->soundBuffer_ == NULL)
201            return;
202
203        alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer());
204        if (ALuint error = alGetError())
205        {
206            COUT(1) << "Sound Error: Could not load file \"" << source << "\": " << SoundManager::getALErrorString(error) << std::endl;
207            return;
208        }
209
210        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
211        this->updateVolume();
212        this->setPitch(this->getPitch());
213        this->setLooping(getLooping());
214        if (this->isPlaying() || this->isPaused())
215        {
216            alSourcePlay(this->audioSource_);
217            if (int error = alGetError())
218                COUT(2) << "Sound: Error playing sound: " << error << std::endl;
219        }
220        if (this->isPaused())
221            alSourcePause(this->audioSource_);
222    }
223}
Note: See TracBrowser for help on using the repository browser.