[6203] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Reto Grieder |
---|
| 24 | * Erwin 'vaiursch' Herrsche |
---|
| 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "SoundBuffer.h" |
---|
| 31 | |
---|
| 32 | #include <AL/alut.h> |
---|
| 33 | #include <vorbis/vorbisfile.h> |
---|
| 34 | |
---|
[6332] | 35 | #include "util/Clock.h" |
---|
| 36 | #include "core/Game.h" |
---|
[6203] | 37 | #include "util/Exception.h" |
---|
| 38 | #include "util/StringUtils.h" |
---|
| 39 | #include "core/Resource.h" |
---|
| 40 | #include "sound/SoundManager.h" |
---|
| 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[6332] | 44 | SoundBuffer::SoundBuffer(const std::string& filename, std::list<shared_ptr<SoundBuffer> >::iterator poolIterator) |
---|
[6270] | 45 | : filename_(filename) |
---|
[6203] | 46 | , audioBuffer_(AL_NONE) |
---|
[6332] | 47 | , poolIterator_(poolIterator) |
---|
[6203] | 48 | { |
---|
[6270] | 49 | if (this->filename_.empty()) |
---|
[6203] | 50 | ThrowException(General, "SoundBuffer construction: fileInfo was NULL"); |
---|
| 51 | |
---|
[6270] | 52 | // Get resource info |
---|
| 53 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(filename); |
---|
| 54 | if (fileInfo == NULL) |
---|
| 55 | { |
---|
| 56 | COUT(2) << "Sound: Warning: Sound file '" << filename << "' not found" << std::endl; |
---|
| 57 | return; |
---|
| 58 | } |
---|
| 59 | // Open data stream |
---|
| 60 | DataStreamPtr dataStream = Resource::open(fileInfo); |
---|
| 61 | |
---|
| 62 | std::string extension(this->filename_.substr(this->filename_.find_last_of('.') + 1)); |
---|
[6203] | 63 | if (getLowercase(extension) == "ogg") |
---|
| 64 | { |
---|
[6332] | 65 | int before = Game::getInstance().getGameClock().getRealMicroseconds(); |
---|
[6203] | 66 | // Try ogg loader |
---|
[6270] | 67 | this->loadOgg(fileInfo, dataStream); |
---|
[6332] | 68 | int after = Game::getInstance().getGameClock().getRealMicroseconds(); |
---|
| 69 | COUT(0) << filename << ": " << (after - before) << std::endl; |
---|
[6203] | 70 | } |
---|
| 71 | else |
---|
| 72 | { |
---|
| 73 | // Try standard OpenAL loader |
---|
[6270] | 74 | this->loadStandard(fileInfo, dataStream); |
---|
[6203] | 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | SoundBuffer::~SoundBuffer() |
---|
| 79 | { |
---|
| 80 | // Destroy buffer |
---|
| 81 | alDeleteBuffers(1, &this->audioBuffer_); |
---|
| 82 | } |
---|
| 83 | |
---|
[6254] | 84 | unsigned int SoundBuffer::getSize() const |
---|
| 85 | { |
---|
| 86 | ALint size; |
---|
| 87 | alGetBufferi(this->audioBuffer_, AL_SIZE, &size); |
---|
[6332] | 88 | return alGetError() ? 0 : size; |
---|
[6254] | 89 | } |
---|
| 90 | |
---|
[6270] | 91 | void SoundBuffer::loadStandard(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream) |
---|
[6203] | 92 | { |
---|
| 93 | // Read everything into a temporary buffer |
---|
[6270] | 94 | char* buffer = new char[fileInfo->size]; |
---|
| 95 | dataStream->read(buffer, fileInfo->size); |
---|
[6203] | 96 | dataStream->seek(0); |
---|
| 97 | |
---|
[6270] | 98 | this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size); |
---|
[6203] | 99 | delete[] buffer; |
---|
| 100 | |
---|
[6260] | 101 | if (!alIsBuffer(this->audioBuffer_)) |
---|
[6232] | 102 | ThrowException(General, "Sound Error: Standard file loader failed: " << alutGetErrorString(alutGetError())); |
---|
[6203] | 103 | } |
---|
| 104 | |
---|
| 105 | size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource) |
---|
| 106 | { |
---|
| 107 | return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | int seekVorbis(void* datasource, ogg_int64_t offset, int whence) |
---|
| 111 | { |
---|
| 112 | Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource); |
---|
[6332] | 113 | switch (whence) |
---|
| 114 | { |
---|
| 115 | case SEEK_SET: |
---|
| 116 | stream->seek(offset); |
---|
| 117 | break; |
---|
| 118 | case SEEK_CUR: |
---|
| 119 | stream->skip(offset); |
---|
| 120 | break; |
---|
| 121 | case SEEK_END: |
---|
| 122 | stream->seek(stream->size() + offset); |
---|
| 123 | break; |
---|
| 124 | default: |
---|
[6203] | 125 | return -1; |
---|
[6332] | 126 | } |
---|
[6203] | 127 | return 0; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | long tellVorbis(void* datasource) |
---|
| 131 | { |
---|
| 132 | return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell()); |
---|
| 133 | } |
---|
| 134 | |
---|
[6270] | 135 | void SoundBuffer::loadOgg(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream) |
---|
[6203] | 136 | { |
---|
| 137 | char inbuffer[256*1024]; |
---|
| 138 | std::vector<char> outbuffer; |
---|
| 139 | outbuffer.reserve(80*1024*1024); |
---|
| 140 | |
---|
| 141 | // Open file with custom streaming |
---|
| 142 | ov_callbacks vorbisCallbacks; |
---|
| 143 | vorbisCallbacks.read_func = &readVorbis; |
---|
| 144 | vorbisCallbacks.seek_func = &seekVorbis; |
---|
| 145 | vorbisCallbacks.tell_func = &tellVorbis; |
---|
| 146 | vorbisCallbacks.close_func = NULL; |
---|
| 147 | |
---|
| 148 | OggVorbis_File vf; |
---|
| 149 | int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks); |
---|
| 150 | if (ret < 0) |
---|
| 151 | { |
---|
| 152 | COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; |
---|
| 153 | ov_clear(&vf); |
---|
[6232] | 154 | ThrowException(General, "Sound Error: Ogg file loader failed when opening the bitstream"); |
---|
[6203] | 155 | } |
---|
| 156 | |
---|
| 157 | int current_section; |
---|
| 158 | int eof = false; |
---|
| 159 | while (!eof) |
---|
| 160 | { |
---|
| 161 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
| 162 | if (ret == 0) |
---|
| 163 | { |
---|
| 164 | eof = true; |
---|
| 165 | } |
---|
| 166 | else if (ret < 0) |
---|
| 167 | { |
---|
| 168 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
| 169 | ov_clear(&vf); |
---|
[6232] | 170 | ThrowException(General, "Sound Error: Ogg file loader failed when decoding the file"); |
---|
[6203] | 171 | } |
---|
| 172 | else |
---|
| 173 | { |
---|
| 174 | outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + ret); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | vorbis_info* vorbisInfo; |
---|
| 179 | vorbisInfo = ov_info(&vf, -1); |
---|
| 180 | ALenum format; |
---|
| 181 | if (vorbisInfo->channels == 1) |
---|
| 182 | format = AL_FORMAT_MONO16; |
---|
| 183 | else |
---|
| 184 | format = AL_FORMAT_STEREO16; |
---|
| 185 | |
---|
| 186 | alGenBuffers(1, &this->audioBuffer_); |
---|
| 187 | alBufferData(this->audioBuffer_, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate); |
---|
| 188 | ov_clear(&vf); |
---|
| 189 | |
---|
[6260] | 190 | if (!alIsBuffer(this->audioBuffer_)) |
---|
[6203] | 191 | ThrowException(General, "Sound: Ogg file loader failed when creating the buffer."); |
---|
| 192 | } |
---|
| 193 | } |
---|