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 | { |
---|
42 | SoundBuffer::SoundBuffer(shared_ptr<ResourceInfo> fileInfo) |
---|
43 | : fileInfo_(fileInfo) |
---|
44 | , audioBuffer_(AL_NONE) |
---|
45 | { |
---|
46 | if (this->fileInfo_ == NULL) |
---|
47 | ThrowException(General, "SoundBuffer construction: fileInfo was NULL"); |
---|
48 | DataStreamPtr dataStream = Resource::open(this->fileInfo_); |
---|
49 | |
---|
50 | std::string extension(this->fileInfo_->basename.substr(this->fileInfo_->basename.find_last_of('.') + 1)); |
---|
51 | if (getLowercase(extension) == "ogg") |
---|
52 | { |
---|
53 | // Try ogg loader |
---|
54 | this->loadOgg(dataStream); |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | // Try standard OpenAL loader |
---|
59 | this->loadStandard(dataStream); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | SoundBuffer::~SoundBuffer() |
---|
64 | { |
---|
65 | // Unregister buffer from SoundManager |
---|
66 | SoundManager::getInstance().removeBuffer(this->fileInfo_); |
---|
67 | |
---|
68 | // Destroy buffer |
---|
69 | alDeleteBuffers(1, &this->audioBuffer_); |
---|
70 | } |
---|
71 | |
---|
72 | void SoundBuffer::loadStandard(DataStreamPtr dataStream) |
---|
73 | { |
---|
74 | // Read everything into a temporary buffer |
---|
75 | char* buffer = new char[this->fileInfo_->size]; |
---|
76 | dataStream->read(buffer, this->fileInfo_->size); |
---|
77 | dataStream->seek(0); |
---|
78 | |
---|
79 | this->audioBuffer_ = alutCreateBufferFromFileImage(buffer, this->fileInfo_->size); |
---|
80 | delete[] buffer; |
---|
81 | |
---|
82 | if (this->audioBuffer_ == AL_NONE) |
---|
83 | ThrowException(General, "Sound Error: Standard file loader failed: " << alutGetErrorString(alutGetError())); |
---|
84 | } |
---|
85 | |
---|
86 | size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource) |
---|
87 | { |
---|
88 | return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb); |
---|
89 | } |
---|
90 | |
---|
91 | int seekVorbis(void* datasource, ogg_int64_t offset, int whence) |
---|
92 | { |
---|
93 | Ogre::DataStream* stream = static_cast<Ogre::DataStream*>(datasource); |
---|
94 | int offset_beg = offset; |
---|
95 | if (whence == SEEK_CUR) |
---|
96 | offset_beg = stream->tell() + offset; |
---|
97 | else if (whence == SEEK_END) |
---|
98 | offset_beg = stream->size() + offset; |
---|
99 | else if (whence != SEEK_SET) |
---|
100 | return -1; |
---|
101 | stream->seek(offset_beg); |
---|
102 | return 0; |
---|
103 | } |
---|
104 | |
---|
105 | long tellVorbis(void* datasource) |
---|
106 | { |
---|
107 | return static_cast<long>(static_cast<Ogre::DataStream*>(datasource)->tell()); |
---|
108 | } |
---|
109 | |
---|
110 | void SoundBuffer::loadOgg(DataStreamPtr dataStream) |
---|
111 | { |
---|
112 | char inbuffer[256*1024]; |
---|
113 | std::vector<char> outbuffer; |
---|
114 | outbuffer.reserve(80*1024*1024); |
---|
115 | |
---|
116 | // Open file with custom streaming |
---|
117 | ov_callbacks vorbisCallbacks; |
---|
118 | vorbisCallbacks.read_func = &readVorbis; |
---|
119 | vorbisCallbacks.seek_func = &seekVorbis; |
---|
120 | vorbisCallbacks.tell_func = &tellVorbis; |
---|
121 | vorbisCallbacks.close_func = NULL; |
---|
122 | |
---|
123 | OggVorbis_File vf; |
---|
124 | int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks); |
---|
125 | if (ret < 0) |
---|
126 | { |
---|
127 | COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; |
---|
128 | ov_clear(&vf); |
---|
129 | ThrowException(General, "Sound Error: Ogg file loader failed when opening the bitstream"); |
---|
130 | } |
---|
131 | |
---|
132 | int current_section; |
---|
133 | int eof = false; |
---|
134 | while (!eof) |
---|
135 | { |
---|
136 | long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); |
---|
137 | if (ret == 0) |
---|
138 | { |
---|
139 | eof = true; |
---|
140 | } |
---|
141 | else if (ret < 0) |
---|
142 | { |
---|
143 | COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; |
---|
144 | ov_clear(&vf); |
---|
145 | ThrowException(General, "Sound Error: Ogg file loader failed when decoding the file"); |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | outbuffer.insert(outbuffer.end(), inbuffer, inbuffer + ret); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | vorbis_info* vorbisInfo; |
---|
154 | vorbisInfo = ov_info(&vf, -1); |
---|
155 | ALenum format; |
---|
156 | if (vorbisInfo->channels == 1) |
---|
157 | format = AL_FORMAT_MONO16; |
---|
158 | else |
---|
159 | format = AL_FORMAT_STEREO16; |
---|
160 | |
---|
161 | alGenBuffers(1, &this->audioBuffer_); |
---|
162 | alBufferData(this->audioBuffer_, format, &outbuffer[0], outbuffer.size(), vorbisInfo->rate); |
---|
163 | ov_clear(&vf); |
---|
164 | |
---|
165 | if (this->audioBuffer_ == AL_NONE) |
---|
166 | ThrowException(General, "Sound: Ogg file loader failed when creating the buffer."); |
---|
167 | } |
---|
168 | } |
---|