Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
May 1, 2006, 12:30:34 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: Namespaces for sound

File:
1 edited

Legend:

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

    r7290 r7460  
    2323#include <cassert>
    2424
    25 using namespace std;
     25namespace OrxSound
     26{
     27  //////////////////
     28  /* SOUND-BUFFER */
     29  //////////////////
     30  /**
     31   *  Creates a Soundbuffer out of an inputfile
     32   * @param fileName The name of the File
     33   */
     34  SoundBuffer::SoundBuffer(const std::string& fileName)
     35  {
     36    this->setClassID(CL_SOUND_BUFFER, "SoundBuffer");
     37    this->setName(fileName);
    2638
    27 //////////////////
    28 /* SOUND-BUFFER */
    29 //////////////////
    30 /**
    31  *  Creates a Soundbuffer out of an inputfile
    32  * @param fileName The name of the File
    33  */
    34 SoundBuffer::SoundBuffer(const std::string& fileName)
    35 {
    36   this->setClassID(CL_SOUND_BUFFER, "SoundBuffer");
    37   this->setName(fileName);
    38 
    39   // generate a Buffer
    40   alGenBuffers(1, &this->bufferID);
    41   SoundEngine::checkError("Generate Buffer", __LINE__);
    42   this->loadWAV(fileName);
    43 }
    44 
    45 SoundBuffer::~SoundBuffer()
    46 {
    47   //  SoundEngine::getInstance()->removeBuffer(this);
    48   alDeleteBuffers(1, &this->bufferID);
    49   SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__);
    50 }
    51 
    52 /**
    53  * @brief loads a Waveform from the local fileSystem into this Source.
    54  * @param fileName the Name of the File to Load.
    55  * @returns true on success.
    56  */
    57 bool SoundBuffer::loadWAV(const std::string& fileName)
    58 {
    59   SDL_AudioSpec wavSpec;
    60   Uint32 wavLength;
    61   Uint8 *wavBuffer;
    62 
    63   /* Load the WAV */
    64   if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL)
    65   {
    66     PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError());
    67     return false;
     39    // generate a Buffer
     40    alGenBuffers(1, &this->bufferID);
     41    SoundEngine::checkError("Generate Buffer", __LINE__);
     42    this->loadWAV(fileName);
    6843  }
    6944
    70   alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec), wavBuffer, wavLength, wavSpec.freq);
    71   SDL_FreeWAV(wavBuffer);
    72   if (SoundEngine::checkError("Could not load Wave file", __LINE__))
    73     return true;
    74   else
    75     return false;
     45  SoundBuffer::~SoundBuffer()
     46  {
     47    //  SoundEngine::getInstance()->removeBuffer(this);
     48    alDeleteBuffers(1, &this->bufferID);
     49    SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__);
     50  }
     51
     52  /**
     53   * @brief loads a Waveform from the local fileSystem into this Source.
     54   * @param fileName the Name of the File to Load.
     55   * @returns true on success.
     56   */
     57  bool SoundBuffer::loadWAV(const std::string& fileName)
     58  {
     59    SDL_AudioSpec wavSpec;
     60    Uint32 wavLength;
     61    Uint8 *wavBuffer;
     62
     63    /* Load the WAV */
     64    if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL)
     65    {
     66      PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError());
     67      return false;
     68    }
     69
     70    alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec), wavBuffer, wavLength, wavSpec.freq);
     71    SDL_FreeWAV(wavBuffer);
     72    if (SoundEngine::checkError("Could not load Wave file", __LINE__))
     73      return true;
     74    else
     75      return false;
     76  }
     77
     78  /**
     79   * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator
     80   * @param audiospec the AudioSpec to convert.
     81   * @returns the AL_FORMAT
     82   */
     83  ALenum SoundBuffer::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
     84  {
     85    assert (audiospec != NULL);
     86    bool stereo = true;
     87    bool is16Bit = true;
     88    if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8)
     89      is16Bit = false;
     90    if (audiospec->channels == 1)
     91      stereo = false;
     92
     93    if (!stereo && !is16Bit)
     94      return AL_FORMAT_MONO8;
     95    else if (!stereo && is16Bit)
     96      return AL_FORMAT_MONO16;
     97    else if (stereo && !is16Bit)
     98      return AL_FORMAT_STEREO8;
     99    else if (stereo && is16Bit)
     100      return AL_FORMAT_STEREO16;
     101  }
    76102}
    77 
    78 /**
    79  * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator
    80  * @param audiospec the AudioSpec to convert.
    81  * @returns the AL_FORMAT
    82  */
    83 ALenum SoundBuffer::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec)
    84 {
    85   assert (audiospec != NULL);
    86   bool stereo = true;
    87   bool is16Bit = true;
    88   if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8)
    89     is16Bit = false;
    90   if (audiospec->channels == 1)
    91     stereo = false;
    92 
    93   if (!stereo && !is16Bit)
    94     return AL_FORMAT_MONO8;
    95   else if (!stereo && is16Bit)
    96     return AL_FORMAT_MONO16;
    97   else if (stereo && !is16Bit)
    98     return AL_FORMAT_STEREO8;
    99   else if (stereo && is16Bit)
    100     return AL_FORMAT_STEREO16;
    101 }
    102 
Note: See TracChangeset for help on using the changeset viewer.