1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND |
---|
17 | |
---|
18 | #include "sound_source.h" |
---|
19 | #include "sound_engine.h" |
---|
20 | |
---|
21 | #include "alincl.h" |
---|
22 | #include "compiler.h" |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | /** |
---|
27 | * @brief creates a SoundSource at position sourceNode with the SoundBuffer buffer |
---|
28 | */ |
---|
29 | SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer) |
---|
30 | { |
---|
31 | this->setClassID(CL_SOUND_SOURCE, "SoundSource"); |
---|
32 | |
---|
33 | // adding the Source to the SourcesList of the SoundEngine |
---|
34 | this->buffer = buffer; |
---|
35 | this->sourceNode = sourceNode; |
---|
36 | this->resident = false; |
---|
37 | |
---|
38 | this->sourceID = 0; |
---|
39 | this->bPlay = false; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | /** |
---|
44 | * @brief construct a SoundSource out of the another soundSource |
---|
45 | * @param source the Source to create this source from. |
---|
46 | * |
---|
47 | * Copies the buffer from source to this Source. |
---|
48 | * Acquires a new SourceID if source is Playing. |
---|
49 | */ |
---|
50 | SoundSource::SoundSource(const SoundSource& source) |
---|
51 | { |
---|
52 | this->setClassID(CL_SOUND_SOURCE, "SoundSource"); |
---|
53 | |
---|
54 | // adding the Source to the SourcesList of the SoundEngine |
---|
55 | this->buffer = source.buffer; |
---|
56 | this->sourceNode = source.sourceNode; |
---|
57 | this->resident = source.resident; |
---|
58 | |
---|
59 | this->sourceID = 0; |
---|
60 | if (source.bPlay == true) |
---|
61 | { |
---|
62 | this->bPlay = true; |
---|
63 | SoundEngine::getInstance()->popALSource(this->sourceID); |
---|
64 | } |
---|
65 | else |
---|
66 | this->bPlay = false; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /** |
---|
71 | * @brief paste a copy of the source into this Source. |
---|
72 | * @param source the SoundSource to paste into this one. |
---|
73 | * @returns a Reference to this Source. |
---|
74 | * |
---|
75 | */ |
---|
76 | SoundSource& SoundSource::operator=(const SoundSource& source) |
---|
77 | { |
---|
78 | this->buffer = source.buffer; |
---|
79 | this->sourceNode = sourceNode; |
---|
80 | this->resident = source.resident; |
---|
81 | |
---|
82 | if (source.bPlay) |
---|
83 | this->play(); |
---|
84 | else |
---|
85 | this->stop(); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | /** |
---|
90 | * @brief compares two Sources with each other. |
---|
91 | * @param source the Source to compare against this One. |
---|
92 | * Two Sources are the same, if the PNodes match, and the Sound Played are the same. |
---|
93 | * The alSource must not match, because no two Sources can have the same alSource. |
---|
94 | */ |
---|
95 | bool SoundSource::operator==(const SoundSource& source) |
---|
96 | { |
---|
97 | return (this->buffer == source.buffer && |
---|
98 | this->bPlay == source.bPlay && |
---|
99 | this->sourceNode == source.sourceNode); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /** |
---|
104 | * @brief deletes a SoundSource |
---|
105 | */ |
---|
106 | SoundSource::~SoundSource() |
---|
107 | { |
---|
108 | this->stop(); |
---|
109 | if (this->sourceID != 0) |
---|
110 | SoundEngine::getInstance()->pushALSource(this->sourceID); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | /** |
---|
115 | * @brief Plays back a SoundSource |
---|
116 | */ |
---|
117 | void SoundSource::play() |
---|
118 | { |
---|
119 | if (this->buffer && this->retrieveSource()) |
---|
120 | { |
---|
121 | if (this->bPlay) |
---|
122 | alSourceStop(this->sourceID); |
---|
123 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
124 | alSourcei (this->sourceID, AL_LOOPING, AL_FALSE ); |
---|
125 | alSourcePlay(this->sourceID); |
---|
126 | |
---|
127 | if (DEBUG >= 3) |
---|
128 | SoundEngine::checkError("Play Source", __LINE__); |
---|
129 | this->bPlay = true; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | * @brief Plays back buffer on this Source |
---|
136 | * @param buffer the buffer to play back on this Source |
---|
137 | */ |
---|
138 | void SoundSource::play(const SoundBuffer* buffer) |
---|
139 | { |
---|
140 | if (!this->retrieveSource()) |
---|
141 | { |
---|
142 | PRINTF(2)("No more Free sources (You might consider raising the Source-Count).\n"); |
---|
143 | return; |
---|
144 | } |
---|
145 | |
---|
146 | alSourceStop(this->sourceID); |
---|
147 | alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); |
---|
148 | alSourcei (this->sourceID, AL_LOOPING, AL_FALSE ); |
---|
149 | alSourcePlay(this->sourceID); |
---|
150 | |
---|
151 | if (unlikely(this->buffer != NULL)) |
---|
152 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
153 | this->bPlay = true; |
---|
154 | |
---|
155 | if (DEBUG >= 3) |
---|
156 | SoundEngine::checkError("Play Source", __LINE__); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * @brief Plays and loops a SoundSource |
---|
162 | */ |
---|
163 | void SoundSource::loop() |
---|
164 | { |
---|
165 | if (this->buffer && this->retrieveSource()) |
---|
166 | { |
---|
167 | if (this->bPlay) |
---|
168 | alSourceStop(this->sourceID); |
---|
169 | |
---|
170 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
171 | alSourcei (this->sourceID, AL_LOOPING, AL_TRUE ); |
---|
172 | alSourcePlay(this->sourceID); |
---|
173 | |
---|
174 | if (DEBUG >= 3) |
---|
175 | SoundEngine::checkError("Play LoopSource", __LINE__); |
---|
176 | this->bPlay = true; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief Plays and loops buffer on this Source |
---|
183 | * @param buffer the buffer to play back on this Source |
---|
184 | */ |
---|
185 | void SoundSource::loop(const SoundBuffer* buffer) |
---|
186 | { |
---|
187 | if (!this->retrieveSource()) |
---|
188 | { |
---|
189 | PRINTF(2)("No more Free sources (You might consider raising the Source-Count).\n"); |
---|
190 | return; |
---|
191 | } |
---|
192 | |
---|
193 | alSourceStop(this->sourceID); |
---|
194 | alSourcei (this->sourceID, AL_BUFFER, buffer->getID()); |
---|
195 | alSourcei (this->sourceID, AL_LOOPING, AL_TRUE ); |
---|
196 | |
---|
197 | alSourcePlay(this->sourceID); |
---|
198 | |
---|
199 | if (unlikely(this->buffer != NULL)) |
---|
200 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
201 | this->bPlay = true; |
---|
202 | |
---|
203 | if (DEBUG >= 3) |
---|
204 | SoundEngine::checkError("Play LoopSource", __LINE__); |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | /** |
---|
209 | * @brief Stops playback of a SoundSource |
---|
210 | */ |
---|
211 | void SoundSource::stop() |
---|
212 | { |
---|
213 | this->bPlay = false; |
---|
214 | if (this->sourceID != 0) |
---|
215 | { |
---|
216 | alSourceStop(this->sourceID); |
---|
217 | if (DEBUG >= 3) |
---|
218 | SoundEngine::checkError("StopSource", __LINE__); |
---|
219 | alSourcei(this->sourceID, AL_BUFFER, 0); |
---|
220 | if (!this->resident) |
---|
221 | SoundEngine::getInstance()->pushALSource(this->sourceID); |
---|
222 | this->sourceID = 0; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | /** |
---|
228 | * @brief Pauses Playback of a SoundSource |
---|
229 | */ |
---|
230 | void SoundSource::pause() |
---|
231 | { |
---|
232 | alSourcePause(this->sourceID); |
---|
233 | if (DEBUG >= 3) |
---|
234 | SoundEngine::checkError("Pause Source", __LINE__); |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | /** |
---|
239 | * @brief Rewinds Playback of a SoundSource |
---|
240 | */ |
---|
241 | void SoundSource::rewind() |
---|
242 | { |
---|
243 | alSourceRewind(this->sourceID); |
---|
244 | |
---|
245 | if (DEBUG >= 3) |
---|
246 | SoundEngine::checkError("Rewind Source", __LINE__); |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | /** |
---|
251 | * @brief sets the RolloffFactor of the Sound emitted from the SoundSource |
---|
252 | * @param rolloffFactor The Factor described |
---|
253 | * |
---|
254 | * this tells openAL how fast the Sounds decay outward from the Source |
---|
255 | */ |
---|
256 | void SoundSource::setRolloffFactor(ALfloat rolloffFactor) |
---|
257 | { |
---|
258 | alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); |
---|
259 | |
---|
260 | if (DEBUG >= 3) |
---|
261 | SoundEngine::checkError("Set Source Rolloff-factor", __LINE__); |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | /** |
---|
266 | * @brief sets the Positional this Source should be attached to. |
---|
267 | * @param sourceNode the Source this is attached to. |
---|
268 | * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels. |
---|
269 | */ |
---|
270 | void SoundSource::setSourceNode(const PNode* sourceNode) |
---|
271 | { |
---|
272 | this->sourceNode = sourceNode; |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * @brief retrieve a Source. |
---|
277 | */ |
---|
278 | bool SoundSource::retrieveSource() |
---|
279 | { |
---|
280 | if (this->sourceID != 0) |
---|
281 | return true; |
---|
282 | else |
---|
283 | { |
---|
284 | SoundEngine::getInstance()->popALSource(this->sourceID); |
---|
285 | if (this->sourceID != 0) |
---|
286 | { |
---|
287 | if (unlikely(this->sourceNode == NULL)) |
---|
288 | resetSource(this->sourceID); |
---|
289 | return true; |
---|
290 | } |
---|
291 | } |
---|
292 | return false; |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | /** |
---|
297 | * @brief reset an alSource to its default Values. |
---|
298 | */ |
---|
299 | void SoundSource::resetSource(ALuint sourceID) |
---|
300 | { |
---|
301 | alSource3f(sourceID, AL_POSITION, 0.0, 0.0, 0.0); |
---|
302 | alSource3f(sourceID, AL_VELOCITY, 0.0, 0.0, 0.0); |
---|
303 | alSource3f(sourceID, AL_DIRECTION, 0.0, 0.0, 0.0); |
---|
304 | alSourcef (sourceID, AL_ROLLOFF_FACTOR, 0.0 ); |
---|
305 | //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE ); |
---|
306 | alSourcef (sourceID, AL_GAIN, SoundEngine::getInstance()->getEffectsVolume()); |
---|
307 | } |
---|