Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9803 in orxonox.OLD for branches/new_class_id/src


Ignore:
Timestamp:
Sep 24, 2006, 12:58:19 PM (18 years ago)
Author:
bensch
Message:

Ported SoundBuffer to match the Data-Paradigm

Location:
branches/new_class_id/src/lib/sound
Files:
3 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/new_class_id/src/lib/sound/Makefile.am

    r5475 r9803  
    77                        sound_source.cc \
    88                        sound_buffer.cc \
     9                        sound_buffer_data.cc \
    910                        ogg_player.cc
    1011
     
    1213                 sound_source.h \
    1314                 sound_buffer.h \
     15                 sound_buffer_data.h \
    1416                 ogg_player.h
  • branches/new_class_id/src/lib/sound/sound_buffer.cc

    r9715 r9803  
    3939  /* SOUND-BUFFER */
    4040  //////////////////
     41  SoundBuffer::SoundBuffer()
     42  : data(new SoundBufferData)
     43  {
     44    this->registerObject(this, SoundBuffer::_objectList);
     45  }
    4146  /**
    42    * Creates a Soundbuffer out of an inputfile
     47   * @brief Creates a Soundbuffer out of an inputfile
    4348   * @param fileName The name of the File
    4449   */
    4550  SoundBuffer::SoundBuffer(const std::string& fileName)
     51  : data(new SoundBufferData)
    4652  {
    4753    this->registerObject(this, SoundBuffer::_objectList);
    4854    this->setName(fileName);
    4955
    50     // generate a Buffer
    51     alGenBuffers(1, &this->bufferID);
    52     SoundEngine::checkError("Generate Buffer", __LINE__);
    53     if (!nocaseCmp(fileName.substr(fileName.size() - 3), "WAV"))
    54     {
    55       this->loadWAV(fileName);
    56     }
    57     else if (!nocaseCmp(fileName.substr(fileName.size() - 3), "OGG"))
    58       this->loadOGG(fileName);
    59 
    60   }
    61 
    62   SoundBuffer::~SoundBuffer()
    63   {
    64     //  SoundEngine::getInstance()->removeBuffer(this);
    65     alDeleteBuffers(1, &this->bufferID);
    66     SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__);
    67   }
    68 
    69   /**
    70    * @brief loads a Waveform from the local fileSystem into this Source.
    71    * @param fileName the Name of the File to Load.
    72    * @returns true on success.
    73    */
    74   bool SoundBuffer::loadWAV(const std::string& fileName)
    75   {
    76     SDL_AudioSpec wavSpec;
    77     Uint32 wavLength;
    78     Uint8 *wavBuffer;
    79 
    80     /* Load the WAV */
    81     if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL)
    82     {
    83       PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError());
    84       return false;
    85     }
    86 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    87     if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) )
    88     {
    89       int cnt = wavLength/2;
    90       Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer;
    91       for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
    92         *wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
    93     }
    94 #endif
    95     alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec),
    96                  wavBuffer, wavLength, wavSpec.freq);
    97 
    98     SDL_FreeWAV(wavBuffer);
    99     if (SoundEngine::checkError("Could not load Wave file", __LINE__))
    100       return true;
    101     else
    102       return false;
    103   }
    104 
    105 
    106 #ifndef AL_FORMAT_VORBIS_EXT
    107 #define AL_FORMAT_VORBIS_EXT 0x100030
    108 #endif
    109   /**
    110    * @brief loads an OGG-file into a SOundBuffer
    111    * @param fileName the Name of the File to load.
    112    * @returns true on success (file exists and is fully loaded), false otherwise.
    113    */
    114   bool SoundBuffer::loadOGG(const std::string& fileName)
    115   {
    116     void*     ovdata;
    117     FILE*     fh;
    118 
    119     fh = fopen( fileName.c_str() , "rb") ;
    120     if( fh != NULL )
    121     {
    122       struct stat sbuf ;
    123 
    124       if(stat( fileName.c_str(), &sbuf ) != -1)
    125       {
    126         ovdata = malloc(sbuf.st_size);
    127         if(ovdata != NULL)
    128         {
    129           fread( ovdata, 1, sbuf.st_size, fh);
    130 
    131           alBufferData( this->bufferID,
    132                         AL_FORMAT_VORBIS_EXT,
    133                         ovdata,
    134                         sbuf.st_size,
    135                         1) ;
    136           SoundEngine::checkError("Could not load OGG file", __LINE__);
    137 
    138           free(ovdata);
    139         }
    140         fclose(fh);
    141       }
    142       else
    143         return false;
    144     }
    145     else
    146       return false;
    147 
    148     return true ;
    149 
    150   }
    151 
    152 
    153   /**
    154    * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator
    155    * @param audiospec the AudioSpec to convert.
    156    * @returns the AL_FORMAT
    157    */
    158   ALenum SoundBuffer::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
    159   {
    160     assert (audiospec != NULL);
    161     bool stereo = true;
    162     bool is16Bit = true;
    163     if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8)
    164       is16Bit = false;
    165     if (audiospec->channels == 1)
    166       stereo = false;
    167 
    168     if (!stereo && !is16Bit)
    169       return AL_FORMAT_MONO8;
    170     else if (!stereo && is16Bit)
    171       return AL_FORMAT_MONO16;
    172     else if (stereo && !is16Bit)
    173       return AL_FORMAT_STEREO8;
    174     else /* if (stereo && is16Bit) */
    175       return AL_FORMAT_STEREO16;
     56    this->load(fileName);
    17657  }
    17758}
  • branches/new_class_id/src/lib/sound/sound_buffer.h

    r9715 r9803  
    1010#include "alincl.h"
    1111
    12 // FORWARD DECLARATION
    13 typedef struct SDL_AudioSpec;
     12#include "sound_buffer_data.h"
    1413
    1514namespace OrxSound
     
    2019    ObjectListDeclaration(SoundBuffer);
    2120  public:
     21    SoundBuffer();
    2222    SoundBuffer(const std::string& fileName);
    23     virtual ~SoundBuffer();
    2423
    25     bool loadWAV(const std::string& fileName);
    26     bool loadOGG(const std::string& fileName);
     24    /** @see SoundBufferData::load */
     25    inline bool load(const std::string& fileName) { return this->data->load(fileName); };
     26    /** @see SoundBufferData::loadWav */
     27    inline bool loadWAV(const std::string& fileName) { return this->data->loadWAV(fileName); };
     28    /** @see SoundBufferData::loadOgg */
     29    inline bool loadOGG(const std::string& fileName) { return this->data->loadOGG(fileName); };
    2730
    2831    /** @returns the ID of the buffer used in this SoundBuffer */
    29     inline ALuint getID() const { return this->bufferID; }
     32    inline ALuint getID() const { return this->data->getID(); }
    3033
    3134  private:
    32     ALenum sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec);
    33 
    34   private:
    35     ALuint        bufferID;             //!< The address of the Buffer.
    36 
    37     ALsizei       size;                 //!< The size of the Buffer.
    38     ALboolean     loop;                 //!< loop information.
     35    SoundBufferData::Pointer    data;
    3936  };
    4037}
  • branches/new_class_id/src/lib/sound/sound_buffer_data.cc

    r9801 r9803  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND
    1717
    18 #include "sound_buffer.h"
     18#include "sound_buffer_data.h"
    1919
    2020#include "sound_engine.h"
     
    3535namespace OrxSound
    3636{
    37   ObjectListDefinition(SoundBuffer);
     37  ObjectListDefinition(SoundBufferData);
    3838  //////////////////
    3939  /* SOUND-BUFFER */
    4040  //////////////////
    4141  /**
    42    *  Creates a Soundbuffer out of an inputfile
     42   * @brief Creates a SoundbufferData out of an inputfile
    4343   * @param fileName The name of the File
    4444   */
    45   SoundBuffer::SoundBuffer(const std::string& fileName)
     45  SoundBufferData::SoundBufferData()
    4646  {
    47     this->registerObject(this, SoundBuffer::_objectList);
    48     this->setName(fileName);
     47    this->registerObject(this, SoundBufferData::_objectList);
     48    this->bufferID = 0;
    4949
    50     // generate a Buffer
    51     alGenBuffers(1, &this->bufferID);
    52     SoundEngine::checkError("Generate Buffer", __LINE__);
    53     if (!nocaseCmp(fileName.substr(fileName.size() - 3), "WAV"))
    54     {
    55       this->loadWAV(fileName);
    56     }
    57     else if (!nocaseCmp(fileName.substr(fileName.size() - 3), "OGG"))
    58       this->loadOGG(fileName);
     50    this->size = 0;
     51    this->loop = AL_FALSE;
     52
    5953
    6054  }
    6155
    62   SoundBuffer::~SoundBuffer()
     56  SoundBufferData::~SoundBufferData()
    6357  {
    6458    //  SoundEngine::getInstance()->removeBuffer(this);
     
    6862
    6963  /**
     64   * @brief check the File-extension and loads either Wav of Ogg
     65   * @param fileName the Name of the File to load.
     66   * @returns true on success (file found, and loaded.)
     67   */
     68  bool SoundBufferData::load(const std::string& fileName)
     69  {
     70    // generate a Buffer
     71    alGenBuffers(1, &this->bufferID);
     72    SoundEngine::checkError("Generate Buffer", __LINE__);
     73    if (!nocaseCmp(fileName.substr(fileName.size() - 3), "WAV"))
     74    {
     75      return this->loadWAV(fileName);
     76    }
     77    else if (!nocaseCmp(fileName.substr(fileName.size() - 3), "OGG"))
     78      return this->loadOGG(fileName);
     79    else
     80      return false;
     81  }
     82
     83  /**
    7084   * @brief loads a Waveform from the local fileSystem into this Source.
    7185   * @param fileName the Name of the File to Load.
    7286   * @returns true on success.
    7387   */
    74   bool SoundBuffer::loadWAV(const std::string& fileName)
     88  bool SoundBufferData::loadWAV(const std::string& fileName)
    7589  {
    7690    SDL_AudioSpec wavSpec;
     
    93107    }
    94108#endif
    95     alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec),
     109    alBufferData(this->bufferID, SoundBufferData::sdlAudioSpecToAlFormat(&wavSpec),
    96110                 wavBuffer, wavLength, wavSpec.freq);
    97111
     
    112126   * @returns true on success (file exists and is fully loaded), false otherwise.
    113127   */
    114   bool SoundBuffer::loadOGG(const std::string& fileName)
     128  bool SoundBufferData::loadOGG(const std::string& fileName)
    115129  {
    116130    void*     ovdata;
     
    156170   * @returns the AL_FORMAT
    157171   */
    158   ALenum SoundBuffer::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
     172  ALenum SoundBufferData::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
    159173  {
    160174    assert (audiospec != NULL);
  • branches/new_class_id/src/lib/sound/sound_buffer_data.h

    r9801 r9803  
    11/*!
    2  * @file sound_buffer.h
    3  * @brief Definition of the sound-buffer-class
     2 * @file sound_buffer_data.h
     3 * @brief Definition of the sound-buffer-datacontainer-class
    44*/
    55
    6 #ifndef _SOUND_BUFFER_H
    7 #define _SOUND_BUFFER_H
     6#ifndef _SOUND_BUFFER_DATA_H
     7#define _SOUND_BUFFER_DATA_H
    88
    99#include "base_object.h"
    1010#include "alincl.h"
     11#include "util/count_pointer.h"
    1112
    1213// FORWARD DECLARATION
     
    1617{
    1718  //! A class that represents a datastructure to play Sounds.
    18   class SoundBuffer : public BaseObject
     19  class SoundBufferData : public BaseObject
    1920  {
    20     ObjectListDeclaration(SoundBuffer);
     21    ObjectListDeclaration(SoundBufferData);
    2122  public:
    22     SoundBuffer(const std::string& fileName);
    23     virtual ~SoundBuffer();
     23    typedef CountPointer<SoundBufferData> Pointer;
    2424
     25  public:
     26    SoundBufferData();
     27    virtual ~SoundBufferData();
     28
     29    bool load(const std::string& fileName);
    2530    bool loadWAV(const std::string& fileName);
    2631    bool loadOGG(const std::string& fileName);
     
    3944  };
    4045}
    41 #endif /* _SOUND_BUFFER_H */
     46#endif /* _SOUND_BUFFER_DATA_H */
Note: See TracChangeset for help on using the changeset viewer.