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_WORLD_ENTITY |
---|
17 | |
---|
18 | #include "sound_engine.h" |
---|
19 | |
---|
20 | #include "p_node.h" |
---|
21 | #include "list.h" |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | |
---|
26 | ////////////////// |
---|
27 | /* SOUND-BUFFER */ |
---|
28 | ////////////////// |
---|
29 | /** |
---|
30 | \brief Creates a Soundbuffer out of an inputfile |
---|
31 | \param fileName The name of the File |
---|
32 | */ |
---|
33 | SoundBuffer::SoundBuffer(const char* fileName) |
---|
34 | { |
---|
35 | SoundEngine::getInstance()->addBuffer(this); |
---|
36 | |
---|
37 | ALenum format; |
---|
38 | ALvoid* data; |
---|
39 | ALsizei freq; |
---|
40 | |
---|
41 | alGenBuffers(1, &this->bufferID); |
---|
42 | alutLoadWAVFile((ALbyte*)fileName, &format, &data, &this->size, &freq, &this->loop); |
---|
43 | alBufferData(this->bufferID, format, data, this->size, freq); |
---|
44 | alutUnloadWAV(format, data, this->size, freq); |
---|
45 | } |
---|
46 | |
---|
47 | SoundBuffer::~SoundBuffer(void) |
---|
48 | { |
---|
49 | SoundEngine::getInstance()->removeBuffer(this); |
---|
50 | alDeleteBuffers(1, &this->bufferID); |
---|
51 | } |
---|
52 | |
---|
53 | ////////////////// |
---|
54 | /* SOUND-SOURCE */ |
---|
55 | ////////////////// |
---|
56 | /** |
---|
57 | \brief creates a SoundSource at position sourceNode with the SoundBuffer buffer |
---|
58 | */ |
---|
59 | SoundSource::SoundSource(SoundBuffer* buffer, PNode* sourceNode) |
---|
60 | { |
---|
61 | SoundEngine::getInstance()->addSource(this); |
---|
62 | |
---|
63 | this->buffer = buffer; |
---|
64 | this->sourceNode = sourceNode; |
---|
65 | |
---|
66 | alGenSources(1, &this->sourceID); |
---|
67 | |
---|
68 | if(alGetError() != AL_NO_ERROR) |
---|
69 | PRINTF(1)("error initializing SoundSource\n"); //return AL_FALSE; |
---|
70 | |
---|
71 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
72 | alSourcef (this->sourceID, AL_PITCH, 1.0 ); |
---|
73 | alSourcef (this->sourceID, AL_GAIN, 1.0 ); |
---|
74 | // alSourcefv(sourceID, AL_POSITION, SourcePos); |
---|
75 | // alSourcefv(sourceID, AL_VELOCITY, SourceVel); |
---|
76 | alSourcei (sourceID, AL_LOOPING, true ); |
---|
77 | } |
---|
78 | |
---|
79 | SoundSource::~SoundSource(void) |
---|
80 | { |
---|
81 | SoundEngine::getInstance()->removeSource(this); |
---|
82 | alDeleteSources(1, &this->sourceID); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | void SoundSource::play() |
---|
87 | { |
---|
88 | alSourcePlay(this->sourceID); |
---|
89 | } |
---|
90 | void SoundSource::stop() |
---|
91 | { |
---|
92 | alSourceStop(this->sourceID); |
---|
93 | } |
---|
94 | void SoundSource::pause() |
---|
95 | { |
---|
96 | alSourcePause(this->sourceID); |
---|
97 | } |
---|
98 | |
---|
99 | void SoundSource::setRolloffFactor(ALfloat rolloffFactor) |
---|
100 | { |
---|
101 | alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | ////////////////// |
---|
107 | /* SOUND-ENGINE */ |
---|
108 | ////////////////// |
---|
109 | /** |
---|
110 | \brief standard constructor |
---|
111 | */ |
---|
112 | SoundEngine::SoundEngine () |
---|
113 | { |
---|
114 | this->setClassName ("SoundEngine"); |
---|
115 | |
---|
116 | this->initAudio(); |
---|
117 | |
---|
118 | this->listener = NULL; |
---|
119 | this->bufferList = new tList<SoundBuffer>; |
---|
120 | this->sourceList = new tList<SoundSource>; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | \brief the singleton reference to this class |
---|
125 | */ |
---|
126 | SoundEngine* SoundEngine::singletonRef = NULL; |
---|
127 | |
---|
128 | /** |
---|
129 | \returns a Pointer to this Class |
---|
130 | */ |
---|
131 | SoundEngine* SoundEngine::getInstance(void) |
---|
132 | { |
---|
133 | if (!SoundEngine::singletonRef) |
---|
134 | SoundEngine::singletonRef = new SoundEngine(); |
---|
135 | return SoundEngine::singletonRef; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | \brief standard deconstructor |
---|
140 | |
---|
141 | */ |
---|
142 | SoundEngine::~SoundEngine () |
---|
143 | { |
---|
144 | SoundEngine::singletonRef = NULL; |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | \brief sets The listener (normaly the Camera) |
---|
150 | */ |
---|
151 | void SoundEngine::setListener(PNode* listener) |
---|
152 | { |
---|
153 | this->listener = listener; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | void SoundEngine::addBuffer(SoundBuffer* buffer) |
---|
158 | { |
---|
159 | this->bufferList->add(buffer); |
---|
160 | } |
---|
161 | |
---|
162 | void SoundEngine::removeBuffer(SoundBuffer* buffer) |
---|
163 | { |
---|
164 | this->bufferList->remove(buffer); |
---|
165 | } |
---|
166 | |
---|
167 | void SoundEngine::addSource(SoundSource* source) |
---|
168 | { |
---|
169 | this->sourceList->add(source); |
---|
170 | } |
---|
171 | void SoundEngine::removeSource(SoundSource* source) |
---|
172 | { |
---|
173 | this->sourceList->remove(source); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | /** |
---|
178 | \brief updates all The positions, Directions and Velocities of all Sounds |
---|
179 | */ |
---|
180 | void SoundEngine::update(void) |
---|
181 | { |
---|
182 | |
---|
183 | // updating the Listeners Position |
---|
184 | if (this->listener) |
---|
185 | { |
---|
186 | alListener3f(AL_POSITION, |
---|
187 | this->listener->getAbsCoor().x, |
---|
188 | this->listener->getAbsCoor().y, |
---|
189 | this->listener->getAbsCoor().z); |
---|
190 | alListener3f(AL_VELOCITY, |
---|
191 | this->listener->getVelocity().x, |
---|
192 | this->listener->getVelocity().y, |
---|
193 | this->listener->getVelocity().z); |
---|
194 | Vector absDirV = this->listener->getAbsDirV(); |
---|
195 | ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z}; |
---|
196 | alListenerfv(AL_ORIENTATION, orientation); |
---|
197 | } |
---|
198 | else |
---|
199 | PRINTF(2)("no listener defined\n"); |
---|
200 | |
---|
201 | // updating all the Sources positions |
---|
202 | tIterator<SoundSource>* iterator = this->sourceList->getIterator(); |
---|
203 | SoundSource* enumSource = iterator->nextElement(); |
---|
204 | while (enumSource) |
---|
205 | { |
---|
206 | alSource3f(enumSource->getID(), AL_POSITION, |
---|
207 | enumSource->getNode()->getAbsCoor().x, |
---|
208 | enumSource->getNode()->getAbsCoor().y, |
---|
209 | enumSource->getNode()->getAbsCoor().z); |
---|
210 | alSource3f(enumSource->getID(), AL_VELOCITY, |
---|
211 | enumSource->getNode()->getVelocity().x, |
---|
212 | enumSource->getNode()->getVelocity().y, |
---|
213 | enumSource->getNode()->getVelocity().z); |
---|
214 | |
---|
215 | enumSource = iterator->nextElement(); |
---|
216 | } |
---|
217 | delete iterator; |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | \brief initializes Audio in general |
---|
222 | */ |
---|
223 | bool SoundEngine::initAudio(void) |
---|
224 | { |
---|
225 | alutInit(NULL, 0); |
---|
226 | alGetError(); |
---|
227 | |
---|
228 | alDopplerFactor(SOUND_DOPPLER_FACTOR); |
---|
229 | alDopplerVelocity(SOUND_DOPPLER_VELOCITY); |
---|
230 | } |
---|
231 | |
---|