Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Ported SoundBuffer to match the Data-Paradigm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.