Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged changes from local sandbox to sound5

File size: 5.5 KB
RevLine 
[6354]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"
[6378]28
[6674]29#include <boost/thread.hpp>
[6931]30#include <AL/al.h>
31#include <AL/alc.h>
[6354]32#include <vorbis/vorbisfile.h>
[6378]33#include "SoundManager.h"
[6506]34#include "util/Sleep.h"
[6354]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
[6931]43    void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section)
[6354]44    {
[6931]45        char inbuffer[4096];
[6354]46        vorbis_info* vorbisInfo;
[6931]47        vorbisInfo = ov_info(vf, -1);
[6354]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        {
[6562]56
[6733]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
[6931]62            {
[6733]63                COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl;
[6931]64            }
[6733]65
[6674]66            if(alcGetCurrentContext() == NULL)
67            {
[6729]68                COUT(2) << "Sound: There is no context, terminating thread for " << dataStream->getName() << std::endl;
[6674]69                return;
70            }
[6562]71
[6733]72            int processed;
[6354]73            alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
74            if (ALint error = alGetError())
[6674]75                COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl;
[6931]76
[6733]77            COUT(4) << "Sound: processed buffers: " << processed << std::endl;
[6354]78
79            if(processed > 0)
80            {
81                ALuint* buffers = new ALuint[processed];
82                alSourceUnqueueBuffers(audioSource, processed, buffers);
83                if (ALint error = alGetError())
[6562]84                    COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl;
[6931]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;
[6354]91
92                for(int i = 0; i < processed; i++)
93                {
[6931]94                    long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
[6354]95                    if (ret == 0)
96                    {
[6931]97                        COUT(4) << "Sound: End of file " << dataStream->getName() << ", terminating thread" << std::endl;
[6726]98                        return;
[6354]99                    }
100                    else if (ret < 0)
101                    {
[6729]102                        COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl;
[6931]103                        ov_clear(vf);
[6354]104                        return;
105                    }
106
107                    alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate);
[6674]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                    }
[6354]116                }
117            }
[6931]118            else
119            {
120                msleep(10); // perhaps another value here is better
121            }
122
[6674]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;
[6729]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
[6674]137                return;
138            }
[6354]139        }
140    }
[6393]141}
Note: See TracBrowser for help on using the repository browser.