Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/blink/src/lib/sound/sound_source.cc @ 10605

Last change on this file since 10605 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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