Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/sound/sound_source.cc @ 8657

Last change on this file since 8657 was 8362, checked in by bensch, 18 years ago

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

File size: 9.7 KB
Line 
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#include "debug.h"
24
25namespace OrxSound
26{
27  /**
28  * @brief creates a SoundSource at position sourceNode with the SoundBuffer buffer
29  */
30  SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer)
31  {
32    this->setClassID(CL_SOUND_SOURCE, "SoundSource");
33
34    // adding the Source to the SourcesList of the SoundEngine
35    this->buffer = buffer;
36    this->sourceNode = sourceNode;
37    this->resident = false;
38
39    this->sourceID = 0;
40    this->bPlay = false;
41  }
42
43
44  /**
45  * @brief construct a SoundSource out of the another soundSource
46  * @param source the Source to create this source from.
47  *
48  * Copies the buffer from source to this Source.
49  * Acquires a new SourceID if source is Playing.
50  */
51  SoundSource::SoundSource(const SoundSource& source)
52  {
53    this->setClassID(CL_SOUND_SOURCE, "SoundSource");
54
55    // adding the Source to the SourcesList of the SoundEngine
56    this->buffer = source.buffer;
57    this->sourceNode = source.sourceNode;
58    this->resident = source.resident;
59
60    this->sourceID = 0;
61    if (source.bPlay == true)
62    {
63      this->bPlay = true;
64      SoundEngine::getInstance()->popALSource(this->sourceID);
65    }
66    else
67      this->bPlay = false;
68  }
69
70
71  /**
72  * @brief paste a copy of the source into this Source.
73  * @param source the SoundSource to paste into this one.
74  * @returns a Reference to this Source.
75  *
76  */
77  SoundSource& SoundSource::operator=(const SoundSource& source)
78  {
79    this->buffer = source.buffer;
80    this->sourceNode = sourceNode;
81    this->resident = source.resident;
82
83    if (source.bPlay)
84      this->play();
85    else
86      this->stop();
87    return *this;
88  }
89
90
91  /**
92  * @brief compares two Sources with each other.
93  * @param source the Source to compare against this One.
94  * Two Sources are the same, if the PNodes match, and the Sound Played are the same.
95  * The alSource must not match, because no two Sources can have the same alSource.
96  */
97  bool SoundSource::operator==(const SoundSource& source)
98  {
99    return (this->buffer == source.buffer &&
100            this->bPlay == source.bPlay &&
101            this->sourceNode == source.sourceNode);
102  }
103
104
105  /**
106  * @brief deletes a SoundSource
107  */
108  SoundSource::~SoundSource()
109  {
110    this->stop();
111    if (this->sourceID != 0)
112      SoundEngine::getInstance()->pushALSource(this->sourceID);
113  }
114
115
116  /**
117  * @brief Plays back a SoundSource
118  */
119  void SoundSource::play()
120  {
121    if (this->buffer && this->retrieveSource())
122    {
123      if (this->bPlay)
124        alSourceStop(this->sourceID);
125
126      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
127      alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
128      alSourcef (this->sourceID, AL_GAIN, 1);
129      alSourcePlay(this->sourceID);
130
131      if (DEBUG_LEVEL >= 3)
132        SoundEngine::checkError("Play Source", __LINE__);
133      this->bPlay = true;
134    }
135  }
136
137
138  /**
139  * @brief Plays back buffer on this Source
140  * @param buffer the buffer to play back on this Source
141  */
142  void SoundSource::play(const SoundBuffer* buffer)
143  {
144    if (!this->retrieveSource())
145    {
146      PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
147      return;
148    }
149
150    alSourceStop(this->sourceID);
151    alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
152    alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
153    alSourcef (this->sourceID, AL_GAIN, 1);
154
155    alSourcePlay(this->sourceID);
156
157    if (unlikely(this->buffer != NULL))
158      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
159    this->bPlay = true;
160
161    if (DEBUG_LEVEL >= 3)
162      SoundEngine::checkError("Play Source", __LINE__);
163  }
164
165
166  /**
167  * @brief Plays back buffer on this Source with gain
168  * @param buffer the buffer to play back on this Source
169  */
170  void SoundSource::play(const SoundBuffer* buffer, float gain)
171  {
172    if (!this->retrieveSource())
173    {
174      PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
175      return;
176    }
177
178    alSourceStop(this->sourceID);
179    alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
180    alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
181    alSourcef (this->sourceID, AL_GAIN, gain);
182
183    alSourcePlay(this->sourceID);
184
185    if (unlikely(this->buffer != NULL))
186      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
187    this->bPlay = true;
188
189    if (DEBUG_LEVEL >= 3)
190      SoundEngine::checkError("Play Source", __LINE__);
191  }
192
193
194  /**
195  * @brief Plays and loops back a SoundSource
196  */
197  void SoundSource::loop()
198  {
199    if (this->buffer && this->retrieveSource())
200    {
201      if (this->bPlay)
202        alSourceStop(this->sourceID);
203
204      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
205      alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
206      alSourcef (this->sourceID, AL_GAIN, 1);
207      alSourcePlay(this->sourceID);
208
209      if (DEBUG_LEVEL >= 3)
210        SoundEngine::checkError("Play Source", __LINE__);
211      this->bPlay = true;
212    }
213  }
214
215  /**
216  * @brief Plays and loops buffer on this Source
217  * @param buffer the buffer to play back on this Source
218  */
219  void SoundSource::loop(const SoundBuffer* buffer)
220  {
221    if (this->buffer && this->retrieveSource())
222    {
223      if (this->bPlay)
224        alSourceStop(this->sourceID);
225
226      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
227      alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
228      alSourcef (this->sourceID, AL_GAIN, 1);
229
230      alSourcePlay(this->sourceID);
231
232      if (DEBUG_LEVEL >= 3)
233        SoundEngine::checkError("Loop Source", __LINE__);
234      this->bPlay = true;
235    }
236  }
237
238
239  /**
240  * @brief Plays and loops buffer on this Source with gain control
241  * @param buffer the buffer to play back on this Source
242  */
243  void SoundSource::loop(const SoundBuffer* buffer, float gain)
244  {
245    if (this->buffer && this->retrieveSource())
246    {
247      if (this->bPlay)
248        alSourceStop(this->sourceID);
249
250      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
251      alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
252      alSourcef (this->sourceID, AL_GAIN, gain);
253
254      alSourcePlay(this->sourceID);
255
256      if (DEBUG_LEVEL >= 3)
257        SoundEngine::checkError("Loop Source", __LINE__);
258      this->bPlay = true;
259    }
260  }
261
262
263  /**
264  * @brief Stops playback of a SoundSource
265  */
266  void SoundSource::stop()
267  {
268    this->bPlay = false;
269    if (this->sourceID != 0)
270    {
271      this->bPlay = false;
272      if (this->sourceID != 0)
273      {
274        alSourceStop(this->sourceID);
275        if (DEBUG_LEVEL >= 3)
276          SoundEngine::checkError("StopSource", __LINE__);
277        alSourcei(this->sourceID, AL_BUFFER, 0);
278        if (!this->resident)
279          SoundEngine::getInstance()->pushALSource(this->sourceID);
280        this->sourceID = 0;
281      }
282    }
283  }
284
285  /**
286  * @brief Pauses Playback of a SoundSource
287  */
288  void SoundSource::pause()
289  {
290    alSourcePause(this->sourceID);
291    if (DEBUG_LEVEL >= 3)
292      SoundEngine::checkError("Pause Source", __LINE__);
293  }
294
295
296  /**
297  * @brief Rewinds Playback of a SoundSource
298  */
299  void SoundSource::rewind()
300  {
301    alSourceRewind(this->sourceID);
302
303    if (DEBUG_LEVEL >= 3)
304      SoundEngine::checkError("Rewind Source", __LINE__);
305  }
306
307
308  /**
309  * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
310  * @param rolloffFactor The Factor described
311  *
312  * this tells openAL how fast the Sounds decay outward from the Source
313  */
314  void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
315  {
316    alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
317
318    if (DEBUG_LEVEL >= 3)
319      SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
320  }
321
322
323  /**
324  * @brief sets the Positional this Source should be attached to.
325  * @param sourceNode the Source this is attached to.
326  * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.
327  */
328  void SoundSource::setSourceNode(const PNode* sourceNode)
329  {
330    this->sourceNode = sourceNode;
331  }
332
333  /**
334  * @brief retrieve a Source.
335  */
336  bool SoundSource::retrieveSource()
337  {
338    if (this->sourceID != 0)
339      return true;
340    else
341    {
342      SoundEngine::getInstance()->popALSource(this->sourceID);
343      if (this->sourceID != 0)
344      {
345        if (unlikely(this->sourceNode == NULL))
346          resetSource(this->sourceID);
347        return true;
348      }
349    }
350    return false;
351  }
352
353
354  /**
355  * @brief reset an alSource to its default Values.
356  */
357  void SoundSource::resetSource(ALuint sourceID)
358  {
359    alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0);
360    alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0);
361    alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0);
362    alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          );
363    //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      );
364    alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume());
365  }
366
367
368  /**
369  * @brief Fades in a Source over a time period
370  * @param duration time perios to fade in
371  */
372  void SoundSource::fadein(const SoundBuffer* buffer, ALfloat duration)
373  {
374    //if (this->buffer && this->retrieveSource())
375    //{
376
377    for (ALfloat n = 0.0; n < 1.0; n+=.01)
378    {
379      alSourcef(this->sourceID, AL_GAIN, n);
380      // sleep(1);
381    }
382
383    //  alSourcePlay(this->sourceID);
384
385    //  if (DEBUG_LEVEL >= 3)
386    //          SoundEngine::checkError("Loop Source", __LINE__);
387    //  this->bPlay = true;
388    //}
389
390  }
391
392
393}
Note: See TracBrowser for help on using the repository browser.