]> git.tdb.fi Git - libs/al.git/commitdiff
Add utility functions for manipulating formats
authorMikko Rasa <tdb@tdb.fi>
Sun, 24 Jun 2018 14:43:58 +0000 (17:43 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 24 Jun 2018 14:43:58 +0000 (17:43 +0300)
source/format.cpp [new file with mode: 0644]
source/format.h
source/mp3decoder.cpp
source/oggdecoder.cpp

diff --git a/source/format.cpp b/source/format.cpp
new file mode 100644 (file)
index 0000000..1cb5b23
--- /dev/null
@@ -0,0 +1,59 @@
+#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
index ed6a1442750a73056102af93e76bb9d40b5edc0e..4895377c062a7a45d39dc9747d9b8dc971add530 100644 (file)
@@ -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
 
index 8ddad47032ee08eae3155ec133747773f9546726..00fc9181bd69a2a2d6318d7d13153c3a71fce363 100644 (file)
@@ -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;
index d28734208d605e2c912444fe662fb3c72c6c5413..78a2b5ac158d20a857db455f7ee7e1e969b12922 100644 (file)
@@ -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()