Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/sound/sound_source.cc @ 8480

Last change on this file since 8480 was 8480, checked in by amaechler, 18 years ago

sound fixed

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