Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: the SoundEngine loads the Sources

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