Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8359 was 8350, checked in by bensch, 18 years ago

fixed out some warnings

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