shithub: sox

Download patch

ref: 4a7da1a2ffeb475912e7c3d94812af27dd421468
parent: b1720d247188415baf55bdc0f9b9d295a739ad6c
author: Ulrich Klauer <ulrich@chirlu.de>
date: Mon Mar 19 06:59:29 EDT 2012

dither: separate -a code from main logic

Make the code layout of "dither" cleaner by separating the automatic
start/stop code (-a option) from the main logic. No functional changes.

--- a/src/dither.c
+++ b/src/dither.c
@@ -262,7 +262,7 @@
   int32_t       history, ranqd1, r;
   double const  * coefs;
   sox_bool      dither_off;
-  int           (*flow)(sox_effect_t *, const sox_sample_t *, sox_sample_t *, size_t *, size_t *);
+  sox_effect_handler_flow flow;
 } priv_t;
 
 #define CONVOLVE _ _ _ _
@@ -299,7 +299,19 @@
   size_t len = *isamp = *osamp = min(*isamp, *osamp);
 
   while (len--) {
-    if (!p->auto_detect || (p->history = ((p->history << 1) + !!(*ibuf & (((unsigned)-1) >> p->prec))))) {
+    if (p->auto_detect) {
+      p->history = (p->history << 1) +
+          !!(*ibuf & (((unsigned)-1) >> p->prec));
+      if (p->history && p->dither_off) {
+        p->dither_off = sox_false;
+        lsx_debug("flow %" PRIuPTR ": on  @ %" PRIu64, effp->flow, p->num_output);
+      } else if (!p->history && !p->dither_off) {
+        p->dither_off = sox_true;
+        lsx_debug("flow %" PRIuPTR ": off @ %" PRIu64, effp->flow, p->num_output);
+      }
+    }
+
+    if (!p->dither_off) {
       int32_t r = RANQD1 >> p->prec;
       double d = ((double)*ibuf++ + r + (p->alt_tpdf? -p->r : (RANQD1 >> p->prec))) / (1 << (32 - p->prec));
       int i = d < 0? d - .5 : d + .5;
@@ -310,16 +322,9 @@
         ++effp->clips, *obuf = SOX_INT_MAX(p->prec) << (32 - p->prec);
       else *obuf = i << (32 - p->prec);
       ++obuf;
-      if (p->dither_off)
-        lsx_debug("flow %" PRIuPTR ": on  @ %" PRIu64, effp->flow, p->num_output);
-      p->dither_off = sox_false;
     }
-    else {
+    else
       *obuf++ = *ibuf++;
-      if (!p->dither_off)
-        lsx_debug("flow %" PRIuPTR ": off @ %" PRIu64, effp->flow, p->num_output);
-      p->dither_off = sox_true;
-    }
     ++p->num_output;
   }
   return SOX_SUCCESS;
--- a/src/dither.h
+++ b/src/dither.h
@@ -11,7 +11,21 @@
   size_t len = *isamp = *osamp = min(*isamp, *osamp);
 
   while (len--) {
-    if (!p->auto_detect || (p->history = ((p->history << 1) + !!(*ibuf & (((unsigned)-1) >> p->prec))))) {
+    if (p->auto_detect) {
+      p->history = (p->history << 1) +
+          !!(*ibuf & (((unsigned)-1) >> p->prec));
+      if (p->history && p->dither_off) {
+        p->dither_off = sox_false;
+        lsx_debug("flow %" PRIuPTR ": on  @ %" PRIu64, effp->flow, p->num_output);
+      } else if (!p->history && !p->dither_off) {
+        p->dither_off = sox_true;
+        memset(p->previous_errors, 0, sizeof(p->previous_errors));
+        memset(p->previous_outputs, 0, sizeof(p->previous_outputs));
+        lsx_debug("flow %" PRIuPTR ": off @ %" PRIu64, effp->flow, p->num_output);
+      }
+    }
+
+    if (!p->dither_off) {
       int32_t r1 = RANQD1 >> p->prec, r2 = RANQD1 >> p->prec; /* Defer add! */
 #ifdef IIR
       double d1, d, output = 0;
@@ -36,20 +50,9 @@
         ++effp->clips, *obuf = SOX_INT_MAX(p->prec) << (32 - p->prec);
       else *obuf = i << (32 - p->prec);
       ++obuf;
-
-      if (p->dither_off)
-        lsx_debug("flow %" PRIuPTR ": on  @ %" PRIu64, effp->flow, p->num_output);
-      p->dither_off = sox_false;
     }
-    else {
+    else
       *obuf++ = *ibuf++;
-      if (!p->dither_off) {
-        lsx_debug("flow %" PRIuPTR ": off @ %" PRIu64, effp->flow, p->num_output);
-        memset(p->previous_errors, 0, sizeof(p->previous_errors));
-        memset(p->previous_outputs, 0, sizeof(p->previous_outputs));
-      }
-      p->dither_off = sox_true;
-    }
     ++p->num_output;
   }
   return SOX_SUCCESS;