Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound5/src/orxonox/sound/SoundStreamer.cc @ 6562

Last change on this file since 6562 was 6562, checked in by erwin, 14 years ago

More tries to fix the sound: there seems to be no context in the stream thread…

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