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