Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: split open the soundengine into
sound_engine: the Handler
sound_source: the sources
sound_buffers: playback-buffers

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
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  if (alcIsExtensionPresent(NULL, (ALubyte*)"ALC_ENUMERATION_EXT") == AL_TRUE) { // try out enumeration extension
292    PRINTF(3)("Enumeration-extension found\n");
293
294    PRINTF(3)("Default device: %s\n", defaultDevice);
295    do
296    {
297      PRINTF(3)("%s\n", devWalk);
298      devWalk += strlen(devWalk)+1;
299    } while (devWalk[0] != '\0');
300
301
302  }
303
304
305  alutInit(NULL, 0);
306  if ((result = alGetError()) != AL_NO_ERROR)
307    SoundEngine::PrintALErrorString(result);
308
309  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
310}
311
312/**
313 *  Transforms AL-errors into something readable
314 * @param err The error found
315*/
316void SoundEngine::PrintALErrorString(ALenum err)
317{
318  switch(err)
319    {
320    case AL_NO_ERROR:
321      PRINTF(4)("AL_NO_ERROR\n");
322      break;
323
324    case AL_INVALID_NAME:
325      PRINTF(2)("AL_INVALID_NAME\n");
326      break;
327
328    case AL_INVALID_ENUM:
329      PRINTF(2)("AL_INVALID_ENUM\n");
330      break;
331
332    case AL_INVALID_VALUE:
333      PRINTF(2)("AL_INVALID_VALUE\n");
334      break;
335
336    case AL_INVALID_OPERATION:
337      PRINTF(2)("AL_INVALID_OPERATION\n");
338      break;
339
340    case AL_OUT_OF_MEMORY:
341      PRINTF(2)("AL_OUT_OF_MEMORY\n");
342      break;
343    };
344}
345
346void SoundEngine::listDevices()
347{
348
349  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
350}
351
352/*
353void SoundEngine::PrintALCErrorString(ALenum err)
354{
355  switch(err)
356    {
357    case ALC_NO_ERROR:
358      PRINTF(4)("AL_NO_ERROR\n");
359      break;
360
361    case ALC_INVALID_DEVICE:
362      PRINTF(2)("ALC_INVALID_DEVICE\n");
363      break;
364
365    case ALC_INVALID_CONTEXT:
366      PRINTF(2)("ALC_INVALID_CONTEXT\n");
367      break;
368
369    case ALC_INVALID_ENUM:
370      PRINTF(2)("ALC_INVALID_ENUM\n");
371      break;
372
373    case ALC_INVALID_VALUE:
374      PRINTF(2)("ALC_INVALID_VALUE\n");
375      break;
376
377    case ALC_OUT_OF_MEMORY:
378      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
379      break;
380    };
381}
382*/
Note: See TracBrowser for help on using the repository browser.