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