Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: SoundSource in the Menu

File size: 11.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"
[7193]26#include "util/loading/resource_manager.h"
[4504]27#include "debug.h"
[7256]28#include "util/preferences.h"
[5427]29#include "globals.h"
[4504]30
31using namespace std;
32
[4597]33
[4504]34//////////////////
35/* SOUND-ENGINE */
36//////////////////
37/**
[7299]38 * @brief standard constructor
[4504]39*/
[4597]40SoundEngine::SoundEngine ()
[4504]41{
[4597]42  this->setClassID(CL_SOUND_ENGINE, "SoundEngine");
43  this->setName("SoundEngine");
44
[4504]45  this->listener = NULL;
[4960]46  this->bufferList = NULL;
47  this->sourceList = NULL;
[5930]48
49  this->device = NULL;
50  this->context = NULL;
51
52  this->maxSourceCount = 32;
[6829]53
54  this->effectsVolume = .80;
55  this->musicVolume = .75;
[7298]56
57  this->sourceMutex = SDL_CreateMutex();
[4504]58}
59
60/**
[7299]61 * @brief the singleton reference to this class
[4504]62*/
63SoundEngine* SoundEngine::singletonRef = NULL;
64
65/**
[7299]66 * @brief standard destructor
[5293]67 */
[4597]68SoundEngine::~SoundEngine ()
[4504]69{
70  // deleting all the SoundSources
[4960]71  if(this->sourceList != NULL)
72  {
[5779]73    while (this->sourceList->size() > 0)
[7290]74      delete static_cast<SoundSource*>(this->sourceList->front());
[4960]75  }
[4504]76
[5930]77  while(!this->ALSources.empty())
78  {
[7291]79    if (alIsSource(this->ALSources.top()))
80    {
81      alDeleteSources(1, &this->ALSources.top());
82      SoundEngine::checkError("Deleting Source", __LINE__);
83    }
84    else
85      PRINTF(1)("%d is not a Source\n", this->ALSources.top());
86
[5930]87    this->ALSources.pop();
88  }
89
[4504]90  // deleting all the SoundBuffers
[4960]91  if (this->bufferList != NULL)
92  {
[5779]93    while(this->bufferList->size() > 0)
[7290]94      ResourceManager::getInstance()->unload(static_cast<SoundBuffer*>(this->bufferList->front()));
[4960]95  }
[5293]96
[4504]97  // removing openAL from AudioResource
[5473]98  //! @todo this should be terminated through alc
99  //alutExit();
[5293]100
[7298]101  SDL_DestroyMutex(this->sourceMutex);
102
[5293]103  SoundEngine::singletonRef = NULL;
[4504]104}
105
106/**
[7299]107 * @brief loads the settings of the SoundEngine from an ini-file
[4985]108 */
[7256]109void SoundEngine::loadSettings()
[4985]110{
[7256]111  MultiType channels = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_AUDIO_CHANNELS, "32");
[7221]112  this->maxSourceCount = channels.getInt();
[4985]113
[7256]114  MultiType effectsVolume = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_EFFECTS_VOLUME, "80");
[7221]115  this->effectsVolume = effectsVolume.getFloat()/100.0;
[6830]116
[7256]117  MultiType musicVolume = Preferences::getInstance()->getString(CONFIG_SECTION_AUDIO, CONFIG_NAME_MUSIC_VOLUME, "75");
[7221]118  this->musicVolume = musicVolume.getFloat()/100.0;
[4985]119}
120
121/**
[7299]122 * @brief creates a new SoundSource.
[4836]123 * @param fileName The Name to load the SoundBuffer from
124 * @param sourceNode The sourceNode to bind this SoundSource to.
125 * @returns The newly created SoundSource
[7318]126 *
127 * acctualy this is nothing more than a wrapper around the ResourceManager.
[4504]128*/
[7221]129SoundSource* SoundEngine::createSource(const std::string& fileName, PNode* sourceNode)
[4504]130{
[7318]131  SoundBuffer* buffer = NULL;
132  if (!fileName.empty())
133  {
134     buffer= (SoundBuffer*)ResourceManager::getInstance()->load(fileName, WAV, RP_LEVEL);
135     if (buffer == NULL)
136       PRINTF(2)("Wav-Sound %s could not be loaded onto new Source\n", fileName.c_str());
137  }
138    return new SoundSource(sourceNode, buffer);
[4504]139}
140
141/**
[7299]142 * @brief Sets the doppler values of openAL
[4836]143 * @param dopplerFactor the extent of the doppler-effect
144 * @param dopplerVelocity the Speed the sound travels
[4504]145*/
146void SoundEngine::setDopplerValues(ALfloat dopplerFactor, ALfloat dopplerVelocity)
147{
148  alDopplerFactor(dopplerFactor);
[6840]149  this->checkError("Setting Doppler Factor", __LINE__);
150
[4504]151  alDopplerVelocity(dopplerVelocity);
[6840]152  this->checkError("Setting Doppler Velocity", __LINE__);
[4504]153}
154
155
[7299]156/**
157 * @brief retrieves an OpenAL Source from the availiable Sources.
158 * @param source the Source to fill with the Value.
159 */
[7318]160void SoundEngine::popALSource(ALuint& source)
[4504]161{
[7284]162  assert (source == 0);
163  /// @TODO try to create more sources if needed
164  if (!this->ALSources.empty())
[5930]165  {
[7298]166    SDL_mutexP(this->sourceMutex);
[7284]167    source = this->ALSources.top();
168    this->ALSources.pop();
[7298]169    SDL_mutexV(this->sourceMutex);
[7284]170  }
[4504]171}
172
[7299]173
174/**
175 * @brief Pushes an OpenAL Source back into the Stack of known sources
176 * @param source the Source to push onto the top of the SourceStack
177 */
[7298]178void SoundEngine::pushALSource(ALuint& source)
179{
180  if (source != 0)
181  {
182    SDL_mutexP(this->sourceMutex);
183    this->ALSources.push(source);
184    SDL_mutexV(this->sourceMutex);
185  }
186};
[4504]187
[7298]188
[4504]189/**
[7299]190 * @brief updates all The positions, Directions and Velocities of all Sounds
191 */
[4746]192void SoundEngine::update()
[4504]193{
194  // updating the Listeners Position
195  if (likely(this->listener != NULL))
[6846]196  {
197    alListener3f(AL_POSITION,
198                 this->listener->getAbsCoor().x,
199                 this->listener->getAbsCoor().y,
200                 this->listener->getAbsCoor().z);
201    alListener3f(AL_VELOCITY,
202                 this->listener->getVelocity().x,
203                 this->listener->getVelocity().y,
204                 this->listener->getVelocity().z);
205    Vector absDirV = this->listener->getAbsDirV();
206    ALfloat orientation [6] = {1,0,0, absDirV.x, absDirV.y, absDirV.z};
207    alListenerfv(AL_ORIENTATION, orientation);
[7291]208    SoundEngine::checkError("SoundEngine::update() - Listener Error", __LINE__);
[6846]209  }
[4504]210  else
211    PRINTF(2)("no listener defined\n");
212
213  // updating all the Sources positions
[5930]214  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
[4960]215  {
[5885]216    list<BaseObject*>::const_iterator sourceIT;
[5779]217    SoundSource* source;
218    for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
[4504]219    {
[5779]220      source = static_cast<SoundSource*>(*sourceIT);
[5930]221      if (source->isPlaying())
[4504]222      {
[6073]223        int play = 0x000;
[5930]224        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
[7291]225        if (DEBUG > 2)
226          SoundEngine::checkError("SoundEngine::update() Play", __LINE__);
[5930]227        if(play == AL_PLAYING)
228        {
229          if (likely(source->getNode() != NULL))
230          {
231            alSource3f(source->getID(), AL_POSITION,
232                       source->getNode()->getAbsCoor().x,
233                       source->getNode()->getAbsCoor().y,
234                       source->getNode()->getAbsCoor().z);
[7291]235            if (DEBUG > 2)
236              SoundEngine::checkError("SoundEngine::update() Set Source Position", __LINE__);
[5930]237            alSource3f(source->getID(), AL_VELOCITY,
238                       source->getNode()->getVelocity().x,
239                       source->getNode()->getVelocity().y,
240                       source->getNode()->getVelocity().z);
[7291]241            if (DEBUG > 2)
242              SoundEngine::checkError("SoundEngine::update() Set Source Velocity", __LINE__);
[5930]243          }
244        }
245        else
246        {
247          source->stop();
248        }
[4504]249      }
250    }
[4960]251  }
[7290]252  SoundEngine::checkError("SoundEngine::update()", __LINE__);
[4504]253}
254
255
256/**
[4836]257 *  initializes Audio in general
[4504]258*/
[4746]259bool SoundEngine::initAudio()
[4504]260{
[7284]261  //   ALenum result;
262  //   PRINTF(3)("Initialisazing openAL sound engine\n");
263  //   const char* defaultDevice =(const char*) alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
264  //   const char* deviceList = (const char*)alcGetString(NULL,ALC_DEVICE_SPECIFIER);
265  //   const char* devWalk = deviceList;
266  //   //  if (alcIsExtensionPresent(NULL, (const ALCchar*)"ALC_ENUMERATION_EXT") == AL_TRUE)
267  //   { // try out enumeration extension
268  //     PRINTF(3)("Enumeration-extension found\n");
269  //
270  //     PRINTF(3)("Default device: %s\n", defaultDevice);
271  //     do
272  //     {
273  //       PRINTF(3)("%s\n", devWalk);
274  //       devWalk += strlen(devWalk)+1;
275  //     }
276  //     while (devWalk[0] != '\0');
277  //  }
[4504]278
[5819]279  // INITIALIZING THE DEVICE:
[7284]280  //   char deviceName[] =
281  //   #ifdef __WIN32__
282  //     "Direct3D";
283  //   #else
284  //     "'( ( devices '( native null ) ) )";
285  //   #endif
[5385]286
[6858]287  this->device = alcOpenDevice(NULL);
[6856]288  this->checkALCError("opening Device", __LINE__);
[5819]289
[6857]290  PRINTF(4)("Audio-Specifier: %s\n", (const char*)alcGetString(this->device, ALC_DEVICE_SPECIFIER));
291  PRINTF(4)("Audio-Extensions: %s\n", (const char*)alcGetString(this->device, ALC_EXTENSIONS));
[6849]292
[6856]293
[6836]294  this->context = alcCreateContext(this->device, NULL);
[6856]295  this->checkALCError("creating Context", __LINE__);
[6849]296
[6856]297  alcMakeContextCurrent(this->context);
298  this->checkALCError("making Context Current", __LINE__);
[6846]299  // #endif
[5819]300
[4504]301  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
[6840]302  this->allocateSources(this->maxSourceCount);
[7290]303  this->checkError("Allocating Sources", __LINE__);
[4504]304}
305
[5917]306
[4504]307/**
[5917]308 * Allocates openAL sources
309 * @param count how many sources to allocate
310 * @returns true on success, false if at least one source could not be allocated
311 */
312bool SoundEngine::allocateSources(unsigned int count)
313{
[6844]314  unsigned int failCount = 0;
[5930]315  for (unsigned int i = 0; i < count; i++)
[5917]316  {
[6836]317    ALuint source = 0;
[5917]318
[5930]319    alGenSources(1, &source);
[6840]320    this->checkError("allocate Source", __LINE__);
[6842]321    if (!alIsSource(source))
322    {
[6844]323      PRINTF(5)("not allocated Source\n");
324      failCount++;
[6842]325      continue;
326    }
[5917]327
[5930]328    alSourcef (source, AL_PITCH,    1.0      );
329    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
330    alSourcei (source, AL_LOOPING,  AL_FALSE );
331    this->ALSources.push(source);
[5917]332  }
[6844]333  if (failCount == 0)
334    return true;
335  else
336  {
337    PRINTF(2)("Failed to allocate %d of %d SoundSources\n", failCount, count);
338  }
[5917]339}
340
[7299]341/**
342 * @brief checks for an OpenAL error
343 * @param error the ErrorMessage to display
344 * @param line on what line did the error occure.
345 */
[7225]346bool SoundEngine::checkError(const std::string& error, unsigned int line)
[6836]347{
348  ALenum errorCode;
349  if ((errorCode = alGetError()) != AL_NO_ERROR)
350  {
[7225]351    PRINTF(1)("Error %s (line:%d): '%s'\n", error.c_str(), line, SoundEngine::getALErrorString(errorCode));
[6836]352    return false;
353  }
354  else
355    return true;
356}
357
[7299]358/**
359 * @brief check for an ALC error.
360 * @brief error the Error-String to display
361 * @param line on that line, the error occured (debugging mode).
362 */
[7225]363bool SoundEngine::checkALCError(const std::string& error, unsigned int line)
[6849]364{
365  ALenum errorCode;
366  if ((errorCode = alcGetError(this->device)) != ALC_NO_ERROR)
367  {
[7225]368    PRINTF(1)("Error %s (line:%d): '%s'\n", error.c_str(), line, SoundEngine::getALCErrorString(errorCode));
[6849]369    return false;
370  }
371  else
372    return true;
373}
374
375
376
377void SoundEngine::listDevices()
378{
379  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
380}
381
[5917]382/**
[4836]383 *  Transforms AL-errors into something readable
384 * @param err The error found
[4504]385*/
[5930]386const char* SoundEngine::getALErrorString(ALenum err)
[4504]387{
388  switch(err)
[6846]389  {
[4504]390    case AL_NO_ERROR:
[5930]391      return ("AL_NO_ERROR");
[4504]392    case AL_INVALID_NAME:
[5930]393      return ("AL_INVALID_NAME");
[4504]394    case AL_INVALID_ENUM:
[5930]395      return ("AL_INVALID_ENUM");
[4504]396    case AL_INVALID_VALUE:
[5930]397      return ("AL_INVALID_VALUE");
[4504]398    case AL_INVALID_OPERATION:
[5930]399      return ("AL_INVALID_OPERATION");
[4504]400    case AL_OUT_OF_MEMORY:
[5930]401      return ("AL_OUT_OF_MEMORY");
[6846]402  };
[4504]403}
404
[4959]405
[6847]406const char* SoundEngine::getALCErrorString(ALenum err)
[4504]407{
408  switch(err)
[7284]409  {
[4504]410    case ALC_NO_ERROR:
[6849]411      return ("AL_NO_ERROR");
[4504]412    case ALC_INVALID_DEVICE:
[6849]413      return ("ALC_INVALID_DEVICE");
[4504]414    case ALC_INVALID_CONTEXT:
[6849]415      return("ALC_INVALID_CONTEXT");
[4504]416    case ALC_INVALID_ENUM:
[6849]417      return("ALC_INVALID_ENUM");
[4504]418    case ALC_INVALID_VALUE:
[6849]419      return ("ALC_INVALID_VALUE");
[4504]420    case ALC_OUT_OF_MEMORY:
[6849]421      return("ALC_OUT_OF_MEMORY");
[7284]422  };
[4504]423}
Note: See TracBrowser for help on using the repository browser.