Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9869 in orxonox.OLD for trunk/src/lib/sound/sound_buffer.cc


Ignore:
Timestamp:
Oct 3, 2006, 12:19:30 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File:
1 edited

Legend:

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

    r8971 r9869  
    1818#include "sound_buffer.h"
    1919
    20 #include "sound_engine.h"
    2120
    22 #include "sdlincl.h"
    23 #include <cassert>
    24 #include "debug.h"
    25 #include "sys/stat.h"
    26 #include "helper_functions.h"
    27 
    28 #ifdef HAVE_SDL_SDL_H
    29 #include <SDL/SDL.h>
    30 #include <SDL/SDL_endian.h>
    31 #else
    32 #include <SDL.h>
    33 #include <SDL_endian.h>
    34 #endif
    3521namespace OrxSound
    3622{
     23  ObjectListDefinition(SoundBuffer);
    3724  //////////////////
    3825  /* SOUND-BUFFER */
    3926  //////////////////
     27  SoundBuffer::SoundBuffer()
     28      : data(new SoundBufferData)
     29  {
     30    this->registerObject(this, SoundBuffer::_objectList);
     31  }
    4032  /**
    41    * Creates a Soundbuffer out of an inputfile
     33   * @brief Creates a Soundbuffer out of an inputfile
    4234   * @param fileName The name of the File
    4335   */
    4436  SoundBuffer::SoundBuffer(const std::string& fileName)
     37      : data(new SoundBufferData)
    4538  {
    46     this->setClassID(CL_SOUND_BUFFER, "SoundBuffer");
    47     this->setName(fileName);
     39    this->registerObject(this, SoundBuffer::_objectList);
     40    this->load(fileName);
     41  }
    4842
    49     // generate a Buffer
    50     alGenBuffers(1, &this->bufferID);
    51     SoundEngine::checkError("Generate Buffer", __LINE__);
    52     if (!nocaseCmp(fileName.substr(fileName.size() - 3), "WAV"))
    53     {
    54       this->loadWAV(fileName);
    55     }
    56     else if (!nocaseCmp(fileName.substr(fileName.size() - 3), "OGG"))
    57       this->loadOGG(fileName);
     43  SoundBuffer::SoundBuffer(const SoundBuffer& buffer)
     44      : data(buffer.data)
     45  {
     46    this->registerObject(this, SoundBuffer::_objectList);
     47  }
    5848
    59   }
     49  SoundBuffer::SoundBuffer(const SoundBufferData::Pointer& dataPointer)
     50      : data(dataPointer)
     51  {
     52    this->registerObject(this, SoundBuffer::_objectList);
     53  };
    6054
    6155  SoundBuffer::~SoundBuffer()
    6256  {
    63     //  SoundEngine::getInstance()->removeBuffer(this);
    64     alDeleteBuffers(1, &this->bufferID);
    65     SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__);
    6657  }
    6758
    68   /**
    69    * @brief loads a Waveform from the local fileSystem into this Source.
    70    * @param fileName the Name of the File to Load.
    71    * @returns true on success.
    72    */
    73   bool SoundBuffer::loadWAV(const std::string& fileName)
    74   {
    75     SDL_AudioSpec wavSpec;
    76     Uint32 wavLength;
    77     Uint8 *wavBuffer;
    78 
    79     /* Load the WAV */
    80     if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL)
    81     {
    82       PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError());
    83       return false;
    84     }
    85 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
    86     if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) )
    87     {
    88       int cnt = wavLength/2;
    89       Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer;
    90       for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts )
    91         *wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts );
    92     }
    93 #endif
    94     alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec),
    95                  wavBuffer, wavLength, wavSpec.freq);
    96 
    97     SDL_FreeWAV(wavBuffer);
    98     if (SoundEngine::checkError("Could not load Wave file", __LINE__))
    99       return true;
    100     else
    101       return false;
    102   }
    103 
    104 
    105 #ifndef AL_FORMAT_VORBIS_EXT
    106 #define AL_FORMAT_VORBIS_EXT 0x100030
    107 #endif
    108   /**
    109    * @brief loads an OGG-file into a SOundBuffer
    110    * @param fileName the Name of the File to load.
    111    * @returns true on success (file exists and is fully loaded), false otherwise.
    112    */
    113   bool SoundBuffer::loadOGG(const std::string& fileName)
    114   {
    115     void*     ovdata;
    116     FILE*     fh;
    117 
    118     fh = fopen( fileName.c_str() , "rb") ;
    119     if( fh != NULL )
    120     {
    121       struct stat sbuf ;
    122 
    123       if(stat( fileName.c_str(), &sbuf ) != -1)
    124       {
    125         ovdata = malloc(sbuf.st_size);
    126         if(ovdata != NULL)
    127         {
    128           fread( ovdata, 1, sbuf.st_size, fh);
    129 
    130           alBufferData( this->bufferID,
    131                         AL_FORMAT_VORBIS_EXT,
    132                         ovdata,
    133                         sbuf.st_size,
    134                         1) ;
    135           SoundEngine::checkError("Could not load OGG file", __LINE__);
    136 
    137           free(ovdata);
    138         }
    139         fclose(fh);
    140       }
    141       else
    142         return false;
    143     }
    144     else
    145       return false;
    146 
    147     return true ;
    148 
    149   }
    150 
    151 
    152   /**
    153    * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator
    154    * @param audiospec the AudioSpec to convert.
    155    * @returns the AL_FORMAT
    156    */
    157   ALenum SoundBuffer::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
    158   {
    159     assert (audiospec != NULL);
    160     bool stereo = true;
    161     bool is16Bit = true;
    162     if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8)
    163       is16Bit = false;
    164     if (audiospec->channels == 1)
    165       stereo = false;
    166 
    167     if (!stereo && !is16Bit)
    168       return AL_FORMAT_MONO8;
    169     else if (!stereo && is16Bit)
    170       return AL_FORMAT_MONO16;
    171     else if (stereo && !is16Bit)
    172       return AL_FORMAT_STEREO8;
    173     else /* if (stereo && is16Bit) */
    174       return AL_FORMAT_STEREO16;
    175   }
    17659}
Note: See TracChangeset for help on using the changeset viewer.