Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: secured the sources-list with a fancy mutex. This should prevent source-stack-errors… (if they would ever occure )

File size: 12.3 KB
Line 
1/*
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
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND
20
21#include "sound_engine.h"
22
23#include "class_list.h"
24
25#include "p_node.h"
26#include "util/loading/resource_manager.h"
27#include "debug.h"
28#include "util/preferences.h"
29#include "globals.h"
30
31using namespace std;
32
33
34//////////////////
35/* SOUND-ENGINE */
36//////////////////
37/**
38 *  standard constructor
39*/
40SoundEngine::SoundEngine ()
41{
42  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
43  this->setName("SoundEngine");
44
45  this->listener = NULL;
46  this->bufferList = NULL;
47  this->sourceList = NULL;
48
49  this->device = NULL;
50  this->context = NULL;
51
52  this->maxSourceCount = 32;
53
54  this->effectsVolume = .80;
55  this->musicVolume = .75;
56
57  this->sourceMutex = SDL_CreateMutex();
58}
59
60/**
61 *  the singleton reference to this class
62*/
63SoundEngine* SoundEngine::singletonRef = NULL;
64
65/**
66 *  standard deconstructor
67 */
68SoundEngine::~SoundEngine ()
69{
70  // deleting all the SoundSources
71  if(this->sourceList != NULL)
72  {
73    while (this->sourceList->size() > 0)
74      delete static_cast<SoundSource*>(this->sourceList->front());
75  }
76
77  while(!this->ALSources.empty())
78  {
79    if (alIsSource(this->ALSources.top()))
80    {
81      alDeleteSources(1, &this->ALSources.top());
82      SoundEngine::checkError("Deleting Source", __LINE__);
83    }
84    else
85      PRINTF(1)("%d is not a Source\n", this->ALSources.top());
86
87    this->ALSources.pop();
88  }
89
90  // deleting all the SoundBuffers
91  if (this->bufferList != NULL)
92  {
93    while(this->bufferList->size() > 0)
94      ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front()));
95  }
96
97  // removing openAL from AudioResource
98  //! @todo this should be terminated through alc
99  //alutExit();
100
101  SDL_DestroyMutex(this->sourceMutex);
102
103  SoundEngine::singletonRef = NULL;
104}
105
106/**
107 * loads the settings of the SoundEngine from an ini-file
108 */
109void SoundEngine::loadSettings()
110{
111  MultiType channels = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_AUDIO_CHANNELS, "32");
112  this->maxSourceCount = channels.getInt();
113
114  MultiType effectsVolume = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_EFFECTS_VOLUME, "80");
115  this->effectsVolume = effectsVolume.getFloat()/100.0;
116
117  MultiType musicVolume = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_MUSIC_VOLUME, "75");
118  this->musicVolume = musicVolume.getFloat()/100.0;
119}
120
121/**
122 *  creates a new SoundSource.
123 * @param fileName The Name to load the SoundBuffer from
124 * @param sourceNode The sourceNode to bind this SoundSource to.
125 * @returns The newly created SoundSource
126
127   acctualy this is nothing more than a wrapper around the ResourceManager.
128*/
129SoundSource* SoundEngine::createSource(const std::string& fileName, PNode* sourceNode)
130{
131  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
132}
133
134/**
135 *  Sets the doppler values of openAL
136 * @param dopplerFactor the extent of the doppler-effect
137 * @param dopplerVelocity the Speed the sound travels
138*/
139void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
140{
141  alDopplerFactor(dopplerFactor);
142  this->checkError("Setting Doppler Factor", __LINE__);
143
144  alDopplerVelocity(dopplerVelocity);
145  this->checkError("Setting Doppler Velocity", __LINE__);
146}
147
148
149void SoundEngine::popALSource(ALuint& source)
150{
151  assert (source == 0);
152
153  /// @TODO try to create more sources if needed
154  if (!this->ALSources.empty())
155  {
156    SDL_mutexP(this->sourceMutex);
157    source = this->ALSources.top();
158    this->ALSources.pop();
159    SDL_mutexV(this->sourceMutex);
160    printf("Retrieve Source %d\n", source);
161  }
162}
163
164void SoundEngine::pushALSource(ALuint& source)
165{
166  if (source != 0)
167  {
168    SDL_mutexP(this->sourceMutex);
169    this->ALSources.push(source);
170    SDL_mutexV(this->sourceMutex);
171  }
172};
173
174
175/**
176 *  updates all The positions, Directions and Velocities of all Sounds
177*/
178void SoundEngine::update()
179{
180  // updating the Listeners Position
181  if (likely(this->listener != NULL))
182  {
183    alListener3f(AL_POSITION,
184                 this->listener->getAbsCoor().x,
185                 this->listener->getAbsCoor().y,
186                 this->listener->getAbsCoor().z);
187    alListener3f(AL_VELOCITY,
188                 this->listener->getVelocity().x,
189                 this->listener->getVelocity().y,
190                 this->listener->getVelocity().z);
191    Vector absDirV = this->listener->getAbsDirV();
192    ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
193    alListenerfv(AL_ORIENTATION, orientation);
194    SoundEngine::checkError("SoundEngine::update() - Listener Error", __LINE__);
195  }
196  else
197    PRINTF(2)("no listener defined\n");
198
199  // updating all the Sources positions
200  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
201  {
202    list<BaseObject*>::const_iterator sourceIT;
203    SoundSource* source;
204    for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
205    {
206      source = static_cast<SoundSource*>(*sourceIT);
207      if (source->isPlaying())
208      {
209        int play = 0x000;
210        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
211        if (DEBUG > 2)
212          SoundEngine::checkError("SoundEngine::update() Play", __LINE__);
213        if(play == AL_PLAYING)
214        {
215          if (likely(source->getNode() != NULL))
216          {
217            alSource3f(source->getID(), AL_POSITION,
218                       source->getNode()->getAbsCoor().x,
219                       source->getNode()->getAbsCoor().y,
220                       source->getNode()->getAbsCoor().z);
221            if (DEBUG > 2)
222              SoundEngine::checkError("SoundEngine::update() Set Source Position", __LINE__);
223            alSource3f(source->getID(), AL_VELOCITY,
224                       source->getNode()->getVelocity().x,
225                       source->getNode()->getVelocity().y,
226                       source->getNode()->getVelocity().z);
227            if (DEBUG > 2)
228              SoundEngine::checkError("SoundEngine::update() Set Source Velocity", __LINE__);
229          }
230        }
231        else
232        {
233          source->stop();
234        }
235      }
236    }
237  }
238  SoundEngine::checkError("SoundEngine::update()", __LINE__);
239}
240
241/**
242 *  Removes all the Buffers that are not anymore needed by any Sources
243*/
244void SoundEngine::flushUnusedBuffers()
245{
246  /// FIXME
247  /*  if(this->sourceList && this->bufferList)
248    {
249      tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
250      SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
251      while (enumBuffer)
252      {
253        tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
254        SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
255        while (enumSource)
256        {
257          if (enumBuffer == enumSource->getBuffer())
258            break;
259          enumSource = (SoundSource*)sourceIterator->nextElement();
260        }
261        delete sourceIterator;
262        if (enumSource == NULL)
263          ResourceManager::getInstance()->unload(enumBuffer);
264        enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
265      }
266      delete bufferIterator;
267  }*/ /// FIXME
268}
269
270/**
271 * flushes all the Buffers
272 * deletes them from the BufferList, and also removes them via the ResourceManager.
273 */
274void SoundEngine::flushAllBuffers()
275{
276  if (this->bufferList)
277  {
278    while (this->bufferList->size() > 0)
279      ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front()), RP_LEVEL);
280  }
281}
282
283/**
284 * deletes all the Sources.
285 */
286void SoundEngine::flushAllSources()
287{
288  if (this->sourceList)
289  {
290    while(this->sourceList->size() > 0)
291      delete this->sourceList->front();
292  }
293}
294
295/**
296 *  initializes Audio in general
297*/
298bool SoundEngine::initAudio()
299{
300  //   ALenum result;
301  //   PRINTF(3)("Initialisazing openAL sound engine\n");
302  //   const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
303  //   const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
304  //   const char* devWalk = deviceList;
305  //   //  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
306  //   { // try out enumeration extension
307  //     PRINTF(3)("Enumeration-extension found\n");
308  //
309  //     PRINTF(3)("Default device: %s\n", defaultDevice);
310  //     do
311  //     {
312  //       PRINTF(3)("%s\n", devWalk);
313  //       devWalk += strlen(devWalk)+1;
314  //     }
315  //     while (devWalk[0] != '\0');
316  //  }
317
318  // INITIALIZING THE DEVICE:
319  //   char deviceName[] =
320  //   #ifdef __WIN32__
321  //     "Direct3D";
322  //   #else
323  //     "'( ( devices '( native null ) ) )";
324  //   #endif
325
326  this->device = alcOpenDevice(NULL);
327  this->checkALCError("opening Device", __LINE__);
328
329  PRINTF(4)("Audio-Specifier: %s\n", (const char*)alcGetString(this->device, ALC_DEVICE_SPECIFIER));
330  PRINTF(4)("Audio-Extensions: %s\n", (const char*)alcGetString(this->device, ALC_EXTENSIONS));
331
332
333  this->context = alcCreateContext(this->device, NULL);
334  this->checkALCError("creating Context", __LINE__);
335
336  alcMakeContextCurrent(this->context);
337  this->checkALCError("making Context Current", __LINE__);
338  // #endif
339
340  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
341  this->allocateSources(this->maxSourceCount);
342  this->checkError("Allocating Sources", __LINE__);
343}
344
345
346/**
347 * Allocates openAL sources
348 * @param count how many sources to allocate
349 * @returns true on success, false if at least one source could not be allocated
350 */
351bool SoundEngine::allocateSources(unsigned int count)
352{
353  unsigned int failCount = 0;
354  for (unsigned int i = 0; i < count; i++)
355  {
356    ALuint source = 0;
357
358    alGenSources(1, &source);
359    this->checkError("allocate Source", __LINE__);
360    if (!alIsSource(source))
361    {
362      PRINTF(5)("not allocated Source\n");
363      failCount++;
364      continue;
365    }
366
367    alSourcef (source, AL_PITCH,    1.0      );
368    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
369    alSourcei (source, AL_LOOPING,  AL_FALSE );
370    this->ALSources.push(source);
371  }
372  if (failCount == 0)
373    return true;
374  else
375  {
376    PRINTF(2)("Failed to allocate %d of %d SoundSources\n", failCount, count);
377  }
378}
379
380bool SoundEngine::checkError(const std::string& error, unsigned int line)
381{
382  ALenum errorCode;
383  if ((errorCode = alGetError()) != AL_NO_ERROR)
384  {
385    PRINTF(1)("Error %s (line:%d): '%s'\n", error.c_str(), line, SoundEngine::getALErrorString(errorCode));
386    return false;
387  }
388  else
389    return true;
390}
391
392bool SoundEngine::checkALCError(const std::string& error, unsigned int line)
393{
394  ALenum errorCode;
395  if ((errorCode = alcGetError(this->device)) != ALC_NO_ERROR)
396  {
397    PRINTF(1)("Error %s (line:%d): '%s'\n", error.c_str(), line, SoundEngine::getALCErrorString(errorCode));
398    return false;
399  }
400  else
401    return true;
402}
403
404
405
406void SoundEngine::listDevices()
407{
408  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
409}
410
411/**
412 *  Transforms AL-errors into something readable
413 * @param err The error found
414*/
415const char* SoundEngine::getALErrorString(ALenum err)
416{
417  switch(err)
418  {
419    case AL_NO_ERROR:
420      return ("AL_NO_ERROR");
421    case AL_INVALID_NAME:
422      return ("AL_INVALID_NAME");
423    case AL_INVALID_ENUM:
424      return ("AL_INVALID_ENUM");
425    case AL_INVALID_VALUE:
426      return ("AL_INVALID_VALUE");
427    case AL_INVALID_OPERATION:
428      return ("AL_INVALID_OPERATION");
429    case AL_OUT_OF_MEMORY:
430      return ("AL_OUT_OF_MEMORY");
431  };
432}
433
434
435const char* SoundEngine::getALCErrorString(ALenum err)
436{
437  switch(err)
438  {
439    case ALC_NO_ERROR:
440      return ("AL_NO_ERROR");
441    case ALC_INVALID_DEVICE:
442      return ("ALC_INVALID_DEVICE");
443    case ALC_INVALID_CONTEXT:
444      return("ALC_INVALID_CONTEXT");
445    case ALC_INVALID_ENUM:
446      return("ALC_INVALID_ENUM");
447    case ALC_INVALID_VALUE:
448      return ("ALC_INVALID_VALUE");
449    case ALC_OUT_OF_MEMORY:
450      return("ALC_OUT_OF_MEMORY");
451  };
452}
Note: See TracBrowser for help on using the repository browser.