]> git.tdb.fi Git - libs/al.git/blob - source/sounddecoder.cpp
Add signature detection for sound files
[libs/al.git] / source / sounddecoder.cpp
1 #include <msp/core/refptr.h>
2 #include <msp/io/file.h>
3 #include <msp/strings/format.h>
4 #include "oggdecoder.h"
5 #include "sounddecoder.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace AL {
11
12 unsupported_sound::unsupported_sound(const string &w):
13         runtime_error(w)
14 { }
15
16
17 SoundDecoder::SoundDecoder():
18         source(0),
19         freq(0),
20         size(0),
21         format(MONO8),
22         eof_flag(false)
23 { }
24
25 SoundDecoder::~SoundDecoder()
26 {
27         delete source;
28 }
29
30 SoundDecoder *SoundDecoder::open_file(const string &fn)
31 {
32         RefPtr<IO::BufferedFile> file = new IO::BufferedFile(fn);
33         SoundDecoder *decoder = open_io(*file);
34         decoder->source = file.release();
35         return decoder;
36 }
37
38 SoundDecoder *SoundDecoder::open_io(IO::Seekable &io)
39 {
40         char sig_buf[8];
41         io.read(sig_buf, sizeof(sig_buf));
42         string signature(sig_buf, sizeof(sig_buf));
43         if(OggDecoder::detect(signature))
44                 return new OggDecoder(io);
45         else
46         {
47                 string sig_hex;
48                 for(unsigned i=0; i<sizeof(sig_buf); ++i)
49                 {
50                         if(i)
51                                 sig_hex += ' ';
52                         sig_hex += Msp::format("%02X", static_cast<unsigned char>(sig_buf[i]));
53                 }
54                 throw unsupported_sound(sig_hex);
55         }
56 }
57
58 } // namespace AL
59 } // namespace Msp