Changeset 8007 for code/branches/sound6/src/orxonox/sound/SoundStreamer.cc
- Timestamp:
- Mar 1, 2011, 5:22:14 AM (14 years ago)
- Location:
- code/branches/sound6
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/sound6
- Property svn:mergeinfo changed
/code/branches/sound4 (added) merged: 6435,6476,6504 /code/branches/sound5 (added) merged: 6505-6511,6562,6674,6726,6729-6730,6733,6767,6769,6931,6962,7720
- Property svn:mergeinfo changed
-
code/branches/sound6/src/orxonox/sound/SoundStreamer.cc
r7163 r8007 27 27 #include "SoundStreamer.h" 28 28 29 #include <al.h> 29 #include <boost/thread.hpp> 30 #include <AL/al.h> 31 #include <AL/alc.h> 30 32 #include <vorbis/vorbisfile.h> 31 33 #include "SoundManager.h" 34 #include "util/Sleep.h" 32 35 33 36 namespace orxonox … … 38 41 long tellVorbis(void* datasource); 39 42 40 void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream )43 void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream, OggVorbis_File* vf, int current_section) 41 44 { 42 // Open file with custom streaming 43 ov_callbacks vorbisCallbacks; 44 vorbisCallbacks.read_func = &readVorbis; 45 vorbisCallbacks.seek_func = &seekVorbis; 46 vorbisCallbacks.tell_func = &tellVorbis; 47 vorbisCallbacks.close_func = NULL; 48 49 OggVorbis_File vf; 50 int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks); 51 if (ret < 0) 52 { 53 COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; 54 ov_clear(&vf); 55 return; 56 } 45 char inbuffer[4096]; 57 46 vorbis_info* vorbisInfo; 58 vorbisInfo = ov_info( &vf, -1);47 vorbisInfo = ov_info(vf, -1); 59 48 ALenum format; 60 49 if (vorbisInfo->channels == 1) … … 63 52 format = AL_FORMAT_STEREO16; 64 53 65 char inbuffer[256*1024]; 66 ALuint initbuffers[4]; 67 alGenBuffers(4, initbuffers); 68 int current_section; 54 while(true) // Stream forever, control through thread control 55 { 69 56 70 for(int i = 0; i < 4; i++) 71 { 72 long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); 73 if (ret == 0) 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 74 62 { 75 return;63 COUT(4) << "Sound: " << dataStream->getName() << " is not playing." << std::endl; 76 64 } 77 else if (ret < 0) 65 66 if(alcGetCurrentContext() == NULL) 78 67 { 79 COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; 80 ov_clear(&vf); 68 COUT(2) << "Sound: There is no context, terminating thread for " << dataStream->getName() << std::endl; 81 69 return; 82 70 } 83 71 84 alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);85 }86 alSourceQueueBuffers(audioSource, 4, initbuffers);87 88 while(true) // Stream forever, control through thread control89 {90 72 int processed; 91 73 alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed); 92 74 if (ALint error = alGetError()) 93 COUT(2) << "Sound Warning: Couldn't get number of processed buffers: " 94 << SoundManager::getALErrorString(error) << std::endl; 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; 95 78 96 79 if(processed > 0) … … 99 82 alSourceUnqueueBuffers(audioSource, processed, buffers); 100 83 if (ALint error = alGetError()) 101 COUT(2) << "Sound Warning: Couldn't unqueue buffers: " 102 << SoundManager::getALErrorString(error) << std::endl; 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; 103 91 104 92 for(int i = 0; i < processed; i++) 105 93 { 106 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); 107 95 if (ret == 0) 108 96 { 97 COUT(4) << "Sound: End of file " << dataStream->getName() << ", terminating thread" << std::endl; 109 98 return; 110 99 } 111 100 else if (ret < 0) 112 101 { 113 COUT(2) << "Sound: libvorbisfile: error reading the file "<< std::endl;114 ov_clear( &vf);102 COUT(2) << "Sound: libvorbisfile: error reading the file " << dataStream->getName() << std::endl; 103 ov_clear(vf); 115 104 return; 116 105 } 117 106 118 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 } 119 116 } 117 } 118 else 119 { 120 msleep(10); // perhaps another value here is better 121 } 120 122 121 alSourceQueueBuffers(audioSource, processed, buffers); 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); 122 130 if (ALint error = alGetError()) 123 COUT(2) << "Sound Warning: Couldn't queue buffers: " 124 << SoundManager::getALErrorString(error) << std::endl; 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; 125 138 } 126 139 }
Note: See TracChangeset
for help on using the changeset viewer.