Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merge the ObjectManager to the trunk
merged with command:
svn merge -r6082:HEAD objectmanager/ ../trunk/

conflicts resolution was easy this time :)
but specially merged the world to network_world

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