Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some nice Class-Stuff

File size: 4.6 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
37  this->sourceID = 0;
38  this->bPlay = false;
39}
40
41/**
42 * @brief construct a SoundSource out of the another soundSource
43 * @param source the Source to create this source from.
44 *
45 * Copies the buffer from source to this Source.
46 * Acquires a new SourceID if source is Playing.
47 */
48SoundSource::SoundSource(const SoundSource& source)
49{
50  this->setClassID(CL_SOUND_SOURCE, "SoundSource");
51
52  // adding the Source to the SourcesList of the SoundEngine
53  this->buffer = source.buffer;
54  this->sourceNode = source.sourceNode;
55
56  this->sourceID = 0;
57  if (source.bPlay == true)
58  {
59    this->bPlay = true;
60    SoundEngine::getInstance()->popALSource(this->sourceID);
61  }
62  else
63    this->bPlay = false;
64}
65
66/**
67 * @brief paste a copy of the source into this Source.
68 * @param source the SoundSource to paste into this one.
69 * @returns a Reference to this Source.
70 *
71 */
72SoundSource& SoundSource::operator=(const SoundSource& source)
73{
74  this->buffer = source.buffer;
75  this->sourceNode = sourceNode;
76
77  if (source.bPlay)
78    this->play();
79  else
80    this->stop();
81}
82
83/**
84 * @brief compares two Sources with each other.
85 * @param source the Source to compare against this One.
86 * Two Sources are the same, if the PNodes match, and the Sound Played are the same.
87 * The alSource must not match, because no two Sources can have the same alSource.
88 */
89bool SoundSource::operator==(const SoundSource& source)
90{
91  return (this->buffer == source.buffer &&
92          this->bPlay == source.bPlay &&
93          this->sourceNode == source.sourceNode);
94}
95
96/**
97 * @brief deletes a SoundSource
98 */
99SoundSource::~SoundSource()
100{
101  this->stop();
102}
103
104/**
105 * @brief Plays back a SoundSource
106 */
107void SoundSource::play()
108{
109  if (this->sourceID == 0)
110    SoundEngine::getInstance()->popALSource(this->sourceID);
111  alSourcePlay(this->sourceID);
112  if (DEBUG >= 3)
113    SoundEngine::checkError("Play Source", __LINE__);
114  this->bPlay = true;
115}
116
117/**
118 * Plays back buffer on this Source
119 * @param buffer the buffer to play back on this Source
120 */
121void SoundSource::play(const SoundBuffer* buffer)
122{
123  if (unlikely(this->sourceID == 0))
124  {
125    SoundEngine::getInstance()->popALSource(this->sourceID);
126    if (sourceID == 0)
127    {
128      PRINTF(2)("No more Free source\n");
129      return;
130    }
131  }
132  //  assert (this->sourceID != 0);
133
134  alSourceStop(this->sourceID);
135  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
136  alSourcePlay(this->sourceID);
137
138  if (unlikely(this->buffer != NULL))
139    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
140  this->bPlay = true;
141
142  if (DEBUG >= 3)
143    SoundEngine::checkError("Play Source", __LINE__);
144}
145
146/**
147 * @brief Stops playback of a SoundSource
148 */
149void SoundSource::stop()
150{
151  this->bPlay = false;
152  if (this->sourceID != 0)
153  {
154    alSourceStop(this->sourceID);
155    if (DEBUG >= 3)
156      SoundEngine::checkError("StopSource", __LINE__);
157    alSourcei(this->sourceID, AL_BUFFER, 0);
158    SoundEngine::getInstance()->pushALSource(this->sourceID);
159    this->sourceID = 0;
160  }
161}
162
163/**
164 * @brief Pauses Playback of a SoundSource
165 */
166void SoundSource::pause()
167{
168  alSourcePause(this->sourceID);
169  if (DEBUG >= 3)
170    SoundEngine::checkError("Pause Source", __LINE__);
171}
172
173/**
174 * @brief Rewinds Playback of a SoundSource
175 */
176void SoundSource::rewind()
177{
178  alSourceRewind(this->sourceID);
179
180  if (DEBUG >= 3)
181    SoundEngine::checkError("Rewind Source", __LINE__);
182}
183
184/**
185 * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
186 * @param rolloffFactor The Factor described
187 *
188 * this tells openAL how fast the Sounds decay outward from the Source
189 */
190void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
191{
192  alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
193
194  if (DEBUG >= 3)
195    SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
196}
197
Note: See TracBrowser for help on using the repository browser.