Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: sound-engine finishes faster.

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