Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5924 was 5917, checked in by bensch, 19 years ago

orxonox/trunk: al-sound-source-allocation

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