Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Commit changes to transfer to netbook

File size: 6.8 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[5];
71        alGenBuffers(5, 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 < 5; 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        }
102
103        while(true) // Stream forever, control through thread control
104        {
105            int processed;
106
107            if(alcGetCurrentContext() == NULL)
108            {
109                COUT(2) << "Sound: There is no context, terminating thread for " << dataStream->getName() << std::endl;
110                return;
111            }
112
113            alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
114            if (ALint error = alGetError())
115                COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl;
116
117            if(processed > 0)
118            {
119                COUT(4) << "Sound: " << processed << std::endl;
120                ALuint* buffers = new ALuint[processed];
121                alSourceUnqueueBuffers(audioSource, processed, buffers);
122                if (ALint error = alGetError())
123                    COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl;
124
125                int info;
126                alGetSourcei(audioSource, AL_SOURCE_STATE, &info);
127                if(info == AL_PLAYING)
128                    COUT(4) << "Sound: " << dataStream->getName() << " is playing." << std::endl;
129                else
130                    COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl;
131
132                for(int i = 0; i < processed; i++)
133                {
134                    long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
135                    if (ret == 0)
136                    {
137                        return;
138                    }
139                    else if (ret < 0)
140                    {
141                        COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl;
142                        ov_clear(&vf);
143                        return;
144                    }
145
146                    alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate);
147                    if(ALint error = alGetError()) {
148                        COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl;
149                        break;
150                    }
151                    alSourceQueueBuffers(audioSource, 1, &buffers[i]);
152                    if (ALint error = alGetError()) {
153                        COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl;
154                    }
155                }
156            }
157            try {
158                boost::this_thread::interruption_point();
159            }
160            catch(boost::thread_interrupted) {
161                COUT(4) << "Sound: Catched interruption. Terminating thread for " << dataStream->getName() << std::endl;
162                ALuint* buffers = new ALuint[5];
163                alSourceUnqueueBuffers(audioSource, 5, buffers);
164                if (ALint error = alGetError())
165                    COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl;
166
167                alDeleteBuffers(5, buffers);
168                if (ALint error = alGetError())
169                    COUT(2) << "Sound: Warning: Couldn't delete buffers: " << getALErrorString(error) << std::endl;
170
171                return;
172            }
173            msleep(100); // perhaps another value here is better
174        }
175    }
176}
Note: See TracBrowser for help on using the repository browser.