Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: music now plays back, as one would expect, and the ReosurceManager handles ogg's.
Also the naming has changed a bit, and doxygen tags are complete in ogg_player.h/cc

File size: 12.5 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"
30
31using namespace std;
32
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      );
112  alSourcef (this->sourceID, AL_GAIN,     1.0      );
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{
139  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
140  alSourcePlay(this->sourceID);
141
142  if (unlikely(this->buffer != NULL))
143    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
144}
145
146/**
[4836]147 *  Stops playback of a SoundSource
[4504]148*/
149void SoundSource::stop()
150{
151  alSourceStop(this->sourceID);
152}
153
154/**
[4836]155 *  Pauses Playback of a SoundSource
[4504]156*/
157void SoundSource::pause()
158{
159  alSourcePause(this->sourceID);
160}
161
162/**
[4836]163 *  Rewinds Playback of a SoundSource
[4504]164*/
165void SoundSource::rewind()
166{
167  alSourceRewind(this->sourceID);
168}
169
170/**
[4836]171 *  sets the RolloffFactor of the Sound emitted from the SoundSource
172 * @param rolloffFactor The Factor described
[4504]173
174   this tells openAL how fast the Sounds decay outward from the Source
175*/
176void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
177{
178  alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
179}
180
181
182
183//////////////////
184/* SOUND-ENGINE */
185//////////////////
186/**
[4836]187 *  standard constructor
[4504]188*/
[4597]189SoundEngine::SoundEngine ()
[4504]190{
[4597]191  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
192  this->setName("SoundEngine");
193
[4504]194  this->listener = NULL;
[4960]195  this->bufferList = NULL;
196  this->sourceList = NULL;
[4504]197}
198
199/**
[4836]200 *  the singleton reference to this class
[4504]201*/
202SoundEngine* SoundEngine::singletonRef = NULL;
203
204/**
[4836]205 *  standard deconstructor
[4504]206
207*/
[4597]208SoundEngine::~SoundEngine ()
[4504]209{
210  SoundEngine::singletonRef = NULL;
211
212  // deleting all the SoundSources
[4960]213  if(this->sourceList != NULL)
214  {
215    tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
216    SoundSource* enumSource = (SoundSource*)sourceIterator->nextElement();
217    while (enumSource)
[4504]218    {
[4960]219        delete enumSource;
220        enumSource = (SoundSource*)sourceIterator->nextElement();
[4504]221    }
[4960]222    delete sourceIterator;
223  }
[4504]224
225  // deleting all the SoundBuffers
[4960]226  if (this->bufferList != NULL)
227  {
228    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
229    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
230    while (enumBuffer)
[4504]231    {
232      ResourceManager::getInstance()->unload(enumBuffer);
[4960]233      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]234    }
[4960]235    delete bufferIterator;
236  }
[4504]237  // removing openAL from AudioResource
238  alutExit();
239}
240
241/**
[4836]242 *  creates a new SoundSource.
243 * @param fileName The Name to load the SoundBuffer from
244 * @param sourceNode The sourceNode to bind this SoundSource to.
245 * @returns The newly created SoundSource
[4504]246
247   acctualy this is nothing more than a wrapper around the ResourceManager.
248*/
249SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
250{
[4885]251  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
[4504]252}
253
254/**
[4836]255 *  Sets the doppler values of openAL
256 * @param dopplerFactor the extent of the doppler-effect
257 * @param dopplerVelocity the Speed the sound travels
[4504]258*/
259void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
260{
261  alDopplerFactor(dopplerFactor);
262  alDopplerVelocity(dopplerVelocity);
263}
264
265
266/**
[4836]267 *  adds a SoundBuffer to the bufferList of the SoundEngine
268 * @param buffer The buffer to add to the bufferList
[4504]269*/
270void SoundEngine::addBuffer(SoundBuffer* buffer)
271{
[4960]272  if (unlikely(this->bufferList == NULL))
273    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
[4504]274}
275
276/**
[4836]277 *  removes a SoundBuffer from the bufferList of the SoundEngine
278 * @param buffer The buffer to delete from the SoundEngine
[4504]279*/
280void SoundEngine::removeBuffer(SoundBuffer* buffer)
281{
282  // look if there are any sources that have the buffer still loaded
[4960]283  tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
284  SoundSource* enumSource = (SoundSource*)sourceIterator->nextElement();
[4504]285  while (enumSource)
286    {
287      if (buffer == enumSource->getBuffer())
[4597]288        delete enumSource;
[4960]289      enumSource = (SoundSource*)sourceIterator->nextElement();
[4504]290    }
291  delete sourceIterator;
292}
293
294/**
[4836]295 *  adds a SoundSource to the sourceList of the SoundEngine
296 * @param source The source to add to the sourceList
[4504]297*/
298void SoundEngine::addSource(SoundSource* source)
299{
[4960]300  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
[4504]301}
302
303/**
[4836]304 *  updates all The positions, Directions and Velocities of all Sounds
[4504]305*/
[4746]306void SoundEngine::update()
[4504]307{
308
309  // updating the Listeners Position
310  if (likely(this->listener != NULL))
311    {
312      alListener3f(AL_POSITION,
[4597]313                   this->listener->getAbsCoor().x,
314                   this->listener->getAbsCoor().y,
315                   this->listener->getAbsCoor().z);
[4504]316      alListener3f(AL_VELOCITY,
[4597]317                   this->listener->getVelocity().x,
318                   this->listener->getVelocity().y,
319                   this->listener->getVelocity().z);
[4504]320      Vector absDirV = this->listener->getAbsDirV();
321      ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
322      alListenerfv(AL_ORIENTATION, orientation);
323    }
324  else
325    PRINTF(2)("no listener defined\n");
326
327  // updating all the Sources positions
[4960]328  if (likely(this->sourceList != NULL))
329  {
330    tIterator<BaseObject>* iterator = this->sourceList->getIterator();
331    SoundSource* enumSource = (SoundSource*)iterator->nextElement();
332    while (enumSource)
[4504]333    {
334      if (likely(enumSource->getNode()!=NULL))
335      {
[4597]336        alSource3f(enumSource->getID(), AL_POSITION,
337                   enumSource->getNode()->getAbsCoor().x,
338                   enumSource->getNode()->getAbsCoor().y,
339                   enumSource->getNode()->getAbsCoor().z);
340        alSource3f(enumSource->getID(), AL_VELOCITY,
341                   enumSource->getNode()->getVelocity().x,
342                   enumSource->getNode()->getVelocity().y,
343                   enumSource->getNode()->getVelocity().z);
[4504]344      }
[4960]345      enumSource = (SoundSource*)iterator->nextElement();
[4504]346    }
[4960]347    delete iterator;
348  }
[4504]349}
350
351/**
[4836]352 *  Removes all the Buffers that are not anymore needed by any Sources
[4504]353*/
[4746]354void SoundEngine::flushUnusedBuffers()
[4504]355{
[4960]356  if(this->sourceList && this->bufferList)
357  {
358    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
359    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
360    while (enumBuffer)
[4504]361    {
[4960]362      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
363      SoundSource* enumSource = (SoundSource*)sourceIterator->nextElement();
[4504]364      while (enumSource)
[4960]365      {
366        if (enumBuffer == enumSource->getBuffer())
367          break;
368        enumSource = (SoundSource*)sourceIterator->nextElement();
369      }
[4504]370      delete sourceIterator;
371      if (enumSource == NULL)
[4597]372        ResourceManager::getInstance()->unload(enumBuffer);
[4960]373      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]374    }
[4960]375    delete bufferIterator;
376  }
[4504]377}
378
379/**
[4836]380 *  SourceEngine::flushAllBuffers
[4504]381*/
[4746]382void SoundEngine::flushAllBuffers()
[4504]383{
[4960]384  if (this->bufferList)
385  {
386    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
387    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
388    while (enumBuffer)
[4504]389    {
390      ResourceManager::getInstance()->unload(enumBuffer, RP_LEVEL);
[4960]391      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]392    }
[4960]393    delete bufferIterator;
394  }
[4504]395}
396
397/**
[4836]398  *  SourceEngine::flushAllBuffers
[4830]399 */
400void SoundEngine::flushAllSources()
401{
[4960]402  if (this->sourceList)
[4830]403  {
[4960]404    tIterator<BaseObject>* Iterator = this->sourceList->getIterator();
405    SoundSource* enumSource = (SoundSource*)Iterator->nextElement();
406    while (enumSource)
407    {
408      delete enumSource;
409      enumSource = (SoundSource*)Iterator->nextElement();
410    }
411    delete Iterator;
[4830]412  }
413}
414
415/**
[4836]416 *  initializes Audio in general
[4504]417*/
[4746]418bool SoundEngine::initAudio()
[4504]419{
420  ALenum result;
421
422  PRINTF(3)("Initialisazing openAL sound library\n");
423
424  alutInit(NULL, 0);
425  if ((result = alGetError()) != AL_NO_ERROR)
426    SoundEngine::PrintALErrorString(result);
427
428  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
429}
430
431/**
[4836]432 *  Transforms AL-errors into something readable
433 * @param err The error found
[4504]434*/
435void SoundEngine::PrintALErrorString(ALenum err)
436{
437  switch(err)
438    {
439    case AL_NO_ERROR:
440      PRINTF(4)("AL_NO_ERROR\n");
441      break;
[4597]442
[4504]443    case AL_INVALID_NAME:
444      PRINTF(2)("AL_INVALID_NAME\n");
445      break;
446
447    case AL_INVALID_ENUM:
448      PRINTF(2)("AL_INVALID_ENUM\n");
449      break;
450
451    case AL_INVALID_VALUE:
452      PRINTF(2)("AL_INVALID_VALUE\n");
453      break;
454
455    case AL_INVALID_OPERATION:
456      PRINTF(2)("AL_INVALID_OPERATION\n");
457      break;
458
459    case AL_OUT_OF_MEMORY:
460      PRINTF(2)("AL_OUT_OF_MEMORY\n");
461      break;
462    };
463}
464
[4959]465void SoundEngine::listDevices()
466{
467
468  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
469}
470
[4504]471/*
472void SoundEngine::PrintALCErrorString(ALenum err)
473{
474  switch(err)
475    {
476    case ALC_NO_ERROR:
477      PRINTF(4)("AL_NO_ERROR\n");
478      break;
479
480    case ALC_INVALID_DEVICE:
481      PRINTF(2)("ALC_INVALID_DEVICE\n");
482      break;
483
484    case ALC_INVALID_CONTEXT:
485      PRINTF(2)("ALC_INVALID_CONTEXT\n");
486      break;
487
488    case ALC_INVALID_ENUM:
489      PRINTF(2)("ALC_INVALID_ENUM\n");
490      break;
491
492    case ALC_INVALID_VALUE:
493      PRINTF(2)("ALC_INVALID_VALUE\n");
494      break;
495
496    case ALC_OUT_OF_MEMORY:
497      PRINTF(2)("ALC_OUT_OF_MEMORY\n");
498      break;
499    };
500}
501*/
Note: See TracBrowser for help on using the repository browser.