Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 424 was 424, checked in by nicolape, 16 years ago

Audio bla

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