Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more debug

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