Changeset 9869 in orxonox.OLD for trunk/src/lib/sound/sound_buffer.cc
- Timestamp:
- Oct 3, 2006, 12:19:30 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/sound/sound_buffer.cc
r8971 r9869 18 18 #include "sound_buffer.h" 19 19 20 #include "sound_engine.h"21 20 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_H29 #include <SDL/SDL.h>30 #include <SDL/SDL_endian.h>31 #else32 #include <SDL.h>33 #include <SDL_endian.h>34 #endif35 21 namespace OrxSound 36 22 { 23 ObjectListDefinition(SoundBuffer); 37 24 ////////////////// 38 25 /* SOUND-BUFFER */ 39 26 ////////////////// 27 SoundBuffer::SoundBuffer() 28 : data(new SoundBufferData) 29 { 30 this->registerObject(this, SoundBuffer::_objectList); 31 } 40 32 /** 41 * Creates a Soundbuffer out of an inputfile33 * @brief Creates a Soundbuffer out of an inputfile 42 34 * @param fileName The name of the File 43 35 */ 44 36 SoundBuffer::SoundBuffer(const std::string& fileName) 37 : data(new SoundBufferData) 45 38 { 46 this->setClassID(CL_SOUND_BUFFER, "SoundBuffer"); 47 this->setName(fileName); 39 this->registerObject(this, SoundBuffer::_objectList); 40 this->load(fileName); 41 } 48 42 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 } 58 48 59 } 49 SoundBuffer::SoundBuffer(const SoundBufferData::Pointer& dataPointer) 50 : data(dataPointer) 51 { 52 this->registerObject(this, SoundBuffer::_objectList); 53 }; 60 54 61 55 SoundBuffer::~SoundBuffer() 62 56 { 63 // SoundEngine::getInstance()->removeBuffer(this);64 alDeleteBuffers(1, &this->bufferID);65 SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__);66 57 } 67 58 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_ENDIAN86 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 #endif94 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 else101 return false;102 }103 104 105 #ifndef AL_FORMAT_VORBIS_EXT106 #define AL_FORMAT_VORBIS_EXT 0x100030107 #endif108 /**109 * @brief loads an OGG-file into a SOundBuffer110 * @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 else142 return false;143 }144 else145 return false;146 147 return true ;148 149 }150 151 152 /**153 * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator154 * @param audiospec the AudioSpec to convert.155 * @returns the AL_FORMAT156 */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 }176 59 }
Note: See TracChangeset
for help on using the changeset viewer.