Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: fixed a soundSource bug. The SoundSource did not release its alSource on Stop.

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  }
155}
156
157
158/**
159 *  updates all The positions, Directions and Velocities of all Sounds
160*/
161void SoundEngine::update()
162{
163  // updating the Listeners Position
164  if (likely(this->listener != NULL))
165  {
166    alListener3f(AL_POSITION,
167                 this->listener->getAbsCoor().x,
168                 this->listener->getAbsCoor().y,
169                 this->listener->getAbsCoor().z);
170    alListener3f(AL_VELOCITY,
171                 this->listener->getVelocity().x,
172                 this->listener->getVelocity().y,
173                 this->listener->getVelocity().z);
174    Vector absDirV = this->listener->getAbsDirV();
175    ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
176    alListenerfv(AL_ORIENTATION, orientation);
177    SoundEngine::checkError("SoundEngine::update() - Listener Error", __LINE__);
178  }
179  else
180    PRINTF(2)("no listener defined\n");
181
182  // updating all the Sources positions
183  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
184  {
185    list<BaseObject*>::const_iterator sourceIT;
186    SoundSource* source;
187    for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
188    {
189      source = static_cast<SoundSource*>(*sourceIT);
190      if (source->isPlaying())
191      {
192        int play = 0x000;
193        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
194        if (DEBUG > 2)
195          SoundEngine::checkError("SoundEngine::update() Play", __LINE__);
196        if(play == AL_PLAYING)
197        {
198          if (likely(source->getNode() != NULL))
199          {
200            alSource3f(source->getID(), AL_POSITION,
201                       source->getNode()->getAbsCoor().x,
202                       source->getNode()->getAbsCoor().y,
203                       source->getNode()->getAbsCoor().z);
204            if (DEBUG > 2)
205              SoundEngine::checkError("SoundEngine::update() Set Source Position", __LINE__);
206            alSource3f(source->getID(), AL_VELOCITY,
207                       source->getNode()->getVelocity().x,
208                       source->getNode()->getVelocity().y,
209                       source->getNode()->getVelocity().z);
210            if (DEBUG > 2)
211              SoundEngine::checkError("SoundEngine::update() Set Source Velocity", __LINE__);
212          }
213        }
214        else
215        {
216          source->stop();
217        }
218      }
219    }
220  }
221  SoundEngine::checkError("SoundEngine::update()", __LINE__);
222}
223
224/**
225 *  Removes all the Buffers that are not anymore needed by any Sources
226*/
227void SoundEngine::flushUnusedBuffers()
228{
229  /// FIXME
230  /*  if(this->sourceList && this->bufferList)
231    {
232      tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
233      SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
234      while (enumBuffer)
235      {
236        tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
237        SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
238        while (enumSource)
239        {
240          if (enumBuffer == enumSource->getBuffer())
241            break;
242          enumSource = (SoundSource*)sourceIterator->nextElement();
243        }
244        delete sourceIterator;
245        if (enumSource == NULL)
246          ResourceManager::getInstance()->unload(enumBuffer);
247        enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
248      }
249      delete bufferIterator;
250  }*/ /// FIXME
251}
252
253/**
254 * flushes all the Buffers
255 * deletes them from the BufferList, and also removes them via the ResourceManager.
256 */
257void SoundEngine::flushAllBuffers()
258{
259  if (this->bufferList)
260  {
261    while (this->bufferList->size() > 0)
262      ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front()), RP_LEVEL);
263  }
264}
265
266/**
267 * deletes all the Sources.
268 */
269void SoundEngine::flushAllSources()
270{
271  if (this->sourceList)
272  {
273    while(this->sourceList->size() > 0)
274      delete this->sourceList->front();
275  }
276}
277
278/**
279 *  initializes Audio in general
280*/
281bool SoundEngine::initAudio()
282{
283  //   ALenum result;
284  //   PRINTF(3)("Initialisazing openAL sound engine\n");
285  //   const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
286  //   const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
287  //   const char* devWalk = deviceList;
288  //   //  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
289  //   { // try out enumeration extension
290  //     PRINTF(3)("Enumeration-extension found\n");
291  //
292  //     PRINTF(3)("Default device: %s\n", defaultDevice);
293  //     do
294  //     {
295  //       PRINTF(3)("%s\n", devWalk);
296  //       devWalk += strlen(devWalk)+1;
297  //     }
298  //     while (devWalk[0] != '\0');
299  //  }
300
301  // INITIALIZING THE DEVICE:
302  //   char deviceName[] =
303  //   #ifdef __WIN32__
304  //     "Direct3D";
305  //   #else
306  //     "'( ( devices '( native null ) ) )";
307  //   #endif
308
309  this->device = alcOpenDevice(NULL);
310  this->checkALCError("opening Device", __LINE__);
311
312  PRINTF(4)("Audio-Specifier: %s\n", (const char*)alcGetString(this->device, ALC_DEVICE_SPECIFIER));
313  PRINTF(4)("Audio-Extensions: %s\n", (const char*)alcGetString(this->device, ALC_EXTENSIONS));
314
315
316  this->context = alcCreateContext(this->device, NULL);
317  this->checkALCError("creating Context", __LINE__);
318
319  alcMakeContextCurrent(this->context);
320  this->checkALCError("making Context Current", __LINE__);
321  // #endif
322
323  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
324  this->allocateSources(this->maxSourceCount);
325  this->checkError("Allocating Sources", __LINE__);
326}
327
328
329/**
330 * Allocates openAL sources
331 * @param count how many sources to allocate
332 * @returns true on success, false if at least one source could not be allocated
333 */
334bool SoundEngine::allocateSources(unsigned int count)
335{
336  unsigned int failCount = 0;
337  for (unsigned int i = 0; i < count; i++)
338  {
339    ALuint source = 0;
340
341    alGenSources(1, &source);
342    this->checkError("allocate Source", __LINE__);
343    if (!alIsSource(source))
344    {
345      PRINTF(5)("not allocated Source\n");
346      failCount++;
347      continue;
348    }
349
350    alSourcef (source, AL_PITCH,    1.0      );
351    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
352    alSourcei (source, AL_LOOPING,  AL_FALSE );
353    this->ALSources.push(source);
354  }
355  if (failCount == 0)
356    return true;
357  else
358  {
359    PRINTF(2)("Failed to allocate %d of %d SoundSources\n", failCount, count);
360  }
361}
362
363bool SoundEngine::checkError(const std::string& error, unsigned int line)
364{
365  ALenum errorCode;
366  if ((errorCode = alGetError()) != AL_NO_ERROR)
367  {
368    PRINTF(1)("Error %s (line:%d): '%s'\n", error.c_str(), line, SoundEngine::getALErrorString(errorCode));
369    return false;
370  }
371  else
372    return true;
373}
374
375bool SoundEngine::checkALCError(const std::string& error, unsigned int line)
376{
377  ALenum errorCode;
378  if ((errorCode = alcGetError(this->device)) != ALC_NO_ERROR)
379  {
380    PRINTF(1)("Error %s (line:%d): '%s'\n", error.c_str(), line, SoundEngine::getALCErrorString(errorCode));
381    return false;
382  }
383  else
384    return true;
385}
386
387
388
389void SoundEngine::listDevices()
390{
391  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
392}
393
394/**
395 *  Transforms AL-errors into something readable
396 * @param err The error found
397*/
398const char* SoundEngine::getALErrorString(ALenum err)
399{
400  switch(err)
401  {
402    case AL_NO_ERROR:
403      return ("AL_NO_ERROR");
404    case AL_INVALID_NAME:
405      return ("AL_INVALID_NAME");
406    case AL_INVALID_ENUM:
407      return ("AL_INVALID_ENUM");
408    case AL_INVALID_VALUE:
409      return ("AL_INVALID_VALUE");
410    case AL_INVALID_OPERATION:
411      return ("AL_INVALID_OPERATION");
412    case AL_OUT_OF_MEMORY:
413      return ("AL_OUT_OF_MEMORY");
414  };
415}
416
417
418const char* SoundEngine::getALCErrorString(ALenum err)
419{
420  switch(err)
421  {
422    case ALC_NO_ERROR:
423      return ("AL_NO_ERROR");
424    case ALC_INVALID_DEVICE:
425      return ("ALC_INVALID_DEVICE");
426    case ALC_INVALID_CONTEXT:
427      return("ALC_INVALID_CONTEXT");
428    case ALC_INVALID_ENUM:
429      return("ALC_INVALID_ENUM");
430    case ALC_INVALID_VALUE:
431      return ("ALC_INVALID_VALUE");
432    case ALC_OUT_OF_MEMORY:
433      return("ALC_OUT_OF_MEMORY");
434  };
435}
Note: See TracBrowser for help on using the repository browser.