From: Mikko Rasa Date: Tue, 20 Nov 2012 16:07:41 +0000 (+0200) Subject: Use Msp::IO in SoundDecoder X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ef9906f379e066cb9c754853a3cc771e8e881356;p=libs%2Fal.git Use Msp::IO in SoundDecoder --- diff --git a/source/buffer.cpp b/source/buffer.cpp index 5aef1b4..8e53964 100644 --- a/source/buffer.cpp +++ b/source/buffer.cpp @@ -1,3 +1,4 @@ +#include #include "buffer.h" #include "waveform.h" @@ -44,7 +45,8 @@ Buffer::Loader::Loader(Buffer &b): void Buffer::Loader::sound_data(const string &data) { Waveform wave; - wave.load_memory(data.data(), data.size()); + IO::Memory mem(data.data(), data.size()); + wave.load_io(mem); buf.data(wave); } diff --git a/source/sounddecoder.cpp b/source/sounddecoder.cpp index d4d1767..c9da9b8 100644 --- a/source/sounddecoder.cpp +++ b/source/sounddecoder.cpp @@ -1,62 +1,47 @@ #include #include +#include +#include #include "sounddecoder.h" using namespace std; namespace { -struct MemorySource +size_t read(void *ptr, size_t size, size_t nmemb, void *src) { - const void *data; - unsigned length; - unsigned pos; - - MemorySource(const void *d, unsigned l): data(d), length(l), pos(0) { } -}; - -size_t memory_read(void *ptr, size_t size, size_t nmemb, void *src) -{ - MemorySource &memsrc = *reinterpret_cast(src); - unsigned len = min(size*nmemb, memsrc.length-memsrc.pos); - memcpy(ptr, reinterpret_cast(memsrc.data)+memsrc.pos, len); - memsrc.pos += len; - + Msp::IO::Base *in = reinterpret_cast(src); + unsigned len = in->read(reinterpret_cast(ptr), size*nmemb); return len/size; } -int memory_seek(void *src, ogg_int64_t offset, int whence) +int seek(void *src, ogg_int64_t offset, int whence) { - MemorySource &memsrc = *reinterpret_cast(src); + Msp::IO::SeekType type; if(whence==SEEK_SET) - memsrc.pos = offset; + type = Msp::IO::S_BEG; else if(whence==SEEK_CUR) - memsrc.pos += offset; + type = Msp::IO::S_CUR; else if(whence==SEEK_END) - memsrc.pos = memsrc.length-offset; - memsrc.pos = min(memsrc.pos, memsrc.length); - - return memsrc.pos; -} + type = Msp::IO::S_END; + else + return -1; -int memory_close(void *src) -{ - delete reinterpret_cast(src); - return 0; + Msp::IO::Seekable *in = reinterpret_cast(src); + return in->seek(offset, type); } -long memory_tell(void *src) +long tell(void *src) { - MemorySource &memsrc = *reinterpret_cast(src); - return memsrc.pos; + return reinterpret_cast(src)->tell(); } -ov_callbacks memory_callbacks= +ov_callbacks io_callbacks = { - &memory_read, - &memory_seek, - &memory_close, - &memory_tell + &read, + &seek, + 0, + &tell }; } // namespace @@ -65,6 +50,7 @@ namespace Msp { namespace AL { SoundDecoder::SoundDecoder(): + source(0), eof_flag(false) { ovfile.datasource = 0; @@ -72,31 +58,32 @@ SoundDecoder::SoundDecoder(): SoundDecoder::~SoundDecoder() { - if(ovfile.datasource) - ov_clear(&ovfile); + close(); } void SoundDecoder::open_file(const string &fn) { if(ovfile.datasource) throw logic_error("Sound has already been opened"); - if(ov_fopen(const_cast(fn.c_str()), &ovfile)<0) + + IO::BufferedFile *file = new IO::BufferedFile(fn); + if(ov_open_callbacks(file, &ovfile, 0, 0, io_callbacks)<0) + { + delete file; throw runtime_error("Could not open ogg vorbis file "+fn); + } + source = file; open_common(); } -void SoundDecoder::open_memory(const void *d, unsigned len) +void SoundDecoder::open_io(Msp::IO::Seekable &io) { if(ovfile.datasource) throw logic_error("Sound has already been opened"); - MemorySource *src = new MemorySource(d, len); - if(ov_open_callbacks(src, &ovfile, 0, 0, memory_callbacks)<0) - { - delete src; - throw runtime_error("Could not open ogg vorbis memory block"); - } + if(ov_open_callbacks(&io, &ovfile, 0, 0, io_callbacks)<0) + throw runtime_error("Could not open ogg vorbis resource"); open_common(); } @@ -120,6 +107,8 @@ void SoundDecoder::close() { if(ovfile.datasource) ov_clear(&ovfile); + delete source; + source = 0; } void SoundDecoder::rewind() diff --git a/source/sounddecoder.h b/source/sounddecoder.h index ad56646..c956659 100644 --- a/source/sounddecoder.h +++ b/source/sounddecoder.h @@ -3,6 +3,7 @@ #include #include +#include #include "format.h" namespace Msp { @@ -16,6 +17,7 @@ class SoundDecoder { private: OggVorbis_File ovfile; + IO::Seekable *source; unsigned freq; unsigned size; Format format; @@ -26,7 +28,7 @@ public: ~SoundDecoder(); void open_file(const std::string &); - void open_memory(const void *, unsigned); + void open_io(IO::Seekable &); private: void open_common(); public: diff --git a/source/waveform.cpp b/source/waveform.cpp index 36978c6..de195f7 100644 --- a/source/waveform.cpp +++ b/source/waveform.cpp @@ -25,10 +25,10 @@ void Waveform::load_file(const string &fn) load(decoder); } -void Waveform::load_memory(const void *d, unsigned len) +void Waveform::load_io(IO::Seekable &io) { SoundDecoder decoder; - decoder.open_memory(d, len); + decoder.open_io(io); load(decoder); } diff --git a/source/waveform.h b/source/waveform.h index c1f0a2d..db0d61d 100644 --- a/source/waveform.h +++ b/source/waveform.h @@ -2,6 +2,7 @@ #define MSP_AL_WAVEFORM_H_ #include +#include #include "format.h" namespace Msp { @@ -22,7 +23,7 @@ public: ~Waveform(); void load_file(const std::string &); - void load_memory(const void *, unsigned); + void load_io(IO::Seekable &); void load(SoundDecoder &); Format get_format() const { return format; }