Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some minor include stuff

File size: 10.0 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
89  alutExit();
[5293]90
91  SoundEngine::singletonRef = NULL;
[4504]92}
93
94/**
[4985]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/**
[4836]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
[4504]112
113   acctualy this is nothing more than a wrapper around the ResourceManager.
114*/
115SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
116{
[4885]117  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
[4504]118}
119
120/**
[4836]121 *  Sets the doppler values of openAL
122 * @param dopplerFactor the extent of the doppler-effect
123 * @param dopplerVelocity the Speed the sound travels
[4504]124*/
125void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
126{
127  alDopplerFactor(dopplerFactor);
128  alDopplerVelocity(dopplerVelocity);
129}
130
131
132/**
[4836]133 *  adds a SoundBuffer to the bufferList of the SoundEngine
134 * @param buffer The buffer to add to the bufferList
[4504]135*/
136void SoundEngine::addBuffer(SoundBuffer* buffer)
137{
[4960]138  if (unlikely(this->bufferList == NULL))
139    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
[4504]140}
141
142/**
[4836]143 *  removes a SoundBuffer from the bufferList of the SoundEngine
144 * @param buffer The buffer to delete from the SoundEngine
[4504]145*/
146void SoundEngine::removeBuffer(SoundBuffer* buffer)
147{
148  // look if there are any sources that have the buffer still loaded
[4960]149  tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
[5115]150  SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
[4504]151  while (enumSource)
152    {
153      if (buffer == enumSource->getBuffer())
[4597]154        delete enumSource;
[4960]155      enumSource = (SoundSource*)sourceIterator->nextElement();
[4504]156    }
157  delete sourceIterator;
158}
159
160/**
[5293]161 * adds a SoundSource to the sourceList of the SoundEngine
[4836]162 * @param source The source to add to the sourceList
[4504]163*/
164void SoundEngine::addSource(SoundSource* source)
165{
[4960]166  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
[4504]167}
168
169/**
[4836]170 *  updates all The positions, Directions and Velocities of all Sounds
[4504]171*/
[4746]172void SoundEngine::update()
[4504]173{
174
175  // updating the Listeners Position
176  if (likely(this->listener != NULL))
177    {
178      alListener3f(AL_POSITION,
[4597]179                   this->listener->getAbsCoor().x,
180                   this->listener->getAbsCoor().y,
181                   this->listener->getAbsCoor().z);
[4504]182      alListener3f(AL_VELOCITY,
[4597]183                   this->listener->getVelocity().x,
184                   this->listener->getVelocity().y,
185                   this->listener->getVelocity().z);
[4504]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
[4960]194  if (likely(this->sourceList != NULL))
195  {
196    tIterator<BaseObject>* iterator = this->sourceList->getIterator();
[5115]197    SoundSource* enumSource = (SoundSource*)iterator->firstElement();
[4960]198    while (enumSource)
[4504]199    {
[4986]200      if (likely(enumSource->getNode() != NULL))
[4504]201      {
[4597]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);
[4504]210      }
[4960]211      enumSource = (SoundSource*)iterator->nextElement();
[4504]212    }
[4960]213    delete iterator;
214  }
[4504]215}
216
217/**
[4836]218 *  Removes all the Buffers that are not anymore needed by any Sources
[4504]219*/
[4746]220void SoundEngine::flushUnusedBuffers()
[4504]221{
[4960]222  if(this->sourceList && this->bufferList)
223  {
224    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
[5115]225    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
[4960]226    while (enumBuffer)
[4504]227    {
[4960]228      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
[5115]229      SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
[4504]230      while (enumSource)
[4960]231      {
232        if (enumBuffer == enumSource->getBuffer())
233          break;
234        enumSource = (SoundSource*)sourceIterator->nextElement();
235      }
[4504]236      delete sourceIterator;
237      if (enumSource == NULL)
[4597]238        ResourceManager::getInstance()->unload(enumBuffer);
[4960]239      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]240    }
[4960]241    delete bufferIterator;
242  }
[4504]243}
244
245/**
[5293]246 * flushes all the Buffers
247 * deletes them from the BufferList, and also removes them via the ResourceManager.
248 */
[4746]249void SoundEngine::flushAllBuffers()
[4504]250{
[4960]251  if (this->bufferList)
252  {
253    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
[5115]254    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
[4960]255    while (enumBuffer)
[4504]256    {
257      ResourceManager::getInstance()->unload(enumBuffer, RP_LEVEL);
[4960]258      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]259    }
[4960]260    delete bufferIterator;
261  }
[4504]262}
263
264/**
[5293]265 * deletes all the Sources.
[4830]266 */
267void SoundEngine::flushAllSources()
268{
[4960]269  if (this->sourceList)
[4830]270  {
[4960]271    tIterator<BaseObject>* Iterator = this->sourceList->getIterator();
[5115]272    SoundSource* enumSource = (SoundSource*)Iterator->firstElement();
[4960]273    while (enumSource)
274    {
275      delete enumSource;
276      enumSource = (SoundSource*)Iterator->nextElement();
277    }
278    delete Iterator;
[4830]279  }
280}
281
282/**
[4836]283 *  initializes Audio in general
[4504]284*/
[4746]285bool SoundEngine::initAudio()
[4504]286{
287  ALenum result;
[5385]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;
[5416]292//  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
293{ // try out enumeration extension
[5386]294    PRINTF(3)("Enumeration-extension found\n");
[4504]295
[5386]296    PRINTF(3)("Default device: %s\n", defaultDevice);
[5385]297    do
298    {
[5386]299      PRINTF(3)("%s\n", devWalk);
[5385]300      devWalk += strlen(devWalk)+1;
301    } while (devWalk[0] != '\0');
[4504]302
[5385]303
304  }
305
306
[4504]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/**
[4836]315 *  Transforms AL-errors into something readable
316 * @param err The error found
[4504]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;
[4597]325
[4504]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
[4959]348void SoundEngine::listDevices()
349{
350
351  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
352}
353
[4504]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.