[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5386] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[5386] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND |
---|
[1853] | 17 | |
---|
[9803] | 18 | #include "sound_buffer_data.h" |
---|
[1853] | 19 | |
---|
[5386] | 20 | #include "sound_engine.h" |
---|
| 21 | |
---|
[6836] | 22 | #include "sdlincl.h" |
---|
| 23 | #include <cassert> |
---|
[8362] | 24 | #include "debug.h" |
---|
[8969] | 25 | #include "sys/stat.h" |
---|
| 26 | #include "helper_functions.h" |
---|
[8362] | 27 | |
---|
[9812] | 28 | /*#ifdef HAVE_SDL_SDL_H |
---|
| 29 | #include <SDL/SDL.h> |
---|
| 30 | // #include <SDL/SDL_endian.h> |
---|
[8293] | 31 | #else |
---|
[9812] | 32 | #include <SDL.h> |
---|
| 33 | //#include <SDL_endian.h> |
---|
| 34 | #endif*/ |
---|
[7460] | 35 | namespace OrxSound |
---|
[3365] | 36 | { |
---|
[9803] | 37 | ObjectListDefinition(SoundBufferData); |
---|
[7460] | 38 | ////////////////// |
---|
| 39 | /* SOUND-BUFFER */ |
---|
| 40 | ////////////////// |
---|
| 41 | /** |
---|
[9803] | 42 | * @brief Creates a SoundbufferData out of an inputfile |
---|
[7460] | 43 | * @param fileName The name of the File |
---|
| 44 | */ |
---|
[9803] | 45 | SoundBufferData::SoundBufferData() |
---|
[7460] | 46 | { |
---|
[9803] | 47 | this->registerObject(this, SoundBufferData::_objectList); |
---|
| 48 | this->bufferID = 0; |
---|
[4320] | 49 | |
---|
[9803] | 50 | this->size = 0; |
---|
| 51 | this->loop = AL_FALSE; |
---|
[9805] | 52 | this->bLoaded = false; |
---|
[7460] | 53 | } |
---|
[5386] | 54 | |
---|
[9803] | 55 | SoundBufferData::~SoundBufferData() |
---|
[7460] | 56 | { |
---|
| 57 | // SoundEngine::getInstance()->removeBuffer(this); |
---|
| 58 | alDeleteBuffers(1, &this->bufferID); |
---|
| 59 | SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__); |
---|
| 60 | } |
---|
[5386] | 61 | |
---|
[7460] | 62 | /** |
---|
[9803] | 63 | * @brief check the File-extension and loads either Wav of Ogg |
---|
| 64 | * @param fileName the Name of the File to load. |
---|
| 65 | * @returns true on success (file found, and loaded.) |
---|
| 66 | */ |
---|
| 67 | bool SoundBufferData::load(const std::string& fileName) |
---|
| 68 | { |
---|
| 69 | // generate a Buffer |
---|
| 70 | alGenBuffers(1, &this->bufferID); |
---|
| 71 | SoundEngine::checkError("Generate Buffer", __LINE__); |
---|
| 72 | if (!nocaseCmp(fileName.substr(fileName.size() - 3), "WAV")) |
---|
| 73 | { |
---|
| 74 | return this->loadWAV(fileName); |
---|
| 75 | } |
---|
| 76 | else if (!nocaseCmp(fileName.substr(fileName.size() - 3), "OGG")) |
---|
| 77 | return this->loadOGG(fileName); |
---|
| 78 | else |
---|
| 79 | return false; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
[7460] | 83 | * @brief loads a Waveform from the local fileSystem into this Source. |
---|
| 84 | * @param fileName the Name of the File to Load. |
---|
| 85 | * @returns true on success. |
---|
| 86 | */ |
---|
[9803] | 87 | bool SoundBufferData::loadWAV(const std::string& fileName) |
---|
[7460] | 88 | { |
---|
| 89 | SDL_AudioSpec wavSpec; |
---|
| 90 | Uint32 wavLength; |
---|
| 91 | Uint8 *wavBuffer; |
---|
[5386] | 92 | |
---|
[7460] | 93 | /* Load the WAV */ |
---|
| 94 | if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL) |
---|
| 95 | { |
---|
| 96 | PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError()); |
---|
| 97 | return false; |
---|
| 98 | } |
---|
[8293] | 99 | #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
---|
[8969] | 100 | if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) ) |
---|
| 101 | { |
---|
| 102 | int cnt = wavLength/2; |
---|
| 103 | Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer; |
---|
| 104 | for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts ) |
---|
| 105 | *wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts ); |
---|
| 106 | } |
---|
[8293] | 107 | #endif |
---|
[9803] | 108 | alBufferData(this->bufferID, SoundBufferData::sdlAudioSpecToAlFormat(&wavSpec), |
---|
[8969] | 109 | wavBuffer, wavLength, wavSpec.freq); |
---|
[8362] | 110 | |
---|
[7460] | 111 | SDL_FreeWAV(wavBuffer); |
---|
| 112 | if (SoundEngine::checkError("Could not load Wave file", __LINE__)) |
---|
[9805] | 113 | { |
---|
| 114 | this->bLoaded = true; |
---|
[7460] | 115 | return true; |
---|
[9805] | 116 | } |
---|
[7460] | 117 | else |
---|
| 118 | return false; |
---|
[6836] | 119 | } |
---|
| 120 | |
---|
[8969] | 121 | |
---|
| 122 | #ifndef AL_FORMAT_VORBIS_EXT |
---|
| 123 | #define AL_FORMAT_VORBIS_EXT 0x100030 |
---|
| 124 | #endif |
---|
[8970] | 125 | /** |
---|
| 126 | * @brief loads an OGG-file into a SOundBuffer |
---|
| 127 | * @param fileName the Name of the File to load. |
---|
| 128 | * @returns true on success (file exists and is fully loaded), false otherwise. |
---|
| 129 | */ |
---|
[9803] | 130 | bool SoundBufferData::loadOGG(const std::string& fileName) |
---|
[8969] | 131 | { |
---|
| 132 | void* ovdata; |
---|
| 133 | FILE* fh; |
---|
| 134 | |
---|
| 135 | fh = fopen( fileName.c_str() , "rb") ; |
---|
| 136 | if( fh != NULL ) |
---|
| 137 | { |
---|
| 138 | struct stat sbuf ; |
---|
| 139 | |
---|
| 140 | if(stat( fileName.c_str(), &sbuf ) != -1) |
---|
| 141 | { |
---|
| 142 | ovdata = malloc(sbuf.st_size); |
---|
| 143 | if(ovdata != NULL) |
---|
| 144 | { |
---|
| 145 | fread( ovdata, 1, sbuf.st_size, fh); |
---|
| 146 | |
---|
| 147 | alBufferData( this->bufferID, |
---|
| 148 | AL_FORMAT_VORBIS_EXT, |
---|
| 149 | ovdata, |
---|
| 150 | sbuf.st_size, |
---|
| 151 | 1) ; |
---|
[8971] | 152 | SoundEngine::checkError("Could not load OGG file", __LINE__); |
---|
[8969] | 153 | |
---|
| 154 | free(ovdata); |
---|
| 155 | } |
---|
| 156 | fclose(fh); |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | return false; |
---|
| 160 | } |
---|
| 161 | else |
---|
| 162 | return false; |
---|
| 163 | |
---|
[9805] | 164 | this->bLoaded = true; |
---|
[8969] | 165 | return true ; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
[7460] | 169 | /** |
---|
| 170 | * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator |
---|
| 171 | * @param audiospec the AudioSpec to convert. |
---|
| 172 | * @returns the AL_FORMAT |
---|
| 173 | */ |
---|
[9803] | 174 | ALenum SoundBufferData::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec) |
---|
[7460] | 175 | { |
---|
| 176 | assert (audiospec != NULL); |
---|
| 177 | bool stereo = true; |
---|
| 178 | bool is16Bit = true; |
---|
| 179 | if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8) |
---|
| 180 | is16Bit = false; |
---|
| 181 | if (audiospec->channels == 1) |
---|
| 182 | stereo = false; |
---|
[1853] | 183 | |
---|
[7460] | 184 | if (!stereo && !is16Bit) |
---|
| 185 | return AL_FORMAT_MONO8; |
---|
| 186 | else if (!stereo && is16Bit) |
---|
| 187 | return AL_FORMAT_MONO16; |
---|
| 188 | else if (stereo && !is16Bit) |
---|
| 189 | return AL_FORMAT_STEREO8; |
---|
[8619] | 190 | else /* if (stereo && is16Bit) */ |
---|
[7460] | 191 | return AL_FORMAT_STEREO16; |
---|
| 192 | } |
---|
[3543] | 193 | } |
---|