Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some minor include stuff

File size: 10.0 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  alutExit();
90
91  SoundEngine::singletonRef = NULL;
92}
93
94/**
95 * loads the settings of the SoundEngine from an ini-file
96 * @param iniParser the IniParser of the inifile
97 */
98void SoundEngine::loadSettings(IniParser* iniParser)
99{
100  const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80");
101  this->musicVolume = atof(musicVolume)/100.0;
102
103  const char* effectsVolume = iniParser->getVar(CONFIG_NAME_EFFECTS_VOLUME, CONFIG_SECTION_AUDIO, "80");
104  this->effectsVolume = atof(effectsVolume)/100.0;
105}
106
107/**
108 *  creates a new SoundSource.
109 * @param fileName The Name to load the SoundBuffer from
110 * @param sourceNode The sourceNode to bind this SoundSource to.
111 * @returns The newly created SoundSource
112
113   acctualy this is nothing more than a wrapper around the ResourceManager.
114*/
115SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
116{
117  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
118}
119
120/**
121 *  Sets the doppler values of openAL
122 * @param dopplerFactor the extent of the doppler-effect
123 * @param dopplerVelocity the Speed the sound travels
124*/
125void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
126{
127  alDopplerFactor(dopplerFactor);
128  alDopplerVelocity(dopplerVelocity);
129}
130
131
132/**
133 *  adds a SoundBuffer to the bufferList of the SoundEngine
134 * @param buffer The buffer to add to the bufferList
135*/
136void SoundEngine::addBuffer(SoundBuffer* buffer)
137{
138  if (unlikely(this->bufferList == NULL))
139    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
140}
141
142/**
143 *  removes a SoundBuffer from the bufferList of the SoundEngine
144 * @param buffer The buffer to delete from the SoundEngine
145*/
146void SoundEngine::removeBuffer(SoundBuffer* buffer)
147{
148  // look if there are any sources that have the buffer still loaded
149  tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
150  SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
151  while (enumSource)
152    {
153      if (buffer == enumSource->getBuffer())
154        delete enumSource;
155      enumSource = (SoundSource*)sourceIterator->nextElement();
156    }
157  delete sourceIterator;
158}
159
160/**
161 * adds a SoundSource to the sourceList of the SoundEngine
162 * @param source The source to add to the sourceList
163*/
164void SoundEngine::addSource(SoundSource* source)
165{
166  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
167}
168
169/**
170 *  updates all The positions, Directions and Velocities of all Sounds
171*/
172void SoundEngine::update()
173{
174
175  // updating the Listeners Position
176  if (likely(this->listener != NULL))
177    {
178      alListener3f(AL_POSITION,
179                   this->listener->getAbsCoor().x,
180                   this->listener->getAbsCoor().y,
181                   this->listener->getAbsCoor().z);
182      alListener3f(AL_VELOCITY,
183                   this->listener->getVelocity().x,
184                   this->listener->getVelocity().y,
185                   this->listener->getVelocity().z);
186      Vector absDirV = this->listener->getAbsDirV();
187      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
188      alListenerfv(AL_ORIENTATION, orientation);
189    }
190  else
191    PRINTF(2)("no listener defined\n");
192
193  // updating all the Sources positions
194  if (likely(this->sourceList != NULL))
195  {
196    tIterator<BaseObject>* iterator = this->sourceList->getIterator();
197    SoundSource* enumSource = (SoundSource*)iterator->firstElement();
198    while (enumSource)
199    {
200      if (likely(enumSource->getNode() != NULL))
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 = (SoundSource*)iterator->nextElement();
212    }
213    delete iterator;
214  }
215}
216
217/**
218 *  Removes all the Buffers that are not anymore needed by any Sources
219*/
220void SoundEngine::flushUnusedBuffers()
221{
222  if(this->sourceList && this->bufferList)
223  {
224    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
225    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
226    while (enumBuffer)
227    {
228      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
229      SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
230      while (enumSource)
231      {
232        if (enumBuffer == enumSource->getBuffer())
233          break;
234        enumSource = (SoundSource*)sourceIterator->nextElement();
235      }
236      delete sourceIterator;
237      if (enumSource == NULL)
238        ResourceManager::getInstance()->unload(enumBuffer);
239      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
240    }
241    delete bufferIterator;
242  }
243}
244
245/**
246 * flushes all the Buffers
247 * deletes them from the BufferList, and also removes them via the ResourceManager.
248 */
249void SoundEngine::flushAllBuffers()
250{
251  if (this->bufferList)
252  {
253    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
254    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
255    while (enumBuffer)
256    {
257      ResourceManager::getInstance()->unload(enumBuffer, RP_LEVEL);
258      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
259    }
260    delete bufferIterator;
261  }
262}
263
264/**
265 * deletes all the Sources.
266 */
267void SoundEngine::flushAllSources()
268{
269  if (this->sourceList)
270  {
271    tIterator<BaseObject>* Iterator = this->sourceList->getIterator();
272    SoundSource* enumSource = (SoundSource*)Iterator->firstElement();
273    while (enumSource)
274    {
275      delete enumSource;
276      enumSource = (SoundSource*)Iterator->nextElement();
277    }
278    delete Iterator;
279  }
280}
281
282/**
283 *  initializes Audio in general
284*/
285bool SoundEngine::initAudio()
286{
287  ALenum result;
288  PRINTF(3)("Initialisazing openAL sound engine\n");
289  const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
290  const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
291  const char* devWalk = deviceList;
292//  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
293{ // try out enumeration extension
294    PRINTF(3)("Enumeration-extension found\n");
295
296    PRINTF(3)("Default device: %s\n", defaultDevice);
297    do
298    {
299      PRINTF(3)("%s\n", devWalk);
300      devWalk += strlen(devWalk)+1;
301    } while (devWalk[0] != '\0');
302
303
304  }
305
306
307  alutInit(NULL, 0);
308  if ((result = alGetError()) != AL_NO_ERROR)
309    SoundEngine::PrintALErrorString(result);
310
311  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
312}
313
314/**
315 *  Transforms AL-errors into something readable
316 * @param err The error found
317*/
318void SoundEngine::PrintALErrorString(ALenum err)
319{
320  switch(err)
321    {
322    case AL_NO_ERROR:
323      PRINTF(4)("AL_NO_ERROR\n");
324      break;
325
326    case AL_INVALID_NAME:
327      PRINTF(2)("AL_INVALID_NAME\n");
328      break;
329
330    case AL_INVALID_ENUM:
331      PRINTF(2)("AL_INVALID_ENUM\n");
332      break;
333
334    case AL_INVALID_VALUE:
335      PRINTF(2)("AL_INVALID_VALUE\n");
336      break;
337
338    case AL_INVALID_OPERATION:
339      PRINTF(2)("AL_INVALID_OPERATION\n");
340      break;
341
342    case AL_OUT_OF_MEMORY:
343      PRINTF(2)("AL_OUT_OF_MEMORY\n");
344      break;
345    };
346}
347
348void SoundEngine::listDevices()
349{
350
351  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
352}
353
354/*
355void SoundEngine::PrintALCErrorString(ALenum err)
356{
357  switch(err)
358    {
359    case ALC_NO_ERROR:
360      PRINTF(4)("AL_NO_ERROR\n");
361      break;
362
363    case ALC_INVALID_DEVICE:
364      PRINTF(2)("ALC_INVALID_DEVICE\n");
365      break;
366
367    case ALC_INVALID_CONTEXT:
368      PRINTF(2)("ALC_INVALID_CONTEXT\n");
369      break;
370
371    case ALC_INVALID_ENUM:
372      PRINTF(2)("ALC_INVALID_ENUM\n");
373      break;
374
375    case ALC_INVALID_VALUE:
376      PRINTF(2)("ALC_INVALID_VALUE\n");
377      break;
378
379    case ALC_OUT_OF_MEMORY:
380      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
381      break;
382    };
383}
384*/
Note: See TracBrowser for help on using the repository browser.