shithub: sox

Download patch

ref: 909fad8e63a39336d247a39b607f998a27097423
parent: a5092b2f444acc4875b2e2e5f82a2e1ebda15fc5
author: Mans Rullgard <mans@mansr.com>
date: Fri Aug 7 10:10:19 EDT 2020

wav: merge format-specific fmt handling in single switch statement

Combine the scattered switch statements checking supported formats
and setting the encoding field into one.

--- a/src/wav.c
+++ b/src/wav.c
@@ -580,18 +580,36 @@
         lsx_report("EXTENSIBLE");
     }
 
+    /* User options take precedence */
+    if (ft->signal.channels == 0 || ft->signal.channels == wChannels)
+        ft->signal.channels = wChannels;
+    else
+        lsx_report("User options overriding channels read in .wav header");
+
+    if (ft->signal.channels == 0) {
+        lsx_fail_errno(ft, SOX_EHDR, "Channel count is zero");
+        return SOX_EOF;
+    }
+
+    if (ft->signal.rate == 0 || ft->signal.rate == dwSamplesPerSecond)
+        ft->signal.rate = dwSamplesPerSecond;
+    else
+        lsx_report("User options overriding rate read in .wav header");
+
     switch (wav->formatTag) {
     case WAVE_FORMAT_PCM:
-        /* Default depends on sample size.  Set that later on. */
-        enc = SOX_ENCODING_UNKNOWN;
-        break;
+        bytespersample = (wav->bitsPerSample + 7) / 8;
 
-    case WAVE_FORMAT_IMA_ADPCM:
-        enc = SOX_ENCODING_IMA_ADPCM;
-        break;
+        if (bytespersample == 1) {
+            enc = SOX_ENCODING_UNSIGNED;
+        } else if (bytespersample <= 4) {
+            enc = SOX_ENCODING_SIGN2;
+        } else {
+            lsx_fail_errno(ft, SOX_EFMT, "%d bytes per sample not suppored",
+                           bytespersample);
+            return SOX_EOF;
+        }
 
-    case WAVE_FORMAT_ADPCM:
-        enc = SOX_ENCODING_MS_ADPCM;
         break;
 
     case WAVE_FORMAT_IEEE_FLOAT:
@@ -606,36 +624,11 @@
         enc = SOX_ENCODING_ULAW;
         break;
 
-    case WAVE_FORMAT_GSM610:
-        enc = SOX_ENCODING_GSM;
-        break;
-
-    default:
-        return wavfail(ft, wav->formatTag);
-    }
-
-    /* User options take precedence */
-    if (ft->signal.channels == 0 || ft->signal.channels == wChannels)
-        ft->signal.channels = wChannels;
-    else
-        lsx_report("User options overriding channels read in .wav header");
-
-    if (ft->signal.channels == 0) {
-        lsx_fail_errno(ft, SOX_EHDR, "Channel count is zero");
-        return SOX_EOF;
-    }
-
-    if (ft->signal.rate == 0 || ft->signal.rate == dwSamplesPerSecond)
-        ft->signal.rate = dwSamplesPerSecond;
-    else
-        lsx_report("User options overriding rate read in .wav header");
-
-    switch (wav->formatTag) {
     case WAVE_FORMAT_ADPCM:
         if (wav_ms_adpcm_fmt(ft, len))
             return SOX_EOF;
 
-        bytespersample = 2;  /* AFTER de-compression */
+        enc = SOX_ENCODING_MS_ADPCM;
         break;
 
     case WAVE_FORMAT_IMA_ADPCM:
@@ -642,7 +635,7 @@
         if (wav_ima_adpcm_fmt(ft, len))
             return SOX_EOF;
 
-        bytespersample = 2;  /* AFTER de-compression */
+        enc = SOX_ENCODING_IMA_ADPCM;
         break;
 
     case WAVE_FORMAT_GSM610:
@@ -649,11 +642,11 @@
         if (wav_gsm_fmt(ft, len))
             return SOX_EOF;
 
-        bytespersample = 2;  /* AFTER de-compression */
+        enc = SOX_ENCODING_GSM;
         break;
 
     default:
-        bytespersample = (wav->bitsPerSample + 7) / 8;
+        return wavfail(ft, wav->formatTag);
         break;
     }
 
@@ -663,29 +656,6 @@
         ft->encoding.bits_per_sample = wav->bitsPerSample;
     else
         lsx_warn("User options overriding size read in .wav header");
-
-    /* Now we have enough information to set default encodings. */
-    if (enc == SOX_ENCODING_UNKNOWN) {
-        switch (bytespersample) {
-        case 1:
-            enc = SOX_ENCODING_UNSIGNED;
-            break;
-
-        case 2:
-        case 3:
-        case 4:
-            enc = SOX_ENCODING_SIGN2;
-            break;
-
-        case 8:
-            enc = SOX_ENCODING_FLOAT;
-            break;
-
-        default:
-            lsx_fail_errno(ft, SOX_EFMT, "Sorry, don't understand .wav size");
-            return SOX_EOF;
-        }
-    }
 
     if (ft->encoding.encoding != SOX_ENCODING_UNKNOWN &&
         ft->encoding.encoding != enc)