| [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> | 
|---|
| [6354] | 30 | #include <al.h> | 
|---|
| [6562] | 31 | #include <alc.h> | 
|---|
| [6354] | 32 | #include <vorbis/vorbisfile.h> | 
|---|
| [6378] | 33 | #include "SoundManager.h" | 
|---|
| [6506] | 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 |  | 
|---|
 | 43 |     void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream) | 
|---|
 | 44 |     { | 
|---|
| [6674] | 45 |         COUT(4) << "Sound: Creating thread for " << dataStream->getName() << std::endl; | 
|---|
| [6733] | 46 |  | 
|---|
 | 47 |         alSourcei(audioSource, AL_BUFFER, 0); | 
|---|
 | 48 |  | 
|---|
| [6354] | 49 |         // Open file with custom streaming | 
|---|
 | 50 |         ov_callbacks vorbisCallbacks; | 
|---|
 | 51 |         vorbisCallbacks.read_func  = &readVorbis; | 
|---|
 | 52 |         vorbisCallbacks.seek_func  = &seekVorbis; | 
|---|
 | 53 |         vorbisCallbacks.tell_func  = &tellVorbis; | 
|---|
 | 54 |         vorbisCallbacks.close_func = NULL; | 
|---|
 | 55 |  | 
|---|
 | 56 |         OggVorbis_File vf; | 
|---|
 | 57 |         int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks); | 
|---|
 | 58 |         if (ret < 0) | 
|---|
 | 59 |         { | 
|---|
 | 60 |             COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; | 
|---|
 | 61 |             ov_clear(&vf); | 
|---|
 | 62 |             return; | 
|---|
 | 63 |         } | 
|---|
 | 64 |         vorbis_info* vorbisInfo; | 
|---|
 | 65 |         vorbisInfo = ov_info(&vf, -1); | 
|---|
 | 66 |         ALenum format; | 
|---|
 | 67 |         if (vorbisInfo->channels == 1) | 
|---|
 | 68 |             format = AL_FORMAT_MONO16; | 
|---|
 | 69 |         else | 
|---|
 | 70 |             format = AL_FORMAT_STEREO16; | 
|---|
 | 71 |  | 
|---|
| [6674] | 72 |         char inbuffer[4096]; | 
|---|
| [6726] | 73 |         ALuint initbuffers[5]; | 
|---|
 | 74 |         alGenBuffers(5, initbuffers); | 
|---|
| [6562] | 75 |         if (ALint error = alGetError()) { | 
|---|
 | 76 |             COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl; | 
|---|
 | 77 |             return; | 
|---|
 | 78 |         } | 
|---|
| [6354] | 79 |         int current_section; | 
|---|
 | 80 |  | 
|---|
| [6726] | 81 |         for(int i = 0; i < 5; i++) | 
|---|
| [6354] | 82 |         { | 
|---|
 | 83 |             long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); | 
|---|
 | 84 |             if (ret == 0) | 
|---|
 | 85 |             { | 
|---|
| [6674] | 86 |                 break; | 
|---|
| [6354] | 87 |             } | 
|---|
 | 88 |             else if (ret < 0) | 
|---|
 | 89 |             { | 
|---|
 | 90 |                 COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; | 
|---|
 | 91 |                 ov_clear(&vf); | 
|---|
 | 92 |                 return; | 
|---|
 | 93 |             } | 
|---|
 | 94 |  | 
|---|
 | 95 |             alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate); | 
|---|
| [6674] | 96 |             if(ALint error = alGetError()) { | 
|---|
 | 97 |                 COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl; | 
|---|
 | 98 |                 break; | 
|---|
 | 99 |              } | 
|---|
 | 100 |              alSourceQueueBuffers(audioSource, 1, &initbuffers[i]); | 
|---|
 | 101 |              if (ALint error = alGetError()) { | 
|---|
 | 102 |                  COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl; | 
|---|
 | 103 |              } | 
|---|
| [6354] | 104 |         } | 
|---|
 | 105 |  | 
|---|
| [6733] | 106 |         alSourcei(audioSource, AL_LOOPING, AL_TRUE); | 
|---|
 | 107 |  | 
|---|
 | 108 |         alSourcePlay(audioSource); | 
|---|
 | 109 |         if(ALint error = alGetError()) | 
|---|
 | 110 |             COUT(2) << "Sound: Could not start ambient sound" << std::endl; | 
|---|
 | 111 |  | 
