]> git.tdb.fi Git - libs/al.git/blob - source/sounddecoder.cpp
Make sound format support optional
[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 #ifdef WITH_LIBMAD
5 #include "mad/mp3decoder.h"
6 #endif
7 #ifdef WITH_LIBVORBIS
8 #include "vorbis/oggdecoder.h"
9 #endif
10 #include "sounddecoder.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace AL {
16
17 unsupported_sound::unsupported_sound(const string &w):
18         runtime_error(w)
19 { }
20
21
22 SoundDecoder::SoundDecoder():
23         source(0),
24         freq(0),
25         size(0),
26         format(MONO8),
27         eof_flag(false)
28 { }
29
30 SoundDecoder::~SoundDecoder()
31 {
32         delete source;
33 }
34
35 SoundDecoder *SoundDecoder::open_file(const string &fn)
36 {
37         RefPtr<IO::BufferedFile> file = new IO::BufferedFile(fn);
38         SoundDecoder *decoder = open_io(*file);
39         decoder->source = file.release();
40         return decoder;
41 }
42
43 SoundDecoder *SoundDecoder::open_io(IO::Seekable &io)
44 {
45         char sig_buf[8];
46         io.read(sig_buf, sizeof(sig_buf));
47         io.seek(0, IO::S_BEG);
48         string signature(sig_buf, sizeof(sig_buf));
49
50 #ifdef WITH_LIBVORBIS
51         if(OggDecoder::detect(signature))
52                 return new OggDecoder(io);
53 #endif
54
55 #ifdef WITH_LIBMAD
56         if(Mp3Decoder::detect(signature))
57                 return new Mp3Decoder(io);
58 #endif
59
60         string sig_hex;
61         for(unsigned i=0; i<sizeof(sig_buf); ++i)
62         {
63                 if(i)
64                         sig_hex += ' ';
65                 sig_hex += Msp::format("%02X", static_cast<unsigned char>(sig_buf[i]));
66         }
67         throw unsupported_sound(sig_hex);
68 }
69
70 } // namespace AL
71 } // namespace Msp