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 | #include "p_node.h" |
---|
20 | #include "list.h" |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | \brief Creates a Soundbuffer out of an inputfile |
---|
27 | \param fileName The name of the File |
---|
28 | */ |
---|
29 | SoundBuffer::SoundBuffer(const char* fileName) |
---|
30 | { |
---|
31 | SoundEngine::getInstance()->addBuffer(this); |
---|
32 | |
---|
33 | ALenum format; |
---|
34 | ALvoid* data; |
---|
35 | ALsizei freq; |
---|
36 | |
---|
37 | alGenBuffers(1, &this->bufferID); |
---|
38 | alutLoadWAVFile((ALbyte*)fileName, &format, &data, &this->size, &freq, &this->loop); |
---|
39 | alBufferData(this->bufferID, format, data, this->size, freq); |
---|
40 | alutUnloadWAV(format, data, this->size, freq); |
---|
41 | } |
---|
42 | |
---|
43 | SoundBuffer::~SoundBuffer(void) |
---|
44 | { |
---|
45 | SoundEngine::getInstance()->removeBuffer(this); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | /** |
---|
51 | \brief creates a SoundSource at position sourceNode with the SoundBuffer buffer |
---|
52 | */ |
---|
53 | SoundSource::SoundSource(SoundBuffer* buffer, PNode* sourceNode) |
---|
54 | { |
---|
55 | SoundEngine::getInstance()->addSource(this); |
---|
56 | |
---|
57 | this->buffer = buffer; |
---|
58 | this->sourceNode = sourceNode; |
---|
59 | } |
---|
60 | |
---|
61 | SoundSource::~SoundSource(void) |
---|
62 | { |
---|
63 | SoundEngine::getInstance()->removeSource(this); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | \brief standard constructor |
---|
68 | */ |
---|
69 | SoundEngine::SoundEngine () |
---|
70 | { |
---|
71 | this->setClassName ("SoundEngine"); |
---|
72 | |
---|
73 | this->initAudio(); |
---|
74 | |
---|
75 | this->listener = NULL; |
---|
76 | this->bufferList = new tList<SoundBuffer>; |
---|
77 | this->sourceList = new tList<SoundSource>; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | \brief the singleton reference to this class |
---|
82 | */ |
---|
83 | SoundEngine* SoundEngine::singletonRef = NULL; |
---|
84 | |
---|
85 | /** |
---|
86 | \returns a Pointer to this Class |
---|
87 | */ |
---|
88 | SoundEngine* SoundEngine::getInstance(void) |
---|
89 | { |
---|
90 | if (!SoundEngine::singletonRef) |
---|
91 | SoundEngine::singletonRef = new SoundEngine(); |
---|
92 | return SoundEngine::singletonRef; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | \brief standard deconstructor |
---|
97 | |
---|
98 | */ |
---|
99 | SoundEngine::~SoundEngine () |
---|
100 | { |
---|
101 | SoundEngine::singletonRef = NULL; |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | \brief sets The listener (normaly the Camera) |
---|
107 | */ |
---|
108 | void SoundEngine::setListener(PNode* listener) |
---|
109 | { |
---|
110 | this->listener = listener; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | void SoundEngine::addBuffer(SoundBuffer* buffer) |
---|
115 | { |
---|
116 | this->bufferList->add(buffer); |
---|
117 | } |
---|
118 | |
---|
119 | void SoundEngine::removeBuffer(SoundBuffer* buffer) |
---|
120 | { |
---|
121 | this->bufferList->remove(buffer); |
---|
122 | } |
---|
123 | |
---|
124 | void SoundEngine::addSource(SoundSource* source) |
---|
125 | { |
---|
126 | this->sourceList->add(source); |
---|
127 | } |
---|
128 | void SoundEngine::removeSource(SoundSource* source) |
---|
129 | { |
---|
130 | this->sourceList->remove(source); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | \brief updates all The positions, Directions and Velocities of all Sounds |
---|
136 | */ |
---|
137 | void SoundEngine::update(void) |
---|
138 | { |
---|
139 | |
---|
140 | // updating the Listeners Position |
---|
141 | if (this->listener) |
---|
142 | { |
---|
143 | alListener3f(AL_POSITION, |
---|
144 | this->listener->getAbsCoor().x, |
---|
145 | this->listener->getAbsCoor().y, |
---|
146 | this->listener->getAbsCoor().z); |
---|
147 | alListener3f(AL_VELOCITY, |
---|
148 | this->listener->getVelocity().x, |
---|
149 | this->listener->getVelocity().y, |
---|
150 | this->listener->getVelocity().z); |
---|
151 | Vector absDirV = this->listener->getAbsDirV(); |
---|
152 | alListener3f(AL_ORIENTATION, |
---|
153 | absDirV.x, |
---|
154 | absDirV.y, |
---|
155 | absDirV.z); |
---|
156 | } |
---|
157 | // updating all the Sources positions |
---|
158 | tIterator<SoundSource>* iterator = this->sourceList->getIterator(); |
---|
159 | SoundSource* enumSource = iterator->nextElement(); |
---|
160 | while (enumSource) |
---|
161 | { |
---|
162 | alSource3f(enumSource->getID(), AL_POSITION, |
---|
163 | enumSource->getNode()->getAbsCoor().x, |
---|
164 | enumSource->getNode()->getAbsCoor().y, |
---|
165 | enumSource->getNode()->getAbsCoor().z); |
---|
166 | alSource3f(enumSource->getID(), AL_VELOCITY, |
---|
167 | enumSource->getNode()->getVelocity().x, |
---|
168 | enumSource->getNode()->getVelocity().y, |
---|
169 | enumSource->getNode()->getVelocity().z); |
---|
170 | |
---|
171 | enumSource = iterator->nextElement(); |
---|
172 | } |
---|
173 | delete iterator; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | \brief initializes Audio in general |
---|
178 | */ |
---|
179 | bool SoundEngine::initAudio(void) |
---|
180 | { |
---|
181 | alutInit(NULL, 0); |
---|
182 | alGetError(); |
---|
183 | } |
---|
184 | |
---|