--- /dev/null
+#include <stdexcept>
+#include "format.h"
+
+using namespace std;
+
+namespace Msp {
+namespace AL {
+
+Format create_format(unsigned sample_size, unsigned n_channels)
+{
+ if(n_channels==1)
+ {
+ if(sample_size==1)
+ return MONO8;
+ else if(sample_size==2)
+ return MONO16;
+ }
+ else if(n_channels==2)
+ {
+ if(sample_size==1)
+ return STEREO8;
+ else if(sample_size==2)
+ return STEREO16;
+ }
+
+ throw invalid_argument("create_format");
+}
+
+unsigned get_sample_size(Format fmt)
+{
+ switch(fmt)
+ {
+ case MONO8:
+ case STEREO8: return 1;
+ case MONO16:
+ case STEREO16: return 2;
+ default: throw invalid_argument("get_sample_size");
+ }
+}
+
+unsigned get_n_channels(Format fmt)
+{
+ switch(fmt)
+ {
+ case MONO8:
+ case MONO16: return 1;
+ case STEREO8:
+ case STEREO16: return 2;
+ default: throw invalid_argument("get_n_channels");
+ }
+}
+
+unsigned get_unit_size(Format fmt)
+{
+ return get_sample_size(fmt)*get_n_channels(fmt);
+}
+
+} // namespace AL
+} // namespace Msp
STEREO16 = AL_FORMAT_STEREO16
};
+Format create_format(unsigned, unsigned);
+
+unsigned get_sample_size(Format);
+unsigned get_n_channels(Format);
+unsigned get_unit_size(Format);
+
} // namespace AL
} // namespace Msp
try
{
decode_frame();
- format = (priv->frame.header.mode==MAD_MODE_SINGLE_CHANNEL ? MONO16 : STEREO16);
+ format = create_format(2, MAD_NCHANNELS(&priv->frame.header));
freq = priv->frame.header.samplerate;
}
catch(...)
unsigned Mp3Decoder::read(char *buf, unsigned len)
{
- unsigned nchan = (format==STEREO16 ? 2 : 1);
+ unsigned nchan = get_n_channels(format);
mad_pcm &pcm = priv->synth.pcm;
unsigned pos = 0;
vorbis_info *info = ov_info(&priv->ovfile, -1);
freq = info->rate;
+ format = create_format(2, info->channels);
- size = ov_pcm_total(&priv->ovfile, 0)*info->channels*2;
-
- switch(info->channels)
- {
- case 1: format = MONO16; break;
- case 2: format = STEREO16; break;
- default: throw unsupported_sound(Msp::format("%d channels", info->channels));
- }
+ size = ov_pcm_total(&priv->ovfile, 0)*get_unit_size(format);
}
OggDecoder::~OggDecoder()