Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: enumeration of the Sound-devices… but it does not really work here

File size: 13.7 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_WORLD_ENTITY
20
21#include "sound_engine.h"
22
23//#include <AL/alc.h> // maybe later
24#include "class_list.h"
25
26#include "p_node.h"
27#include "list.h"
28#include "resource_manager.h"
29#include "debug.h"
30#include "ini_parser.h"
31
32using namespace std;
33
34//////////////////
35/* SOUND-BUFFER */
36//////////////////
37/**
38 *  Creates a Soundbuffer out of an inputfile
39 * @param fileName The name of the File
40*/
41SoundBuffer::SoundBuffer(const char* fileName)
42{
43  this->setClassID(CL_SOUND_BUFFER, "SoundBuffer");
44  this->setName(fileName);
45
46  SoundEngine::getInstance()->addBuffer(this);
47
48  ALenum format;
49  ALvoid* data;
50  ALsizei freq;
51
52  ALenum result;
53
54  // generate a Buffer
55  alGenBuffers(1, &this->bufferID);
56  if ((result = alGetError()) != AL_NO_ERROR)
57    SoundEngine::PrintALErrorString(result);
58
59  // read in the wav data
60  /* according to http://www.edenwaith.com/products/pige/tutorials/openal.php the alutLoadWAVFile differs from platform to platform*/
61#ifdef __APPLE__
62  alutLoadWAVFile((ALbyte*)fileName, &format, &data, &this->size, &freq);
63#elif defined __WIN32__
64  alutLoadWAVFile(fileName, &format, &data, &size, &freq, &this->loop);
65#else
66  alutLoadWAVFile((ALbyte*)fileName, &format, &data, &this->size, &freq, &this->loop);
67#endif
68  if ((result = alGetError()) != AL_NO_ERROR)
69    SoundEngine::PrintALErrorString(result);
70
71  // send the loaded wav data to the buffer
72  alBufferData(this->bufferID, format, data, this->size, freq);
73  if ((result = alGetError()) != AL_NO_ERROR)
74    SoundEngine::PrintALErrorString(result);
75
76  // remove the wav data (redundant)
77  alutUnloadWAV(format, data, this->size, freq);
78  if ((result = alGetError()) != AL_NO_ERROR)
79    SoundEngine::PrintALErrorString(result);
80}
81
82SoundBuffer::~SoundBuffer()
83{
84//  SoundEngine::getInstance()->removeBuffer(this);
85  alDeleteBuffers(1, &this->bufferID);
86}
87
88//////////////////
89/* SOUND-SOURCE */
90//////////////////
91/**
92 *  creates a SoundSource at position sourceNode with the SoundBuffer buffer
93*/
94SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer)
95{
96  this->setClassID(CL_SOUND_SOURCE, "SoundSource");
97
98  ALenum result;
99
100  // adding the Source to the SourcesList of the SoundEngine
101  SoundEngine::getInstance()->addSource(this);
102
103  this->buffer = buffer;
104  this->sourceNode = sourceNode;
105
106  alGenSources(1, &this->sourceID);
107  if ((result = alGetError()) != AL_NO_ERROR)
108    SoundEngine::PrintALErrorString(result);
109  if (this->buffer != NULL)
110    alSourcei (this->sourceID, AL_BUFFER,   this->buffer->getID());
111  alSourcef (this->sourceID, AL_PITCH,    1.0      );
112  alSourcef (this->sourceID, AL_GAIN,     SoundEngine::getInstance()->getEffectsVolume() );
113  alSourcei (sourceID, AL_LOOPING,  AL_FALSE     );
114}
115
116/**
117 *  deletes a SoundSource
118*/
119SoundSource::~SoundSource()
120{
121  //SoundEngine::getInstance()->removeSource(this);
122  alDeleteSources(1, &this->sourceID);
123}
124
125/**
126 *  Plays back a SoundSource
127*/
128void SoundSource::play()
129{
130  alSourcePlay(this->sourceID);
131}
132
133/**
134 * Plays back buffer on this Source
135 * @param buffer the buffer to play back on this Source
136 */
137void SoundSource::play(const SoundBuffer* buffer)
138{
139  alSourceStop(this->sourceID);
140  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
141  alSourcePlay(this->sourceID);
142
143  if (unlikely(this->buffer != NULL))
144    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
145}
146
147/**
148 *  Stops playback of a SoundSource
149*/
150void SoundSource::stop()
151{
152  alSourceStop(this->sourceID);
153}
154
155/**
156 *  Pauses Playback of a SoundSource
157*/
158void SoundSource::pause()
159{
160  alSourcePause(this->sourceID);
161}
162
163/**
164 *  Rewinds Playback of a SoundSource
165*/
166void SoundSource::rewind()
167{
168  alSourceRewind(this->sourceID);
169}
170
171/**
172 *  sets the RolloffFactor of the Sound emitted from the SoundSource
173 * @param rolloffFactor The Factor described
174
175   this tells openAL how fast the Sounds decay outward from the Source
176*/
177void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
178{
179  alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
180}
181
182
183
184//////////////////
185/* SOUND-ENGINE */
186//////////////////
187/**
188 *  standard constructor
189*/
190SoundEngine::SoundEngine ()
191{
192  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
193  this->setName("SoundEngine");
194
195  this->listener = NULL;
196  this->bufferList = NULL;
197  this->sourceList = NULL;
198}
199
200/**
201 *  the singleton reference to this class
202*/
203SoundEngine* SoundEngine::singletonRef = NULL;
204
205/**
206 *  standard deconstructor
207 */
208SoundEngine::~SoundEngine ()
209{
210  // deleting all the SoundSources
211  if(this->sourceList != NULL)
212  {
213    tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
214    SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
215    while (enumSource)
216    {
217        delete enumSource;
218        enumSource = (SoundSource*)sourceIterator->nextElement();
219    }
220    delete sourceIterator;
221  }
222
223  // deleting all the SoundBuffers
224  if (this->bufferList != NULL)
225  {
226    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
227    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
228    while (enumBuffer)
229    {
230      ResourceManager::getInstance()->unload(enumBuffer);
231      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
232    }
233    delete bufferIterator;
234  }
235
236  // removing openAL from AudioResource
237  alutExit();
238
239  SoundEngine::singletonRef = NULL;
240}
241
242/**
243 * loads the settings of the SoundEngine from an ini-file
244 * @param iniParser the IniParser of the inifile
245 */
246void SoundEngine::loadSettings(IniParser* iniParser)
247{
248  const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80");
249  this->musicVolume = atof(musicVolume)/100.0;
250
251  const char* effectsVolume = iniParser->getVar(CONFIG_NAME_EFFECTS_VOLUME, CONFIG_SECTION_AUDIO, "80");
252  this->effectsVolume = atof(effectsVolume)/100.0;
253}
254
255/**
256 *  creates a new SoundSource.
257 * @param fileName The Name to load the SoundBuffer from
258 * @param sourceNode The sourceNode to bind this SoundSource to.
259 * @returns The newly created SoundSource
260
261   acctualy this is nothing more than a wrapper around the ResourceManager.
262*/
263SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
264{
265  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
266}
267
268/**
269 *  Sets the doppler values of openAL
270 * @param dopplerFactor the extent of the doppler-effect
271 * @param dopplerVelocity the Speed the sound travels
272*/
273void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
274{
275  alDopplerFactor(dopplerFactor);
276  alDopplerVelocity(dopplerVelocity);
277}
278
279
280/**
281 *  adds a SoundBuffer to the bufferList of the SoundEngine
282 * @param buffer The buffer to add to the bufferList
283*/
284void SoundEngine::addBuffer(SoundBuffer* buffer)
285{
286  if (unlikely(this->bufferList == NULL))
287    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
288}
289
290/**
291 *  removes a SoundBuffer from the bufferList of the SoundEngine
292 * @param buffer The buffer to delete from the SoundEngine
293*/
294void SoundEngine::removeBuffer(SoundBuffer* buffer)
295{
296  // look if there are any sources that have the buffer still loaded
297  tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
298  SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
299  while (enumSource)
300    {
301      if (buffer == enumSource->getBuffer())
302        delete enumSource;
303      enumSource = (SoundSource*)sourceIterator->nextElement();
304    }
305  delete sourceIterator;
306}
307
308/**
309 * adds a SoundSource to the sourceList of the SoundEngine
310 * @param source The source to add to the sourceList
311*/
312void SoundEngine::addSource(SoundSource* source)
313{
314  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
315}
316
317/**
318 *  updates all The positions, Directions and Velocities of all Sounds
319*/
320void SoundEngine::update()
321{
322
323  // updating the Listeners Position
324  if (likely(this->listener != NULL))
325    {
326      alListener3f(AL_POSITION,
327                   this->listener->getAbsCoor().x,
328                   this->listener->getAbsCoor().y,
329                   this->listener->getAbsCoor().z);
330      alListener3f(AL_VELOCITY,
331                   this->listener->getVelocity().x,
332                   this->listener->getVelocity().y,
333                   this->listener->getVelocity().z);
334      Vector absDirV = this->listener->getAbsDirV();
335      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
336      alListenerfv(AL_ORIENTATION, orientation);
337    }
338  else
339    PRINTF(2)("no listener defined\n");
340
341  // updating all the Sources positions
342  if (likely(this->sourceList != NULL))
343  {
344    tIterator<BaseObject>* iterator = this->sourceList->getIterator();
345    SoundSource* enumSource = (SoundSource*)iterator->firstElement();
346    while (enumSource)
347    {
348      if (likely(enumSource->getNode() != NULL))
349      {
350        alSource3f(enumSource->getID(), AL_POSITION,
351                   enumSource->getNode()->getAbsCoor().x,
352                   enumSource->getNode()->getAbsCoor().y,
353                   enumSource->getNode()->getAbsCoor().z);
354        alSource3f(enumSource->getID(), AL_VELOCITY,
355                   enumSource->getNode()->getVelocity().x,
356                   enumSource->getNode()->getVelocity().y,
357                   enumSource->getNode()->getVelocity().z);
358      }
359      enumSource = (SoundSource*)iterator->nextElement();
360    }
361    delete iterator;
362  }
363}
364
365/**
366 *  Removes all the Buffers that are not anymore needed by any Sources
367*/
368void SoundEngine::flushUnusedBuffers()
369{
370  if(this->sourceList && this->bufferList)
371  {
372    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
373    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
374    while (enumBuffer)
375    {
376      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
377      SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
378      while (enumSource)
379      {
380        if (enumBuffer == enumSource->getBuffer())
381          break;
382        enumSource = (SoundSource*)sourceIterator->nextElement();
383      }
384      delete sourceIterator;
385      if (enumSource == NULL)
386        ResourceManager::getInstance()->unload(enumBuffer);
387      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
388    }
389    delete bufferIterator;
390  }
391}
392
393/**
394 * flushes all the Buffers
395 * deletes them from the BufferList, and also removes them via the ResourceManager.
396 */
397void SoundEngine::flushAllBuffers()
398{
399  if (this->bufferList)
400  {
401    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
402    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
403    while (enumBuffer)
404    {
405      ResourceManager::getInstance()->unload(enumBuffer, RP_LEVEL);
406      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
407    }
408    delete bufferIterator;
409  }
410}
411
412/**
413 * deletes all the Sources.
414 */
415void SoundEngine::flushAllSources()
416{
417  if (this->sourceList)
418  {
419    tIterator<BaseObject>* Iterator = this->sourceList->getIterator();
420    SoundSource* enumSource = (SoundSource*)Iterator->firstElement();
421    while (enumSource)
422    {
423      delete enumSource;
424      enumSource = (SoundSource*)Iterator->nextElement();
425    }
426    delete Iterator;
427  }
428}
429
430/**
431 *  initializes Audio in general
432*/
433bool SoundEngine::initAudio()
434{
435  ALenum result;
436  PRINTF(3)("Initialisazing openAL sound engine\n");
437  const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
438  const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
439  const char* devWalk = deviceList;
440  if (alcIsExtensionPresent(NULL, (ALubyte*)"ALC_ENUMERATION_EXT") == AL_TRUE) { // try out enumeration extension
441    printf("Enumeration-extension found\n");
442
443    printf("Default device: %s\n", defaultDevice);
444    do
445    {
446      printf("%s\n", devWalk);
447      devWalk += strlen(devWalk)+1;
448    } while (devWalk[0] != '\0');
449
450
451  }
452
453
454  alutInit(NULL, 0);
455  if ((result = alGetError()) != AL_NO_ERROR)
456    SoundEngine::PrintALErrorString(result);
457
458  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
459}
460
461/**
462 *  Transforms AL-errors into something readable
463 * @param err The error found
464*/
465void SoundEngine::PrintALErrorString(ALenum err)
466{
467  switch(err)
468    {
469    case AL_NO_ERROR:
470      PRINTF(4)("AL_NO_ERROR\n");
471      break;
472
473    case AL_INVALID_NAME:
474      PRINTF(2)("AL_INVALID_NAME\n");
475      break;
476
477    case AL_INVALID_ENUM:
478      PRINTF(2)("AL_INVALID_ENUM\n");
479      break;
480
481    case AL_INVALID_VALUE:
482      PRINTF(2)("AL_INVALID_VALUE\n");
483      break;
484
485    case AL_INVALID_OPERATION:
486      PRINTF(2)("AL_INVALID_OPERATION\n");
487      break;
488
489    case AL_OUT_OF_MEMORY:
490      PRINTF(2)("AL_OUT_OF_MEMORY\n");
491      break;
492    };
493}
494
495void SoundEngine::listDevices()
496{
497
498  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
499}
500
501/*
502void SoundEngine::PrintALCErrorString(ALenum err)
503{
504  switch(err)
505    {
506    case ALC_NO_ERROR:
507      PRINTF(4)("AL_NO_ERROR\n");
508      break;
509
510    case ALC_INVALID_DEVICE:
511      PRINTF(2)("ALC_INVALID_DEVICE\n");
512      break;
513
514    case ALC_INVALID_CONTEXT:
515      PRINTF(2)("ALC_INVALID_CONTEXT\n");
516      break;
517
518    case ALC_INVALID_ENUM:
519      PRINTF(2)("ALC_INVALID_ENUM\n");
520      break;
521
522    case ALC_INVALID_VALUE:
523      PRINTF(2)("ALC_INVALID_VALUE\n");
524      break;
525
526    case ALC_OUT_OF_MEMORY:
527      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
528      break;
529    };
530}
531*/
Note: See TracBrowser for help on using the repository browser.