Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5955 in orxonox.OLD for branches/powerups/src/lib/sound


Ignore:
Timestamp:
Dec 7, 2005, 1:05:10 PM (19 years ago)
Author:
bensch
Message:

branches/powerups: merged the trunk to branches/powerup
merged with commandsvn merge ../trunk/ powerups/ -r5848:HEAD
conflicts resolved in favor of the trunk

Location:
branches/powerups/src/lib/sound
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/powerups/src/lib/sound/sound_buffer.cc

    r5422 r5955  
    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
  • branches/powerups/src/lib/sound/sound_engine.cc

    r5871 r5955  
    2727#include "resource_manager.h"
    2828#include "debug.h"
    29 #include "ini_parser.h"
     29#include "parser/ini_parser/ini_parser.h"
    3030#include "globals.h"
    3131
     
    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
     
    8697void SoundEngine::loadSettings(IniParser* iniParser)
    8798{
     99  const char* channels = iniParser->getVar(CONFIG_NAME_AUDIO_CHANNELS, CONFIG_SECTION_AUDIO, "32");
     100  this->maxSourceCount = atoi(channels);
    88101  const char* musicVolume = iniParser->getVar(CONFIG_NAME_MUSIC_VOLUME, CONFIG_SECTION_AUDIO, "80");
    89102  this->musicVolume = atof(musicVolume)/100.0;
     
    118131
    119132
    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*>::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);
     133void SoundEngine::popALSource(ALuint& source)
     134{
     135  if (source != 0)
     136    return;
     137  else
     138  {
     139
     140    /// @TODO try to create more sources if needed
     141    if (!this->ALSources.empty())
     142    {
     143
     144      source = this->ALSources.top();
     145      printf("test: : %d\n", source);
     146      this->ALSources.pop();
    144147    }
    145148  }
    146149}
    147150
    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 }
    156151
    157152/**
     
    180175
    181176  // updating all the Sources positions
    182   if (likely(this->sourceList != NULL))
    183   {
    184     list<BaseObject*>::iterator sourceIT;
     177  if (likely(this->sourceList != NULL || (this->sourceList = ClassList::getList(CL_SOUND_SOURCE)) != NULL))
     178  {
     179    list<BaseObject*>::const_iterator sourceIT;
    185180    SoundSource* source;
    186181    for (sourceIT = this->sourceList->begin(); sourceIT != this->sourceList->end(); sourceIT++)
    187182    {
    188183      source = static_cast<SoundSource*>(*sourceIT);
    189       if (likely(source->getNode() != NULL))
     184      if (source->isPlaying())
    190185      {
    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);
     186        int play;
     187        alGetSourcei(source->getID(), AL_SOURCE_STATE, &play);
     188        if(play == AL_PLAYING)
     189        {
     190          if (likely(source->getNode() != NULL))
     191          {
     192            alSource3f(source->getID(), AL_POSITION,
     193                       source->getNode()->getAbsCoor().x,
     194                       source->getNode()->getAbsCoor().y,
     195                       source->getNode()->getAbsCoor().z);
     196            alSource3f(source->getID(), AL_VELOCITY,
     197                       source->getNode()->getVelocity().x,
     198                       source->getNode()->getVelocity().y,
     199                       source->getNode()->getVelocity().z);
     200          }
     201
     202        }
     203        else
     204        {
     205          source->stop();
     206        }
    199207      }
    200208    }
     
    281289
    282290  // INITIALIZING THE DEVICE:
     291#ifndef AL_VERSION_1_1
    283292  ALubyte deviceName[] =
     293#else
     294  ALCchar deviceName[] =
     295#endif
    284296#ifdef __WIN32__
    285       "native";
     297      "Direct3D";
    286298#else
    287       "'( ( devices '( native arts null ) ) )";
     299      "'( ( devices '( native null ) ) )";
    288300#endif
    289301  //
     
    296308
    297309  if ((result = alGetError()) != AL_NO_ERROR)
    298     SoundEngine::PrintALErrorString(result);
     310    PRINTF(2)("%s\n", SoundEngine::getALErrorString(result));
    299311
    300312  this->setDopplerValues(SOUND_DOPPLER_FACTOR, SOUND_DOPPLER_VELOCITY);
     313}
     314
     315
     316/**
     317 * Allocates openAL sources
     318 * @param count how many sources to allocate
     319 * @returns true on success, false if at least one source could not be allocated
     320 */
     321bool SoundEngine::allocateSources(unsigned int count)
     322{
     323  ALenum result;
     324  // Setting default values.
     325  for (unsigned int i = 0; i < count; i++)
     326  {
     327    ALuint source;
     328
     329    alGenSources(1, &source);
     330    if ((result = alGetError()) != AL_NO_ERROR)
     331      PRINTF(1)("Error Generating Sources: '%s'\n", SoundEngine::getALErrorString(result));
     332
     333    alSourcef (source, AL_PITCH,    1.0      );
     334    alSourcef (source, AL_GAIN,     this->getEffectsVolume() );
     335    alSourcei (source, AL_LOOPING,  AL_FALSE );
     336    this->ALSources.push(source);
     337  }
     338  return true;
    301339}
    302340
     
    305343 * @param err The error found
    306344*/
    307 void SoundEngine::PrintALErrorString(ALenum err)
     345const char* SoundEngine::getALErrorString(ALenum err)
    308346{
    309347  switch(err)
    310348    {
    311349    case AL_NO_ERROR:
    312       PRINTF(4)("AL_NO_ERROR\n");
    313       break;
    314 
     350      return ("AL_NO_ERROR");
    315351    case AL_INVALID_NAME:
    316       PRINTF(2)("AL_INVALID_NAME\n");
    317       break;
    318 
     352      return ("AL_INVALID_NAME");
    319353    case AL_INVALID_ENUM:
    320       PRINTF(2)("AL_INVALID_ENUM\n");
    321       break;
    322 
     354      return ("AL_INVALID_ENUM");
    323355    case AL_INVALID_VALUE:
    324       PRINTF(2)("AL_INVALID_VALUE\n");
    325       break;
    326 
     356      return ("AL_INVALID_VALUE");
    327357    case AL_INVALID_OPERATION:
    328       PRINTF(2)("AL_INVALID_OPERATION\n");
    329       break;
    330 
     358      return ("AL_INVALID_OPERATION");
    331359    case AL_OUT_OF_MEMORY:
    332       PRINTF(2)("AL_OUT_OF_MEMORY\n");
    333       break;
     360      return ("AL_OUT_OF_MEMORY");
    334361    };
    335362}
     
    337364void SoundEngine::listDevices()
    338365{
    339 
    340366  printf("%s\n",(const char*)alcGetString(NULL, ALC_DEVICE_SPECIFIER));
    341367}
  • branches/powerups/src/lib/sound/sound_engine.h

    r5819 r5955  
    1414
    1515#include <list>
     16#include <stack>
    1617
    1718#define SOUND_DOPPLER_FACTOR       0.001          //!< A factor for the audible doppler effect
     
    2223class IniParser;
    2324
     25
    2426//! A class that handles audio via the openAudioLibrary
    2527class SoundEngine : public BaseObject {
    26 
    2728  public:
    2829    virtual ~SoundEngine();
     
    4748
    4849  // administrative
    49     void addBuffer(SoundBuffer* buffer);
    50     void removeBuffer(SoundBuffer* buffer);
    51     void addSource(SoundSource* source);
     50    void popALSource(ALuint& source);
     51    void pushALSource(ALuint& source) { if (source != 0) this->ALSources.push(source); };
    5252
    5353    void flushUnusedBuffers();
    5454    void flushAllBuffers();
    5555    void flushAllSources();
     56
    5657    bool initAudio();
     58    bool allocateSources(unsigned int count);
    5759
    5860  // error handling:
    59     static void PrintALErrorString(ALenum err);
    60   //  static void PrintALCErrorString(ALenum err);
    61 
     61    static const char* getALErrorString(ALenum err);
    6262
    6363  private:
    6464    SoundEngine();
     65
    6566    void listDevices();
    6667
    6768  private:
    68     static SoundEngine*      singletonRef;             //!< Reference to this class
     69    static SoundEngine*            singletonRef;             //!< Reference to this class
    6970
    70     ALCdevice*               device;                   //!< the used audio-device.
    71     ALCcontext*              context;                  //!< the context, currently in use.
     71    ALCdevice*                     device;                   //!< the used audio-device.
     72    ALCcontext*                    context;                  //!< the context, currently in use.
    7273
    73     float                    musicVolume;              //!< the maximum volume of the music in % (0f,1f]
    74     float                    effectsVolume;            //!< the maximum volume of sound-effects in % (0f,1f]
    75     PNode*                   listener;                 //!< The listener of the Scene
     74    float                          musicVolume;              //!< the maximum volume of the music in % (0f,1f]
     75    float                          effectsVolume;            //!< the maximum volume of sound-effects in % (0f,1f]
     76    PNode*                         listener;                 //!< The listener of the Scene
    7677
    77     std::list<BaseObject*>*  bufferList;               //!< A list of buffers
    78     std::list<BaseObject*>*  sourceList;               //!< A list for all the sources in the scene.
     78    const std::list<BaseObject*>*  bufferList;               //!< A list of buffers
     79    const std::list<BaseObject*>*  sourceList;               //!< A list for all the sources in the scene.
    7980
    80 
     81    unsigned int                   maxSourceCount;           //!< How many Sources is the Maximum
     82    std::stack<ALuint>             ALSources;                //!< A list of real openAL-Sources, the engine allocates, and stores for reuse.
    8183};
    8284
  • branches/powerups/src/lib/sound/sound_source.cc

    r5386 r5955  
    1818#include "sound_source.h"
    1919#include "sound_engine.h"
     20
    2021#include "alincl.h"
    2122#include "compiler.h"
     
    3334
    3435  // adding the Source to the SourcesList of the SoundEngine
    35   SoundEngine::getInstance()->addSource(this);
    36 
    3736  this->buffer = buffer;
    3837  this->sourceNode = sourceNode;
    3938
    40   alGenSources(1, &this->sourceID);
    41   if ((result = alGetError()) != AL_NO_ERROR)
    42     SoundEngine::PrintALErrorString(result);
    43   if (this->buffer != NULL)
    44     alSourcei (this->sourceID, AL_BUFFER,   this->buffer->getID());
    45   alSourcef (this->sourceID, AL_PITCH,    1.0      );
    46   alSourcef (this->sourceID, AL_GAIN,     SoundEngine::getInstance()->getEffectsVolume() );
    47   alSourcei (sourceID, AL_LOOPING,  AL_FALSE     );
     39  this->sourceID = 0;
     40  this->bPlay = false;
    4841}
    4942
     
    5346SoundSource::~SoundSource()
    5447{
    55   //SoundEngine::getInstance()->removeSource(this);
    56   alDeleteSources(1, &this->sourceID);
     48  SoundEngine::getInstance()->pushALSource(this->sourceID);
    5749}
    5850
     
    6254void SoundSource::play()
    6355{
     56  if (this->sourceID == 0)
     57    SoundEngine::getInstance()->popALSource(this->sourceID);
    6458  alSourcePlay(this->sourceID);
     59  this->bPlay = true;
    6560}
    6661
     
    7166void SoundSource::play(const SoundBuffer* buffer)
    7267{
     68  if (unlikely(this->sourceID == 0))
     69    SoundEngine::getInstance()->popALSource(this->sourceID);
     70
     71  printf("%d\n",sourceID);
    7372  alSourceStop(this->sourceID);
    7473  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
     
    7776  if (unlikely(this->buffer != NULL))
    7877    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
     78  this->bPlay = true;
    7979}
    8080
     
    8484void SoundSource::stop()
    8585{
     86  this->bPlay = false;
    8687  alSourceStop(this->sourceID);
     88  SoundEngine::getInstance()->pushALSource(this->sourceID);
    8789}
    8890
  • branches/powerups/src/lib/sound/sound_source.h

    r5386 r5955  
    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; }
     
    3941
    4042  private:
     43    bool                   bPlay;                 //!< If the Source is Playing.
    4144    ALuint                 sourceID;              //!< The ID of the Source
    4245    const SoundBuffer*     buffer;                //!< The buffer to play in this source.
Note: See TracChangeset for help on using the changeset viewer.