Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5930 in orxonox.OLD for trunk/src/lib/sound


Ignore:
Timestamp:
Dec 5, 2005, 12:50:26 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: remake of the SoundEngine (faster, and millions of SoundSources can be defined, as Many are not needed for playing

Location:
trunk/src/lib/sound
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/sound/sound_buffer.cc

    r5422 r5930  
    2020#include "sound_engine.h"
    2121
    22 
    2322using namespace std;
    24 
    2523
    2624//////////////////
     
    3634  this->setName(fileName);
    3735
    38   SoundEngine::getInstance()->addBuffer(this);
    39 
    4036  ALenum format;
    4137  ALvoid* data;
     
    4743  alGenBuffers(1, &this->bufferID);
    4844  if ((result = alGetError()) != AL_NO_ERROR)
    49     SoundEngine::PrintALErrorString(result);
     45    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
    5046
    5147  // read in the wav data
     
    5955#endif
    6056  if ((result = alGetError()) != AL_NO_ERROR)
    61     SoundEngine::PrintALErrorString(result);
     57    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
    6258
    6359  // send the loaded wav data to the buffer
    6460  alBufferData(this->bufferID, format, data, this->size, freq);
    6561  if ((result = alGetError()) != AL_NO_ERROR)
    66     SoundEngine::PrintALErrorString(result);
     62    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
    6763
    6864  // remove the wav data (redundant)
    6965  alutUnloadWAV(format, data, this->size, freq);
    7066  if ((result = alGetError()) != AL_NO_ERROR)
    71     SoundEngine::PrintALErrorString(result);
     67    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
    7268}
    7369
  • trunk/src/lib/sound/sound_engine.cc

    r5917 r5930  
    4747  this->bufferList = NULL;
    4848  this->sourceList = NULL;
     49
     50  this->device = NULL;
     51  this->context = NULL;
     52
     53  this->maxSourceCount = 32;
    4954}
    5055
     
    6469    while (this->sourceList->size() > 0)
    6570      delete dynamic_cast<SoundSource*>(this->sourceList->front());
     71  }
     72
     73  while(!this->ALSources.empty())
     74  {
     75    alDeleteSources(1, &this->ALSources.top());
     76    this->ALSources.pop();
    6677  }
    6778
     
    118129
    119130
    120 /**
    121  *  adds a SoundBuffer to the bufferList of the SoundEngine
    122  * @param buffer The buffer to add to the bufferList
    123 */
    124 void SoundEngine::addBuffer(SoundBuffer* buffer)
    125 {
    126   if (unlikely(this->bufferList == NULL))
    127     this->bufferList = ClassList::getList(CL_SOUND_BUFFER);
    128 }
    129 
    130 /**
    131  *  removes a SoundBuffer from the bufferList of the SoundEngine
    132  * @param buffer The buffer to delete from the SoundEngine
    133 */
    134 void SoundEngine::removeBuffer(SoundBuffer* buffer)
    135 {
    136   // look if there are any sources that have the buffer still loaded
    137   if (this->sourceList != NULL)
    138   {
    139     list<BaseObject*>::const_iterator source;
    140     for (source = this->sourceList->begin(); source != this->sourceList->end(); source++)
    141     {
    142       if (buffer == static_cast<SoundSource*>(*source)->getBuffer())
    143         delete (*source);
     131void SoundEngine::popALSource(ALuint& source)
     132{
     133  if (source != 0)
     134    return;
     135  else
     136  {
     137
     138    /// @TODO try to create more sources if needed
     139    if (!this->ALSources.empty())
     140    {
     141
     142      source = this->ALSources.top();
     143      printf("test: : %d\n", source);
     144      this->ALSources.pop();
    144145    }
    145146  }
    146147}
    147148
    148 /**
    149  * adds a SoundSource to the sourceList of the SoundEngine
    150  * @param source The source to add to the sourceList
    151 */
    152 void SoundEngine::addSource(SoundSource* source)
    153 {
    154   this->sourceList = ClassList::getList(CL_SOUND_SOURCE);
    155 }
    156149
    157150/**
     
    180173
    181174  // updating all the Sources positions
    182   if (likely(this->sourceList != NULL))
     175  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
    183176  {
    184177    list<BaseObject*>::const_iterator sourceIT;
     
    187180    {
    188181      source = static_cast<SoundSource*>(*sourceIT);
    189       if (likely(source->getNode() != NULL))
     182      if (source->isPlaying())
    190183      {
    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);
     184        int play;
     185        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
     186        if(play == AL_PLAYING)
     187        {
     188          if (likely(source->getNode() != NULL))
     189          {
     190            alSource3f(source->getID(), AL_POSITION,
     191                       source->getNode()->getAbsCoor().x,
     192                       source->getNode()->getAbsCoor().y,
     193                       source->getNode()->getAbsCoor().z);
     194            alSource3f(source->getID(), AL_VELOCITY,
     195                       source->getNode()->getVelocity().x,
     196                       source->getNode()->getVelocity().y,
     197                       source->getNode()->getVelocity().z);
     198          }
     199
     200        }
     201        else
     202        {
     203          source->stop();
     204        }
    199205      }
    200206    }
     
    300306
    301307  if ((result = alGetError()) != AL_NO_ERROR)
    302     SoundEngine::PrintALErrorString(result);
     308    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
    303309
    304310  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
     
    313319bool SoundEngine::allocateSources(unsigned int count)
    314320{
    315   ALuint* sourceList = new ALuint[count];
    316321  ALenum result;
    317 
    318   alGenSources(count, sourceList);
    319   if ((result = alGetError()) != AL_NO_ERROR)
    320   {
    321     SoundEngine::PrintALErrorString(result);
    322     return false;
    323   }
    324 
    325   /// @TODO check syntax
    326 
    327 
    328322  // Setting default values.
    329   for (int i = 0; i < count; i++)
    330   {
    331     alSourcef (sourceList[i], AL_PITCH,    1.0      );
    332     alSourcef (sourceList[i], AL_GAIN,     this->getEffectsVolume() );
    333     alSourcei (sourceList[i], AL_LOOPING,  AL_FALSE );
    334     this->ALSources.push(sourceList[i]);
     323  for (unsigned int i = 0; i < count; i++)
     324  {
     325    ALuint source;
     326
     327    alGenSources(1, &source);
     328    if ((result = alGetError()) != AL_NO_ERROR)
     329      PRINTF(1)("Error Generating Sources: '%s'\n", SoundEngine::getALErrorString(result));
     330
     331    alSourcef (source, AL_PITCH,    1.0      );
     332    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
     333    alSourcei (source, AL_LOOPING,  AL_FALSE );
     334    this->ALSources.push(source);
    335335  }
    336336  return true;
     
    341341 * @param err The error found
    342342*/
    343 void SoundEngine::PrintALErrorString(ALenum err)
     343const char* SoundEngine::getALErrorString(ALenum err)
    344344{
    345345  switch(err)
    346346    {
    347347    case AL_NO_ERROR:
    348       PRINTF(4)("AL_NO_ERROR\n");
    349       break;
    350 
     348      return ("AL_NO_ERROR");
    351349    case AL_INVALID_NAME:
    352       PRINTF(2)("AL_INVALID_NAME\n");
    353       break;
    354 
     350      return ("AL_INVALID_NAME");
    355351    case AL_INVALID_ENUM:
    356       PRINTF(2)("AL_INVALID_ENUM\n");
    357       break;
    358 
     352      return ("AL_INVALID_ENUM");
    359353    case AL_INVALID_VALUE:
    360       PRINTF(2)("AL_INVALID_VALUE\n");
    361       break;
    362 
     354      return ("AL_INVALID_VALUE");
    363355    case AL_INVALID_OPERATION:
    364       PRINTF(2)("AL_INVALID_OPERATION\n");
    365       break;
    366 
     356      return ("AL_INVALID_OPERATION");
    367357    case AL_OUT_OF_MEMORY:
    368       PRINTF(2)("AL_OUT_OF_MEMORY\n");
    369       break;
     358      return ("AL_OUT_OF_MEMORY");
    370359    };
    371360}
     
    373362void SoundEngine::listDevices()
    374363{
    375 
    376364  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
    377365}
  • trunk/src/lib/sound/sound_engine.h

    r5917 r5930  
    2626//! A class that handles audio via the openAudioLibrary
    2727class SoundEngine : public BaseObject {
    28 
    2928  public:
    3029    virtual ~SoundEngine();
     
    4948
    5049  // administrative
    51     void addBuffer(SoundBuffer* buffer);
    52     void removeBuffer(SoundBuffer* buffer);
    53     void addSource(SoundSource* source);
     50    void popALSource(ALuint& source);
     51    void pushALSource(ALuint& source) { if (source != 0) this->ALSources.push(source); };
    5452
    5553    void flushUnusedBuffers();
     
    5755    void flushAllSources();
    5856
     57    bool initAudio();
    5958    bool allocateSources(unsigned int count);
    60     bool initAudio();
    6159
    6260  // error handling:
    63     static void PrintALErrorString(ALenum err);
    64   //  static void PrintALCErrorString(ALenum err);
    65 
     61    static const char* getALErrorString(ALenum err);
    6662
    6763  private:
    6864    SoundEngine();
     65
    6966    void listDevices();
    7067
  • trunk/src/lib/sound/sound_source.cc

    r5917 r5930  
    3434
    3535  // adding the Source to the SourcesList of the SoundEngine
    36   SoundEngine::getInstance()->addSource(this);
    37 
    3836  this->buffer = buffer;
    3937  this->sourceNode = sourceNode;
    4038
    41   alGenSources(1, &this->sourceID);
    42   if ((result = alGetError()) != AL_NO_ERROR)
    43     SoundEngine::PrintALErrorString(result);
    44   if (this->buffer != NULL)
    45     alSourcei (this->sourceID, AL_BUFFER,   this->buffer->getID());
    46   alSourcef (this->sourceID, AL_PITCH,    1.0      );
    47   alSourcef (this->sourceID, AL_GAIN,     SoundEngine::getInstance()->getEffectsVolume() );
    48   alSourcei (sourceID, AL_LOOPING,  AL_FALSE     );
     39  this->sourceID = 0;
     40  this->bPlay = false;
    4941}
    5042
     
    5446SoundSource::~SoundSource()
    5547{
    56   //SoundEngine::getInstance()->removeSource(this);
    57   alDeleteSources(1, &this->sourceID);
     48  SoundEngine::getInstance()->pushALSource(this->sourceID);
    5849}
    5950
     
    6354void SoundSource::play()
    6455{
     56  if (this->sourceID == 0)
     57    SoundEngine::getInstance()->popALSource(this->sourceID);
    6558  alSourcePlay(this->sourceID);
     59  this->bPlay = true;
    6660}
    6761
     
    7266void SoundSource::play(const SoundBuffer* buffer)
    7367{
     68  if (unlikely(this->sourceID == 0))
     69    SoundEngine::getInstance()->popALSource(this->sourceID);
     70
     71  printf("%d\n",sourceID);
    7472  alSourceStop(this->sourceID);
    7573  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
     
    7876  if (unlikely(this->buffer != NULL))
    7977    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
     78  this->bPlay = true;
    8079}
    8180
     
    8584void SoundSource::stop()
    8685{
     86  this->bPlay = false;
    8787  alSourceStop(this->sourceID);
     88  SoundEngine::getInstance()->pushALSource(this->sourceID);
    8889}
    8990
  • trunk/src/lib/sound/sound_source.h

    r5917 r5930  
    3131    /** @returns The ID of this Source */
    3232    inline ALuint getID() const { return this->sourceID; }
     33    /** @returns true, if the Source is Playing */
     34    inline bool   isPlaying() const { return this->bPlay; };
    3335    /** @returns the SoundBuffer of this Source */
    3436    inline const SoundBuffer* getBuffer() const { return this->buffer; }
Note: See TracChangeset for help on using the changeset viewer.