[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" |
---|
| 25 | |
---|
[8293] | 26 | #ifdef HAVE_SDL_SDL_H |
---|
| 27 | #include <SDL/SDL.h> |
---|
| 28 | #include <SDL/SDL_endian.h> |
---|
| 29 | #else |
---|
| 30 | #include <SDL.h> |
---|
| 31 | #include <SDL_endian.h> |
---|
| 32 | #endif |
---|
[7460] | 33 | namespace OrxSound |
---|
[3365] | 34 | { |
---|
[7460] | 35 | ////////////////// |
---|
| 36 | /* SOUND-BUFFER */ |
---|
| 37 | ////////////////// |
---|
| 38 | /** |
---|
| 39 | * Creates a Soundbuffer out of an inputfile |
---|
| 40 | * @param fileName The name of the File |
---|
| 41 | */ |
---|
| 42 | SoundBuffer::SoundBuffer(const std::string& fileName) |
---|
| 43 | { |
---|
| 44 | this->setClassID(CL_SOUND_BUFFER, "SoundBuffer"); |
---|
| 45 | this->setName(fileName); |
---|
[4320] | 46 | |
---|
[7460] | 47 | // generate a Buffer |
---|
| 48 | alGenBuffers(1, &this->bufferID); |
---|
| 49 | SoundEngine::checkError("Generate Buffer", __LINE__); |
---|
| 50 | this->loadWAV(fileName); |
---|
| 51 | } |
---|
[5386] | 52 | |
---|
[7460] | 53 | SoundBuffer::~SoundBuffer() |
---|
| 54 | { |
---|
| 55 | // SoundEngine::getInstance()->removeBuffer(this); |
---|
| 56 | alDeleteBuffers(1, &this->bufferID); |
---|
| 57 | SoundEngine::checkError("SoundBuffer: Delete Buffer", __LINE__); |
---|
| 58 | } |
---|
[5386] | 59 | |
---|
[7460] | 60 | /** |
---|
| 61 | * @brief loads a Waveform from the local fileSystem into this Source. |
---|
| 62 | * @param fileName the Name of the File to Load. |
---|
| 63 | * @returns true on success. |
---|
| 64 | */ |
---|
| 65 | bool SoundBuffer::loadWAV(const std::string& fileName) |
---|
| 66 | { |
---|
| 67 | SDL_AudioSpec wavSpec; |
---|
| 68 | Uint32 wavLength; |
---|
| 69 | Uint8 *wavBuffer; |
---|
[5386] | 70 | |
---|
[7460] | 71 | /* Load the WAV */ |
---|
| 72 | if( SDL_LoadWAV(fileName.c_str(), &wavSpec, &wavBuffer, &wavLength) == NULL) |
---|
| 73 | { |
---|
| 74 | PRINTF(2)("Could not open %s: %s\n", fileName.c_str(), SDL_GetError()); |
---|
| 75 | return false; |
---|
| 76 | } |
---|
[8293] | 77 | #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
---|
| 78 | if ( !( wavSpec.format == AUDIO_U8 || wavSpec.format == AUDIO_S8 ) ) { |
---|
| 79 | int cnt = wavLength/2; |
---|
| 80 | Uint16* wavBufferAsShorts = ( Uint16* )wavBuffer; |
---|
| 81 | for ( int i = 0; i < cnt; ++i, ++wavBufferAsShorts ) |
---|
| 82 | *wavBufferAsShorts = SDL_Swap16( *wavBufferAsShorts ); |
---|
| 83 | } |
---|
| 84 | #endif |
---|
[8362] | 85 | alBufferData(this->bufferID, SoundBuffer::sdlAudioSpecToAlFormat(&wavSpec), |
---|
[8293] | 86 | wavBuffer, wavLength, wavSpec.freq); |
---|
[8362] | 87 | |
---|
[7460] | 88 | SDL_FreeWAV(wavBuffer); |
---|
| 89 | if (SoundEngine::checkError("Could not load Wave file", __LINE__)) |
---|
| 90 | return true; |
---|
| 91 | else |
---|
| 92 | return false; |
---|
[6836] | 93 | } |
---|
| 94 | |
---|
[7460] | 95 | /** |
---|
| 96 | * @brief converts an SDL_AudioSpec into a valid OpenAL AUDIO_FORMAT enumerator |
---|
| 97 | * @param audiospec the AudioSpec to convert. |
---|
| 98 | * @returns the AL_FORMAT |
---|
| 99 | */ |
---|
| 100 | ALenum SoundBuffer::sdlAudioSpecToAlFormat(const SDL_AudioSpec* audiospec) |
---|
| 101 | { |
---|
| 102 | assert (audiospec != NULL); |
---|
| 103 | bool stereo = true; |
---|
| 104 | bool is16Bit = true; |
---|
| 105 | if (audiospec->format == AUDIO_U8 || audiospec->format == AUDIO_S8) |
---|
| 106 | is16Bit = false; |
---|
| 107 | if (audiospec->channels == 1) |
---|
| 108 | stereo = false; |
---|
[1853] | 109 | |
---|
[7460] | 110 | if (!stereo && !is16Bit) |
---|
| 111 | return AL_FORMAT_MONO8; |
---|
| 112 | else if (!stereo && is16Bit) |
---|
| 113 | return AL_FORMAT_MONO16; |
---|
| 114 | else if (stereo && !is16Bit) |
---|
| 115 | return AL_FORMAT_STEREO8; |
---|
[8528] | 116 | else /* if (stereo && is16Bit) */ |
---|
[7460] | 117 | return AL_FORMAT_STEREO16; |
---|
| 118 | } |
---|
[3543] | 119 | } |
---|