Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/audio/AudioStream.cc @ 423

Last change on this file since 423 was 423, checked in by nicolape, 16 years ago
File size: 4.9 KB
RevLine 
[410]1
2#include "AudioStream.h"
3
4namespace audio
5{
[419]6        void AudioStream::open(std::string path)
[410]7        {
[423]8            int result;
9                        loaded = false;
[410]10           
[411]11            path = "audio/ambient/" + path + ".ogg"; 
12           
[423]13            if(!(oggFile = fopen(path.c_str(), "rb")))
14                        {
15                orxonox::Error("Could not open Ogg file "+path);
16                                return;
17                        }
18
[410]19            if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
20            {
[423]21        fclose(oggFile);               
22              orxonox::Error("Could not open Ogg stream. " + errorString(result));
23                                return;
24            }
25
26                        loaded = true;
[410]27       
28            vorbisInfo = ov_info(&oggStream, -1);
29            vorbisComment = ov_comment(&oggStream, -1);
30       
31            if(vorbisInfo->channels == 1)
32                format = AL_FORMAT_MONO16;
33            else
34                format = AL_FORMAT_STEREO16;
35               
36               
37            alGenBuffers(2, buffers);
38            check();
39            alGenSources(1, &source);
40            check();
41           
42            alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
43            alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
44            alSource3f(source, AL_DIRECTION,       0.0, 0.0, 0.0);
45            alSourcef (source, AL_ROLLOFF_FACTOR,  0.0          );
[423]46            alSourcei (source, AL_SOURCE_RELATIVE, AL_FALSE      );
[410]47        }
48       
49       
50       
51       
52        void AudioStream::release()
[423]53        {
54                if (loaded)
55                {
[410]56            alSourceStop(source);
57            empty();
58            alDeleteSources(1, &source);
59            check();
60            alDeleteBuffers(1, buffers);
61            check();
62       
[423]63            ov_clear(&oggStream);
64                        loaded = false;
65                }
[410]66        }
67       
68       
69       
70       
71        void AudioStream::display()
[423]72        {
73                if (loaded)
74                {
[419]75            std::cout
[410]76                << "version         " << vorbisInfo->version         << "\n"
77                << "channels        " << vorbisInfo->channels        << "\n"
78                << "rate (hz)       " << vorbisInfo->rate            << "\n"
79                << "bitrate upper   " << vorbisInfo->bitrate_upper   << "\n"
80                << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n"
81                << "bitrate lower   " << vorbisInfo->bitrate_lower   << "\n"
82                << "bitrate window  " << vorbisInfo->bitrate_window  << "\n"
83                << "\n"
84                << "vendor " << vorbisComment->vendor << "\n";
85               
86            for(int i = 0; i < vorbisComment->comments; i++)
[419]87                std::cout << "   " << vorbisComment->user_comments[i] << "\n";
[410]88               
[423]89            std::cout << std::endl;     
90                }
[410]91        }
92       
93       
94       
95       
96        bool AudioStream::playback()
[423]97        {
98                if (!loaded)
99                {
100                        return false;
101                }
102
[410]103            if(playing())
104                return true;
105               
106            if(!stream(buffers[0]))
107                return false;
108               
109            if(!stream(buffers[1]))
110                return false;
111           
112            alSourceQueueBuffers(source, 2, buffers);
113            alSourcePlay(source);
114           
115            return true;
116        }
117       
118       
119       
120       
121        bool AudioStream::playing()
[423]122        {
123                if (!loaded)
124                {
125                        return false;
126                }
127
[410]128            ALenum state;
129            alGetSourcei(source, AL_SOURCE_STATE, &state);
130            return (state == AL_PLAYING);
131        }
132       
133       
134       
135       
136        bool AudioStream::update()
137        {
138            int processed;
139            bool active = true;
140       
141            alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
142       
143            while(processed--)
144            {
145                ALuint buffer;
146               
147                alSourceUnqueueBuffers(source, 1, &buffer);
148                check();
149       
150                active = stream(buffer);
151       
152                alSourceQueueBuffers(source, 1, &buffer);
153                check();
154            }
155       
156            return active;
157        }
158       
159       
160       
161       
162        bool AudioStream::stream(ALuint buffer)
163        {
164            char pcm[BUFFER_SIZE];
165            int  size = 0;
166            int  section;
167            int  result;
168       
169            while(size < BUFFER_SIZE)
170            {
171                result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, &section);
172           
173                if(result > 0)
174                    size += result;
175                else
176                    if(result < 0)
[423]177                        orxonox::Error(errorString(result));
[410]178                    else
179                        break;
180            }
181           
182            if(size == 0)
183                return false;
184               
185            alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
186            check();
187           
188            return true;
189        }
190       
191       
[423]192
[410]193        void AudioStream::empty()
194        {
195            int queued;
196           
197            alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
198           
199            while(queued--)
200            {
201                ALuint buffer;
202           
203                alSourceUnqueueBuffers(source, 1, &buffer);
204                check();
205            }
206        }
207       
208       
209       
210       
211        void AudioStream::check()
212        {
213                int error = alGetError();
214       
215                if(error != AL_NO_ERROR)
[423]216                        orxonox::Error("OpenAL error was raised.");
[410]217        }
218       
219       
220       
221        std::string AudioStream::errorString(int code)
222        {
223            switch(code)
224            {
225                case OV_EREAD:
226                    return std::string("Read from media.");
227                case OV_ENOTVORBIS:
228                    return std::string("Not Vorbis data.");
229                case OV_EVERSION:
230                    return std::string("Vorbis version mismatch.");
231                case OV_EBADHEADER:
232                    return std::string("Invalid Vorbis header.");
233                case OV_EFAULT:
[419]234                    return std::string("Internal logic fault (bug or heap/stack corruption.");
[410]235                default:
236                    return std::string("Unknown Ogg error.");
237            }
238        }
[419]239}
240
Note: See TracBrowser for help on using the repository browser.