[Orxonox-commit 1636] r6354 - code/branches/presentation2/src/orxonox/sound
erwin at orxonox.net
erwin at orxonox.net
Tue Dec 15 00:16:06 CET 2009
Author: erwin
Date: 2009-12-15 00:16:06 +0100 (Tue, 15 Dec 2009)
New Revision: 6354
Added:
code/branches/presentation2/src/orxonox/sound/SoundStreamer.cc
code/branches/presentation2/src/orxonox/sound/SoundStreamer.h
Modified:
code/branches/presentation2/src/orxonox/sound/CMakeLists.txt
Log:
Implemented streamer thread, TODO: patch SoundBuffer to use it
Modified: code/branches/presentation2/src/orxonox/sound/CMakeLists.txt
===================================================================
--- code/branches/presentation2/src/orxonox/sound/CMakeLists.txt 2009-12-14 20:38:31 UTC (rev 6353)
+++ code/branches/presentation2/src/orxonox/sound/CMakeLists.txt 2009-12-14 23:16:06 UTC (rev 6354)
@@ -4,5 +4,6 @@
SoundBuffer.cc
SoundManager.cc
WorldSound.cc
+ SoundStreamer.cc
)
Added: code/branches/presentation2/src/orxonox/sound/SoundStreamer.cc
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundStreamer.cc (rev 0)
+++ code/branches/presentation2/src/orxonox/sound/SoundStreamer.cc 2009-12-14 23:16:06 UTC (rev 6354)
@@ -0,0 +1,127 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Erwin 'vaiursch' Herrsche
+ * Co-authors:
+ *
+ */
+#include "SoundStreamer.h"
+#include "SoundManager.h"
+#include <al.h>
+#include <vorbis/vorbisfile.h>
+
+namespace orxonox
+{
+ // vorbis callbacks
+ size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource);
+ int seekVorbis(void* datasource, ogg_int64_t offset, int whence);
+ long tellVorbis(void* datasource);
+
+ void orxonox::SoundStreamer::operator()(ALuint audioSource, DataStreamPtr dataStream)
+ {
+ // Open file with custom streaming
+ ov_callbacks vorbisCallbacks;
+ vorbisCallbacks.read_func = &readVorbis;
+ vorbisCallbacks.seek_func = &seekVorbis;
+ vorbisCallbacks.tell_func = &tellVorbis;
+ vorbisCallbacks.close_func = NULL;
+
+ OggVorbis_File vf;
+ int ret = ov_open_callbacks(dataStream.get(), &vf, NULL, 0, vorbisCallbacks);
+ if (ret < 0)
+ {
+ COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
+ ov_clear(&vf);
+ return;
+ }
+ vorbis_info* vorbisInfo;
+ vorbisInfo = ov_info(&vf, -1);
+ ALenum format;
+ if (vorbisInfo->channels == 1)
+ format = AL_FORMAT_MONO16;
+ else
+ format = AL_FORMAT_STEREO16;
+
+ char inbuffer[256*1024];
+ ALuint initbuffers[4];
+ alGenBuffers(4, initbuffers);
+ int current_section;
+
+ for(int i = 0; i < 4; i++)
+ {
+ long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section);
+ if (ret == 0)
+ {
+ return;
+ }
+ else if (ret < 0)
+ {
+ COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
+ ov_clear(&vf);
+ return;
+ }
+
+ alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate);
+ }
+ alSourceQueueBuffers(audioSource, 4, initbuffers);
+
+ while(true) // Stream forever, control through thread control
+ {
+ int processed;
+ alGetSourcei(audioSource, AL_BUFFERS_PROCESSED, &processed);
+ if (ALint error = alGetError())
+ COUT(2) << "Sound Warning: Couldn't get number of processed buffers: "
+ << SoundManager::getALErrorString(error) << std::endl;
+
+ if(processed > 0)
+ {
+ ALuint* buffers = new ALuint[processed];
+ alSourceUnqueueBuffers(audioSource, processed, buffers);
+ if (ALint error = alGetError())
+ COUT(2) << "Sound Warning: Couldn't unqueue buffers: "
+ << SoundManager::getALErrorString(error) << std::endl;
+
+ for(int i = 0; i < processed; i++)
+ {
+ long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section);
+ if (ret == 0)
+ {
+ return;
+ }
+ else if (ret < 0)
+ {
+ COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl;
+ ov_clear(&vf);
+ return;
+ }
+
+ alBufferData(buffers[i], format, &inbuffer, ret, vorbisInfo->rate);
+ }
+
+ alSourceQueueBuffers(audioSource, processed, buffers);
+ if (ALint error = alGetError())
+ COUT(2) << "Sound Warning: Couldn't queue buffers: "
+ << SoundManager::getALErrorString(error) << std::endl;
+ }
+ }
+ }
+}
\ No newline at end of file
Added: code/branches/presentation2/src/orxonox/sound/SoundStreamer.h
===================================================================
--- code/branches/presentation2/src/orxonox/sound/SoundStreamer.h (rev 0)
+++ code/branches/presentation2/src/orxonox/sound/SoundStreamer.h 2009-12-14 23:16:06 UTC (rev 6354)
@@ -0,0 +1,45 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Erwin 'vaiursch' Herrsche
+ * Co-authors:
+ *
+ */
+
+#ifndef _SoundStreamer_H__
+#define _SoundStreamer_H__
+
+#include "OrxonoxPrereqs.h"
+
+#include <string>
+#include <OgreDataStream.h>
+
+namespace orxonox
+{
+ class _OrxonoxExport SoundStreamer // class for stream threads
+ {
+ public:
+ void operator()(ALuint audioSource, DataStreamPtr dataStream);
+ };
+}
+
+#endif /* _SoundStreamer_H__ */
More information about the Orxonox-commit
mailing list