[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 | |
---|
[5283] | 21 | #include <iostream> |
---|
| 22 | |
---|
[4750] | 23 | #include "ogg_player.h" |
---|
| 24 | |
---|
[4985] | 25 | #include "sound_engine.h" |
---|
| 26 | |
---|
[4750] | 27 | #include "debug.h" |
---|
| 28 | |
---|
[4961] | 29 | /** |
---|
| 30 | * initializes an Ogg-player from a file |
---|
| 31 | * @param fileName the file to load |
---|
| 32 | */ |
---|
| 33 | OggPlayer::OggPlayer(const char* fileName) |
---|
[4750] | 34 | { |
---|
[4961] | 35 | this->setClassID(CL_SOUND_OGG_PLAYER, "OggPlayer"); |
---|
[6827] | 36 | |
---|
[6842] | 37 | this->trackLoaded = false; |
---|
[6827] | 38 | this->source = 0; |
---|
[6842] | 39 | this->buffers[0] = 0; |
---|
| 40 | this->buffers[1] = 0; |
---|
[6827] | 41 | |
---|
[4961] | 42 | if (fileName != NULL) |
---|
| 43 | { |
---|
[6842] | 44 | if (this->open(fileName)) |
---|
| 45 | this->setName(fileName); |
---|
[4961] | 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
[6872] | 49 | OggPlayer::~ OggPlayer() |
---|
| 50 | { |
---|
| 51 | this->release(); |
---|
| 52 | } |
---|
| 53 | |
---|
[4961] | 54 | /** |
---|
| 55 | * opens a file for playback |
---|
| 56 | * @param fileName the file to open |
---|
| 57 | */ |
---|
[6842] | 58 | bool OggPlayer::open(const char* fileName) |
---|
[4961] | 59 | { |
---|
[6842] | 60 | if (this->buffers[0] == 0) |
---|
| 61 | alGenBuffers(2, this->buffers); |
---|
| 62 | SoundEngine::checkError("Allocating Buffers", __LINE__); |
---|
| 63 | |
---|
| 64 | if (this->source == 0) |
---|
| 65 | SoundEngine::getInstance()->popALSource(this->source); |
---|
| 66 | if (this->source == 0) |
---|
| 67 | { |
---|
| 68 | this->trackLoaded = false; |
---|
| 69 | return false; |
---|
| 70 | } |
---|
| 71 | |
---|
[6828] | 72 | int result; |
---|
[4750] | 73 | |
---|
[6828] | 74 | if(!(oggFile = fopen(fileName, "rb"))) |
---|
[6872] | 75 | { |
---|
[6828] | 76 | PRINTF(2)("Could not open Ogg file."); |
---|
[6872] | 77 | return false; |
---|
| 78 | } |
---|
[4750] | 79 | |
---|
[6828] | 80 | if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0) |
---|
| 81 | { |
---|
| 82 | fclose(oggFile); |
---|
[4750] | 83 | |
---|
[6828] | 84 | PRINTF(2)("Could not open Ogg stream. %s", errorString(result)); |
---|
[6872] | 85 | fclose(oggFile); |
---|
| 86 | return false; |
---|
[6828] | 87 | } |
---|
[4750] | 88 | |
---|
[6828] | 89 | vorbisInfo = ov_info(&oggStream, -1); |
---|
| 90 | vorbisComment = ov_comment(&oggStream, -1); |
---|
[4750] | 91 | |
---|
[6828] | 92 | if(vorbisInfo->channels == 1) |
---|
| 93 | format = AL_FORMAT_MONO16; |
---|
| 94 | else |
---|
| 95 | format = AL_FORMAT_STEREO16; |
---|
[4750] | 96 | |
---|
[6828] | 97 | alSource3f(source, AL_POSITION, 0.0, 0.0, 0.0); |
---|
| 98 | alSource3f(source, AL_VELOCITY, 0.0, 0.0, 0.0); |
---|
| 99 | alSource3f(source, AL_DIRECTION, 0.0, 0.0, 0.0); |
---|
| 100 | alSourcef (source, AL_ROLLOFF_FACTOR, 0.0 ); |
---|
| 101 | alSourcei (source, AL_SOURCE_RELATIVE, AL_TRUE ); |
---|
| 102 | alSourcef (source, AL_GAIN, SoundEngine::getInstance()->getMusicVolume()); |
---|
[6842] | 103 | |
---|
| 104 | this->trackLoaded = true; |
---|
| 105 | return true; |
---|
[4750] | 106 | } |
---|
| 107 | |
---|
[4961] | 108 | /** |
---|
| 109 | * releases a stream |
---|
| 110 | */ |
---|
| 111 | void OggPlayer::release() |
---|
[4750] | 112 | { |
---|
[6872] | 113 | if (!this->trackLoaded) |
---|
| 114 | return; |
---|
[6828] | 115 | alSourceStop(source); |
---|
| 116 | empty(); |
---|
| 117 | SoundEngine::getInstance()->pushALSource(source); |
---|
[6842] | 118 | this->source = 0; |
---|
[6828] | 119 | check(); |
---|
[6842] | 120 | alDeleteBuffers(2, buffers); |
---|
| 121 | this->buffers[0] = 0; |
---|
| 122 | this->buffers[1] = 0; |
---|
[6828] | 123 | check(); |
---|
[4750] | 124 | |
---|
[6828] | 125 | ov_clear(&oggStream); |
---|
[6842] | 126 | this->trackLoaded = false; |
---|
[4750] | 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
[4961] | 130 | /** |
---|
| 131 | * plays back the sound |
---|
| 132 | * @return true if running, false otherwise |
---|
| 133 | */ |
---|
| 134 | bool OggPlayer::playback() |
---|
[4750] | 135 | { |
---|
[6842] | 136 | if (!this->trackLoaded) |
---|
| 137 | return false; |
---|
| 138 | |
---|
[6828] | 139 | if(playing()) |
---|
| 140 | return true; |
---|
[4750] | 141 | |
---|
[6828] | 142 | if(!stream(buffers[0])) |
---|
| 143 | return false; |
---|
[4750] | 144 | |
---|
[6828] | 145 | if(!stream(buffers[1])) |
---|
| 146 | return false; |
---|
[4750] | 147 | |
---|
[6828] | 148 | alSourceQueueBuffers(source, 2, buffers); |
---|
| 149 | alSourcePlay(source); |
---|
[4750] | 150 | |
---|
[6828] | 151 | return true; |
---|
[4750] | 152 | } |
---|
| 153 | |
---|
[4961] | 154 | /** |
---|
| 155 | * |
---|
| 156 | * @returns true if the file is playing |
---|
| 157 | */ |
---|
| 158 | bool OggPlayer::playing() |
---|
[4750] | 159 | { |
---|
[6842] | 160 | if (!this->trackLoaded) |
---|
| 161 | return false; |
---|
[6828] | 162 | ALenum state; |
---|
[4750] | 163 | |
---|
[6828] | 164 | alGetSourcei(this->source, AL_SOURCE_STATE, &state); |
---|
[4750] | 165 | |
---|
[6828] | 166 | return (state == AL_PLAYING); |
---|
[4750] | 167 | } |
---|
| 168 | |
---|
[4961] | 169 | /** |
---|
| 170 | * updates the stream, this has to be done every few parts of a second, for sound-consistency |
---|
| 171 | * @returns true, if the Sound is playing flawlessly |
---|
| 172 | */ |
---|
| 173 | bool OggPlayer::update() |
---|
[4750] | 174 | { |
---|
[6872] | 175 | if (unlikely(!this->trackLoaded)) |
---|
[6842] | 176 | return false; |
---|
| 177 | |
---|
[6828] | 178 | int processed; |
---|
| 179 | bool active = true; |
---|
[4750] | 180 | |
---|
[6828] | 181 | alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed); |
---|
[4750] | 182 | |
---|
[6828] | 183 | while(processed--) |
---|
| 184 | { |
---|
| 185 | ALuint buffer; |
---|
[4750] | 186 | |
---|
[6828] | 187 | alSourceUnqueueBuffers(source, 1, &buffer); |
---|
| 188 | check(); |
---|
[4750] | 189 | |
---|
[6828] | 190 | active = stream(buffer); |
---|
[4750] | 191 | |
---|
[6828] | 192 | alSourceQueueBuffers(source, 1, &buffer); |
---|
| 193 | check(); |
---|
| 194 | } |
---|
[4750] | 195 | |
---|
[6828] | 196 | return active; |
---|
[4750] | 197 | } |
---|
| 198 | |
---|
[4961] | 199 | /** |
---|
| 200 | * gets a new Stream from buffer |
---|
| 201 | * @param buffer the buffer to get the stream from |
---|
| 202 | * @return true, if everything worked as planed |
---|
| 203 | */ |
---|
| 204 | bool OggPlayer::stream(ALuint buffer) |
---|
[4750] | 205 | { |
---|
[6828] | 206 | char pcm[BUFFER_SIZE]; |
---|
| 207 | int size = 0; |
---|
| 208 | int section; |
---|
| 209 | int result; |
---|
[4750] | 210 | |
---|
[6828] | 211 | while(size < BUFFER_SIZE) |
---|
| 212 | { |
---|
| 213 | result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, §ion); |
---|
[4750] | 214 | |
---|
[6828] | 215 | if(result > 0) |
---|
| 216 | size += result; |
---|
| 217 | else |
---|
| 218 | if(result < 0) |
---|
| 219 | throw errorString(result); |
---|
| 220 | else |
---|
| 221 | break; |
---|
| 222 | } |
---|
[4750] | 223 | |
---|
[6828] | 224 | if(size == 0) |
---|
| 225 | return false; |
---|
[4750] | 226 | |
---|
[6828] | 227 | alBufferData(buffer, format, pcm, size, vorbisInfo->rate); |
---|
| 228 | check(); |
---|
[4750] | 229 | |
---|
[6828] | 230 | return true; |
---|
[4750] | 231 | } |
---|
| 232 | |
---|
| 233 | |
---|
[4961] | 234 | /** |
---|
| 235 | * empties the buffers |
---|
| 236 | */ |
---|
| 237 | void OggPlayer::empty() |
---|
[4750] | 238 | { |
---|
[6828] | 239 | int queued; |
---|
[4750] | 240 | |
---|
[6828] | 241 | alGetSourcei(source, AL_BUFFERS_QUEUED, &queued); |
---|
[4750] | 242 | |
---|
[6828] | 243 | while(queued--) |
---|
| 244 | { |
---|
| 245 | ALuint buffer; |
---|
[4750] | 246 | |
---|
[6828] | 247 | alSourceUnqueueBuffers(source, 1, &buffer); |
---|
| 248 | check(); |
---|
| 249 | } |
---|
[4750] | 250 | } |
---|
| 251 | |
---|
| 252 | |
---|
[4961] | 253 | /** |
---|
[6828] | 254 | * displays some info about the ogg-file |
---|
| 255 | */ |
---|
| 256 | void OggPlayer::debug() |
---|
| 257 | { |
---|
| 258 | cout |
---|
[6842] | 259 | << "version " << vorbisInfo->version << "\n" |
---|
| 260 | << "channels " << vorbisInfo->channels << "\n" |
---|
| 261 | << "rate (hz) " << vorbisInfo->rate << "\n" |
---|
| 262 | << "bitrate upper " << vorbisInfo->bitrate_upper << "\n" |
---|
| 263 | << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n" |
---|
| 264 | << "bitrate lower " << vorbisInfo->bitrate_lower << "\n" |
---|
| 265 | << "bitrate window " << vorbisInfo->bitrate_window << "\n" |
---|
| 266 | << "\n" |
---|
| 267 | << "vendor " << vorbisComment->vendor << "\n"; |
---|
[6828] | 268 | |
---|
| 269 | for(int i = 0; i < vorbisComment->comments; i++) |
---|
| 270 | cout << " " << vorbisComment->user_comments[i] << "\n"; |
---|
| 271 | |
---|
| 272 | cout << endl; |
---|
| 273 | } |
---|
| 274 | |
---|
[6872] | 275 | /** |
---|
| 276 | * checks for errors |
---|
| 277 | */ |
---|
| 278 | void OggPlayer::check() |
---|
| 279 | { |
---|
| 280 | int error = alGetError(); |
---|
| 281 | if(error != AL_NO_ERROR) |
---|
| 282 | PRINTF(2)("OpenAL error was raised."); |
---|
| 283 | } |
---|
[6828] | 284 | |
---|
| 285 | /** |
---|
[4961] | 286 | * returns errors |
---|
| 287 | * @param code the error-code |
---|
| 288 | * @return the error as a String |
---|
| 289 | */ |
---|
| 290 | const char* OggPlayer::errorString(int code) |
---|
[4750] | 291 | { |
---|
[6828] | 292 | switch(code) |
---|
| 293 | { |
---|
| 294 | case OV_EREAD: |
---|
| 295 | return ("Read from media."); |
---|
| 296 | case OV_ENOTVORBIS: |
---|
| 297 | return ("Not Vorbis data."); |
---|
| 298 | case OV_EVERSION: |
---|
| 299 | return ("Vorbis version mismatch."); |
---|
| 300 | case OV_EBADHEADER: |
---|
| 301 | return ("Invalid Vorbis header."); |
---|
| 302 | case OV_EFAULT: |
---|
| 303 | return ("Internal logic fault (bug or heap/stack corruption."); |
---|
| 304 | default: |
---|
| 305 | return ("Unknown Ogg error."); |
---|
| 306 | } |
---|
[4750] | 307 | } |
---|