|---|
| [6354] | 112 |         while(true) // Stream forever, control through thread control | 
|---|
 | 113 |         { | 
|---|
| [6562] | 114 |  | 
|---|
| [6733] | 115 |             int info; | 
|---|
 | 116 |             alGetSourcei(audioSource, AL_SOURCE_STATE, &info); | 
|---|
 | 117 |             if(info == AL_PLAYING) | 
|---|
 | 118 |                 COUT(4) << "Sound: " << dataStream->getName() << " is playing." << std::endl; | 
|---|
 | 119 |             else | 
|---|
 | 120 |                 COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl; | 
|---|
 | 121 |  | 
|---|
| [6674] | 122 |             if(alcGetCurrentContext() == NULL) | 
|---|
 | 123 |             { | 
|---|
| [6729] | 124 |                 COUT(2) << "Sound: There is no context, terminating thread for " << dataStream->getName() << std::endl; | 
|---|
| [6674] | 125 |                 return; | 
|---|
 | 126 |             } | 
|---|
| [6562] | 127 |  | 
|---|
| [6733] | 128 |             int processed; | 
|---|
| [6354] | 129 |             alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed); | 
|---|
 | 130 |             if (ALint error = alGetError()) | 
|---|
| [6674] | 131 |                 COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl; | 
|---|
| [6733] | 132 |             COUT(4) << "Sound: processed buffers: " << processed << std::endl; | 
|---|
| [6354] | 133 |  | 
|---|
 | 134 |             if(processed > 0) | 
|---|
 | 135 |             { | 
|---|
 | 136 |                 ALuint* buffers = new ALuint[processed]; | 
|---|
 | 137 |                 alSourceUnqueueBuffers(audioSource, processed, buffers); | 
|---|
 | 138 |                 if (ALint error = alGetError()) | 
|---|
| [6562] | 139 |                     COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl; | 
|---|
| [6354] | 140 |  | 
|---|
 | 141 |                 for(int i = 0; i < processed; i++) | 
|---|
 | 142 |                 { | 
|---|
 | 143 |                     long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); | 
|---|
 | 144 |                     if (ret == 0) | 
|---|
 | 145 |                     { | 
|---|
| [6726] | 146 |                         return; | 
|---|
| [6354] | 147 |                     } | 
|---|
 | 148 |                     else if (ret < 0) | 
|---|
 | 149 |                     { | 
|---|
| [6729] | 150 |                         COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl; | 
|---|
| [6354] | 151 |                         ov_clear(&vf); | 
|---|
 | 152 |                         return; | 
|---|
 | 153 |                     } | 
|---|
 | 154 |  | 
|---|
 | 155 |                     alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate); | 
|---|
| [6674] | 156 |                     if(ALint error = alGetError()) { | 
|---|
 | 157 |                         COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl; | 
|---|
 | 158 |                         break; | 
|---|
 | 159 |                     } | 
|---|
 | 160 |                     alSourceQueueBuffers(audioSource, 1, &buffers[i]); | 
|---|
 | 161 |                     if (ALint error = alGetError()) { | 
|---|
 | 162 |                         COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl; | 
|---|
 | 163 |                     } | 
|---|
| [6354] | 164 |                 } | 
|---|
 | 165 |             } | 
|---|
| [6674] | 166 |             try { | 
|---|
 | 167 |                 boost::this_thread::interruption_point(); | 
|---|
 | 168 |             } | 
|---|
 | 169 |             catch(boost::thread_interrupted) { | 
|---|
 | 170 |                 COUT(4) << "Sound: Catched interruption. Terminating thread for " << dataStream->getName() << std::endl; | 
|---|
| [6729] | 171 |                 ALuint* buffers = new ALuint[5]; | 
|---|
 | 172 |                 alSourceUnqueueBuffers(audioSource, 5, buffers); | 
|---|
 | 173 |                 if (ALint error = alGetError()) | 
|---|
 | 174 |                     COUT(2) << "Sound: Warning: Couldn't unqueue buffers: " << getALErrorString(error) << std::endl; | 
|---|
 | 175 |  | 
|---|
 | 176 |                 alDeleteBuffers(5, buffers); | 
|---|
 | 177 |                 if (ALint error = alGetError()) | 
|---|
 | 178 |                     COUT(2) << "Sound: Warning: Couldn't delete buffers: " << getALErrorString(error) << std::endl; | 
|---|
 | 179 |  | 
|---|
| [6674] | 180 |                 return; | 
|---|
 | 181 |             } | 
|---|
| [6733] | 182 |             msleep(50); // perhaps another value here is better | 
|---|
| [6354] | 183 |         } | 
|---|
 | 184 |     } | 
|---|
| [6393] | 185 | } | 
|---|