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