Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

better debug, (non-threaded again)

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