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
RevLine 
[4744]1/*
[8255]2        orxonox - the future of 3D-vertical-scrollers
[1853]3
[8255]4        Copyright (C) 2004 orx
[1853]5
[8255]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.
[1855]10
[8255]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"
[1853]23
[7460]24namespace OrxSound
[5386]25{
[8350]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");
[1856]32
[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  {
52    this->setClassID(CL_SOUND_SOURCE, "SoundSource");
[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();
86    return *this;
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  /**
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    }
[7810]176
[8350]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);
[7810]181
[8350]182    alSourcePlay(this->sourceID);
[1853]183
[8350]184    if (unlikely(this->buffer != NULL))
185      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
186    this->bPlay = true;
[5386]187
[8350]188    if (DEBUG_LEVEL >= 3)
189      SoundEngine::checkError("Play Source", __LINE__);
190  }
[7317]191
[7290]192
[8350]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);
[5386]202
[8350]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);
[7317]207
[8350]208      if (DEBUG_LEVEL >= 3)
209        SoundEngine::checkError("Play Source", __LINE__);
210      this->bPlay = true;
211    }
212  }
[7290]213
[8350]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);
[5386]224
[8350]225      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
226      alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
227      alSourcef (this->sourceID, AL_GAIN, 1);
[7317]228
[8350]229      alSourcePlay(this->sourceID);
[7317]230
[8350]231      if (DEBUG_LEVEL >= 3)
232        SoundEngine::checkError("Loop Source", __LINE__);
233      this->bPlay = true;
234    }
235  }
[7317]236
237
[8350]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);
[8255]248
[8350]249      alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
250      alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE);
251      alSourcef (this->sourceID, AL_GAIN, gain);
[8255]252
[8350]253      alSourcePlay(this->sourceID);
[8255]254
[8350]255      if (DEBUG_LEVEL >= 3)
256        SoundEngine::checkError("Loop Source", __LINE__);
257      this->bPlay = true;
258    }
259  }
[8255]260
261
[8350]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  }
[8255]283
[8350]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  }
[8255]293
294
[8350]295  /**
296  * @brief Rewinds Playback of a SoundSource
297  */
298  void SoundSource::rewind()
299  {
300    alSourceRewind(this->sourceID);
[8255]301
[8350]302    if (DEBUG_LEVEL >= 3)
303      SoundEngine::checkError("Rewind Source", __LINE__);
304  }
[8255]305
306
[8350]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);
[8255]316
[8350]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
[7317]392}
Note: See TracBrowser for help on using the repository browser.