Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8807 was 8793, checked in by patrick, 18 years ago

trunk: merged the weather engine branche to the trunk

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