Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/lib/sound/sound_engine.cc @ 5886

Last change on this file since 5886 was 5879, checked in by bensch, 19 years ago

controll: woring on al 1.1 and 1.0

File size: 9.5 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*>::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*>::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
289#ifdef __WIN32__
290      "native";
291#else
292      "'( ( devices '( native arts null ) ) )";
293#endif
294  //
295  this->device = alcOpenDevice(deviceName);
296
297  this->context = alcCreateContext(this->device, NULL);
298
299  alcMakeContextCurrent(this->context);
300
301
302  if ((result = alGetError()) != AL_NO_ERROR)
303    SoundEngine::PrintALErrorString(result);
304
305  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
306}
307
308/**
309 *  Transforms AL-errors into something readable
310 * @param err The error found
311*/
312void SoundEngine::PrintALErrorString(ALenum err)
313{
314  switch(err)
315    {
316    case AL_NO_ERROR:
317      PRINTF(4)("AL_NO_ERROR\n");
318      break;
319
320    case AL_INVALID_NAME:
321      PRINTF(2)("AL_INVALID_NAME\n");
322      break;
323
324    case AL_INVALID_ENUM:
325      PRINTF(2)("AL_INVALID_ENUM\n");
326      break;
327
328    case AL_INVALID_VALUE:
329      PRINTF(2)("AL_INVALID_VALUE\n");
330      break;
331
332    case AL_INVALID_OPERATION:
333      PRINTF(2)("AL_INVALID_OPERATION\n");
334      break;
335
336    case AL_OUT_OF_MEMORY:
337      PRINTF(2)("AL_OUT_OF_MEMORY\n");
338      break;
339    };
340}
341
342void SoundEngine::listDevices()
343{
344
345  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
346}
347
348/*
349void SoundEngine::PrintALCErrorString(ALenum err)
350{
351  switch(err)
352    {
353    case ALC_NO_ERROR:
354      PRINTF(4)("AL_NO_ERROR\n");
355      break;
356
357    case ALC_INVALID_DEVICE:
358      PRINTF(2)("ALC_INVALID_DEVICE\n");
359      break;
360
361    case ALC_INVALID_CONTEXT:
362      PRINTF(2)("ALC_INVALID_CONTEXT\n");
363      break;
364
365    case ALC_INVALID_ENUM:
366      PRINTF(2)("ALC_INVALID_ENUM\n");
367      break;
368
369    case ALC_INVALID_VALUE:
370      PRINTF(2)("ALC_INVALID_VALUE\n");
371      break;
372
373    case ALC_OUT_OF_MEMORY:
374      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
375      break;
376    };
377}
378*/
Note: See TracBrowser for help on using the repository browser.