shithub: sox

Download patch

ref: 021c7886e4ecfe58022848aa3f4877e1986e3089
parent: d68424fe69dec6bedd35bdaea03ecb3e7c3c6c1e
author: Mans Rullgard <mans@mansr.com>
date: Wed Aug 5 10:16:13 EDT 2020

stats: improve bit depth reporting [bug #267]

The right-hand figure reported for bit depth is somewhat erratic,
often changing if the signal is inverted or changed in other ways
that should not affect the value.

Change the calculation such that it always reports the number of
bits required to represent the largest sample value, positive or
negative, in two's complement notation.

--- a/src/stats.c
+++ b/src/stats.c
@@ -130,10 +130,9 @@
   for (; result && !(mask & 1); --result, mask >>= 1);
   if (x)
     *x = result;
-  mask = SOX_FLOAT_64BIT_TO_SAMPLE(max, dummy);
-  if (min < 0)
-    mask |= ~(SOX_FLOAT_64BIT_TO_SAMPLE(min, dummy) << 1);
-  for (; result && !(mask & SOX_SAMPLE_MIN); --result, mask <<= 1);
+  min = -fmax(fabs(min), fabs(max));
+  mask = SOX_FLOAT_64BIT_TO_SAMPLE(min, dummy) << 1;
+  for (; result && (mask & SOX_SAMPLE_MIN); --result, mask <<= 1);
   return result;
 }