Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ogg-files loadable (hopefully)

File size: 9.3 KB
RevLine 
[4744]1/*
[8495]2  orxonox - the future of 3D-vertical-scrollers
[8969]3
[8495]4  Copyright (C) 2004 orx
[8969]5
[8495]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.
[8969]10
[8495]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();
[8969]87
88    return *this;
[8350]89  }
[7299]90
[7317]91
[8350]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  }
[7299]104
[7317]105
[8350]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  }
[4320]115
[7317]116
[8350]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);
[7318]126
[8350]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);
[4320]131
[8350]132      if (DEBUG_LEVEL >= 3)
133        SoundEngine::checkError("Play Source", __LINE__);
134      this->bPlay = true;
135    }
136  }
[7317]137
[5930]138
[8350]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    }
[5386]150
[8350]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);
[7290]155
[8350]156    alSourcePlay(this->sourceID);
[1853]157
[8350]158    if (unlikely(this->buffer != NULL))
159      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
160    this->bPlay = true;
[7317]161
[8350]162    if (DEBUG_LEVEL >= 3)
163      SoundEngine::checkError("Play Source", __LINE__);
164  }
[7810]165
166
[8350]167  /**
[8793]168   * @brief Plays back buffer on this Source with gain
169   * @param buffer the buffer to play back on this Source
[8969]170   * @param gain the gain of the sound buffer
[8350]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    }
[7810]179
[8350]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);
[7810]184
[8350]185    alSourcePlay(this->sourceID);
[1853]186
[8350]187    if (unlikely(this->buffer != NULL))
188      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
189    this->bPlay = true;
[5386]190
[8350]191    if (DEBUG_LEVEL >= 3)
192      SoundEngine::checkError("Play Source", __LINE__);
193  }
[8793]194
195  /**
[8495]196   * @brief Plays back buffer on this Source with gain and looping possibility
197   * @param buffer the buffer to play back on this Source
[8793]198   *  @param gain the gain of the sound buffer
199   * @param loop if true, sound gets looped
200   */
[8495]201  void SoundSource::play(const SoundBuffer* buffer, float gain, bool loop)
[8350]202  {
[8495]203    if (!this->retrieveSource())
[8350]204    {
[8495]205      PRINTF(3)("No more Free sources (You might consider raising the Source-Count).\n");
206      return;
[8350]207    }
[7290]208
[8495]209    alSourceStop(this->sourceID);
210    alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
[8793]211
[8495]212    if (loop)
[8793]213      alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
[8495]214    else
[8793]215      alSourcei (this->sourceID, AL_LOOPING,  AL_FALSE);
216
[8495]217    alSourcef (this->sourceID, AL_GAIN, gain);
[5386]218
[8495]219    alSourcePlay(this->sourceID);
[7317]220
[8495]221    if (unlikely(this->buffer != NULL))
[8350]222      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
[8495]223    this->bPlay = true;
[8255]224
[8495]225    if (DEBUG_LEVEL >= 3)
226      SoundEngine::checkError("Play Source", __LINE__);
[8793]227  }
[8255]228
[8793]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  }
[8255]239
[8350]240  /**
[8793]241   * @brief Stops playback of a SoundSource
242   */
[8350]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  }
[8255]261
[8350]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  }
[8255]271
272
[8350]273  /**
274  * @brief Rewinds Playback of a SoundSource
275  */
276  void SoundSource::rewind()
277  {
278    alSourceRewind(this->sourceID);
[8255]279
[8350]280    if (DEBUG_LEVEL >= 3)
281      SoundEngine::checkError("Rewind Source", __LINE__);
282  }
[8255]283
284
[8350]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);
[8255]294
[8350]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
[8495]360    //  alSourcePlay(this->sourceID);
[8350]361
[8495]362    //  if (DEBUG_LEVEL >= 3)
363    //    SoundEngine::checkError("Loop Source", __LINE__);
364    //  this->bPlay = true;
[8350]365    //}
366
367  }
368
369
[7317]370}
Note: See TracBrowser for help on using the repository browser.