1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * ... |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | #include "AudioStream.h" |
---|
30 | |
---|
31 | namespace audio |
---|
32 | { |
---|
33 | AudioStream::AudioStream(std::string path) |
---|
34 | { |
---|
35 | this->path = path; |
---|
36 | loaded = false; |
---|
37 | } |
---|
38 | |
---|
39 | void AudioStream::open() |
---|
40 | { |
---|
41 | int result; |
---|
42 | |
---|
43 | |
---|
44 | if(!(oggFile = fopen(path.c_str(), "rb"))) |
---|
45 | { |
---|
46 | orxonox::Error("Could not open Ogg file "+path); |
---|
47 | return; |
---|
48 | } |
---|
49 | |
---|
50 | if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0) |
---|
51 | { |
---|
52 | fclose(oggFile); |
---|
53 | orxonox::Error("Could not open Ogg stream. " + errorString(result)); |
---|
54 | return; |
---|
55 | } |
---|
56 | |
---|
57 | loaded = true; |
---|
58 | |
---|
59 | vorbisInfo = ov_info(&oggStream, -1); |
---|
60 | vorbisComment = ov_comment(&oggStream, -1); |
---|
61 | |
---|
62 | if(vorbisInfo->channels == 1) |
---|
63 | format = AL_FORMAT_MONO16; |
---|
64 | else |
---|
65 | format = AL_FORMAT_STEREO16; |
---|
66 | |
---|
67 | |
---|
68 | alGenBuffers(2, buffers); |
---|
69 | check(); |
---|
70 | alGenSources(1, &source); |
---|
71 | check(); |
---|
72 | |
---|
73 | alSource3f(source, AL_POSITION, 0.0, 0.0, 0.0); |
---|
74 | alSource3f(source, AL_VELOCITY, 0.0, 0.0, 0.0); |
---|
75 | alSource3f(source, AL_DIRECTION, 0.0, 0.0, 0.0); |
---|
76 | alSourcef (source, AL_ROLLOFF_FACTOR, 0.0 ); |
---|
77 | alSourcei (source, AL_SOURCE_RELATIVE, AL_FALSE ); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | void AudioStream::release() |
---|
84 | { |
---|
85 | |
---|
86 | alSourceStop(source); |
---|
87 | empty(); |
---|
88 | alDeleteSources(1, &source); |
---|
89 | check(); |
---|
90 | alDeleteBuffers(1, buffers); |
---|
91 | check(); |
---|
92 | |
---|
93 | ov_clear(&oggStream); |
---|
94 | loaded = false; |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | void AudioStream::display() |
---|
102 | { |
---|
103 | if (loaded) |
---|
104 | { |
---|
105 | std::cout |
---|
106 | << "version " << vorbisInfo->version << "\n" |
---|
107 | << "channels " << vorbisInfo->channels << "\n" |
---|
108 | << "rate (hz) " << vorbisInfo->rate << "\n" |
---|
109 | << "bitrate upper " << vorbisInfo->bitrate_upper << "\n" |
---|
110 | << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n" |
---|
111 | << "bitrate lower " << vorbisInfo->bitrate_lower << "\n" |
---|
112 | << "bitrate window " << vorbisInfo->bitrate_window << "\n" |
---|
113 | << "\n" |
---|
114 | << "vendor " << vorbisComment->vendor << "\n"; |
---|
115 | |
---|
116 | for(int i = 0; i < vorbisComment->comments; i++) |
---|
117 | std::cout << " " << vorbisComment->user_comments[i] << "\n"; |
---|
118 | |
---|
119 | std::cout << std::endl; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | bool AudioStream::playback() |
---|
127 | { |
---|
128 | if (!loaded) |
---|
129 | { |
---|
130 | return false; |
---|
131 | } |
---|
132 | |
---|
133 | if(playing()) |
---|
134 | return true; |
---|
135 | |
---|
136 | if(!stream(buffers[0])) |
---|
137 | return false; |
---|
138 | |
---|
139 | if(!stream(buffers[1])) |
---|
140 | return false; |
---|
141 | |
---|
142 | alSourceQueueBuffers(source, 2, buffers); |
---|
143 | alSourcePlay(source); |
---|
144 | |
---|
145 | return true; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | bool AudioStream::playing() |
---|
152 | { |
---|
153 | if (!loaded) |
---|
154 | { |
---|
155 | return false; |
---|
156 | } |
---|
157 | |
---|
158 | ALenum state; |
---|
159 | alGetSourcei(source, AL_SOURCE_STATE, &state); |
---|
160 | return (state == AL_PLAYING); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | bool AudioStream::update() |
---|
167 | { |
---|
168 | int processed; |
---|
169 | bool active = true; |
---|
170 | |
---|
171 | alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed); |
---|
172 | |
---|
173 | while(processed--) |
---|
174 | { |
---|
175 | ALuint buffer; |
---|
176 | |
---|
177 | alSourceUnqueueBuffers(source, 1, &buffer); |
---|
178 | check(); |
---|
179 | |
---|
180 | active = stream(buffer); |
---|
181 | |
---|
182 | alSourceQueueBuffers(source, 1, &buffer); |
---|
183 | check(); |
---|
184 | } |
---|
185 | |
---|
186 | if (active==false) |
---|
187 | { |
---|
188 | loaded = false; |
---|
189 | } |
---|
190 | return active; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | bool AudioStream::stream(ALuint buffer) |
---|
197 | { |
---|
198 | char pcm[BUFFER_SIZE]; |
---|
199 | int size = 0; |
---|
200 | int section; |
---|
201 | int result; |
---|
202 | |
---|
203 | while(size < BUFFER_SIZE) |
---|
204 | { |
---|
205 | result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, §ion); |
---|
206 | |
---|
207 | if(result > 0) |
---|
208 | size += result; |
---|
209 | else |
---|
210 | if(result < 0) |
---|
211 | orxonox::Error(errorString(result)); |
---|
212 | else |
---|
213 | break; |
---|
214 | } |
---|
215 | |
---|
216 | if(size == 0) |
---|
217 | return false; |
---|
218 | |
---|
219 | alBufferData(buffer, format, pcm, size, vorbisInfo->rate); |
---|
220 | check(); |
---|
221 | |
---|
222 | return true; |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | |
---|
227 | void AudioStream::empty() |
---|
228 | { |
---|
229 | int queued; |
---|
230 | |
---|
231 | alGetSourcei(source, AL_BUFFERS_QUEUED, &queued); |
---|
232 | |
---|
233 | while(queued--) |
---|
234 | { |
---|
235 | ALuint buffer; |
---|
236 | |
---|
237 | alSourceUnqueueBuffers(source, 1, &buffer); |
---|
238 | check(); |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | void AudioStream::check() |
---|
246 | { |
---|
247 | int error = alGetError(); |
---|
248 | |
---|
249 | if(error != AL_NO_ERROR) |
---|
250 | orxonox::Error("OpenAL error was raised."); |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | std::string AudioStream::errorString(int code) |
---|
256 | { |
---|
257 | switch(code) |
---|
258 | { |
---|
259 | case OV_EREAD: |
---|
260 | return std::string("Read from media."); |
---|
261 | case OV_ENOTVORBIS: |
---|
262 | return std::string("Not Vorbis data."); |
---|
263 | case OV_EVERSION: |
---|
264 | return std::string("Vorbis version mismatch."); |
---|
265 | case OV_EBADHEADER: |
---|
266 | return std::string("Invalid Vorbis header."); |
---|
267 | case OV_EFAULT: |
---|
268 | return std::string("Internal logic fault (bug or heap/stack corruption."); |
---|
269 | default: |
---|
270 | return std::string("Unknown Ogg error."); |
---|
271 | } |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|