Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: all working function

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
32using namespace std;
33
34
35//////////////////
36/* SOUND-ENGINE */
37//////////////////
38/**
39 *  standard constructor
40*/
41SoundEngine::SoundEngine ()
42{
43  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
44  this->setName("SoundEngine");
45
46  this->listener = NULL;
47  this->bufferList = NULL;
48  this->sourceList = NULL;
49}
50
51/**
52 *  the singleton reference to this class
53*/
54SoundEngine* SoundEngine::singletonRef = NULL;
55
56/**
57 *  standard deconstructor
58 */
59SoundEngine::~SoundEngine ()
60{
61  // deleting all the SoundSources
62  if(this->sourceList != NULL)
63  {
64    tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
65    SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
66    while (enumSource)
67    {
68        delete enumSource;
69        enumSource = (SoundSource*)sourceIterator->nextElement();
70    }
71    delete sourceIterator;
72  }
73
74  // deleting all the SoundBuffers
75  if (this->bufferList != NULL)
76  {
77    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
78    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
79    while (enumBuffer)
80    {
81      ResourceManager::getInstance()->unload(enumBuffer);
82      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
83    }
84    delete bufferIterator;
85  }
86
87  // removing openAL from AudioResource
88  alutExit();
89
90  SoundEngine::singletonRef = NULL;
91}
92
93/**
94 * loads the settings of the SoundEngine from an ini-file
95 * @param iniParser the IniParser of the inifile
96 */
97void SoundEngine::loadSettings(IniParser* iniParser)
98{
99  const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80");
100  this->musicVolume = atof(musicVolume)/100.0;
101
102  const char* effectsVolume = iniParser->getVar(CONFIG_NAME_EFFECTS_VOLUME, CONFIG_SECTION_AUDIO, "80");
103  this->effectsVolume = atof(effectsVolume)/100.0;
104}
105
106/**
107 *  creates a new SoundSource.
108 * @param fileName The Name to load the SoundBuffer from
109 * @param sourceNode The sourceNode to bind this SoundSource to.
110 * @returns The newly created SoundSource
111
112   acctualy this is nothing more than a wrapper around the ResourceManager.
113*/
114SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
115{
116  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
117}
118
119/**
120 *  Sets the doppler values of openAL
121 * @param dopplerFactor the extent of the doppler-effect
122 * @param dopplerVelocity the Speed the sound travels
123*/
124void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
125{
126  alDopplerFactor(dopplerFactor);
127  alDopplerVelocity(dopplerVelocity);
128}
129
130
131/**
132 *  adds a SoundBuffer to the bufferList of the SoundEngine
133 * @param buffer The buffer to add to the bufferList
134*/
135void SoundEngine::addBuffer(SoundBuffer* buffer)
136{
137  if (unlikely(this->bufferList == NULL))
138    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
139}
140
141/**
142 *  removes a SoundBuffer from the bufferList of the SoundEngine
143 * @param buffer The buffer to delete from the SoundEngine
144*/
145void SoundEngine::removeBuffer(SoundBuffer* buffer)
146{
147  // look if there are any sources that have the buffer still loaded
148  tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
149  SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
150  while (enumSource)
151    {
152      if (buffer == enumSource->getBuffer())
153        delete enumSource;
154      enumSource = (SoundSource*)sourceIterator->nextElement();
155    }
156  delete sourceIterator;
157}
158
159/**
160 * adds a SoundSource to the sourceList of the SoundEngine
161 * @param source The source to add to the sourceList
162*/
163void SoundEngine::addSource(SoundSource* source)
164{
165  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
166}
167
168/**
169 *  updates all The positions, Directions and Velocities of all Sounds
170*/
171void SoundEngine::update()
172{
173
174  // updating the Listeners Position
175  if (likely(this->listener != NULL))
176    {
177      alListener3f(AL_POSITION,
178                   this->listener->getAbsCoor().x,
179                   this->listener->getAbsCoor().y,
180                   this->listener->getAbsCoor().z);
181      alListener3f(AL_VELOCITY,
182                   this->listener->getVelocity().x,
183                   this->listener->getVelocity().y,
184                   this->listener->getVelocity().z);
185      Vector absDirV = this->listener->getAbsDirV();
186      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
187      alListenerfv(AL_ORIENTATION, orientation);
188    }
189  else
190    PRINTF(2)("no listener defined\n");
191
192  // updating all the Sources positions
193  if (likely(this->sourceList != NULL))
194  {
195    tIterator<BaseObject>* iterator = this->sourceList->getIterator();
196    SoundSource* enumSource = (SoundSource*)iterator->firstElement();
197    while (enumSource)
198    {
199      if (likely(enumSource->getNode() != NULL))
200      {
201        alSource3f(enumSource->getID(), AL_POSITION,
202                   enumSource->getNode()->getAbsCoor().x,
203                   enumSource->getNode()->getAbsCoor().y,
204                   enumSource->getNode()->getAbsCoor().z);
205        alSource3f(enumSource->getID(), AL_VELOCITY,
206                   enumSource->getNode()->getVelocity().x,
207                   enumSource->getNode()->getVelocity().y,
208                   enumSource->getNode()->getVelocity().z);
209      }
210      enumSource = (SoundSource*)iterator->nextElement();
211    }
212    delete iterator;
213  }
214}
215
216/**
217 *  Removes all the Buffers that are not anymore needed by any Sources
218*/
219void SoundEngine::flushUnusedBuffers()
220{
221  if(this->sourceList && this->bufferList)
222  {
223    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
224    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
225    while (enumBuffer)
226    {
227      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
228      SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
229      while (enumSource)
230      {
231        if (enumBuffer == enumSource->getBuffer())
232          break;
233        enumSource = (SoundSource*)sourceIterator->nextElement();
234      }
235      delete sourceIterator;
236      if (enumSource == NULL)
237        ResourceManager::getInstance()->unload(enumBuffer);
238      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
239    }
240    delete bufferIterator;
241  }
242}
243
244/**
245 * flushes all the Buffers
246 * deletes them from the BufferList, and also removes them via the ResourceManager.
247 */
248void SoundEngine::flushAllBuffers()
249{
250  if (this->bufferList)
251  {
252    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
253    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
254    while (enumBuffer)
255    {
256      ResourceManager::getInstance()->unload(enumBuffer, RP_LEVEL);
257      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
258    }
259    delete bufferIterator;
260  }
261}
262
263/**
264 * deletes all the Sources.
265 */
266void SoundEngine::flushAllSources()
267{
268  if (this->sourceList)
269  {
270    tIterator<BaseObject>* Iterator = this->sourceList->getIterator();
271    SoundSource* enumSource = (SoundSource*)Iterator->firstElement();
272    while (enumSource)
273    {
274      delete enumSource;
275      enumSource = (SoundSource*)Iterator->nextElement();
276    }
277    delete Iterator;
278  }
279}
280
281/**
282 *  initializes Audio in general
283*/
284bool SoundEngine::initAudio()
285{
286  ALenum result;
287  PRINTF(3)("Initialisazing openAL sound engine\n");
288  const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
289  const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
290  const char* devWalk = deviceList;
291#ifndef ALCchar
292#define ALCchar ALCubyte
293#endif
294  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE) { // 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.