Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: retrieve Source is better now (more modular)

File size: 5.8 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->retrieveSource())
120  {
121    alSourcePlay(this->sourceID);
122    if (DEBUG >= 3)
123      SoundEngine::checkError("Play Source", __LINE__);
124    this->bPlay = true;
125  }
126}
127
128
129/**
130 * @brief Plays back buffer on this Source
131 * @param buffer the buffer to play back on this Source
132 */
133void SoundSource::play(const SoundBuffer* buffer)
134{
135  if (!this->retrieveSource())
136  {
137    PRINTF(2)("No more Free sources (You might consider raising the Source-Count).\n");
138    return;
139  }
140
141  alSourceStop(this->sourceID);
142  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
143  alSourcePlay(this->sourceID);
144
145  if (unlikely(this->buffer != NULL))
146    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
147  this->bPlay = true;
148
149  if (DEBUG >= 3)
150    SoundEngine::checkError("Play Source", __LINE__);
151}
152
153
154/**
155 * @brief Stops playback of a SoundSource
156 */
157void SoundSource::stop()
158{
159  this->bPlay = false;
160  if (this->sourceID != 0)
161  {
162    alSourceStop(this->sourceID);
163    if (DEBUG >= 3)
164      SoundEngine::checkError("StopSource", __LINE__);
165    alSourcei(this->sourceID, AL_BUFFER, 0);
166    if (!this->resident)
167      SoundEngine::getInstance()->pushALSource(this->sourceID);
168    this->sourceID = 0;
169  }
170}
171
172
173/**
174 * @brief Pauses Playback of a SoundSource
175 */
176void SoundSource::pause()
177{
178  alSourcePause(this->sourceID);
179  if (DEBUG >= 3)
180    SoundEngine::checkError("Pause Source", __LINE__);
181}
182
183
184/**
185 * @brief Rewinds Playback of a SoundSource
186 */
187void SoundSource::rewind()
188{
189  alSourceRewind(this->sourceID);
190
191  if (DEBUG >= 3)
192    SoundEngine::checkError("Rewind Source", __LINE__);
193}
194
195
196/**
197 * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
198 * @param rolloffFactor The Factor described
199 *
200 * this tells openAL how fast the Sounds decay outward from the Source
201 */
202void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
203{
204  alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
205
206  if (DEBUG >= 3)
207    SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
208}
209
210
211/**
212 * @brief sets the Positional this Source should be attached to.
213 * @param sourceNode the Source this is attached to.
214 * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.
215 */
216void SoundSource::setSourceNode(const PNode* sourceNode)
217{
218  this->sourceNode = sourceNode;
219}
220
221/**
222 * @brief retrieve a Source.
223 */
224bool SoundSource::retrieveSource()
225{
226  if (this->sourceID != 0)
227    return true;
228  else
229  {
230    SoundEngine::getInstance()->popALSource(this->sourceID);
231    if (this->sourceID != 0)
232    {
233      if (unlikely(this->sourceNode == NULL))
234        resetSource(this->sourceID);
235      return true;
236    }
237  }
238  return false;
239}
240
241
242/**
243 * @brief reset an alSource to its default Values.
244 */
245void SoundSource::resetSource(ALuint sourceID)
246{
247  alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0);
248  alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0);
249  alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0);
250  alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          );
251  alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      );
252  alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume());
253}
Note: See TracBrowser for help on using the repository browser.