Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6232 was 6232, checked in by rgrieder, 14 years ago

Fixed two problems in the sound system: Buffers did not get removed from the list in the SoundManager and failed creation was not caught.

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