[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 | |
---|
[5386] | 18 | #include "sound_buffer.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 | |
---|
[8293] | 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 |
---|
[7460] | 35 | namespace OrxSound |
---|
[3365] | 36 | { |
---|
[9715] | 37 | ObjectListDefinition(SoundBuffer); |
---|
[7460] | 38 | ////////////////// |
---|
| 39 | /* SOUND-BUFFER */ |
---|
| 40 | ////////////////// |
---|
| 41 | /** |
---|
| 42 | * Creates a Soundbuffer out of an inputfile |
---|
| 43 | * @param fileName The name of the File |
---|
| 44 | */ |
---|
| 45 | SoundBuffer::SoundBuffer(const std::string& fileName) |
---|
| 46 | { |
---|
[9686] | 47 | this->registerObject(this, SoundBuffer::_objectList); |
---|
[7460] | 48 | this->setName(fileName); |
---|
[4320] | 49 | |
---|
[7460] | 50 | // generate a Buffer |
---|
| 51 | alGenBuffers(1, &this->bufferID); |
---|
| 52 | SoundEngine::checkError("Generate Buffer", __LINE__); |
---|
[8969] | 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 | |
---|
[7460] | 60 | } |
---|
[5386] | 61 | |
---|
[7460] | 62 | SoundBuffer::~SoundBuffer() |
---|
| 63 | { |
---|
| 64 | // SoundEngine::getInstance()->removeBuffer(this); |
---|
| 65 | alDeleteBuffers(1, &this->bufferID); |
---|
| 66 | SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__); |
---|
| 67 | } |
---|
[5386] | 68 | |
---|
[7460] | 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; |
---|
[5386] | 79 | |
---|
[7460] | 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 | } |
---|
[8293] | 86 | #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
---|
[8969] | 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 | } |
---|
[8293] | 94 | #endif |
---|
[8362] | 95 | alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec), |
---|
[8969] | 96 | wavBuffer, wavLength, wavSpec.freq); |
---|
[8362] | 97 | |
---|
[7460] | 98 | SDL_FreeWAV(wavBuffer); |
---|
| 99 | if (SoundEngine::checkError("Could not load Wave file", __LINE__)) |
---|
| 100 | return true; |
---|
| 101 | else |
---|
| 102 | return false; |
---|
[6836] | 103 | } |
---|
| 104 | |
---|
[8969] | 105 | |
---|
| 106 | #ifndef AL_FORMAT_VORBIS_EXT |
---|
| 107 | #define AL_FORMAT_VORBIS_EXT 0x100030 |
---|
| 108 | #endif |
---|
[8970] | 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 | */ |
---|
[8969] | 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) ; |
---|
[8971] | 136 | SoundEngine::checkError("Could not load OGG file", __LINE__); |
---|
[8969] | 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 | |
---|
[7460] | 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; |
---|
[1853] | 167 | |
---|
[7460] | 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; |
---|
[8619] | 174 | else /* if (stereo && is16Bit) */ |
---|
[7460] | 175 | return AL_FORMAT_STEREO16; |
---|
| 176 | } |
---|
[3543] | 177 | } |
---|