Changeset 513 for code/branches/FICN/src/audio/AudioStream.cc
- Timestamp:
- Dec 13, 2007, 5:58:14 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/audio/AudioStream.cc
r430 r513 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 */ 27 1 28 2 29 #include "AudioStream.h" … … 8 35 this->path = path; 9 36 loaded = false; 10 } 37 } 11 38 12 39 void AudioStream::open() … … 14 41 int result; 15 42 16 43 17 44 if(!(oggFile = fopen(path.c_str(), "rb"))) 18 45 { … … 23 50 if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0) 24 51 { 25 fclose(oggFile); 52 fclose(oggFile); 26 53 orxonox::Error("Could not open Ogg stream. " + errorString(result)); 27 54 return; … … 29 56 30 57 loaded = true; 31 58 32 59 vorbisInfo = ov_info(&oggStream, -1); 33 60 vorbisComment = ov_comment(&oggStream, -1); 34 61 35 62 if(vorbisInfo->channels == 1) 36 63 format = AL_FORMAT_MONO16; 37 64 else 38 65 format = AL_FORMAT_STEREO16; 39 40 66 67 41 68 alGenBuffers(2, buffers); 42 69 check(); 43 70 alGenSources(1, &source); 44 71 check(); 45 72 46 73 alSource3f(source, AL_POSITION, 0.0, 0.0, 0.0); 47 74 alSource3f(source, AL_VELOCITY, 0.0, 0.0, 0.0); … … 50 77 alSourcei (source, AL_SOURCE_RELATIVE, AL_FALSE ); 51 78 } 52 53 54 55 79 80 81 82 56 83 void AudioStream::release() 57 84 { … … 63 90 alDeleteBuffers(1, buffers); 64 91 check(); 65 92 66 93 ov_clear(&oggStream); 67 94 loaded = false; 68 69 } 70 71 72 73 95 96 } 97 98 99 100 74 101 void AudioStream::display() 75 102 { … … 86 113 << "\n" 87 114 << "vendor " << vorbisComment->vendor << "\n"; 88 115 89 116 for(int i = 0; i < vorbisComment->comments; i++) 90 117 std::cout << " " << vorbisComment->user_comments[i] << "\n"; 91 92 std::cout << std::endl; 118 119 std::cout << std::endl; 93 120 } 94 121 } 95 96 97 98 122 123 124 125 99 126 bool AudioStream::playback() 100 127 { … … 106 133 if(playing()) 107 134 return true; 108 135 109 136 if(!stream(buffers[0])) 110 137 return false; 111 138 112 139 if(!stream(buffers[1])) 113 140 return false; 114 141 115 142 alSourceQueueBuffers(source, 2, buffers); 116 143 alSourcePlay(source); 117 144 118 145 return true; 119 146 } 120 121 122 123 147 148 149 150 124 151 bool AudioStream::playing() 125 152 { … … 133 160 return (state == AL_PLAYING); 134 161 } 135 136 137 138 162 163 164 165 139 166 bool AudioStream::update() 140 167 { 141 168 int processed; 142 169 bool active = true; 143 170 144 171 alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed); 145 172 146 173 while(processed--) 147 174 { 148 175 ALuint buffer; 149 176 150 177 alSourceUnqueueBuffers(source, 1, &buffer); 151 178 check(); 152 179 153 180 active = stream(buffer); 154 181 155 182 alSourceQueueBuffers(source, 1, &buffer); 156 183 check(); 157 184 } 158 185 159 186 if (active==false) 160 187 { … … 163 190 return active; 164 191 } 165 166 167 168 192 193 194 195 169 196 bool AudioStream::stream(ALuint buffer) 170 197 { … … 173 200 int section; 174 201 int result; 175 202 176 203 while(size < BUFFER_SIZE) 177 204 { 178 205 result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, §ion); 179 206 180 207 if(result > 0) 181 208 size += result; … … 186 213 break; 187 214 } 188 215 189 216 if(size == 0) 190 217 return false; 191 218 192 219 alBufferData(buffer, format, pcm, size, vorbisInfo->rate); 193 220 check(); 194 221 195 222 return true; 196 223 } 197 198 224 225 199 226 200 227 void AudioStream::empty() 201 228 { 202 229 int queued; 203 230 204 231 alGetSourcei(source, AL_BUFFERS_QUEUED, &queued); 205 232 206 233 while(queued--) 207 234 { 208 235 ALuint buffer; 209 236 210 237 alSourceUnqueueBuffers(source, 1, &buffer); 211 238 check(); 212 239 } 213 240 } 214 215 216 217 241 242 243 244 218 245 void AudioStream::check() 219 246 { 220 247 int error = alGetError(); 221 248 222 249 if(error != AL_NO_ERROR) 223 250 orxonox::Error("OpenAL error was raised."); 224 251 } 225 226 227 252 253 254 228 255 std::string AudioStream::errorString(int code) 229 256 {
Note: See TracChangeset
for help on using the changeset viewer.