Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ois_update/src/orxonox/sound/SoundStreamer.cc @ 7701

Last change on this file since 7701 was 7664, checked in by youngk, 14 years ago

Corrected some serious bug in OpenAL Mac and reduced warnings.

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Erwin 'vaiursch' Herrsche
24 *   Co-authors:
25 *
26 */
27#include "SoundStreamer.h"
28
29#ifdef ORXONOX_PLATFORM_APPLE
30#include "openal/al.h"
31#else
32#include <al.h>
33#endif
34
35#include <vorbis/vorbisfile.h>
36#include "SoundManager.h"
37
38namespace orxonox
39{
40    // vorbis callbacks
41    size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource);
42    int seekVorbis(void* datasource, ogg_int64_t offset, int whence);
43    long tellVorbis(void* datasource);
44
45    void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream)
46    {
47        // Open file with custom streaming
48        ov_callbacks vorbisCallbacks;
49        vorbisCallbacks.read_func  = &readVorbis;
50        vorbisCallbacks.seek_func  = &seekVorbis;
51        vorbisCallbacks.tell_func  = &tellVorbis;
52        vorbisCallbacks.close_func = NULL;
53
54        OggVorbis_File vf;
55        int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks);
56        if (ret < 0)
57        {
58            COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
59            ov_clear(&vf);
60            return;
61        }
62        vorbis_info* vorbisInfo;
63        vorbisInfo = ov_info(&vf, -1);
64        ALenum format;
65        if (vorbisInfo->channels == 1)
66            format = AL_FORMAT_MONO16;
67        else
68            format = AL_FORMAT_STEREO16;
69
70        char inbuffer[256*1024];
71        ALuint initbuffers[4];
72        alGenBuffers(4, initbuffers);
73        int current_section;
74
75        for(int i = 0; i < 4; i++)
76        {
77            long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
78            if (ret == 0)
79            {
80                return;
81            }
82            else if (ret < 0)
83            {
84                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
85                ov_clear(&vf);
86                return;
87            }
88
89            alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
90        }
91        alSourceQueueBuffers(audioSource, 4, initbuffers);
92
93        while(true) // Stream forever, control through thread control
94        {
95            int processed;
96            alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
97            if (ALint error = alGetError())
98            COUT(2) << "Sound Warning: Couldn't get number of processed buffers: "
99                    << SoundManager::getALErrorString(error) << std::endl;
100
101            if(processed > 0)
102            {
103                ALuint* buffers = new ALuint[processed];
104                alSourceUnqueueBuffers(audioSource, processed, buffers);
105                if (ALint error = alGetError())
106                    COUT(2) << "Sound Warning: Couldn't unqueue buffers: "
107                    << SoundManager::getALErrorString(error) << std::endl;
108
109                for(int i = 0; i < processed; i++)
110                {
111                    long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
112                    if (ret == 0)
113                    {
114                        return;
115                    }
116                    else if (ret < 0)
117                    {
118                        COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
119                        ov_clear(&vf);
120                        return;
121                    }
122
123                    alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate);
124                }
125
126                alSourceQueueBuffers(audioSource, processed, buffers);
127                if (ALint error = alGetError())
128                    COUT(2) << "Sound Warning: Couldn't queue buffers: "
129                    << SoundManager::getALErrorString(error) << std::endl;
130            }
131        }
132    }
133}
Note: See TracBrowser for help on using the repository browser.