Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10497 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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