Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/sound/sound_engine.cc @ 6774

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

orxonox/trunk: merged the christmas branche to the trunk
merged with command:
svn merge -r6165:HEAD christmas_branche/ ../trunk/
no conflicts

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