Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/sound/sound_source.cc @ 8710

Last change on this file since 8710 was 8495, checked in by bensch, 19 years ago

merged the branche atmos back. no conflicts

File size: 8.9 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  */
169  void SoundSource::play(const SoundBuffer* buffer, float gain)
170  {
171    if (!this->retrieveSource())
172    {
173      PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
174      return;
175    }
176
177    alSourceStop(this->sourceID);
178    alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
179    alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
180    alSourcef (this->sourceID, AL_GAIN, gain);
181
182    alSourcePlay(this->sourceID);
183
184    if (unlikely(this->buffer != NULL))
185      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
186    this->bPlay = true;
187
188    if (DEBUG_LEVEL >= 3)
189      SoundEngine::checkError("Play Source", __LINE__);
190  }
191 
192   /**
193   * @brief Plays back buffer on this Source with gain and looping possibility
194   * @param buffer the buffer to play back on this Source
195    */
196  void SoundSource::play(const SoundBuffer* buffer, float gain, bool loop)
197  {
198    if (!this->retrieveSource())
199    {
200      PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
201      return;
202    }
203
204    alSourceStop(this->sourceID);
205    alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
206   
207    if (loop)
208        alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
209    else
210        alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
211   
212    alSourcef (this->sourceID, AL_GAIN, gain);
213
214    alSourcePlay(this->sourceID);
215
216    if (unlikely(this->buffer != NULL))
217      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
218    this->bPlay = true;
219
220    if (DEBUG_LEVEL >= 3)
221      SoundEngine::checkError("Play Source", __LINE__);
222  } 
223
224
225  /**
226  * @brief Stops playback of a SoundSource
227  */
228  void SoundSource::stop()
229  {
230    this->bPlay = false;
231    if (this->sourceID != 0)
232    {
233      this->bPlay = false;
234      if (this->sourceID != 0)
235      {
236        alSourceStop(this->sourceID);
237        if (DEBUG_LEVEL >= 3)
238          SoundEngine::checkError("StopSource", __LINE__);
239        alSourcei(this->sourceID, AL_BUFFER, 0);
240        if (!this->resident)
241          SoundEngine::getInstance()->pushALSource(this->sourceID);
242        this->sourceID = 0;
243      }
244    }
245  }
246
247  /**
248  * @brief Pauses Playback of a SoundSource
249  */
250  void SoundSource::pause()
251  {
252    alSourcePause(this->sourceID);
253    if (DEBUG_LEVEL >= 3)
254      SoundEngine::checkError("Pause Source", __LINE__);
255  }
256
257
258  /**
259  * @brief Rewinds Playback of a SoundSource
260  */
261  void SoundSource::rewind()
262  {
263    alSourceRewind(this->sourceID);
264
265    if (DEBUG_LEVEL >= 3)
266      SoundEngine::checkError("Rewind Source", __LINE__);
267  }
268
269
270  /**
271  * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
272  * @param rolloffFactor The Factor described
273  *
274  * this tells openAL how fast the Sounds decay outward from the Source
275  */
276  void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
277  {
278    alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
279
280    if (DEBUG_LEVEL >= 3)
281      SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
282  }
283
284
285  /**
286  * @brief sets the Positional this Source should be attached to.
287  * @param sourceNode the Source this is attached to.
288  * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.
289  */
290  void SoundSource::setSourceNode(const PNode* sourceNode)
291  {
292    this->sourceNode = sourceNode;
293  }
294
295  /**
296  * @brief retrieve a Source.
297  */
298  bool SoundSource::retrieveSource()
299  {
300    if (this->sourceID != 0)
301      return true;
302    else
303    {
304      SoundEngine::getInstance()->popALSource(this->sourceID);
305      if (this->sourceID != 0)
306      {
307        if (unlikely(this->sourceNode == NULL))
308          resetSource(this->sourceID);
309        return true;
310      }
311    }
312    return false;
313  }
314
315
316  /**
317  * @brief reset an alSource to its default Values.
318  */
319  void SoundSource::resetSource(ALuint sourceID)
320  {
321    alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0);
322    alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0);
323    alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0);
324    alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          );
325    //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      );
326    alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume());
327  }
328
329
330  /**
331  * @brief Fades in a Source over a time period
332  * @param duration time perios to fade in
333  */
334  void SoundSource::fadein(const SoundBuffer* buffer, ALfloat duration)
335  {
336    //if (this->buffer && this->retrieveSource())
337    //{
338
339    for (ALfloat n = 0.0; n < 1.0; n+=.01)
340    {
341      alSourcef(this->sourceID, AL_GAIN, n);
342      // sleep(1);
343    }
344
345    //  alSourcePlay(this->sourceID);
346
347    //  if (DEBUG_LEVEL >= 3)
348    //    SoundEngine::checkError("Loop Source", __LINE__);
349    //  this->bPlay = true;
350    //}
351
352  }
353
354
355}
Note: See TracBrowser for help on using the repository browser.