| [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 |  | 
|---|
|  | 35 | #include "util/Exception.h" | 
|---|
|  | 36 | #include "util/StringUtils.h" | 
|---|
|  | 37 | #include "core/Resource.h" | 
|---|
|  | 38 | #include "sound/SoundManager.h" | 
|---|
|  | 39 |  | 
|---|
|  | 40 | namespace orxonox | 
|---|
|  | 41 | { | 
|---|
| [6332] | 42 | SoundBuffer::SoundBuffer(const std::string& filename, std::list<shared_ptr<SoundBuffer> >::iterator poolIterator) | 
|---|
| [6270] | 43 | : filename_(filename) | 
|---|
| [6203] | 44 | , audioBuffer_(AL_NONE) | 
|---|
| [6332] | 45 | , poolIterator_(poolIterator) | 
|---|
| [6203] | 46 | { | 
|---|
| [6270] | 47 | if (this->filename_.empty()) | 
|---|
| [6203] | 48 | ThrowException(General, "SoundBuffer construction: fileInfo was NULL"); | 
|---|
|  | 49 |  | 
|---|
| [6270] | 50 | // Get resource info | 
|---|
|  | 51 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(filename); | 
|---|
|  | 52 | if (fileInfo == NULL) | 
|---|
|  | 53 | { | 
|---|
|  | 54 | COUT(2) << "Sound: Warning: Sound file '" << filename << "' not found" << std::endl; | 
|---|
|  | 55 | return; | 
|---|
|  | 56 | } | 
|---|
|  | 57 | // Open data stream | 
|---|
|  | 58 | DataStreamPtr dataStream = Resource::open(fileInfo); | 
|---|
|  | 59 |  | 
|---|
|  | 60 | std::string extension(this->filename_.substr(this->filename_.find_last_of('.') + 1)); | 
|---|
| [6203] | 61 | if (getLowercase(extension) == "ogg") | 
|---|
|  | 62 | { | 
|---|
|  | 63 | // Try ogg loader | 
|---|
| [6270] | 64 | this->loadOgg(fileInfo, dataStream); | 
|---|
| [6203] | 65 | } | 
|---|
|  | 66 | else | 
|---|
|  | 67 | { | 
|---|
|  | 68 | // Try standard OpenAL loader | 
|---|
| [6270] | 69 | this->loadStandard(fileInfo, dataStream); | 
|---|
| [6203] | 70 | } | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
|  | 73 | SoundBuffer::~SoundBuffer() | 
|---|
|  | 74 | { | 
|---|
|  | 75 | // Destroy buffer | 
|---|
|  | 76 | alDeleteBuffers(1, &this->audioBuffer_); | 
|---|
|  | 77 | } | 
|---|
|  | 78 |  | 
|---|
| [6254] | 79 | unsigned int SoundBuffer::getSize() const | 
|---|
|  | 80 | { | 
|---|
|  | 81 | ALint size; | 
|---|
|  | 82 | alGetBufferi(this->audioBuffer_, AL_SIZE, &size); | 
|---|
| [6332] | 83 | return alGetError() ? 0 : size; | 
|---|
| [6254] | 84 | } | 
|---|
|  | 85 |  | 
|---|
| [6270] | 86 | void SoundBuffer::loadStandard(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream) | 
|---|
| [6203] | 87 | { | 
|---|
|  | 88 | // Read everything into a temporary buffer | 
|---|
| [6270] | 89 | char* buffer = new char[fileInfo->size]; | 
|---|
|  | 90 | dataStream->read(buffer, fileInfo->size); | 
|---|
| [6203] | 91 | dataStream->seek(0); | 
|---|
|  | 92 |  | 
|---|
| [6270] | 93 | this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, fileInfo->size); | 
|---|
| [6203] | 94 | delete[] buffer; | 
|---|
|  | 95 |  | 
|---|
| [6260] | 96 | if (!alIsBuffer(this->audioBuffer_)) | 
|---|
| [6232] | 97 | ThrowException(General, "Sound Error: Standard file loader failed: " << alutGetErrorString(alutGetError())); | 
|---|
| [6203] | 98 | } | 
|---|
|  | 99 |  | 
|---|
|  | 100 | size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource) | 
|---|
|  | 101 | { | 
|---|
|  | 102 | return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb); | 
|---|
|  | 103 | } | 
|---|
|  | 104 |  | 
|---|
|  | 105 | int seekVorbis(void* datasource, ogg_int64_t offset, int whence) | 
|---|
|  | 106 | { | 
|---|
|  | 107 | Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource); | 
|---|
| [6332] | 108 | switch (whence) | 
|---|
|  | 109 | { | 
|---|
|  | 110 | case SEEK_SET: | 
|---|
|  | 111 | stream->seek(offset); | 
|---|
|  | 112 | break; | 
|---|
|  | 113 | case SEEK_CUR: | 
|---|
|  | 114 | stream->skip(offset); | 
|---|
|  | 115 | break; | 
|---|
|  | 116 | case SEEK_END: | 
|---|
|  | 117 | stream->seek(stream->size() + offset); | 
|---|
|  | 118 | break; | 
|---|
|  | 119 | default: | 
|---|
| [6203] | 120 | return -1; | 
|---|
| [6332] | 121 | } | 
|---|
| [6203] | 122 | return 0; | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
|  | 125 | long tellVorbis(void* datasource) | 
|---|
|  | 126 | { | 
|---|
|  | 127 | return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell()); | 
|---|
|  | 128 | } | 
|---|
|  | 129 |  | 
|---|
| [6270] | 130 | void SoundBuffer::loadOgg(const shared_ptr<ResourceInfo>& fileInfo, DataStreamPtr dataStream) | 
|---|
| [6203] | 131 | { | 
|---|
|  | 132 | char inbuffer[256*1024]; | 
|---|
|  | 133 | std::vector<char> outbuffer; | 
|---|
|  | 134 | outbuffer.reserve(80*1024*1024); | 
|---|
|  | 135 |  | 
|---|
|  | 136 | // Open file with custom streaming | 
|---|
|  | 137 | ov_callbacks vorbisCallbacks; | 
|---|
|  | 138 | vorbisCallbacks.read_func  = &readVorbis; | 
|---|
|  | 139 | vorbisCallbacks.seek_func  = &seekVorbis; | 
|---|
|  | 140 | vorbisCallbacks.tell_func  = &tellVorbis; | 
|---|
|  | 141 | vorbisCallbacks.close_func = NULL; | 
|---|
|  | 142 |  | 
|---|
|  | 143 | OggVorbis_File vf; | 
|---|
|  | 144 | int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks); | 
|---|
|  | 145 | if (ret < 0) | 
|---|
|  | 146 | { | 
|---|
|  | 147 | COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; | 
|---|
|  | 148 | ov_clear(&vf); | 
|---|
| [6232] | 149 | ThrowException(General, "Sound Error: Ogg file loader failed when opening the bitstream"); | 
|---|
| [6203] | 150 | } | 
|---|
|  | 151 |  | 
|---|
|  | 152 | int current_section; | 
|---|
|  | 153 | int eof = false; | 
|---|
|  | 154 | while (!eof) | 
|---|
|  | 155 | { | 
|---|
|  | 156 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); | 
|---|
|  | 157 | if (ret == 0) | 
|---|
|  | 158 | { | 
|---|
|  | 159 | eof = true; | 
|---|
|  | 160 | } | 
|---|
|  | 161 | else if (ret < 0) | 
|---|
|  | 162 | { | 
|---|
|  | 163 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; | 
|---|
|  | 164 | ov_clear(&vf); | 
|---|
| [6232] | 165 | ThrowException(General, "Sound Error: Ogg file loader failed when decoding the file"); | 
|---|
| [6203] | 166 | } | 
|---|
|  | 167 | else | 
|---|
|  | 168 | { | 
|---|
|  | 169 | outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + ret); | 
|---|
|  | 170 | } | 
|---|
|  | 171 | } | 
|---|
|  | 172 |  | 
|---|
|  | 173 | vorbis_info* vorbisInfo; | 
|---|
|  | 174 | vorbisInfo = ov_info(&vf, -1); | 
|---|
|  | 175 | ALenum format; | 
|---|
|  | 176 | if (vorbisInfo->channels == 1) | 
|---|
|  | 177 | format = AL_FORMAT_MONO16; | 
|---|
|  | 178 | else | 
|---|
|  | 179 | format = AL_FORMAT_STEREO16; | 
|---|
|  | 180 |  | 
|---|
|  | 181 | alGenBuffers(1, &this->audioBuffer_); | 
|---|
|  | 182 | alBufferData(this->audioBuffer_, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate); | 
|---|
|  | 183 | ov_clear(&vf); | 
|---|
|  | 184 |  | 
|---|
| [6260] | 185 | if (!alIsBuffer(this->audioBuffer_)) | 
|---|
| [6203] | 186 | ThrowException(General, "Sound: Ogg file loader failed when creating the buffer."); | 
|---|
|  | 187 | } | 
|---|
|  | 188 | } | 
|---|