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 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | BaseSound::BaseSound() |
---|
45 | : bPooling_(false) |
---|
46 | , volume_(1.0) |
---|
47 | , bLooping_(false) |
---|
48 | , state_(Stopped) |
---|
49 | , pitch_ (1.0) |
---|
50 | { |
---|
51 | RegisterRootObject(BaseSound); |
---|
52 | |
---|
53 | // Initialise audioSource_ to a value that is not a source |
---|
54 | // 0 is unfortunately not guaranteed to be no source ID. |
---|
55 | this->audioSource_ = 123456789; |
---|
56 | while (alIsSource(++this->audioSource_)); |
---|
57 | } |
---|
58 | |
---|
59 | BaseSound::~BaseSound() |
---|
60 | { |
---|
61 | this->stop(); |
---|
62 | } |
---|
63 | |
---|
64 | void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode) |
---|
65 | { |
---|
66 | XMLPortParam(BaseSound, "volume", setVolume, getVolume, xmlelement, mode); |
---|
67 | XMLPortParam(BaseSound, "looping", setLooping, getLooping, xmlelement, mode); |
---|
68 | XMLPortParam(BaseSound, "pitch", setPitch, getPitch, xmlelement, mode); |
---|
69 | XMLPortParam(BaseSound, "source", setSource, getSource, xmlelement, mode); |
---|
70 | } |
---|
71 | |
---|
72 | void BaseSound::doPlay() |
---|
73 | { |
---|
74 | this->state_ = Playing; |
---|
75 | if (GameMode::playsSound() && this->getSourceState() != AL_PLAYING && this->soundBuffer_ != NULL) |
---|
76 | { |
---|
77 | if (!alIsSource(this->audioSource_)) |
---|
78 | { |
---|
79 | this->audioSource_ = SoundManager::getInstance().getSoundSource(); |
---|
80 | if (!alIsSource(this->audioSource_)) |
---|
81 | return; |
---|
82 | this->initialiseSource(); |
---|
83 | } |
---|
84 | |
---|
85 | alSourcePlay(this->audioSource_); |
---|
86 | if (int error = alGetError()) |
---|
87 | COUT(2) << "Sound: Error playing sound: " << SoundManager::getALErrorString(error) << std::endl; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | void BaseSound::doStop() |
---|
92 | { |
---|
93 | this->state_ = Stopped; |
---|
94 | if (alIsSource(this->audioSource_)) |
---|
95 | { |
---|
96 | alSourceStop(this->audioSource_); |
---|
97 | // Release buffer |
---|
98 | alSourcei(this->audioSource_, AL_BUFFER, AL_NONE); |
---|
99 | // Release source again |
---|
100 | SoundManager::getInstance().releaseSoundSource(this->audioSource_); |
---|
101 | // Get a no source ID |
---|
102 | this->audioSource_ += 123455; |
---|
103 | while (alIsSource(++this->audioSource_)); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | void BaseSound::doPause() |
---|
108 | { |
---|
109 | if (this->isStopped()) |
---|
110 | return; |
---|
111 | this->state_ = Paused; |
---|
112 | if (alIsSource(this->audioSource_)) |
---|
113 | alSourcePause(this->audioSource_); |
---|
114 | } |
---|
115 | |
---|
116 | ALint BaseSound::getSourceState() const |
---|
117 | { |
---|
118 | if (alIsSource(this->audioSource_)) |
---|
119 | { |
---|
120 | ALint state; |
---|
121 | alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state); |
---|
122 | return state; |
---|
123 | } |
---|
124 | else |
---|
125 | return AL_INITIAL; |
---|
126 | } |
---|
127 | |
---|
128 | void BaseSound::initialiseSource() |
---|
129 | { |
---|
130 | this->updateVolume(); |
---|
131 | this->setPitch(this->getPitch()); |
---|
132 | this->setLooping(this->getLooping()); |
---|
133 | alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); |
---|
134 | alSource3f(this->audioSource_, AL_VELOCITY, 0, 0, 0); |
---|
135 | alSource3f(this->audioSource_, AL_DIRECTION, 0, 0, 0); |
---|
136 | if (ALint error = alGetError()) |
---|
137 | COUT(2) << "Sound Warning: Setting source parameters to 0 failed: " |
---|
138 | << SoundManager::getALErrorString(error) << std::endl; |
---|
139 | assert(this->soundBuffer_ != NULL); |
---|
140 | alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer()); |
---|
141 | if (ALuint error = alGetError()) |
---|
142 | COUT(1) << "Sound Error: Could not set buffer \"" << this->source_ << "\": " << SoundManager::getALErrorString(error) << std::endl; |
---|
143 | } |
---|
144 | |
---|
145 | void BaseSound::setVolume(float vol) |
---|
146 | { |
---|
147 | this->volume_ = clamp(vol, 0.0f, 1.0f); |
---|
148 | if (this->volume_ != vol) |
---|
149 | COUT(2) << "Sound warning: volume out of range, clamping value." << std::endl; |
---|
150 | this->updateVolume(); |
---|
151 | } |
---|
152 | |
---|
153 | void BaseSound::updateVolume() |
---|
154 | { |
---|
155 | if (alIsSource(this->audioSource_)) |
---|
156 | { |
---|
157 | float volume = this->volume_ * this->getRealVolume(); |
---|
158 | alSourcef(this->audioSource_, AL_GAIN, volume); |
---|
159 | if (int error = alGetError()) |
---|
160 | COUT(2) << "Sound: Error setting volume to " << volume |
---|
161 | << ": " << SoundManager::getALErrorString(error) << std::endl; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | void BaseSound::setLooping(bool val) |
---|
166 | { |
---|
167 | this->bLooping_ = val; |
---|
168 | if (alIsSource(this->audioSource_)) |
---|
169 | alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE)); |
---|
170 | } |
---|
171 | |
---|
172 | void BaseSound::setPitch(float pitch) |
---|
173 | { |
---|
174 | if (pitch > 2 || pitch < 0.5) |
---|
175 | { |
---|
176 | COUT(2) << "Sound warning: pitch out of range, cropping value." << std::endl; |
---|
177 | pitch = pitch > 2 ? 2 : pitch; |
---|
178 | pitch = pitch < 0.5 ? 0.5 : pitch; |
---|
179 | } |
---|
180 | this->pitch_ = pitch; |
---|
181 | if (alIsSource(this->audioSource_)) |
---|
182 | { |
---|
183 | if (int error = alGetError()) |
---|
184 | COUT(2) << "Sound: Error setting pitch: " << SoundManager::getALErrorString(error) << std::endl; |
---|
185 | alSourcef(this->audioSource_, AL_PITCH, pitch); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | void BaseSound::setSource(const std::string& source) |
---|
190 | { |
---|
191 | if (!GameMode::playsSound()) |
---|
192 | { |
---|
193 | this->source_ = source; |
---|
194 | return; |
---|
195 | } |
---|
196 | |
---|
197 | if (this->soundBuffer_ != NULL) |
---|
198 | { |
---|
199 | if (this->soundBuffer_->getFilename() == source) |
---|
200 | { |
---|
201 | assert(this->source_ == source_); |
---|
202 | return; |
---|
203 | } |
---|
204 | // Stopping is imperative here! |
---|
205 | if (alIsSource(this->audioSource_)) |
---|
206 | { |
---|
207 | alSourceStop(this->audioSource_); |
---|
208 | alSourcei(this->audioSource_, AL_BUFFER, AL_NONE); |
---|
209 | } |
---|
210 | SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_); |
---|
211 | this->soundBuffer_.reset(); |
---|
212 | } |
---|
213 | |
---|
214 | this->source_ = source; |
---|
215 | // Don't load "" |
---|
216 | if (source_.empty()) |
---|
217 | return; |
---|
218 | |
---|
219 | // Get new sound buffer |
---|
220 | this->soundBuffer_ = SoundManager::getInstance().getSoundBuffer(this->source_); |
---|
221 | if (this->soundBuffer_ == NULL) |
---|
222 | return; |
---|
223 | |
---|
224 | if (alIsSource(this->audioSource_)) // already playing or paused |
---|
225 | { |
---|
226 | // Set new buffer |
---|
227 | alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer()); |
---|
228 | if (ALuint error = alGetError()) |
---|
229 | { |
---|
230 | COUT(1) << "Sound Error: Could not set buffer \"" << source << "\": " << SoundManager::getALErrorString(error) << std::endl; |
---|
231 | return; |
---|
232 | } |
---|
233 | |
---|
234 | // Sound was already playing or paused because there was a source acquired |
---|
235 | assert(this->isPlaying() || this->isPaused()); |
---|
236 | alSourcePlay(this->audioSource_); |
---|
237 | if (int error = alGetError()) |
---|
238 | COUT(2) << "Sound: Error playing sound: " << SoundManager::getALErrorString(error) << std::endl; |
---|
239 | if (this->isPaused()) |
---|
240 | alSourcePause(this->audioSource_); |
---|
241 | } |
---|
242 | else // No source acquired so far, but might be set to playing or paused |
---|
243 | { |
---|
244 | State state = this->state_; // save |
---|
245 | if (this->isPlaying() || this->isPaused()) |
---|
246 | doPlay(); |
---|
247 | if (state == Paused) |
---|
248 | { |
---|
249 | this->state_ = Paused; |
---|
250 | doPause(); |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | void BaseSound::stateChanged() |
---|
256 | { |
---|
257 | switch (this->state_) |
---|
258 | { |
---|
259 | case Playing: |
---|
260 | this->play(); |
---|
261 | break; |
---|
262 | case Paused: |
---|
263 | this->pause(); |
---|
264 | break; |
---|
265 | case Stopped: |
---|
266 | default: |
---|
267 | this->stop(); |
---|
268 | break; |
---|
269 | } |
---|
270 | } |
---|
271 | } |
---|