Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2011-04-17 14:21:29 +0000
committerStijn Buys <ingar@osirion.org>2011-04-17 14:21:29 +0000
commit09d68d3d1d77d45343e3562c0b5e0cd6816d47d3 (patch)
tree6b89c585fe8cd8bd49699cdb349566d3f382010e /src/audio/vorbisfile.cc
parent8264546908f1722b4d0f0e91c42dd791ba8535c4 (diff)
Initial Ogg Vorbis sounds effect support.
Diffstat (limited to 'src/audio/vorbisfile.cc')
-rw-r--r--src/audio/vorbisfile.cc71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/audio/vorbisfile.cc b/src/audio/vorbisfile.cc
new file mode 100644
index 0000000..572b945
--- /dev/null
+++ b/src/audio/vorbisfile.cc
@@ -0,0 +1,71 @@
+/*
+ audio/vorbis.cc
+ This file is part of the Osirion project and is distributed under
+ the terms of the GNU General Public License version 2
+*/
+
+#include "string.h"
+
+#include <iostream>
+#include <string>
+
+#include "audio/vorbisfile.h"
+#include "filesystem/filesystem.h"
+#include "sys/sys.h"
+
+#include <vorbis/codec.h>
+#include <vorbis/vorbisfile.h>
+
+namespace audio
+{
+
+PCM *Vorbis::load(std::string const & filename)
+{
+ if (!filename.size())
+ return 0;
+
+ filesystem::File *fs_file = filesystem::open(filename.c_str());
+ if (!fs_file) {
+ return 0;
+ }
+
+ std::string vorbis_path(fs_file->path());
+ vorbis_path.append(fs_file->name());
+ filesystem::close(fs_file);
+
+ OggVorbis_File vorbis_file;
+ if (ov_fopen(vorbis_path.c_str(), &vorbis_file) < 0 ) {
+ return 0;
+ }
+
+ vorbis_info* vorbis_streaminfo;
+ vorbis_streaminfo = ov_info(&vorbis_file, -1);
+
+ unsigned int samplerate = vorbis_streaminfo->rate;
+ unsigned int channels = vorbis_streaminfo->channels;
+ unsigned int bitspersample = 16; // always.
+
+ const size_t blocksize = 1024*1024; // 1Mb blocks
+
+ PCM *pcm = new PCM(samplerate, bitspersample, channels, blocksize);
+ pcm->set_size(0);
+
+ long bytesread = 0;
+
+ do {
+ // enlarge the pcm buffer if required
+ if (pcm->size() + 4096 > pcm->buff_size()) {
+ pcm->grow(pcm->buff_size() + blocksize);
+ }
+ bytesread = ov_read(&vorbis_file, (char *)pcm->data() + pcm->size(),4096,0,2,1,0);
+ pcm->set_size( pcm->size() + bytesread);
+ } while (bytesread > 0);
+
+ ov_clear(&vorbis_file);
+ con_debug << " " << filename << " " << pcm->samplerate() << "Hz " << pcm->bitspersample() << "bit "
+ << pcm->channels() << " chan " << pcm->size() << " bytes" << std::endl;
+
+ return pcm;
+}
+
+}