Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5411 was 5409, checked in by bensch, 20 years ago

orxonox/trunk: all working function

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"
[4504]31
32using namespace std;
33
[4597]34
[4504]35//////////////////
36/* SOUND-ENGINE */
37//////////////////
38/**
[4836]39 *  standard constructor
[4504]40*/
[4597]41SoundEngine::SoundEngine ()
[4504]42{
[4597]43  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
44  this->setName("SoundEngine");
45
[4504]46  this->listener = NULL;
[4960]47  this->bufferList = NULL;
48  this->sourceList = NULL;
[4504]49}
50
51/**
[4836]52 *  the singleton reference to this class
[4504]53*/
54SoundEngine* SoundEngine::singletonRef = NULL;
55
56/**
[4836]57 *  standard deconstructor
[5293]58 */
[4597]59SoundEngine::~SoundEngine ()
[4504]60{
61  // deleting all the SoundSources
[4960]62  if(this->sourceList != NULL)
63  {
64    tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
[5115]65    SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
[4960]66    while (enumSource)
[4504]67    {
[4960]68        delete enumSource;
69        enumSource = (SoundSource*)sourceIterator->nextElement();
[4504]70    }
[4960]71    delete sourceIterator;
72  }
[4504]73
74  // deleting all the SoundBuffers
[4960]75  if (this->bufferList != NULL)
76  {
77    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
[5115]78    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
[4960]79    while (enumBuffer)
[4504]80    {
81      ResourceManager::getInstance()->unload(enumBuffer);
[4960]82      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]83    }
[4960]84    delete bufferIterator;
85  }
[5293]86
[4504]87  // removing openAL from AudioResource
88  alutExit();
[5293]89
90  SoundEngine::singletonRef = NULL;
[4504]91}
92
93/**
[4985]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/**
[4836]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
[4504]111
112   acctualy this is nothing more than a wrapper around the ResourceManager.
113*/
114SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
115{
[4885]116  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
[4504]117}
118
119/**
[4836]120 *  Sets the doppler values of openAL
121 * @param dopplerFactor the extent of the doppler-effect
122 * @param dopplerVelocity the Speed the sound travels
[4504]123*/
124void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
125{
126  alDopplerFactor(dopplerFactor);
127  alDopplerVelocity(dopplerVelocity);
128}
129
130
131/**
[4836]132 *  adds a SoundBuffer to the bufferList of the SoundEngine
133 * @param buffer The buffer to add to the bufferList
[4504]134*/
135void SoundEngine::addBuffer(SoundBuffer* buffer)
136{
[4960]137  if (unlikely(this->bufferList == NULL))
138    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
[4504]139}
140
141/**
[4836]142 *  removes a SoundBuffer from the bufferList of the SoundEngine
143 * @param buffer The buffer to delete from the SoundEngine
[4504]144*/
145void SoundEngine::removeBuffer(SoundBuffer* buffer)
146{
147  // look if there are any sources that have the buffer still loaded
[4960]148  tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
[5115]149  SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
[4504]150  while (enumSource)
151    {
152      if (buffer == enumSource->getBuffer())
[4597]153        delete enumSource;
[4960]154      enumSource = (SoundSource*)sourceIterator->nextElement();
[4504]155    }
156  delete sourceIterator;
157}
158
159/**
[5293]160 * adds a SoundSource to the sourceList of the SoundEngine
[4836]161 * @param source The source to add to the sourceList
[4504]162*/
163void SoundEngine::addSource(SoundSource* source)
164{
[4960]165  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
[4504]166}
167
168/**
[4836]169 *  updates all The positions, Directions and Velocities of all Sounds
[4504]170*/
[4746]171void SoundEngine::update()
[4504]172{
173
174  // updating the Listeners Position
175  if (likely(this->listener != NULL))
176    {
177      alListener3f(AL_POSITION,
[4597]178                   this->listener->getAbsCoor().x,
179                   this->listener->getAbsCoor().y,
180                   this->listener->getAbsCoor().z);
[4504]181      alListener3f(AL_VELOCITY,
[4597]182                   this->listener->getVelocity().x,
183                   this->listener->getVelocity().y,
184                   this->listener->getVelocity().z);
[4504]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
[4960]193  if (likely(this->sourceList != NULL))
194  {
195    tIterator<BaseObject>* iterator = this->sourceList->getIterator();
[5115]196    SoundSource* enumSource = (SoundSource*)iterator->firstElement();
[4960]197    while (enumSource)
[4504]198    {
[4986]199      if (likely(enumSource->getNode() != NULL))
[4504]200      {
[4597]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);
[4504]209      }
[4960]210      enumSource = (SoundSource*)iterator->nextElement();
[4504]211    }
[4960]212    delete iterator;
213  }
[4504]214}
215
216/**
[4836]217 *  Removes all the Buffers that are not anymore needed by any Sources
[4504]218*/
[4746]219void SoundEngine::flushUnusedBuffers()
[4504]220{
[4960]221  if(this->sourceList && this->bufferList)
222  {
223    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
[5115]224    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
[4960]225    while (enumBuffer)
[4504]226    {
[4960]227      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
[5115]228      SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
[4504]229      while (enumSource)
[4960]230      {
231        if (enumBuffer == enumSource->getBuffer())
232          break;
233        enumSource = (SoundSource*)sourceIterator->nextElement();
234      }
[4504]235      delete sourceIterator;
236      if (enumSource == NULL)
[4597]237        ResourceManager::getInstance()->unload(enumBuffer);
[4960]238      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]239    }
[4960]240    delete bufferIterator;
241  }
[4504]242}
243
244/**
[5293]245 * flushes all the Buffers
246 * deletes them from the BufferList, and also removes them via the ResourceManager.
247 */
[4746]248void SoundEngine::flushAllBuffers()
[4504]249{
[4960]250  if (this->bufferList)
251  {
252    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
[5115]253    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
[4960]254    while (enumBuffer)
[4504]255    {
256      ResourceManager::getInstance()->unload(enumBuffer, RP_LEVEL);
[4960]257      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]258    }
[4960]259    delete bufferIterator;
260  }
[4504]261}
262
263/**
[5293]264 * deletes all the Sources.
[4830]265 */
266void SoundEngine::flushAllSources()
267{
[4960]268  if (this->sourceList)
[4830]269  {
[4960]270    tIterator<BaseObject>* Iterator = this->sourceList->getIterator();
[5115]271    SoundSource* enumSource = (SoundSource*)Iterator->firstElement();
[4960]272    while (enumSource)
273    {
274      delete enumSource;
275      enumSource = (SoundSource*)Iterator->nextElement();
276    }
277    delete Iterator;
[4830]278  }
279}
280
281/**
[4836]282 *  initializes Audio in general
[4504]283*/
[4746]284bool SoundEngine::initAudio()
[4504]285{
286  ALenum result;
[5385]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;
[5409]291#ifndef ALCchar
292#define ALCchar ALCubyte
293#endif
294  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE) { // 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.