Changeset 6931 for code/branches/sound5/src/orxonox
- Timestamp:
- May 20, 2010, 1:51:27 PM (15 years ago)
- Location:
- code/branches/sound5/src/orxonox/sound
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/sound5/src/orxonox/sound/AmbientSound.cc
r6769 r6931 36 36 #include "SoundManager.h" 37 37 #include "SoundStreamer.h" 38 #include "util/Sleep.h" 38 39 39 40 #include <AL/alut.h> … … 42 43 { 43 44 CreateFactory(AmbientSound); 44 45 46 // vorbis callbacks 47 size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource); 48 int seekVorbis(void* datasource, ogg_int64_t offset, int whence); 49 long tellVorbis(void* datasource); 50 45 51 AmbientSound::AmbientSound(BaseObject* creator) 46 52 : BaseObject(creator) … … 51 57 52 58 // Ambient sounds always fade in 53 this->setVolume(0);59 //this->setVolume(0); 54 60 this->registerVariables(); 55 61 } … … 95 101 void AmbientSound::stop() 96 102 { 97 if (GameMode::playsSound()) 103 if (GameMode::playsSound()) 98 104 SoundManager::getInstance().unregisterAmbientSound(this); 99 105 } … … 170 176 if (this->soundstreamthread_.get_id() != boost::thread::id()) 171 177 { 172 this->soundstreamthread_.interrupt(); // unhandled interruptions lead to thread terminating ;-) 173 } 178 this->soundstreamthread_.interrupt(); // terminate an old thread if necessary 179 } 180 181 // queue some init buffers 182 COUT(4) << "Sound: Creating thread for " << source << std::endl; 174 183 // Get resource info 175 184 shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source); … … 181 190 // Open data stream 182 191 DataStreamPtr dataStream = Resource::open(fileInfo); 183 184 this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream); 192 193 alSourcei(this->audioSource_, AL_BUFFER, 0); 194 195 // Open file with custom streaming 196 ov_callbacks vorbisCallbacks; 197 vorbisCallbacks.read_func = &readVorbis; 198 vorbisCallbacks.seek_func = &seekVorbis; 199 vorbisCallbacks.tell_func = &tellVorbis; 200 vorbisCallbacks.close_func = NULL; 201 202 OggVorbis_File* vf = new OggVorbis_File(); 203 int ret = ov_open_callbacks(dataStream.get(), vf, NULL, 0, vorbisCallbacks); 204 if (ret < 0) 205 { 206 COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; 207 ov_clear(vf); 208 return; 209 } 210 vorbis_info* vorbisInfo; 211 vorbisInfo = ov_info(vf, -1); 212 ALenum format; 213 if (vorbisInfo->channels == 1) 214 format = AL_FORMAT_MONO16; 215 else 216 format = AL_FORMAT_STEREO16; 217 218 char inbuffer[4096]; 219 ALuint initbuffers[10]; 220 alGenBuffers(10, initbuffers); 221 if (ALint error = alGetError()) { 222 COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl; 223 return; 224 } 225 int current_section; 226 227 for(int i = 0; i < 10; i++) 228 { 229 long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); 230 if (ret == 0) 231 { 232 break; 233 } 234 else if (ret < 0) 235 { 236 COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; 237 ov_clear(vf); 238 return; 239 } 240 241 alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate); 242 if(ALint error = alGetError()) { 243 COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl; 244 break; 245 } 246 alSourceQueueBuffers(this->audioSource_, 1, &initbuffers[i]); 247 if (ALint error = alGetError()) { 248 COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl; 249 } 250 } 251 252 this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream, vf, current_section); 185 253 if(this->soundstreamthread_ == boost::thread()) 186 254 COUT(2) << "Sound: Failed to create thread." << std::endl; -
code/branches/sound5/src/orxonox/sound/SoundStreamer.cc
r6769 r6931 28 28 29 29 #include <boost/thread.hpp> 30 #include < al.h>31 #include < alc.h>30 #include <AL/al.h> 31 #include <AL/alc.h> 32 32 #include <vorbis/vorbisfile.h> 33 33 #include "SoundManager.h" … … 41 41 long tellVorbis(void* datasource); 42 42 43 void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream )43 void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section) 44 44 { 45 COUT(4) << "Sound: Creating thread for " << dataStream->getName() << std::endl; 46 47 alSourcei(audioSource, AL_BUFFER, 0); 48 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 } 45 char inbuffer[4096]; 64 46 vorbis_info* vorbisInfo; 65 vorbisInfo = ov_info( &vf, -1);47 vorbisInfo = ov_info(vf, -1); 66 48 ALenum format; 67 49 if (vorbisInfo->channels == 1) … … 69 51 else 70 52 format = AL_FORMAT_STEREO16; 71 72 char inbuffer[4096];73 ALuint initbuffers[5];74 alGenBuffers(5, initbuffers);75 if (ALint error = alGetError()) {76 COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl;77 return;78 }79 int current_section;80 81 for(int i = 0; i < 5; i++)82 {83 long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section);84 if (ret == 0)85 {86 break;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);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 }104 }105 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" << getALErrorString(error) << std::endl;111 53 112 54 while(true) // Stream forever, control through thread control … … 118 60 COUT(4) << "Sound: " << dataStream->getName() << " is playing." << std::endl; 119 61 else 62 { 120 63 COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl; 64 } 121 65 122 66 if(alcGetCurrentContext() == NULL) … … 130 74 if (ALint error = alGetError()) 131 75 COUT(2) << "Sound: Warning: Couldn't get number of processed buffers: " << getALErrorString(error) << std::endl; 76 132 77 COUT(4) << "Sound: processed buffers: " << processed << std::endl; 133 78 … … 138 83 if (ALint error = alGetError()) 139 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; 140 91 141 92 for(int i = 0; i < processed; i++) 142 93 { 143 long ret = ov_read( &vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section);94 long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); 144 95 if (ret == 0) 145 96 { 97 COUT(4) << "Sound: End of file " << dataStream->getName() << ", terminating thread" << std::endl; 146 98 return; 147 99 } … … 149 101 { 150 102 COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl; 151 ov_clear( &vf);103 ov_clear(vf); 152 104 return; 153 105 } … … 164 116 } 165 117 } 118 else 119 { 120 msleep(10); // perhaps another value here is better 121 } 122 166 123 try { 167 124 boost::this_thread::interruption_point(); … … 180 137 return; 181 138 } 182 msleep(50); // perhaps another value here is better183 139 } 184 140 } -
code/branches/sound5/src/orxonox/sound/SoundStreamer.h
r6506 r6931 32 32 33 33 #include <string> 34 #include <vorbis/vorbisfile.h> 34 35 #include <OgreDataStream.h> 35 36 #include "core/CorePrereqs.h" … … 40 41 { 41 42 public: 42 void operator()(ALuint audioSource, DataStreamPtr dataStream );43 void operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section); 43 44 }; 44 45 }
Note: See TracChangeset
for help on using the changeset viewer.