Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 708 was 708, checked in by rgrieder, 16 years ago
  • added Vector2, Vector3, Matrix3, ColourValue, Quaternion and String to the misc folder as header files (each of them contains #include <string> … typedef std::string String , etc.)
  • please use String from now on by including <misc/String.h"
  • removed #include <OgreVector3.h", etc. from "CoreIncludes.h" (adjusted all source files)
  • adjusted all the source files (except network, that keeps <string> for the moment) (what a mess..)
  • moved usleep hack to misc/Sleep.h
  • relative include paths for files from other root directories (like misc, network, etc.) (but it stills writes "../Orxonox.h" when in folder orxonox/objects)
  • "OgreSceneManager.h" —> <OgreSceneManager.h>
  • included OrxonoxPrereqs in every file in folder orxonox
  • moved HUD and ParticleInterface to namespace orxonox
  • removed some using namespace Ogre/std when appropriate
  • I hope I haven't forgotten important points..
File size: 6.0 KB
Line 
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
28
29#include "AudioStream.h"
30#include "orxonox/core/Debug.h"
31
32namespace audio
33{
34        AudioStream::AudioStream(orxonox::String path)
35        {
36                this->path = path;
37                loaded = false;
38        }
39
40        void AudioStream::open()
41        {
42            //int result;
43      errno_t result;
44
45
46            if(fopen_s(&oggFile, path.c_str(), "rb"))
47                        {
48                orxonox::Error("Could not open Ogg file "+path);
49                                return;
50                        }
51      else
52      {
53        COUT(4) << "Opened Ogg file" << path << std::endl;
54      }
55
56            /*if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
57            {
58        fclose(oggFile);
59              orxonox::Error("Could not open Ogg stream. " + errorString(result));
60                                return;
61            }*/
62
63                        loaded = true;
64
65            vorbisInfo = ov_info(&oggStream, -1);
66            vorbisComment = ov_comment(&oggStream, -1);
67
68            if(vorbisInfo->channels == 1)
69                format = AL_FORMAT_MONO16;
70            else
71                format = AL_FORMAT_STEREO16;
72
73
74            alGenBuffers(2, buffers);
75            check();
76            alGenSources(1, &source);
77            check();
78
79            alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
80            alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
81            alSource3f(source, AL_DIRECTION,       0.0, 0.0, 0.0);
82            alSourcef (source, AL_ROLLOFF_FACTOR,  0.0          );
83            alSourcei (source, AL_SOURCE_RELATIVE, AL_FALSE      );
84        }
85
86
87
88
89        void AudioStream::release()
90        {
91
92            alSourceStop(source);
93            empty();
94            alDeleteSources(1, &source);
95            check();
96            alDeleteBuffers(1, buffers);
97            check();
98
99            ov_clear(&oggStream);
100                        loaded = false;
101
102        }
103
104
105
106
107        void AudioStream::display()
108        {
109                if (loaded)
110                {
111            COUT(3)
112                << "version         " << vorbisInfo->version         << std::endl
113                << "channels        " << vorbisInfo->channels        << std::endl
114                << "rate (hz)       " << vorbisInfo->rate            << std::endl
115                << "bitrate upper   " << vorbisInfo->bitrate_upper   << std::endl
116                << "bitrate nominal " << vorbisInfo->bitrate_nominal << std::endl
117                << "bitrate lower   " << vorbisInfo->bitrate_lower   << std::endl
118                << "bitrate window  " << vorbisInfo->bitrate_window  << std::endl
119                << std::endl
120                << "vendor " << vorbisComment->vendor << std::endl;
121
122            for(int i = 0; i < vorbisComment->comments; i++)
123                COUT(3) << "   " << vorbisComment->user_comments[i] << std::endl;
124
125            COUT(3) << std::endl;
126                }
127        }
128
129
130
131
132        bool AudioStream::playback()
133        {
134                if (!loaded)
135                {
136                        return false;
137                }
138
139            if(playing())
140                return true;
141
142            if(!stream(buffers[0]))
143                return false;
144
145            if(!stream(buffers[1]))
146                return false;
147
148            alSourceQueueBuffers(source, 2, buffers);
149            alSourcePlay(source);
150
151            return true;
152        }
153
154
155
156
157        bool AudioStream::playing()
158        {
159                if (!loaded)
160                {
161                        return false;
162                }
163
164            ALenum state;
165            alGetSourcei(source, AL_SOURCE_STATE, &state);
166            return (state == AL_PLAYING);
167        }
168
169
170
171
172        bool AudioStream::update()
173        {
174            int processed;
175            bool active = true;
176
177            alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
178
179            while(processed--)
180            {
181                ALuint buffer;
182
183                alSourceUnqueueBuffers(source, 1, &buffer);
184                check();
185
186                active = stream(buffer);
187
188                alSourceQueueBuffers(source, 1, &buffer);
189                check();
190            }
191
192                        if (active==false)
193                        {
194                                loaded = false;
195                        }
196            return active;
197        }
198
199
200
201
202        bool AudioStream::stream(ALuint buffer)
203        {
204            char pcm[BUFFER_SIZE];
205            int  size = 0;
206            int  section;
207            int  result;
208
209            while(size < BUFFER_SIZE)
210            {
211                result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, &section);
212
213                if(result > 0)
214                    size += result;
215                else
216                    if(result < 0)
217                        orxonox::Error(errorString(result));
218                    else
219                        break;
220            }
221
222            if(size == 0)
223                return false;
224
225            alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
226            check();
227
228            return true;
229        }
230
231
232
233        void AudioStream::empty()
234        {
235            int queued;
236
237            alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
238
239            while(queued--)
240            {
241                ALuint buffer;
242
243                alSourceUnqueueBuffers(source, 1, &buffer);
244                check();
245            }
246        }
247
248
249
250
251        void AudioStream::check()
252        {
253                int error = alGetError();
254
255                if(error != AL_NO_ERROR)
256                        orxonox::Error("OpenAL error was raised.");
257        }
258
259
260
261        orxonox::String AudioStream::errorString(int code)
262        {
263            switch(code)
264            {
265                case OV_EREAD:
266                    return orxonox::String("Read from media.");
267                case OV_ENOTVORBIS:
268                    return orxonox::String("Not Vorbis data.");
269                case OV_EVERSION:
270                    return orxonox::String("Vorbis version mismatch.");
271                case OV_EBADHEADER:
272                    return orxonox::String("Invalid Vorbis header.");
273                case OV_EFAULT:
274                    return orxonox::String("Internal logic fault (bug or heap/stack corruption.");
275                default:
276                    return orxonox::String("Unknown Ogg error.");
277            }
278        }
279}
280
Note: See TracBrowser for help on using the repository browser.