Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/sound5/src/orxonox/sound/SoundStreamer.cc @ 10422

Last change on this file since 10422 was 6931, checked in by erwin, 15 years ago

Merged changes from local sandbox to sound5

File size: 5.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#include <boost/thread.hpp>
30#include <AL/al.h>
31#include <AL/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, OggVorbis_File* vf, int current_section)
44    {
45        char inbuffer[4096];
46        vorbis_info* vorbisInfo;
47        vorbisInfo = ov_info(vf, -1);
48        ALenum format;
49        if (vorbisInfo->channels == 1)
50            format = AL_FORMAT_MONO16;
51        else
52            format = AL_FORMAT_STEREO16;
53
54        while(true) // Stream forever, control through thread control
55        {
56
57            int info;
58            alGetSourcei(audioSource, AL_SOURCE_STATE, &info);
59            if(info == AL_PLAYING)
60                COUT(4) << "Sound: " << dataStream->getName() << " is playing." << std::endl;
61            else
62            {
63                COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl;
64            }
65
66            if(alcGetCurrentContext() == NULL)
67            {
68                COUT(2) << "Sound: There is no context, terminating thread for " << dataStream->getName() << std::endl;
69                return;
70            }
71
72            int processed;
73            alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
74            if (ALint error = alGetError())
75                COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl;
76
77            COUT(4) << "Sound: processed buffers: " << processed << std::endl;
78
79            if(processed > 0)
80            {
81                ALuint* buffers = new ALuint[processed];
82                alSourceUnqueueBuffers(audioSource, processed, buffers);
83                if (ALint error = alGetError())
84                    COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl;
85           
86                int queued;
87                alGetSourcei(audioSource, AL_BUFFERS_QUEUED, &queued);
88                if (ALint error = alGetError())
89                    COUT(2) << "Sound: Warning: Couldn't get number of queued buffers: " << getALErrorString(error) << std::endl;
90                COUT(4) << "Sound: queued buffers: " << queued << std::endl;
91
92                for(int i = 0; i < processed; i++)
93                {
94                    long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
95                    if (ret == 0)
96                    {
97                        COUT(4) << "Sound: End of file " << dataStream->getName() << ", terminating thread" << std::endl;
98                        return;
99                    }
100                    else if (ret < 0)
101                    {
102                        COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl;
103                        ov_clear(vf);
104                        return;
105                    }
106
107                    alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate);
108                    if(ALint error = alGetError()) {
109                        COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl;
110                        break;
111                    }
112                    alSourceQueueBuffers(audioSource, 1, &buffers[i]);
113                    if (ALint error = alGetError()) {
114                        COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl;
115                    }
116                }
117            }
118            else
119            {
120                msleep(10); // perhaps another value here is better
121            }
122
123            try {
124                boost::this_thread::interruption_point();
125            }
126            catch(boost::thread_interrupted) {
127                COUT(4) << "Sound: Catched interruption. Terminating thread for " << dataStream->getName() << std::endl;
128                ALuint* buffers = new ALuint[5];
129                alSourceUnqueueBuffers(audioSource, 5, buffers);
130                if (ALint error = alGetError())
131                    COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl;
132
133                alDeleteBuffers(5, buffers);
134                if (ALint error = alGetError())
135                    COUT(2) << "Sound: Warning: Couldn't delete buffers: " << getALErrorString(error) << std::endl;
136
137                return;
138            }
139        }
140    }
141}
Note: See TracBrowser for help on using the repository browser.