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