Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: reimplemented the list functions, as i did before in revision 5110.
This time, i looked out for the bugs, and i think i found one

@patrick: i know, that you do not want to code at the moment… :/ → see mail

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