Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

A few new tries to fix the sound problem: no success

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