Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/sound/sound_engine.cc @ 5473

Last change on this file since 5473 was 5473, checked in by bensch, 19 years ago

orxonox/trunk: sound-engine finishes faster.

File size: 10.1 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   code has been taken from http://www.devmaster.net/articles.php?catID=6
16   The code has been applied to our needs, and many things have been changed.
17*/
18
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND
20
21#include "sound_engine.h"
22
23//#include <AL/alc.h> // maybe later
24#include "class_list.h"
25
26#include "p_node.h"
27#include "list.h"
28#include "resource_manager.h"
29#include "debug.h"
30#include "ini_parser.h"
31#include "globals.h"
32
33using namespace std;
34
35
36//////////////////
37/* SOUND-ENGINE */
38//////////////////
39/**
40 *  standard constructor
41*/
42SoundEngine::SoundEngine ()
43{
44  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
45  this->setName("SoundEngine");
46
47  this->listener = NULL;
48  this->bufferList = NULL;
49  this->sourceList = NULL;
50}
51
52/**
53 *  the singleton reference to this class
54*/
55SoundEngine* SoundEngine::singletonRef = NULL;
56
57/**
58 *  standard deconstructor
59 */
60SoundEngine::~SoundEngine ()
61{
62  // deleting all the SoundSources
63  if(this->sourceList != NULL)
64  {
65    tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
66    SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
67    while (enumSource)
68    {
69        delete enumSource;
70        enumSource = (SoundSource*)sourceIterator->nextElement();
71    }
72    delete sourceIterator;
73  }
74
75  // deleting all the SoundBuffers
76  if (this->bufferList != NULL)
77  {
78    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
79    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
80    while (enumBuffer)
81    {
82      ResourceManager::getInstance()->unload(enumBuffer);
83      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
84    }
85    delete bufferIterator;
86  }
87
88  // removing openAL from AudioResource
89  //! @todo this should be terminated through alc
90  //alutExit();
91
92  SoundEngine::singletonRef = NULL;
93}
94
95/**
96 * loads the settings of the SoundEngine from an ini-file
97 * @param iniParser the IniParser of the inifile
98 */
99void SoundEngine::loadSettings(IniParser* iniParser)
100{
101  const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80");
102  this->musicVolume = atof(musicVolume)/100.0;
103
104  const char* effectsVolume = iniParser->getVar(CONFIG_NAME_EFFECTS_VOLUME, CONFIG_SECTION_AUDIO, "80");
105  this->effectsVolume = atof(effectsVolume)/100.0;
106}
107
108/**
109 *  creates a new SoundSource.
110 * @param fileName The Name to load the SoundBuffer from
111 * @param sourceNode The sourceNode to bind this SoundSource to.
112 * @returns The newly created SoundSource
113
114   acctualy this is nothing more than a wrapper around the ResourceManager.
115*/
116SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
117{
118  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
119}
120
121/**
122 *  Sets the doppler values of openAL
123 * @param dopplerFactor the extent of the doppler-effect
124 * @param dopplerVelocity the Speed the sound travels
125*/
126void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
127{
128  alDopplerFactor(dopplerFactor);
129  alDopplerVelocity(dopplerVelocity);
130}
131
132
133/**
134 *  adds a SoundBuffer to the bufferList of the SoundEngine
135 * @param buffer The buffer to add to the bufferList
136*/
137void SoundEngine::addBuffer(SoundBuffer* buffer)
138{
139  if (unlikely(this->bufferList == NULL))
140    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
141}
142
143/**
144 *  removes a SoundBuffer from the bufferList of the SoundEngine
145 * @param buffer The buffer to delete from the SoundEngine
146*/
147void SoundEngine::removeBuffer(SoundBuffer* buffer)
148{
149  // look if there are any sources that have the buffer still loaded
150  tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
151  SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
152  while (enumSource)
153    {
154      if (buffer == enumSource->getBuffer())
155        delete enumSource;
156      enumSource = (SoundSource*)sourceIterator->nextElement();
157    }
158  delete sourceIterator;
159}
160
161/**
162 * adds a SoundSource to the sourceList of the SoundEngine
163 * @param source The source to add to the sourceList
164*/
165void SoundEngine::addSource(SoundSource* source)
166{
167  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
168}
169
170/**
171 *  updates all The positions, Directions and Velocities of all Sounds
172*/
173void SoundEngine::update()
174{
175
176  // updating the Listeners Position
177  if (likely(this->listener != NULL))
178    {
179      alListener3f(AL_POSITION,
180                   this->listener->getAbsCoor().x,
181                   this->listener->getAbsCoor().y,
182                   this->listener->getAbsCoor().z);
183      alListener3f(AL_VELOCITY,
184                   this->listener->getVelocity().x,
185                   this->listener->getVelocity().y,
186                   this->listener->getVelocity().z);
187      Vector absDirV = this->listener->getAbsDirV();
188      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
189      alListenerfv(AL_ORIENTATION, orientation);
190    }
191  else
192    PRINTF(2)("no listener defined\n");
193
194  // updating all the Sources positions
195  if (likely(this->sourceList != NULL))
196  {
197    tIterator<BaseObject>* iterator = this->sourceList->getIterator();
198    SoundSource* enumSource = (SoundSource*)iterator->firstElement();
199    while (enumSource)
200    {
201      if (likely(enumSource->getNode() != NULL))
202      {
203        alSource3f(enumSource->getID(), AL_POSITION,
204                   enumSource->getNode()->getAbsCoor().x,
205                   enumSource->getNode()->getAbsCoor().y,
206                   enumSource->getNode()->getAbsCoor().z);
207        alSource3f(enumSource->getID(), AL_VELOCITY,
208                   enumSource->getNode()->getVelocity().x,
209                   enumSource->getNode()->getVelocity().y,
210                   enumSource->getNode()->getVelocity().z);
211      }
212      enumSource = (SoundSource*)iterator->nextElement();
213    }
214    delete iterator;
215  }
216}
217
218/**
219 *  Removes all the Buffers that are not anymore needed by any Sources
220*/
221void SoundEngine::flushUnusedBuffers()
222{
223  if(this->sourceList && this->bufferList)
224  {
225    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
226    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
227    while (enumBuffer)
228    {
229      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
230      SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
231      while (enumSource)
232      {
233        if (enumBuffer == enumSource->getBuffer())
234          break;
235        enumSource = (SoundSource*)sourceIterator->nextElement();
236      }
237      delete sourceIterator;
238      if (enumSource == NULL)
239        ResourceManager::getInstance()->unload(enumBuffer);
240      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
241    }
242    delete bufferIterator;
243  }
244}
245
246/**
247 * flushes all the Buffers
248 * deletes them from the BufferList, and also removes them via the ResourceManager.
249 */
250void SoundEngine::flushAllBuffers()
251{
252  if (this->bufferList)
253  {
254    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
255    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
256    while (enumBuffer)
257    {
258      ResourceManager::getInstance()->unload(enumBuffer, RP_LEVEL);
259      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
260    }
261    delete bufferIterator;
262  }
263}
264
265/**
266 * deletes all the Sources.
267 */
268void SoundEngine::flushAllSources()
269{
270  if (this->sourceList)
271  {
272    tIterator<BaseObject>* Iterator = this->sourceList->getIterator();
273    SoundSource* enumSource = (SoundSource*)Iterator->firstElement();
274    while (enumSource)
275    {
276      delete enumSource;
277      enumSource = (SoundSource*)Iterator->nextElement();
278    }
279    delete Iterator;
280  }
281}
282
283/**
284 *  initializes Audio in general
285*/
286bool SoundEngine::initAudio()
287{
288  ALenum result;
289  PRINTF(3)("Initialisazing openAL sound engine\n");
290  const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
291  const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
292  const char* devWalk = deviceList;
293//  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
294{ // try out enumeration extension
295    PRINTF(3)("Enumeration-extension found\n");
296
297    PRINTF(3)("Default device: %s\n", defaultDevice);
298    do
299    {
300      PRINTF(3)("%s\n", devWalk);
301      devWalk += strlen(devWalk)+1;
302    } while (devWalk[0] != '\0');
303
304
305  }
306
307
308  alutInit(NULL, 0);
309  if ((result = alGetError()) != AL_NO_ERROR)
310    SoundEngine::PrintALErrorString(result);
311
312  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
313}
314
315/**
316 *  Transforms AL-errors into something readable
317 * @param err The error found
318*/
319void SoundEngine::PrintALErrorString(ALenum err)
320{
321  switch(err)
322    {
323    case AL_NO_ERROR:
324      PRINTF(4)("AL_NO_ERROR\n");
325      break;
326
327    case AL_INVALID_NAME:
328      PRINTF(2)("AL_INVALID_NAME\n");
329      break;
330
331    case AL_INVALID_ENUM:
332      PRINTF(2)("AL_INVALID_ENUM\n");
333      break;
334
335    case AL_INVALID_VALUE:
336      PRINTF(2)("AL_INVALID_VALUE\n");
337      break;
338
339    case AL_INVALID_OPERATION:
340      PRINTF(2)("AL_INVALID_OPERATION\n");
341      break;
342
343    case AL_OUT_OF_MEMORY:
344      PRINTF(2)("AL_OUT_OF_MEMORY\n");
345      break;
346    };
347}
348
349void SoundEngine::listDevices()
350{
351
352  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
353}
354
355/*
356void SoundEngine::PrintALCErrorString(ALenum err)
357{
358  switch(err)
359    {
360    case ALC_NO_ERROR:
361      PRINTF(4)("AL_NO_ERROR\n");
362      break;
363
364    case ALC_INVALID_DEVICE:
365      PRINTF(2)("ALC_INVALID_DEVICE\n");
366      break;
367
368    case ALC_INVALID_CONTEXT:
369      PRINTF(2)("ALC_INVALID_CONTEXT\n");
370      break;
371
372    case ALC_INVALID_ENUM:
373      PRINTF(2)("ALC_INVALID_ENUM\n");
374      break;
375
376    case ALC_INVALID_VALUE:
377      PRINTF(2)("ALC_INVALID_VALUE\n");
378      break;
379
380    case ALC_OUT_OF_MEMORY:
381      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
382      break;
383    };
384}
385*/
Note: See TracBrowser for help on using the repository browser.