Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/mountain_lake/src/lib/sound/sound_source.cc @ 8902

Last change on this file since 8902 was 8793, checked in by patrick, 18 years ago

trunk: merged the weather engine branche to the trunk

File size: 9.3 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  }
88
89
90  /**
91  * @brief compares two Sources with each other.
92  * @param source the Source to compare against this One.
93  * Two Sources are the same, if the PNodes match, and the Sound Played are the same.
94  * The alSource must not match, because no two Sources can have the same alSource.
95  */
96  bool SoundSource::operator==(const SoundSource& source)
97  {
98    return (this->buffer == source.buffer &&
99            this->bPlay == source.bPlay &&
100            this->sourceNode == source.sourceNode);
101  }
102
103
104  /**
105  * @brief deletes a SoundSource
106  */
107  SoundSource::~SoundSource()
108  {
109    this->stop();
110    if (this->sourceID != 0)
111      SoundEngine::getInstance()->pushALSource(this->sourceID);
112  }
113
114
115  /**
116  * @brief Plays back a SoundSource
117  */
118  void SoundSource::play()
119  {
120    if (this->buffer && this->retrieveSource())
121    {
122      if (this->bPlay)
123        alSourceStop(this->sourceID);
124
125      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
126      alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
127      alSourcef (this->sourceID, AL_GAIN, 1);
128      alSourcePlay(this->sourceID);
129
130      if (DEBUG_LEVEL >= 3)
131        SoundEngine::checkError("Play Source", __LINE__);
132      this->bPlay = true;
133    }
134  }
135
136
137  /**
138  * @brief Plays back buffer on this Source
139  * @param buffer the buffer to play back on this Source
140  */
141  void SoundSource::play(const SoundBuffer* buffer)
142  {
143    if (!this->retrieveSource())
144    {
145      PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
146      return;
147    }
148
149    alSourceStop(this->sourceID);
150    alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
151    alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
152    alSourcef (this->sourceID, AL_GAIN, 1);
153
154    alSourcePlay(this->sourceID);
155
156    if (unlikely(this->buffer != NULL))
157      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
158    this->bPlay = true;
159
160    if (DEBUG_LEVEL >= 3)
161      SoundEngine::checkError("Play Source", __LINE__);
162  }
163
164
165  /**
166   * @brief Plays back buffer on this Source with gain
167   * @param buffer the buffer to play back on this Source
168   * @param gain the gain of the sound buffer
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   * @brief Plays back buffer on this Source with gain and looping possibility
195   * @param buffer the buffer to play back on this Source
196   *  @param gain the gain of the sound buffer
197   * @param loop if true, sound gets looped
198   */
199  void SoundSource::play(const SoundBuffer* buffer, float gain, bool loop)
200  {
201    if (!this->retrieveSource())
202    {
203      PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
204      return;
205    }
206
207    alSourceStop(this->sourceID);
208    alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
209
210    if (loop)
211      alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
212    else
213      alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
214
215    alSourcef (this->sourceID, AL_GAIN, gain);
216
217    alSourcePlay(this->sourceID);
218
219    if (unlikely(this->buffer != NULL))
220      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
221    this->bPlay = true;
222
223    if (DEBUG_LEVEL >= 3)
224      SoundEngine::checkError("Play Source", __LINE__);
225  }
226
227  /**
228   * @brief Changes the volume of an (active) buffer
229   * @param buffer the buffer to play back on this Source
230   * @param gain the new gain value
231   */
232  void SoundSource::gain(const SoundBuffer* buffer, float gain)
233  {
234    // alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
235    alSourcef (this->sourceID, AL_GAIN, gain);
236  }
237
238  /**
239   * @brief Stops playback of a SoundSource
240   */
241  void SoundSource::stop()
242  {
243    this->bPlay = false;
244    if (this->sourceID != 0)
245    {
246      this->bPlay = false;
247      if (this->sourceID != 0)
248      {
249        alSourceStop(this->sourceID);
250        if (DEBUG_LEVEL >= 3)
251          SoundEngine::checkError("StopSource", __LINE__);
252        alSourcei(this->sourceID, AL_BUFFER, 0);
253        if (!this->resident)
254          SoundEngine::getInstance()->pushALSource(this->sourceID);
255        this->sourceID = 0;
256      }
257    }
258  }
259
260  /**
261  * @brief Pauses Playback of a SoundSource
262  */
263  void SoundSource::pause()
264  {
265    alSourcePause(this->sourceID);
266    if (DEBUG_LEVEL >= 3)
267      SoundEngine::checkError("Pause Source", __LINE__);
268  }
269
270
271  /**
272  * @brief Rewinds Playback of a SoundSource
273  */
274  void SoundSource::rewind()
275  {
276    alSourceRewind(this->sourceID);
277
278    if (DEBUG_LEVEL >= 3)
279      SoundEngine::checkError("Rewind Source", __LINE__);
280  }
281
282
283  /**
284  * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
285  * @param rolloffFactor The Factor described
286  *
287  * this tells openAL how fast the Sounds decay outward from the Source
288  */
289  void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
290  {
291    alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
292
293    if (DEBUG_LEVEL >= 3)
294      SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
295  }
296
297
298  /**
299  * @brief sets the Positional this Source should be attached to.
300  * @param sourceNode the Source this is attached to.
301  * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.
302  */
303  void SoundSource::setSourceNode(const PNode* sourceNode)
304  {
305    this->sourceNode = sourceNode;
306  }
307
308  /**
309  * @brief retrieve a Source.
310  */
311  bool SoundSource::retrieveSource()
312  {
313    if (this->sourceID != 0)
314      return true;
315    else
316    {
317      SoundEngine::getInstance()->popALSource(this->sourceID);
318      if (this->sourceID != 0)
319      {
320        if (unlikely(this->sourceNode == NULL))
321          resetSource(this->sourceID);
322        return true;
323      }
324    }
325    return false;
326  }
327
328
329  /**
330  * @brief reset an alSource to its default Values.
331  */
332  void SoundSource::resetSource(ALuint sourceID)
333  {
334    alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0);
335    alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0);
336    alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0);
337    alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          );
338    //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      );
339    alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume());
340  }
341
342
343  /**
344  * @brief Fades in a Source over a time period
345  * @param duration time perios to fade in
346  */
347  void SoundSource::fadein(const SoundBuffer* buffer, ALfloat duration)
348  {
349    //if (this->buffer && this->retrieveSource())
350    //{
351
352    for (ALfloat n = 0.0; n < 1.0; n+=.01)
353    {
354      alSourcef(this->sourceID, AL_GAIN, n);
355      // sleep(1);
356    }
357
358    //  alSourcePlay(this->sourceID);
359
360    //  if (DEBUG_LEVEL >= 3)
361    //    SoundEngine::checkError("Loop Source", __LINE__);
362    //  this->bPlay = true;
363    //}
364
365  }
366
367
368}
Note: See TracBrowser for help on using the repository browser.