Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/sound/SoundStreamer.cc @ 6371

Last change on this file since 6371 was 6354, checked in by erwin, 15 years ago

Implemented streamer thread, TODO patch SoundBuffer to use it

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#include "SoundManager.h"
29#include <al.h>
30#include <vorbis/vorbisfile.h>
31
32namespace orxonox
33{
34    // vorbis callbacks
35    size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource);
36    int seekVorbis(void* datasource, ogg_int64_t offset, int whence);
37    long tellVorbis(void* datasource);
38
39    void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream)
40    {
41        // Open file with custom streaming
42        ov_callbacks vorbisCallbacks;
43        vorbisCallbacks.read_func  = &readVorbis;
44        vorbisCallbacks.seek_func  = &seekVorbis;
45        vorbisCallbacks.tell_func  = &tellVorbis;
46        vorbisCallbacks.close_func = NULL;
47
48        OggVorbis_File vf;
49        int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks);
50        if (ret < 0)
51        {
52            COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
53            ov_clear(&vf);
54            return;
55        }
56        vorbis_info* vorbisInfo;
57        vorbisInfo = ov_info(&vf, -1);
58        ALenum format;
59        if (vorbisInfo->channels == 1)
60            format = AL_FORMAT_MONO16;
61        else
62            format = AL_FORMAT_STEREO16;
63
64        char inbuffer[256*1024];
65        ALuint initbuffers[4];
66        alGenBuffers(4, initbuffers);
67        int current_section;
68
69        for(int i = 0; i < 4; i++)
70        {
71            long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
72            if (ret == 0)
73            {
74                return;
75            }
76            else if (ret < 0)
77            {
78                COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
79                ov_clear(&vf);
80                return;
81            }
82
83            alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
84        }
85        alSourceQueueBuffers(audioSource, 4, initbuffers);
86
87        while(true) // Stream forever, control through thread control
88        {
89            int processed;
90            alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
91            if (ALint error = alGetError())
92            COUT(2) << "Sound Warning: Couldn't get number of processed buffers: "
93                    << SoundManager::getALErrorString(error) << std::endl;
94
95            if(processed > 0)
96            {
97                ALuint* buffers = new ALuint[processed];
98                alSourceUnqueueBuffers(audioSource, processed, buffers);
99                if (ALint error = alGetError())
100                    COUT(2) << "Sound Warning: Couldn't unqueue buffers: "
101                    << SoundManager::getALErrorString(error) << std::endl;
102
103                for(int i = 0; i < processed; i++)
104                {
105                    long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
106                    if (ret == 0)
107                    {
108                        return;
109                    }
110                    else if (ret < 0)
111                    {
112                        COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
113                        ov_clear(&vf);
114                        return;
115                    }
116
117                    alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate);
118                }
119
120                alSourceQueueBuffers(audioSource, processed, buffers);
121                if (ALint error = alGetError())
122                    COUT(2) << "Sound Warning: Couldn't queue buffers: "
123                    << SoundManager::getALErrorString(error) << std::endl;
124            }
125        }
126    }
127}
Note: See TracBrowser for help on using the repository browser.