Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6694 was 6222, checked in by bensch, 19 years ago

orxonox/trunk: merged the christmas branche to the trunk
merged with command:
svn merge -r6165:HEAD christmas_branche/ ../trunk/
no conflicts

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
[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;
[4504]53}
54
55/**
[4836]56 *  the singleton reference to this class
[4504]57*/
58SoundEngine* SoundEngine::singletonRef = NULL;
59
60/**
[4836]61 *  standard deconstructor
[5293]62 */
[4597]63SoundEngine::~SoundEngine ()
[4504]64{
65  // deleting all the SoundSources
[4960]66  if(this->sourceList != NULL)
67  {
[5779]68    while (this->sourceList->size() > 0)
69      delete dynamic_cast<SoundSource*>(this->sourceList->front());
[4960]70  }
[4504]71
[5930]72  while(!this->ALSources.empty())
73  {
74    alDeleteSources(1, &this->ALSources.top());
75    this->ALSources.pop();
76  }
77
[4504]78  // deleting all the SoundBuffers
[4960]79  if (this->bufferList != NULL)
80  {
[5779]81    while(this->bufferList->size() > 0)
82      ResourceManager::getInstance()->unload(dynamic_cast<SoundBuffer*>(this->bufferList->front()));
[4960]83  }
[5293]84
[4504]85  // removing openAL from AudioResource
[5473]86  //! @todo this should be terminated through alc
87  //alutExit();
[5293]88
89  SoundEngine::singletonRef = NULL;
[4504]90}
91
92/**
[4985]93 * loads the settings of the SoundEngine from an ini-file
94 * @param iniParser the IniParser of the inifile
95 */
96void SoundEngine::loadSettings(IniParser* iniParser)
97{
[5953]98  const char* channels = iniParser->getVar(CONFIG_NAME_AUDIO_CHANNELS, CONFIG_SECTION_AUDIO, "32");
99  this->maxSourceCount = atoi(channels);
[4985]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
[5930]132void SoundEngine::popALSource(ALuint& source)
[4504]133{
[5930]134  if (source != 0)
135    return;
136  else
137  {
[4504]138
[5930]139    /// @TODO try to create more sources if needed
140    if (!this->ALSources.empty())
[4504]141    {
[5930]142      source = this->ALSources.top();
143      this->ALSources.pop();
[4504]144    }
[5779]145  }
[4504]146}
147
148
149/**
[4836]150 *  updates all The positions, Directions and Velocities of all Sounds
[4504]151*/
[4746]152void SoundEngine::update()
[4504]153{
154
155  // updating the Listeners Position
156  if (likely(this->listener != NULL))
157    {
158      alListener3f(AL_POSITION,
[4597]159                   this->listener->getAbsCoor().x,
160                   this->listener->getAbsCoor().y,
161                   this->listener->getAbsCoor().z);
[4504]162      alListener3f(AL_VELOCITY,
[4597]163                   this->listener->getVelocity().x,
164                   this->listener->getVelocity().y,
165                   this->listener->getVelocity().z);
[4504]166      Vector absDirV = this->listener->getAbsDirV();
167      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
168      alListenerfv(AL_ORIENTATION, orientation);
169    }
170  else
171    PRINTF(2)("no listener defined\n");
172
173  // updating all the Sources positions
[5930]174  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
[4960]175  {
[5885]176    list<BaseObject*>::const_iterator sourceIT;
[5779]177    SoundSource* source;
178    for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
[4504]179    {
[5779]180      source = static_cast<SoundSource*>(*sourceIT);
[5930]181      if (source->isPlaying())
[4504]182      {
[6073]183        int play = 0x000;
[5930]184        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
185        if(play == AL_PLAYING)
186        {
187          if (likely(source->getNode() != NULL))
188          {
189            alSource3f(source->getID(), AL_POSITION,
190                       source->getNode()->getAbsCoor().x,
191                       source->getNode()->getAbsCoor().y,
192                       source->getNode()->getAbsCoor().z);
193            alSource3f(source->getID(), AL_VELOCITY,
194                       source->getNode()->getVelocity().x,
195                       source->getNode()->getVelocity().y,
196                       source->getNode()->getVelocity().z);
197          }
198
199        }
200        else
201        {
202          source->stop();
203        }
[4504]204      }
205    }
[4960]206  }
[4504]207}
208
209/**
[4836]210 *  Removes all the Buffers that are not anymore needed by any Sources
[4504]211*/
[4746]212void SoundEngine::flushUnusedBuffers()
[4504]213{
[5819]214  /// FIXME
[5779]215/*  if(this->sourceList && this->bufferList)
[4960]216  {
217    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
[5115]218    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
[4960]219    while (enumBuffer)
[4504]220    {
[4960]221      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
[5115]222      SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
[4504]223      while (enumSource)
[4960]224      {
225        if (enumBuffer == enumSource->getBuffer())
226          break;
227        enumSource = (SoundSource*)sourceIterator->nextElement();
228      }
[4504]229      delete sourceIterator;
230      if (enumSource == NULL)
[4597]231        ResourceManager::getInstance()->unload(enumBuffer);
[4960]232      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]233    }
[4960]234    delete bufferIterator;
[5779]235}*/ /// FIXME
[4504]236}
237
238/**
[5293]239 * flushes all the Buffers
240 * deletes them from the BufferList, and also removes them via the ResourceManager.
241 */
[4746]242void SoundEngine::flushAllBuffers()
[4504]243{
[4960]244  if (this->bufferList)
245  {
[5779]246    while (this->bufferList->size() > 0)
247      ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front()), RP_LEVEL);
[4960]248  }
[4504]249}
250
251/**
[5293]252 * deletes all the Sources.
[4830]253 */
254void SoundEngine::flushAllSources()
255{
[4960]256  if (this->sourceList)
[4830]257  {
[5779]258    while(this->sourceList->size() > 0)
259      delete this->sourceList->front();
[4830]260  }
261}
262
263/**
[4836]264 *  initializes Audio in general
[4504]265*/
[4746]266bool SoundEngine::initAudio()
[4504]267{
268  ALenum result;
[5385]269  PRINTF(3)("Initialisazing openAL sound engine\n");
270  const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
271  const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
272  const char* devWalk = deviceList;
[5416]273//  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
274{ // try out enumeration extension
[5386]275    PRINTF(3)("Enumeration-extension found\n");
[4504]276
[5386]277    PRINTF(3)("Default device: %s\n", defaultDevice);
[5385]278    do
279    {
[5386]280      PRINTF(3)("%s\n", devWalk);
[5385]281      devWalk += strlen(devWalk)+1;
282    } while (devWalk[0] != '\0');
[4504]283
[5385]284
[5982]285
[5385]286  }
[5819]287  // INITIALIZING THE DEVICE:
[5996]288
[6076]289#ifdef AL_VERSION_1_1
[5915]290  ALCchar deviceName[] =
[5996]291
[5819]292#ifdef __WIN32__
[5915]293      "Direct3D";
[5819]294#else
[5915]295      "'( ( devices '( native null ) ) )";
[5819]296#endif
297  //
298  this->device = alcOpenDevice(deviceName);
[5385]299
[5819]300  this->context = alcCreateContext(this->device, NULL);
301
[5996]302  alcMakeContextCurrent(this->context);
[6076]303#else
304  alutInit(0, NULL);
305#endif
[5819]306
[4504]307  if ((result = alGetError()) != AL_NO_ERROR)
[5930]308    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
[4504]309
310  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
311}
312
[5917]313
[4504]314/**
[5917]315 * Allocates openAL sources
316 * @param count how many sources to allocate
317 * @returns true on success, false if at least one source could not be allocated
318 */
319bool SoundEngine::allocateSources(unsigned int count)
320{
321  ALenum result;
[5930]322  // Setting default values.
323  for (unsigned int i = 0; i < count; i++)
[5917]324  {
[5930]325    ALuint source;
[5917]326
[5930]327    alGenSources(1, &source);
328    if ((result = alGetError()) != AL_NO_ERROR)
329      PRINTF(1)("Error Generating Sources: '%s'\n", SoundEngine::getALErrorString(result));
[5917]330
[5930]331    alSourcef (source, AL_PITCH,    1.0      );
332    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
333    alSourcei (source, AL_LOOPING,  AL_FALSE );
334    this->ALSources.push(source);
[5917]335  }
336  return true;
337}
338
339/**
[4836]340 *  Transforms AL-errors into something readable
341 * @param err The error found
[4504]342*/
[5930]343const char* SoundEngine::getALErrorString(ALenum err)
[4504]344{
345  switch(err)
346    {
347    case AL_NO_ERROR:
[5930]348      return ("AL_NO_ERROR");
[4504]349    case AL_INVALID_NAME:
[5930]350      return ("AL_INVALID_NAME");
[4504]351    case AL_INVALID_ENUM:
[5930]352      return ("AL_INVALID_ENUM");
[4504]353    case AL_INVALID_VALUE:
[5930]354      return ("AL_INVALID_VALUE");
[4504]355    case AL_INVALID_OPERATION:
[5930]356      return ("AL_INVALID_OPERATION");
[4504]357    case AL_OUT_OF_MEMORY:
[5930]358      return ("AL_OUT_OF_MEMORY");
[4504]359    };
360}
361
[4959]362void SoundEngine::listDevices()
363{
364  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
365}
366
[4504]367/*
368void SoundEngine::PrintALCErrorString(ALenum err)
369{
370  switch(err)
371    {
372    case ALC_NO_ERROR:
373      PRINTF(4)("AL_NO_ERROR\n");
374      break;
375
376    case ALC_INVALID_DEVICE:
377      PRINTF(2)("ALC_INVALID_DEVICE\n");
378      break;
379
380    case ALC_INVALID_CONTEXT:
381      PRINTF(2)("ALC_INVALID_CONTEXT\n");
382      break;
383
384    case ALC_INVALID_ENUM:
385      PRINTF(2)("ALC_INVALID_ENUM\n");
386      break;
387
388    case ALC_INVALID_VALUE:
389      PRINTF(2)("ALC_INVALID_VALUE\n");
390      break;
391
392    case ALC_OUT_OF_MEMORY:
393      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
394      break;
395    };
396}
397*/
Note: See TracBrowser for help on using the repository browser.