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 | } |
---|
51 | |
---|
52 | ////////////////// |
---|
53 | /* SOUND-SOURCE */ |
---|
54 | ////////////////// |
---|
55 | /** |
---|
56 | \brief creates a SoundSource at position sourceNode with the SoundBuffer buffer |
---|
57 | */ |
---|
58 | SoundSource::SoundSource(SoundBuffer* buffer, PNode* sourceNode) |
---|
59 | { |
---|
60 | SoundEngine::getInstance()->addSource(this); |
---|
61 | |
---|
62 | this->buffer = buffer; |
---|
63 | this->sourceNode = sourceNode; |
---|
64 | |
---|
65 | alGenSources(1, &this->sourceID); |
---|
66 | |
---|
67 | if(alGetError() != AL_NO_ERROR) |
---|
68 | PRINTF(1)("error initializing SoundSource\n"); //return AL_FALSE; |
---|
69 | |
---|
70 | alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID()); |
---|
71 | alSourcef (this->sourceID, AL_PITCH, 1.0 ); |
---|
72 | alSourcef (this->sourceID, AL_GAIN, 1.0 ); |
---|
73 | // alSourcefv(sourceID, AL_POSITION, SourcePos); |
---|
74 | // alSourcefv(sourceID, AL_VELOCITY, SourceVel); |
---|
75 | alSourcei (sourceID, AL_LOOPING, true ); |
---|
76 | |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | SoundSource::~SoundSource(void) |
---|
81 | { |
---|
82 | SoundEngine::getInstance()->removeSource(this); |
---|
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 | |
---|
100 | ////////////////// |
---|
101 | /* SOUND-ENGINE */ |
---|
102 | ////////////////// |
---|
103 | /** |
---|
104 | \brief standard constructor |
---|
105 | */ |
---|
106 | SoundEngine::SoundEngine () |
---|
107 | { |
---|
108 | this->setClassName ("SoundEngine"); |
---|
109 | |
---|
110 | this->initAudio(); |
---|
111 | |
---|
112 | this->listener = NULL; |
---|
113 | this->bufferList = new tList<SoundBuffer>; |
---|
114 | this->sourceList = new tList<SoundSource>; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | \brief the singleton reference to this class |
---|
119 | */ |
---|
120 | SoundEngine* SoundEngine::singletonRef = NULL; |
---|
121 | |
---|
122 | /** |
---|
123 | \returns a Pointer to this Class |
---|
124 | */ |
---|
125 | SoundEngine* SoundEngine::getInstance(void) |
---|
126 | { |
---|
127 | if (!SoundEngine::singletonRef) |
---|
128 | SoundEngine::singletonRef = new SoundEngine(); |
---|
129 | return SoundEngine::singletonRef; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | \brief standard deconstructor |
---|
134 | |
---|
135 | */ |
---|
136 | SoundEngine::~SoundEngine () |
---|
137 | { |
---|
138 | SoundEngine::singletonRef = NULL; |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | \brief sets The listener (normaly the Camera) |
---|
144 | */ |
---|
145 | void SoundEngine::setListener(PNode* listener) |
---|
146 | { |
---|
147 | this->listener = listener; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | void SoundEngine::addBuffer(SoundBuffer* buffer) |
---|
152 | { |
---|
153 | this->bufferList->add(buffer); |
---|
154 | } |
---|
155 | |
---|
156 | void SoundEngine::removeBuffer(SoundBuffer* buffer) |
---|
157 | { |
---|
158 | this->bufferList->remove(buffer); |
---|
159 | } |
---|
160 | |
---|
161 | void SoundEngine::addSource(SoundSource* source) |
---|
162 | { |
---|
163 | this->sourceList->add(source); |
---|
164 | } |
---|
165 | void SoundEngine::removeSource(SoundSource* source) |
---|
166 | { |
---|
167 | this->sourceList->remove(source); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | /** |
---|
172 | \brief updates all The positions, Directions and Velocities of all Sounds |
---|
173 | */ |
---|
174 | void SoundEngine::update(void) |
---|
175 | { |
---|
176 | |
---|
177 | // updating the Listeners Position |
---|
178 | if (this->listener) |
---|
179 | { |
---|
180 | alListener3f(AL_POSITION, |
---|
181 | this->listener->getAbsCoor().x, |
---|
182 | this->listener->getAbsCoor().y, |
---|
183 | this->listener->getAbsCoor().z); |
---|
184 | alListener3f(AL_VELOCITY, |
---|
185 | this->listener->getVelocity().x, |
---|
186 | this->listener->getVelocity().y, |
---|
187 | this->listener->getVelocity().z); |
---|
188 | Vector absDirV = this->listener->getAbsDirV(); |
---|
189 | alListener3f(AL_ORIENTATION, |
---|
190 | absDirV.x, |
---|
191 | absDirV.y, |
---|
192 | absDirV.z); |
---|
193 | } |
---|
194 | else |
---|
195 | PRINTF(2)("no listener defined\n"); |
---|
196 | |
---|
197 | // updating all the Sources positions |
---|
198 | tIterator<SoundSource>* iterator = this->sourceList->getIterator(); |
---|
199 | SoundSource* enumSource = iterator->nextElement(); |
---|
200 | while (enumSource) |
---|
201 | { |
---|
202 | alSource3f(enumSource->getID(), AL_POSITION, |
---|
203 | enumSource->getNode()->getAbsCoor().x, |
---|
204 | enumSource->getNode()->getAbsCoor().y, |
---|
205 | enumSource->getNode()->getAbsCoor().z); |
---|
206 | alSource3f(enumSource->getID(), AL_VELOCITY, |
---|
207 | enumSource->getNode()->getVelocity().x, |
---|
208 | enumSource->getNode()->getVelocity().y, |
---|
209 | enumSource->getNode()->getVelocity().z); |
---|
210 | |
---|
211 | enumSource = iterator->nextElement(); |
---|
212 | } |
---|
213 | delete iterator; |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | \brief initializes Audio in general |
---|
218 | */ |
---|
219 | bool SoundEngine::initAudio(void) |
---|
220 | { |
---|
221 | alutInit(NULL, 0); |
---|
222 | alGetError(); |
---|
223 | } |
---|
224 | |
---|