Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6840 was 6840, checked in by bensch, 18 years ago

trunk: some minor stupid bug, that held me up for two hours

File size: 10.7 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
[4960]23#include "class_list.h"
[4504]24
25#include "p_node.h"
26#include "resource_manager.h"
27#include "debug.h"
[5944]28#include "parser/ini_parser/ini_parser.h"
[5427]29#include "globals.h"
[4504]30
31using namespace std;
32
[4597]33
[4504]34//////////////////
35/* SOUND-ENGINE */
36//////////////////
37/**
[4836]38 *  standard constructor
[4504]39*/
[4597]40SoundEngine::SoundEngine ()
[4504]41{
[4597]42  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
43  this->setName("SoundEngine");
44
[4504]45  this->listener = NULL;
[4960]46  this->bufferList = NULL;
47  this->sourceList = NULL;
[5930]48
49  this->device = NULL;
50  this->context = NULL;
51
52  this->maxSourceCount = 32;
[6829]53
54  this->effectsVolume = .80;
55  this->musicVolume = .75;
[4504]56}
57
58/**
[4836]59 *  the singleton reference to this class
[4504]60*/
61SoundEngine* SoundEngine::singletonRef = NULL;
62
63/**
[4836]64 *  standard deconstructor
[5293]65 */
[4597]66SoundEngine::~SoundEngine ()
[4504]67{
68  // deleting all the SoundSources
[4960]69  if(this->sourceList != NULL)
70  {
[5779]71    while (this->sourceList->size() > 0)
72      delete dynamic_cast<SoundSource*>(this->sourceList->front());
[4960]73  }
[4504]74
[5930]75  while(!this->ALSources.empty())
76  {
77    alDeleteSources(1, &this->ALSources.top());
78    this->ALSources.pop();
79  }
80
[4504]81  // deleting all the SoundBuffers
[4960]82  if (this->bufferList != NULL)
83  {
[5779]84    while(this->bufferList->size() > 0)
85      ResourceManager::getInstance()->unload(dynamic_cast<SoundBuffer*>(this->bufferList->front()));
[4960]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{
[5953]101  const char* channels = iniParser->getVar(CONFIG_NAME_AUDIO_CHANNELS, CONFIG_SECTION_AUDIO, "32");
102  this->maxSourceCount = atoi(channels);
[4985]103
104  const char* effectsVolume = iniParser->getVar(CONFIG_NAME_EFFECTS_VOLUME, CONFIG_SECTION_AUDIO, "80");
105  this->effectsVolume = atof(effectsVolume)/100.0;
[6830]106
[6840]107  const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "75");
108  this->musicVolume = atof(musicVolume)/100.0;
[4985]109}
110
111/**
[4836]112 *  creates a new SoundSource.
113 * @param fileName The Name to load the SoundBuffer from
114 * @param sourceNode The sourceNode to bind this SoundSource to.
115 * @returns The newly created SoundSource
[4504]116
117   acctualy this is nothing more than a wrapper around the ResourceManager.
118*/
119SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
120{
[4885]121  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
[4504]122}
123
124/**
[4836]125 *  Sets the doppler values of openAL
126 * @param dopplerFactor the extent of the doppler-effect
127 * @param dopplerVelocity the Speed the sound travels
[4504]128*/
129void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
130{
131  alDopplerFactor(dopplerFactor);
[6840]132  this->checkError("Setting Doppler Factor", __LINE__);
133
[4504]134  alDopplerVelocity(dopplerVelocity);
[6840]135  this->checkError("Setting Doppler Velocity", __LINE__);
[4504]136}
137
138
[5930]139void SoundEngine::popALSource(ALuint& source)
[4504]140{
[5930]141  if (source != 0)
142    return;
143  else
144  {
[4504]145
[5930]146    /// @TODO try to create more sources if needed
147    if (!this->ALSources.empty())
[4504]148    {
[5930]149      source = this->ALSources.top();
150      this->ALSources.pop();
[4504]151    }
[5779]152  }
[4504]153}
154
155
156/**
[4836]157 *  updates all The positions, Directions and Velocities of all Sounds
[4504]158*/
[4746]159void SoundEngine::update()
[4504]160{
161
162  // updating the Listeners Position
163  if (likely(this->listener != NULL))
164    {
165      alListener3f(AL_POSITION,
[4597]166                   this->listener->getAbsCoor().x,
167                   this->listener->getAbsCoor().y,
168                   this->listener->getAbsCoor().z);
[4504]169      alListener3f(AL_VELOCITY,
[4597]170                   this->listener->getVelocity().x,
171                   this->listener->getVelocity().y,
172                   this->listener->getVelocity().z);
[4504]173      Vector absDirV = this->listener->getAbsDirV();
174      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
175      alListenerfv(AL_ORIENTATION, orientation);
176    }
177  else
178    PRINTF(2)("no listener defined\n");
179
180  // updating all the Sources positions
[5930]181  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
[4960]182  {
[5885]183    list<BaseObject*>::const_iterator sourceIT;
[5779]184    SoundSource* source;
185    for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
[4504]186    {
[5779]187      source = static_cast<SoundSource*>(*sourceIT);
[5930]188      if (source->isPlaying())
[4504]189      {
[6073]190        int play = 0x000;
[5930]191        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
192        if(play == AL_PLAYING)
193        {
194          if (likely(source->getNode() != NULL))
195          {
196            alSource3f(source->getID(), AL_POSITION,
197                       source->getNode()->getAbsCoor().x,
198                       source->getNode()->getAbsCoor().y,
199                       source->getNode()->getAbsCoor().z);
200            alSource3f(source->getID(), AL_VELOCITY,
201                       source->getNode()->getVelocity().x,
202                       source->getNode()->getVelocity().y,
203                       source->getNode()->getVelocity().z);
204          }
205
206        }
207        else
208        {
209          source->stop();
210        }
[4504]211      }
212    }
[4960]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{
[5819]221  /// FIXME
[5779]222/*  if(this->sourceList && this->bufferList)
[4960]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;
[5779]242}*/ /// FIXME
[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  {
[5779]253    while (this->bufferList->size() > 0)
254      ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front()), RP_LEVEL);
[4960]255  }
[4504]256}
257
258/**
[5293]259 * deletes all the Sources.
[4830]260 */
261void SoundEngine::flushAllSources()
262{
[4960]263  if (this->sourceList)
[4830]264  {
[5779]265    while(this->sourceList->size() > 0)
266      delete this->sourceList->front();
[4830]267  }
268}
269
270/**
[4836]271 *  initializes Audio in general
[4504]272*/
[4746]273bool SoundEngine::initAudio()
[4504]274{
275  ALenum result;
[5385]276  PRINTF(3)("Initialisazing openAL sound engine\n");
277  const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
278  const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
279  const char* devWalk = deviceList;
[5416]280//  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
281{ // try out enumeration extension
[5386]282    PRINTF(3)("Enumeration-extension found\n");
[4504]283
[5386]284    PRINTF(3)("Default device: %s\n", defaultDevice);
[5385]285    do
286    {
[5386]287      PRINTF(3)("%s\n", devWalk);
[5385]288      devWalk += strlen(devWalk)+1;
289    } while (devWalk[0] != '\0');
[4504]290
[5385]291
[5982]292
[5385]293  }
[5819]294  // INITIALIZING THE DEVICE:
[6840]295/*
[6076]296#ifdef AL_VERSION_1_1
[5915]297  ALCchar deviceName[] =
[5996]298
[5819]299#ifdef __WIN32__
[5915]300      "Direct3D";
[5819]301#else
[5915]302      "'( ( devices '( native null ) ) )";
[5819]303#endif
304  //
305  this->device = alcOpenDevice(deviceName);
[5385]306
[5819]307  this->context = alcCreateContext(this->device, NULL);
308
[5996]309  alcMakeContextCurrent(this->context);
[6840]310#else*/
[6836]311  this->device = alcOpenDevice(NULL);
[6840]312  SoundEngine::checkError("Error opening Device", __LINE__);
[6836]313  this->context = alcCreateContext(this->device, NULL);
[6840]314  SoundEngine::checkError("Error creating Context", __LINE__);
[6836]315  alcMakeContextCurrent(this->context);
[6840]316  this->checkError("Making Context Current", __LINE__);
317// #endif
[5819]318
[4504]319  if ((result = alGetError()) != AL_NO_ERROR)
[5930]320    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
[4504]321
322  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
[6840]323  this->allocateSources(this->maxSourceCount);
[4504]324}
325
[5917]326
[4504]327/**
[5917]328 * Allocates openAL sources
329 * @param count how many sources to allocate
330 * @returns true on success, false if at least one source could not be allocated
331 */
332bool SoundEngine::allocateSources(unsigned int count)
333{
[5930]334  for (unsigned int i = 0; i < count; i++)
[5917]335  {
[6836]336    ALuint source = 0;
[5917]337
[5930]338    alGenSources(1, &source);
[6840]339    printf("%d %d\n", i, source);
340    this->checkError("allocate Source", __LINE__);
[5917]341
[5930]342    alSourcef (source, AL_PITCH,    1.0      );
343    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
344    alSourcei (source, AL_LOOPING,  AL_FALSE );
345    this->ALSources.push(source);
[5917]346  }
347  return true;
348}
349
[6836]350bool SoundEngine::checkError(const char* error, unsigned int line)
351{
352  ALenum errorCode;
353  if ((errorCode = alGetError()) != AL_NO_ERROR)
354  {
355    PRINTF(1)("Error %s (line:%d): '%s'\n", error, line, SoundEngine::getALErrorString(errorCode));
356    return false;
357  }
358  else
359    return true;
360}
361
[5917]362/**
[4836]363 *  Transforms AL-errors into something readable
364 * @param err The error found
[4504]365*/
[5930]366const char* SoundEngine::getALErrorString(ALenum err)
[4504]367{
368  switch(err)
369    {
370    case AL_NO_ERROR:
[5930]371      return ("AL_NO_ERROR");
[4504]372    case AL_INVALID_NAME:
[5930]373      return ("AL_INVALID_NAME");
[4504]374    case AL_INVALID_ENUM:
[5930]375      return ("AL_INVALID_ENUM");
[4504]376    case AL_INVALID_VALUE:
[5930]377      return ("AL_INVALID_VALUE");
[4504]378    case AL_INVALID_OPERATION:
[5930]379      return ("AL_INVALID_OPERATION");
[4504]380    case AL_OUT_OF_MEMORY:
[5930]381      return ("AL_OUT_OF_MEMORY");
[4504]382    };
383}
384
[4959]385void SoundEngine::listDevices()
386{
387  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
388}
389
[4504]390/*
391void SoundEngine::PrintALCErrorString(ALenum err)
392{
393  switch(err)
394    {
395    case ALC_NO_ERROR:
396      PRINTF(4)("AL_NO_ERROR\n");
397      break;
398
399    case ALC_INVALID_DEVICE:
400      PRINTF(2)("ALC_INVALID_DEVICE\n");
401      break;
402
403    case ALC_INVALID_CONTEXT:
404      PRINTF(2)("ALC_INVALID_CONTEXT\n");
405      break;
406
407    case ALC_INVALID_ENUM:
408      PRINTF(2)("ALC_INVALID_ENUM\n");
409      break;
410
411    case ALC_INVALID_VALUE:
412      PRINTF(2)("ALC_INVALID_VALUE\n");
413      break;
414
415    case ALC_OUT_OF_MEMORY:
416      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
417      break;
418    };
419}
420*/
Note: See TracBrowser for help on using the repository browser.