shithub: sox

Download patch

ref: bbb4030c093f7ad2d2187b5413bc40b83ba9cfd4
parent: 7cb3bcb34700aeab5af9125a014ba2af5eff8c0f
author: robs <robs>
date: Mon May 25 13:58:23 EDT 2009

Fix 'silence' empty output file with A-law input

--- a/ChangeLog
+++ b/ChangeLog
@@ -97,6 +97,7 @@
   o Fix `repeat' sometimes stopping repeating too soon.  (robs)
   o Fix `repeat' sometimes repeating wrong audio segments.  (robs)
   o Fix [2332343] 'silence' segfault with certain lengths. (cbagwell)
+  o Fix `silence' empty output file with A-law input.  (robs)
   o Fix temporary file problems in Windows (cygwin) with normalise and
     other effects.  (robs)
   o Fix [2779041] spectrogram PNG file is invalid on Windows.  (robs)
--- a/src/silence.c
+++ b/src/silence.c
@@ -271,44 +271,21 @@
     return(SOX_SUCCESS);
 }
 
-static int aboveThreshold(sox_effect_t * effp, sox_sample_t value, double threshold, int unit)
+static sox_bool aboveThreshold(sox_effect_t const * effp,
+    sox_sample_t value /* >= 0 */, double threshold, int unit)
 {
-    double ratio;
-    int rc;
-    sox_sample_t dummy_clipped_count = 0;
+  /* When scaling low bit data, noise values got scaled way up */
+  /* Only consider the original bits when looking for silence */
+  sox_sample_t masked_value = value & (-1 << (32 - effp->in_signal.precision));
 
-    /* When scaling low bit data, noise values got scaled way up */
-    /* Only consider the original bits when looking for silence */
-    switch(effp->in_signal.precision)
-    {
-        SOX_SAMPLE_LOCALS;
-        case 8:
-            value = SOX_SAMPLE_TO_SIGNED_8BIT(value, dummy_clipped_count);
-            ratio = (double)abs(value) / (double)SOX_INT8_MAX;
-            break;
-        case 16:
-            value = SOX_SAMPLE_TO_SIGNED_16BIT(value, dummy_clipped_count);
-            ratio = (double)abs(value) / (double)SOX_INT16_MAX;
-            break;
-        case 24:
-            value = SOX_SAMPLE_TO_SIGNED_24BIT(value, dummy_clipped_count);
-            ratio = (double)abs(value) / (double)SOX_INT24_MAX;
-            break;
-        case 32:
-            value = SOX_SAMPLE_TO_SIGNED_32BIT(value,);
-            ratio = (double)abs(value) / (double)SOX_INT32_MAX;
-            break;
-        default:
-            ratio = 0;
-    }
+  double scaled_value = (double)masked_value / SOX_SAMPLE_MAX;
 
-    if (unit == '%')
-        ratio *= 100.0;
-    else if (unit == 'd')
-        ratio = log10(ratio) * 20.0;
-    rc = (ratio >= threshold);
+  if (unit == '%')
+    scaled_value *= 100;
+  else if (unit == 'd')
+    scaled_value = linear_to_dB(scaled_value);
 
-    return rc;
+  return scaled_value >= threshold;
 }
 
 static sox_sample_t compute_rms(sox_effect_t * effp, sox_sample_t sample)