#include <msp/core/refptr.h>
#include <msp/io/file.h>
-#include "oggdecoder.h"
+#include <msp/strings/format.h>
+#ifdef WITH_LIBMAD
+#include "mad/mp3decoder.h"
+#endif
+#ifdef WITH_LIBVORBIS
+#include "vorbis/oggdecoder.h"
+#endif
#include "sounddecoder.h"
using namespace std;
namespace Msp {
namespace AL {
+unsupported_sound::unsupported_sound(const string &w):
+ runtime_error(w)
+{ }
+
+
SoundDecoder::SoundDecoder():
source(0),
freq(0),
SoundDecoder *SoundDecoder::open_file(const string &fn)
{
RefPtr<IO::BufferedFile> file = new IO::BufferedFile(fn);
- SoundDecoder *decoder = new OggDecoder(*file);
+ SoundDecoder *decoder = open_io(*file);
decoder->source = file.release();
return decoder;
}
SoundDecoder *SoundDecoder::open_io(IO::Seekable &io)
{
- return new OggDecoder(io);
+ char sig_buf[8];
+ io.read(sig_buf, sizeof(sig_buf));
+ io.seek(0, IO::S_BEG);
+ string signature(sig_buf, sizeof(sig_buf));
+
+#ifdef WITH_LIBVORBIS
+ if(OggDecoder::detect(signature))
+ return new OggDecoder(io);
+#endif
+
+#ifdef WITH_LIBMAD
+ if(Mp3Decoder::detect(signature))
+ return new Mp3Decoder(io);
+#endif
+
+ string sig_hex;
+ for(unsigned i=0; i<sizeof(sig_buf); ++i)
+ {
+ if(i)
+ sig_hex += ' ';
+ sig_hex += Msp::format("%02X", static_cast<unsigned char>(sig_buf[i]));
+ }
+ throw unsupported_sound(sig_hex);
}
} // namespace AL