Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/sound/sound_source.cc @ 7646

Last change on this file since 7646 was 7646, checked in by amaechler, 18 years ago

branches/atmospheric_engine: added loop function in sound_source

File size: 6.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
24using namespace std;
25
26/**
27 * @brief creates a SoundSource at position sourceNode with the SoundBuffer buffer
28 */
29SoundSource::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 */
50SoundSource::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 */
76SoundSource& 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}
87
88
89/**
90 * @brief compares two Sources with each other.
91 * @param source the Source to compare against this One.
92 * Two Sources are the same, if the PNodes match, and the Sound Played are the same.
93 * The alSource must not match, because no two Sources can have the same alSource.
94 */
95bool SoundSource::operator==(const SoundSource& source)
96{
97  return (this->buffer == source.buffer &&
98          this->bPlay == source.bPlay &&
99          this->sourceNode == source.sourceNode);
100}
101
102
103/**
104 * @brief deletes a SoundSource
105 */
106SoundSource::~SoundSource()
107{
108  this->stop();
109  if (this->sourceID != 0)
110    SoundEngine::getInstance()->pushALSource(this->sourceID);
111}
112
113
114/**
115 * @brief Plays back a SoundSource
116 */
117void SoundSource::play()
118{
119  if (this->buffer && this->retrieveSource())
120  {
121    if (this->bPlay)
122      alSourceStop(this->sourceID);
123    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
124    alSourcePlay(this->sourceID);
125
126    if (DEBUG >= 3)
127      SoundEngine::checkError("Play Source", __LINE__);
128    this->bPlay = true;
129  }
130}
131
132
133/**
134 * @brief Plays back buffer on this Source
135 * @param buffer the buffer to play back on this Source
136 */
137void SoundSource::play(const SoundBuffer* buffer)
138{
139  if (!this->retrieveSource())
140  {
141    PRINTF(2)("No more Free sources (You might consider raising the Source-Count).\n");
142    return;
143  }
144
145  alSourceStop(this->sourceID);
146  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
147  alSourcePlay(this->sourceID);
148
149  if (unlikely(this->buffer != NULL))
150    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
151  this->bPlay = true;
152
153  if (DEBUG >= 3)
154    SoundEngine::checkError("Play Source", __LINE__);
155}
156
157
158/**
159 * @brief Plays back buffer on this Source and loops it
160 * @param buffer the buffer to play back on this Source
161 */
162void SoundSource::loop(const SoundBuffer* buffer)
163{
164  if (!this->retrieveSource())
165  {
166    PRINTF(2)("No more Free sources (You might consider raising the Source-Count).\n");
167    return;
168  }
169
170  alSourceStop(this->sourceID);
171  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
172  alSourcei (this->sourceID, AL_LOOPING,  AL_TRUE  );
173
174  alSourcePlay(this->sourceID);
175
176  if (unlikely(this->buffer != NULL))
177    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
178  this->bPlay = true;
179
180  if (DEBUG >= 3)
181    SoundEngine::checkError("Play LoopSource", __LINE__);
182}
183
184
185/**
186 * @brief Stops playback of a SoundSource
187 */
188void SoundSource::stop()
189{
190  this->bPlay = false;
191  if (this->sourceID != 0)
192  {
193    alSourceStop(this->sourceID);
194    if (DEBUG >= 3)
195      SoundEngine::checkError("StopSource", __LINE__);
196    alSourcei(this->sourceID, AL_BUFFER, 0);
197    if (!this->resident)
198      SoundEngine::getInstance()->pushALSource(this->sourceID);
199    this->sourceID = 0;
200  }
201}
202
203
204/**
205 * @brief Pauses Playback of a SoundSource
206 */
207void SoundSource::pause()
208{
209  alSourcePause(this->sourceID);
210  if (DEBUG >= 3)
211    SoundEngine::checkError("Pause Source", __LINE__);
212}
213
214
215/**
216 * @brief Rewinds Playback of a SoundSource
217 */
218void SoundSource::rewind()
219{
220  alSourceRewind(this->sourceID);
221
222  if (DEBUG >= 3)
223    SoundEngine::checkError("Rewind Source", __LINE__);
224}
225
226
227/**
228 * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
229 * @param rolloffFactor The Factor described
230 *
231 * this tells openAL how fast the Sounds decay outward from the Source
232 */
233void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
234{
235  alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
236
237  if (DEBUG >= 3)
238    SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
239}
240
241
242/**
243 * @brief sets the Positional this Source should be attached to.
244 * @param sourceNode the Source this is attached to.
245 * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.
246 */
247void SoundSource::setSourceNode(const PNode* sourceNode)
248{
249  this->sourceNode = sourceNode;
250}
251
252/**
253 * @brief retrieve a Source.
254 */
255bool SoundSource::retrieveSource()
256{
257  if (this->sourceID != 0)
258    return true;
259  else
260  {
261    SoundEngine::getInstance()->popALSource(this->sourceID);
262    if (this->sourceID != 0)
263    {
264      if (unlikely(this->sourceNode == NULL))
265        resetSource(this->sourceID);
266      return true;
267    }
268  }
269  return false;
270}
271
272
273/**
274 * @brief reset an alSource to its default Values.
275 */
276void SoundSource::resetSource(ALuint sourceID)
277{
278  alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0);
279  alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0);
280  alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0);
281  alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          );
282  //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      );
283  alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume());
284}
Note: See TracBrowser for help on using the repository browser.