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