Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: playing of different Buffer on one Sources works, but without overlapping.
so what we do now is
stop(oldSound)
load(newSound)
play(newSound)

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