Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/lib/sound/sound_engine.cc @ 5842

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

orxonox/trunk: sound_engine compiles on tardis

File size: 9.4 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 "class_list.h"
24
25#include "p_node.h"
26#include "list.h"
27#include "resource_manager.h"
28#include "debug.h"
29#include "ini_parser.h"
30#include "globals.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    while (this->sourceList->size() > 0)
65      delete dynamic_cast<SoundSource*>(this->sourceList->front());
66  }
67
68  // deleting all the SoundBuffers
69  if (this->bufferList != NULL)
70  {
71    while(this->bufferList->size() > 0)
72      ResourceManager::getInstance()->unload(dynamic_cast<SoundBuffer*>(this->bufferList->front()));
73  }
74
75  // removing openAL from AudioResource
76  //! @todo this should be terminated through alc
77  //alutExit();
78
79  SoundEngine::singletonRef = NULL;
80}
81
82/**
83 * loads the settings of the SoundEngine from an ini-file
84 * @param iniParser the IniParser of the inifile
85 */
86void SoundEngine::loadSettings(IniParser* iniParser)
87{
88  const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80");
89  this->musicVolume = atof(musicVolume)/100.0;
90
91  const char* effectsVolume = iniParser->getVar(CONFIG_NAME_EFFECTS_VOLUME, CONFIG_SECTION_AUDIO, "80");
92  this->effectsVolume = atof(effectsVolume)/100.0;
93}
94
95/**
96 *  creates a new SoundSource.
97 * @param fileName The Name to load the SoundBuffer from
98 * @param sourceNode The sourceNode to bind this SoundSource to.
99 * @returns The newly created SoundSource
100
101   acctualy this is nothing more than a wrapper around the ResourceManager.
102*/
103SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
104{
105  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
106}
107
108/**
109 *  Sets the doppler values of openAL
110 * @param dopplerFactor the extent of the doppler-effect
111 * @param dopplerVelocity the Speed the sound travels
112*/
113void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
114{
115  alDopplerFactor(dopplerFactor);
116  alDopplerVelocity(dopplerVelocity);
117}
118
119
120/**
121 *  adds a SoundBuffer to the bufferList of the SoundEngine
122 * @param buffer The buffer to add to the bufferList
123*/
124void SoundEngine::addBuffer(SoundBuffer* buffer)
125{
126  if (unlikely(this->bufferList == NULL))
127    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
128}
129
130/**
131 *  removes a SoundBuffer from the bufferList of the SoundEngine
132 * @param buffer The buffer to delete from the SoundEngine
133*/
134void SoundEngine::removeBuffer(SoundBuffer* buffer)
135{
136  // look if there are any sources that have the buffer still loaded
137  if (this->sourceList != NULL)
138  {
139    list<BaseObject*>::iterator source;
140    for (source = this->sourceList->begin(); source != this->sourceList->end(); source++)
141    {
142      if (buffer == static_cast<SoundSource*>(*source)->getBuffer())
143        delete (*source);
144    }
145  }
146}
147
148/**
149 * adds a SoundSource to the sourceList of the SoundEngine
150 * @param source The source to add to the sourceList
151*/
152void SoundEngine::addSource(SoundSource* source)
153{
154  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
155}
156
157/**
158 *  updates all The positions, Directions and Velocities of all Sounds
159*/
160void SoundEngine::update()
161{
162
163  // updating the Listeners Position
164  if (likely(this->listener != NULL))
165    {
166      alListener3f(AL_POSITION,
167                   this->listener->getAbsCoor().x,
168                   this->listener->getAbsCoor().y,
169                   this->listener->getAbsCoor().z);
170      alListener3f(AL_VELOCITY,
171                   this->listener->getVelocity().x,
172                   this->listener->getVelocity().y,
173                   this->listener->getVelocity().z);
174      Vector absDirV = this->listener->getAbsDirV();
175      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
176      alListenerfv(AL_ORIENTATION, orientation);
177    }
178  else
179    PRINTF(2)("no listener defined\n");
180
181  // updating all the Sources positions
182  if (likely(this->sourceList != NULL))
183  {
184    list<BaseObject*>::iterator sourceIT;
185    SoundSource* source;
186    for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
187    {
188      source = static_cast<SoundSource*>(*sourceIT);
189      if (likely(source->getNode() != NULL))
190      {
191        alSource3f(source->getID(), AL_POSITION,
192                   source->getNode()->getAbsCoor().x,
193                   source->getNode()->getAbsCoor().y,
194                   source->getNode()->getAbsCoor().z);
195        alSource3f(source->getID(), AL_VELOCITY,
196                   source->getNode()->getVelocity().x,
197                   source->getNode()->getVelocity().y,
198                   source->getNode()->getVelocity().z);
199      }
200    }
201  }
202}
203
204/**
205 *  Removes all the Buffers that are not anymore needed by any Sources
206*/
207void SoundEngine::flushUnusedBuffers()
208{
209  /// FIXME
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  // INITIALIZING THE DEVICE:
283  ALchar deviceName[] =
284#ifdef __WIN32__
285      "native";
286#else
287      "'( ( devices '( native arts null ) ) )";
288#endif
289  //
290  this->device = alcOpenDevice(deviceName);
291
292  this->context = alcCreateContext(this->device, NULL);
293
294  alcMakeContextCurrent(this->context);
295
296
297  if ((result = alGetError()) != AL_NO_ERROR)
298    SoundEngine::PrintALErrorString(result);
299
300  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
301}
302
303/**
304 *  Transforms AL-errors into something readable
305 * @param err The error found
306*/
307void SoundEngine::PrintALErrorString(ALenum err)
308{
309  switch(err)
310    {
311    case AL_NO_ERROR:
312      PRINTF(4)("AL_NO_ERROR\n");
313      break;
314
315    case AL_INVALID_NAME:
316      PRINTF(2)("AL_INVALID_NAME\n");
317      break;
318
319    case AL_INVALID_ENUM:
320      PRINTF(2)("AL_INVALID_ENUM\n");
321      break;
322
323    case AL_INVALID_VALUE:
324      PRINTF(2)("AL_INVALID_VALUE\n");
325      break;
326
327    case AL_INVALID_OPERATION:
328      PRINTF(2)("AL_INVALID_OPERATION\n");
329      break;
330
331    case AL_OUT_OF_MEMORY:
332      PRINTF(2)("AL_OUT_OF_MEMORY\n");
333      break;
334    };
335}
336
337void SoundEngine::listDevices()
338{
339
340  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
341}
342
343/*
344void SoundEngine::PrintALCErrorString(ALenum err)
345{
346  switch(err)
347    {
348    case ALC_NO_ERROR:
349      PRINTF(4)("AL_NO_ERROR\n");
350      break;
351
352    case ALC_INVALID_DEVICE:
353      PRINTF(2)("ALC_INVALID_DEVICE\n");
354      break;
355
356    case ALC_INVALID_CONTEXT:
357      PRINTF(2)("ALC_INVALID_CONTEXT\n");
358      break;
359
360    case ALC_INVALID_ENUM:
361      PRINTF(2)("ALC_INVALID_ENUM\n");
362      break;
363
364    case ALC_INVALID_VALUE:
365      PRINTF(2)("ALC_INVALID_VALUE\n");
366      break;
367
368    case ALC_OUT_OF_MEMORY:
369      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
370      break;
371    };
372}
373*/
Note: See TracBrowser for help on using the repository browser.