From: Mikko Rasa Date: Sun, 24 Jun 2018 14:43:58 +0000 (+0300) Subject: Add utility functions for manipulating formats X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=227da1e7a20432e056e7ab42444cb56f197c53d6;p=libs%2Fal.git Add utility functions for manipulating formats --- diff --git a/source/format.cpp b/source/format.cpp new file mode 100644 index 0000000..1cb5b23 --- /dev/null +++ b/source/format.cpp @@ -0,0 +1,59 @@ +#include +#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 diff --git a/source/format.h b/source/format.h index ed6a144..4895377 100644 --- a/source/format.h +++ b/source/format.h @@ -14,6 +14,12 @@ enum Format 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 diff --git a/source/mp3decoder.cpp b/source/mp3decoder.cpp index 8ddad47..00fc918 100644 --- a/source/mp3decoder.cpp +++ b/source/mp3decoder.cpp @@ -55,7 +55,7 @@ Mp3Decoder::Mp3Decoder(IO::Seekable &io): 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(...) @@ -92,7 +92,7 @@ void Mp3Decoder::rewind() 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; diff --git a/source/oggdecoder.cpp b/source/oggdecoder.cpp index d287342..78a2b5a 100644 --- a/source/oggdecoder.cpp +++ b/source/oggdecoder.cpp @@ -89,15 +89,9 @@ OggDecoder::OggDecoder(IO::Seekable &io): 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()