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