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