diff options
Diffstat (limited to 'src/audio/vorbisfile.cc')
-rw-r--r-- | src/audio/vorbisfile.cc | 71 |
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; +} + +} |