]> git.tdb.fi Git - libs/al.git/blobdiff - source/sounddecoder.cpp
Add signature detection for sound files
[libs/al.git] / source / sounddecoder.cpp
index 138117f730a9210545b807a0338bd8887861fa1d..a2cd51af4624154cca52312157cfb18f31a9b21e 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/core/refptr.h>
 #include <msp/io/file.h>
+#include <msp/strings/format.h>
 #include "oggdecoder.h"
 #include "sounddecoder.h"
 
@@ -29,14 +30,29 @@ SoundDecoder::~SoundDecoder()
 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));
+       string signature(sig_buf, sizeof(sig_buf));
+       if(OggDecoder::detect(signature))
+               return new OggDecoder(io);
+       else
+       {
+               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