Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6028 was 6027, checked in by snellen, 19 years ago

space_ship.cc updated

File size: 9.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
[5386]19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND
[4504]20
21#include "sound_engine.h"
22
[4960]23#include "class_list.h"
[4504]24
25#include "p_node.h"
26#include "list.h"
27#include "resource_manager.h"
28#include "debug.h"
[4985]29#include "ini_parser.h"
[5427]30#include "globals.h"
[4504]31
32using namespace std;
33
[4597]34
[4504]35//////////////////
36/* SOUND-ENGINE */
37//////////////////
38/**
[4836]39 *  standard constructor
[4504]40*/
[4597]41SoundEngine::SoundEngine ()
[4504]42{
[4597]43  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
44  this->setName("SoundEngine");
45
[4504]46  this->listener = NULL;
[4960]47  this->bufferList = NULL;
48  this->sourceList = NULL;
[4504]49}
50
51/**
[4836]52 *  the singleton reference to this class
[4504]53*/
54SoundEngine* SoundEngine::singletonRef = NULL;
55
56/**
[4836]57 *  standard deconstructor
[5293]58 */
[4597]59SoundEngine::~SoundEngine ()
[4504]60{
61  // deleting all the SoundSources
[4960]62  if(this->sourceList != NULL)
63  {
[5779]64    while (this->sourceList->size() > 0)
65      delete dynamic_cast<SoundSource*>(this->sourceList->front());
[4960]66  }
[4504]67
68  // deleting all the SoundBuffers
[4960]69  if (this->bufferList != NULL)
70  {
[5779]71    while(this->bufferList->size() > 0)
72      ResourceManager::getInstance()->unload(dynamic_cast<SoundBuffer*>(this->bufferList->front()));
[4960]73  }
[5293]74
[4504]75  // removing openAL from AudioResource
[5473]76  //! @todo this should be terminated through alc
77  //alutExit();
[5293]78
79  SoundEngine::singletonRef = NULL;
[4504]80}
81
82/**
[4985]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/**
[4836]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
[4504]100
101   acctualy this is nothing more than a wrapper around the ResourceManager.
102*/
103SoundSource* SoundEngine::createSource(const char* fileName, PNode* sourceNode)
104{
[4885]105  return new SoundSource(sourceNode, (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL));
[4504]106}
107
108/**
[4836]109 *  Sets the doppler values of openAL
110 * @param dopplerFactor the extent of the doppler-effect
111 * @param dopplerVelocity the Speed the sound travels
[4504]112*/
113void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
114{
115  alDopplerFactor(dopplerFactor);
116  alDopplerVelocity(dopplerVelocity);
117}
118
119
120/**
[4836]121 *  adds a SoundBuffer to the bufferList of the SoundEngine
122 * @param buffer The buffer to add to the bufferList
[4504]123*/
124void SoundEngine::addBuffer(SoundBuffer* buffer)
125{
[4960]126  if (unlikely(this->bufferList == NULL))
127    this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
[4504]128}
129
130/**
[4836]131 *  removes a SoundBuffer from the bufferList of the SoundEngine
132 * @param buffer The buffer to delete from the SoundEngine
[4504]133*/
134void SoundEngine::removeBuffer(SoundBuffer* buffer)
135{
136  // look if there are any sources that have the buffer still loaded
[5779]137  if (this->sourceList != NULL)
138  {
139    list<BaseObject*>::iterator source;
140    for (source = this->sourceList->begin(); source != this->sourceList->end(); source++)
[4504]141    {
[5779]142      if (buffer == static_cast<SoundSource*>(*source)->getBuffer())
143        delete (*source);
[4504]144    }
[5779]145  }
[4504]146}
147
148/**
[5293]149 * adds a SoundSource to the sourceList of the SoundEngine
[4836]150 * @param source The source to add to the sourceList
[4504]151*/
152void SoundEngine::addSource(SoundSource* source)
153{
[4960]154  this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
[4504]155}
156
157/**
[4836]158 *  updates all The positions, Directions and Velocities of all Sounds
[4504]159*/
[4746]160void SoundEngine::update()
[4504]161{
162
163  // updating the Listeners Position
164  if (likely(this->listener != NULL))
165    {
166      alListener3f(AL_POSITION,
[4597]167                   this->listener->getAbsCoor().x,
168                   this->listener->getAbsCoor().y,
169                   this->listener->getAbsCoor().z);
[4504]170      alListener3f(AL_VELOCITY,
[4597]171                   this->listener->getVelocity().x,
172                   this->listener->getVelocity().y,
173                   this->listener->getVelocity().z);
[4504]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
[4960]182  if (likely(this->sourceList != NULL))
183  {
[5779]184    list<BaseObject*>::iterator sourceIT;
185    SoundSource* source;
186    for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
[4504]187    {
[5779]188      source = static_cast<SoundSource*>(*sourceIT);
189      if (likely(source->getNode() != NULL))
[4504]190      {
[5779]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);
[4504]199      }
200    }
[4960]201  }
[4504]202}
203
204/**
[4836]205 *  Removes all the Buffers that are not anymore needed by any Sources
[4504]206*/
[4746]207void SoundEngine::flushUnusedBuffers()
[4504]208{
[5819]209  /// FIXME
[5779]210/*  if(this->sourceList && this->bufferList)
[4960]211  {
212    tIterator<BaseObject>* bufferIterator = this->bufferList->getIterator();
[5115]213    SoundBuffer* enumBuffer = (SoundBuffer*)bufferIterator->firstElement();
[4960]214    while (enumBuffer)
[4504]215    {
[4960]216      tIterator<BaseObject>* sourceIterator = this->sourceList->getIterator();
[5115]217      SoundSource* enumSource = (SoundSource*)sourceIterator->firstElement();
[4504]218      while (enumSource)
[4960]219      {
220        if (enumBuffer == enumSource->getBuffer())
221          break;
222        enumSource = (SoundSource*)sourceIterator->nextElement();
223      }
[4504]224      delete sourceIterator;
225      if (enumSource == NULL)
[4597]226        ResourceManager::getInstance()->unload(enumBuffer);
[4960]227      enumBuffer = (SoundBuffer*)bufferIterator->nextElement();
[4504]228    }
[4960]229    delete bufferIterator;
[5779]230}*/ /// FIXME
[4504]231}
232
233/**
[5293]234 * flushes all the Buffers
235 * deletes them from the BufferList, and also removes them via the ResourceManager.
236 */
[4746]237void SoundEngine::flushAllBuffers()
[4504]238{
[4960]239  if (this->bufferList)
240  {
[5779]241    while (this->bufferList->size() > 0)
242      ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front()), RP_LEVEL);
[4960]243  }
[4504]244}
245
246/**
[5293]247 * deletes all the Sources.
[4830]248 */
249void SoundEngine::flushAllSources()
250{
[4960]251  if (this->sourceList)
[4830]252  {
[5779]253    while(this->sourceList->size() > 0)
254      delete this->sourceList->front();
[4830]255  }
256}
257
258/**
[4836]259 *  initializes Audio in general
[4504]260*/
[4746]261bool SoundEngine::initAudio()
[4504]262{
263  ALenum result;
[5385]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;
[5416]268//  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
269{ // try out enumeration extension
[5386]270    PRINTF(3)("Enumeration-extension found\n");
[4504]271
[5386]272    PRINTF(3)("Default device: %s\n", defaultDevice);
[5385]273    do
274    {
[5386]275      PRINTF(3)("%s\n", devWalk);
[5385]276      devWalk += strlen(devWalk)+1;
277    } while (devWalk[0] != '\0');
[4504]278
[5385]279
280  }
281
[5819]282  // INITIALIZING THE DEVICE:
[6014]283#ifndef AL_VERSION_1_1
[5844]284  ALubyte deviceName[] =
[5879]285#else
[6027]286  ALCbyte deviceName[] =
[5879]287#endif
288
[5819]289#ifdef __WIN32__
[5910]290      "Direct3D";
[5819]291#else
[5910]292      "'( ( devices '( native null ) ) )";
[5819]293#endif
294  //
295  this->device = alcOpenDevice(deviceName);
[5385]296
[5819]297  this->context = alcCreateContext(this->device, NULL);
298
299  alcMakeContextCurrent(this->context);
300
301
[4504]302  if ((result = alGetError()) != AL_NO_ERROR)
303    SoundEngine::PrintALErrorString(result);
304
305  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
306}
307
308/**
[4836]309 *  Transforms AL-errors into something readable
310 * @param err The error found
[4504]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;
[4597]319
[4504]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
[4959]342void SoundEngine::listDevices()
343{
344
345  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
346}
347
[4504]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.