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