Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ClassList is now in std::list style
ShellCommand is now in std::list style

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