Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/sound/sound_source.cc @ 9351

Last change on this file since 9351 was 8969, checked in by bensch, 18 years ago

orxonox/trunk: ogg-files loadable (hopefully)

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