1 | |
---|
2 | #include "AudioStream.h" |
---|
3 | |
---|
4 | namespace audio |
---|
5 | { |
---|
6 | void AudioStream::open(std::string path) |
---|
7 | { |
---|
8 | int result; |
---|
9 | loaded = false; |
---|
10 | |
---|
11 | path = "audio/ambient/" + path + ".ogg"; |
---|
12 | |
---|
13 | if(!(oggFile = fopen(path.c_str(), "rb"))) |
---|
14 | { |
---|
15 | orxonox::Error("Could not open Ogg file "+path); |
---|
16 | return; |
---|
17 | } |
---|
18 | |
---|
19 | if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0) |
---|
20 | { |
---|
21 | fclose(oggFile); |
---|
22 | orxonox::Error("Could not open Ogg stream. " + errorString(result)); |
---|
23 | return; |
---|
24 | } |
---|
25 | |
---|
26 | loaded = true; |
---|
27 | |
---|
28 | vorbisInfo = ov_info(&oggStream, -1); |
---|
29 | vorbisComment = ov_comment(&oggStream, -1); |
---|
30 | |
---|
31 | if(vorbisInfo->channels == 1) |
---|
32 | format = AL_FORMAT_MONO16; |
---|
33 | else |
---|
34 | format = AL_FORMAT_STEREO16; |
---|
35 | |
---|
36 | |
---|
37 | alGenBuffers(2, buffers); |
---|
38 | check(); |
---|
39 | alGenSources(1, &source); |
---|
40 | check(); |
---|
41 | |
---|
42 | alSource3f(source, AL_POSITION, 0.0, 0.0, 0.0); |
---|
43 | alSource3f(source, AL_VELOCITY, 0.0, 0.0, 0.0); |
---|
44 | alSource3f(source, AL_DIRECTION, 0.0, 0.0, 0.0); |
---|
45 | alSourcef (source, AL_ROLLOFF_FACTOR, 0.0 ); |
---|
46 | alSourcei (source, AL_SOURCE_RELATIVE, AL_FALSE ); |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | void AudioStream::release() |
---|
53 | { |
---|
54 | if (loaded) |
---|
55 | { |
---|
56 | alSourceStop(source); |
---|
57 | empty(); |
---|
58 | alDeleteSources(1, &source); |
---|
59 | check(); |
---|
60 | alDeleteBuffers(1, buffers); |
---|
61 | check(); |
---|
62 | |
---|
63 | ov_clear(&oggStream); |
---|
64 | loaded = false; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | void AudioStream::display() |
---|
72 | { |
---|
73 | if (loaded) |
---|
74 | { |
---|
75 | std::cout |
---|
76 | << "version " << vorbisInfo->version << "\n" |
---|
77 | << "channels " << vorbisInfo->channels << "\n" |
---|
78 | << "rate (hz) " << vorbisInfo->rate << "\n" |
---|
79 | << "bitrate upper " << vorbisInfo->bitrate_upper << "\n" |
---|
80 | << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n" |
---|
81 | << "bitrate lower " << vorbisInfo->bitrate_lower << "\n" |
---|
82 | << "bitrate window " << vorbisInfo->bitrate_window << "\n" |
---|
83 | << "\n" |
---|
84 | << "vendor " << vorbisComment->vendor << "\n"; |
---|
85 | |
---|
86 | for(int i = 0; i < vorbisComment->comments; i++) |
---|
87 | std::cout << " " << vorbisComment->user_comments[i] << "\n"; |
---|
88 | |
---|
89 | std::cout << std::endl; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | bool AudioStream::playback() |
---|
97 | { |
---|
98 | if (!loaded) |
---|
99 | { |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | if(playing()) |
---|
104 | return true; |
---|
105 | |
---|
106 | if(!stream(buffers[0])) |
---|
107 | return false; |
---|
108 | |
---|
109 | if(!stream(buffers[1])) |
---|
110 | return false; |
---|
111 | |
---|
112 | alSourceQueueBuffers(source, 2, buffers); |
---|
113 | alSourcePlay(source); |
---|
114 | |
---|
115 | return true; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | bool AudioStream::playing() |
---|
122 | { |
---|
123 | if (!loaded) |
---|
124 | { |
---|
125 | return false; |
---|
126 | } |
---|
127 | |
---|
128 | ALenum state; |
---|
129 | alGetSourcei(source, AL_SOURCE_STATE, &state); |
---|
130 | return (state == AL_PLAYING); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | bool AudioStream::update() |
---|
137 | { |
---|
138 | int processed; |
---|
139 | bool active = true; |
---|
140 | |
---|
141 | alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed); |
---|
142 | |
---|
143 | while(processed--) |
---|
144 | { |
---|
145 | ALuint buffer; |
---|
146 | |
---|
147 | alSourceUnqueueBuffers(source, 1, &buffer); |
---|
148 | check(); |
---|
149 | |
---|
150 | active = stream(buffer); |
---|
151 | |
---|
152 | alSourceQueueBuffers(source, 1, &buffer); |
---|
153 | check(); |
---|
154 | } |
---|
155 | |
---|
156 | return active; |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | bool AudioStream::stream(ALuint buffer) |
---|
163 | { |
---|
164 | char pcm[BUFFER_SIZE]; |
---|
165 | int size = 0; |
---|
166 | int section; |
---|
167 | int result; |
---|
168 | |
---|
169 | while(size < BUFFER_SIZE) |
---|
170 | { |
---|
171 | result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, §ion); |
---|
172 | |
---|
173 | if(result > 0) |
---|
174 | size += result; |
---|
175 | else |
---|
176 | if(result < 0) |
---|
177 | orxonox::Error(errorString(result)); |
---|
178 | else |
---|
179 | break; |
---|
180 | } |
---|
181 | |
---|
182 | if(size == 0) |
---|
183 | return false; |
---|
184 | |
---|
185 | alBufferData(buffer, format, pcm, size, vorbisInfo->rate); |
---|
186 | check(); |
---|
187 | |
---|
188 | return true; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | void AudioStream::empty() |
---|
194 | { |
---|
195 | int queued; |
---|
196 | |
---|
197 | alGetSourcei(source, AL_BUFFERS_QUEUED, &queued); |
---|
198 | |
---|
199 | while(queued--) |
---|
200 | { |
---|
201 | ALuint buffer; |
---|
202 | |
---|
203 | alSourceUnqueueBuffers(source, 1, &buffer); |
---|
204 | check(); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | void AudioStream::check() |
---|
212 | { |
---|
213 | int error = alGetError(); |
---|
214 | |
---|
215 | if(error != AL_NO_ERROR) |
---|
216 | orxonox::Error("OpenAL error was raised."); |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | std::string AudioStream::errorString(int code) |
---|
222 | { |
---|
223 | switch(code) |
---|
224 | { |
---|
225 | case OV_EREAD: |
---|
226 | return std::string("Read from media."); |
---|
227 | case OV_ENOTVORBIS: |
---|
228 | return std::string("Not Vorbis data."); |
---|
229 | case OV_EVERSION: |
---|
230 | return std::string("Vorbis version mismatch."); |
---|
231 | case OV_EBADHEADER: |
---|
232 | return std::string("Invalid Vorbis header."); |
---|
233 | case OV_EFAULT: |
---|
234 | return std::string("Internal logic fault (bug or heap/stack corruption."); |
---|
235 | default: |
---|
236 | return std::string("Unknown Ogg error."); |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|