shithub: sox

Download patch

ref: 250f564ceae0930c9ac1dc9b827de03fd1189e5b
parent: 83a3a0a316e27909c6f31655bc7c8c8a2585e2b1
author: cbagwell <cbagwell>
date: Mon Sep 13 21:58:17 EDT 2004

More bugfixes to clipping on MIN side.

--- a/src/dcshift.c
+++ b/src/dcshift.c
@@ -76,11 +76,11 @@
 
         dcs->uselimiter = 1; /* ok, we'll use it */
         /* The following equation is derived so that there is no 
-	 * discontinuity in output amplitudes */
+         * discontinuity in output amplitudes */
         /* and a ST_SAMPLE_MAX input always maps to a ST_SAMPLE_MAX output 
-	 * when the limiter is activated. */
+         * when the limiter is activated. */
         /* (NOTE: There **WILL** be a discontinuity in the slope of the 
-	 * output amplitudes when using the limiter.) */
+         * output amplitudes when using the limiter.) */
         dcs->limiterthreshhold = ST_SAMPLE_MAX * (ONE - (fabs(dcs->dcshift) - dcs->limitergain));
     }
 
@@ -125,10 +125,10 @@
          dcs->clipped++;
          return ST_SAMPLE_MAX;
     }
-    else if (v < -ST_SAMPLE_MAX)
+    else if (v < ST_SAMPLE_MIN)
     {
         dcs->clipped++;
-        return -ST_SAMPLE_MAX;
+        return ST_SAMPLE_MIN;
     }
     /* else */
     return (st_sample_t) v;
@@ -171,11 +171,17 @@
                 }
                 else if (sample < -limiterthreshhold && dcshift < 0)
                 {
+                        /* Note this should really be ST_SAMPLE_MIN but
+                         * the clip() below will take care of the overflow.
+                         */
                         sample =  (sample + limiterthreshhold) * limitergain / (ST_SAMPLE_MAX - limiterthreshhold) - limiterthreshhold + dcshift;
                         dcs->limited++;
                 }
                 else
                 {
+                        /* Note this should consider ST_SAMPLE_MIN but
+                         * the clip() below will take care of the overflow.
+                         */
                         sample = dcshift * ST_SAMPLE_MAX + sample;
                 }
 
--- a/src/pan.c
+++ b/src/pan.c
@@ -99,10 +99,10 @@
  */
 static st_sample_t clip(pan_t pan, PAN_FLOAT value)
 {
-    if (value < -ST_SAMPLE_MAX) 
+    if (value < ST_SAMPLE_MIN) 
     {
         pan->clipped++;
-        return -ST_SAMPLE_MAX;
+        return ST_SAMPLE_MIN;
     }
     else if (value > ST_SAMPLE_MAX) 
     {
--- a/src/pitch.c
+++ b/src/pitch.c
@@ -277,10 +277,10 @@
 
 static st_sample_t clip(pitch_t pitch, PITCH_FLOAT v)
 {
-    if (v < -ST_SAMPLE_MAX)
+    if (v < ST_SAMPLE_MIN)
     {
         pitch->clipped++;
-        return -ST_SAMPLE_MAX;
+        return ST_SAMPLE_MIN;
     }
     else if (v > ST_SAMPLE_MAX)
     {
--- a/src/polyphas.c
+++ b/src/polyphas.c
@@ -539,8 +539,8 @@
 {
         if (sample > ST_SAMPLE_MAX)
         return ST_SAMPLE_MAX;
-        if (sample < -ST_SAMPLE_MAX)
-        return -ST_SAMPLE_MAX;
+        if (sample < ST_SAMPLE_MIN)
+        return ST_SAMPLE_MIN;
         return sample;
 }
 
--- a/src/resample.c
+++ b/src/resample.c
@@ -357,9 +357,9 @@
                 // orig: *obuf++ = r->Y[i] * ISCALE;
                 Float ftemp = r->Y[i] * ISCALE;
 
-                if (ftemp >= ST_SAMPLE_MAX)
+                if (ftemp > ST_SAMPLE_MAX)
                         *obuf = ST_SAMPLE_MAX;
-                else if (ftemp <= ST_SAMPLE_MIN)
+                else if (ftemp < ST_SAMPLE_MIN)
                         *obuf = ST_SAMPLE_MIN;
                 else
                         *obuf = ftemp;
--- a/src/speed.c
+++ b/src/speed.c
@@ -95,10 +95,10 @@
 /* clip if necessary, and report. */
 static st_sample_t clip(speed_t speed, SPEED_FLOAT v)
 {
-    if (v < -ST_SAMPLE_MAX)
+    if (v < ST_SAMPLE_MIN)
     {
         speed->clipped++;
-        return -ST_SAMPLE_MAX;
+        return ST_SAMPLE_MIN;
     }
     else if (v > ST_SAMPLE_MAX)
     {
--- a/src/stretch.c
+++ b/src/stretch.c
@@ -107,10 +107,10 @@
  */
 static st_sample_t clip(stretch_t stretch, STRETCH_FLOAT v)
 {
-    if (v < -ST_SAMPLE_MAX)
+    if (v < ST_SAMPLE_MIN)
     {
         stretch->clipped++;
-        return -ST_SAMPLE_MAX;
+        return ST_SAMPLE_MIN;
     }
     else if (v > ST_SAMPLE_MAX)
     {