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