shithub: sox

Download patch

ref: e995776d8097934fbce2791c0da8749e08382d94
parent: 22be79e45c4ddf331c574f53bca3259998ce4f61
author: robs <robs>
date: Sat Dec 16 16:48:08 EST 2006

Nullify effects when they've nothing to do.

--- a/ChangeLog
+++ b/ChangeLog
@@ -63,7 +63,11 @@
     conversions.  (robs)
   o Added large file support.  (Reuben Thomas)
   o Higher quality audio speed adjustment.  (robs)
-  o Added ability to merge e.g. 2 mono files to 1 stereo file.  (robs)
+  o Added ability to merge e.g. 2 mono files to 1 stereo file
+    [FR# 1297076].  (robs)
+  o Several effects are now optimised in situations where they need
+    do nothing, e.g. changing rate from 8000 to 8000, or changing volume
+    by 0dB [Bug# 1395781].  (robs)
 
 sox-12.18.2
 -----------
--- a/TODO
+++ b/TODO
@@ -51,8 +51,6 @@
     of drain (ie. can't run the "reverse" effect twice
     on the same command line and have a good output file).
 
-  o Make resample effects idempotent when rates are the same.
-
   o Add MP3 support to WAV handler. Can copy most work from mp3.c.
 
   o Implement speex (http://www.speex.org).
--- a/src/dcshift.c
+++ b/src/dcshift.c
@@ -76,6 +76,9 @@
 {
     dcs_t dcs = (dcs_t) effp->priv;
 
+    if (dcs->dcshift == 0)
+      return ST_EFF_NULL;
+
     if (effp->outinfo.channels != effp->ininfo.channels)
     {
         st_warn("DCSHIFT cannot handle different channels (in=%d, out=%d)"
--- a/src/equalizer.c
+++ b/src/equalizer.c
@@ -94,6 +94,9 @@
   double amp;
   double alpha;
 
+  if (eq->gain == 0)
+    return ST_EFF_NULL;
+
   /* Sample rate */
   eq->rate = effp->ininfo.rate;
 
--- a/src/pitch.c
+++ b/src/pitch.c
@@ -433,6 +433,9 @@
         return ST_EOF;
     }
 
+    if (pitch->shift == 0)
+      return ST_EFF_NULL;
+
     return ST_SUCCESS;
 }
 
--- a/src/polyphas.c
+++ b/src/polyphas.c
@@ -378,10 +378,7 @@
     int k;
 
     if (effp->ininfo.rate == effp->outinfo.rate)
-    {
-        st_fail("Input and Output rate must not be the same to use polyphase effect");
-        return(ST_EOF);
-    }
+      return ST_EFF_NULL;
 
     st_initrand();
 
--- a/src/rabbit.c
+++ b/src/rabbit.c
@@ -83,10 +83,9 @@
   double in_rate = floor(effp->ininfo.rate / effp->globalinfo->speed + .5)
     * effp->globalinfo->speed;/* Make speedr more accurate (st_rate_t is int) */
 
-  if (effp->ininfo.rate == effp->outinfo.rate) {
-    st_fail("Input and Output rates must be different to use rabbit effect");
-    return (ST_EOF);
-  }
+  if (effp->ininfo.rate == effp->outinfo.rate)
+    return ST_EFF_NULL;
+
   if (effp->ininfo.channels != effp->outinfo.channels) {
     st_fail("number of Input and Output channels must be equal to use rabbit effect");
     return (ST_EOF);
--- a/src/repeat.c
+++ b/src/repeat.c
@@ -59,6 +59,9 @@
 {
         repeat_t repeat = (repeat_t)effp->priv;
 
+        if (repeat->repeats == 1)
+          return ST_EFF_NULL;
+
         if ((repeat->fp = tmpfile()) == NULL) {
                 st_fail("repeat: could not create temporary file");
                 return (ST_EOF);
--- a/src/resample.c
+++ b/src/resample.c
@@ -225,10 +225,7 @@
     * effp->globalinfo->speed;/* Make speedr more accurate (st_rate_t is int) */
 
         if (effp->ininfo.rate == effp->outinfo.rate)
-        {
-            st_fail("Input and Output rates must be different to use resample effect");
-            return(ST_EOF);
-        }
+          return ST_EFF_NULL;
                 
         r->Factor = (double)effp->outinfo.rate / in_rate;
 
--- a/src/sox.c
+++ b/src/sox.c
@@ -1180,16 +1180,25 @@
 
 static int start_effects(void)
 {
-    int e, ret = ST_SUCCESS;
+    int e, j, ret = ST_SUCCESS;
 
     for(e = 1; e < neffects; e++) {
         efftab[e].clippedCount = 0;
         if ((ret = (*efftab[e].h->start)(&efftab[e])) == ST_EOF)
             break;
-        if (efftabR[e].name)
+        if (ret == ST_EFF_NULL) {
+          st_report("'%s' has no effect in this configuration.",efftab[e].name);
+          --neffects;
+          for (j = e--; j < neffects; ++j) {
+            efftab[j] = efftab[j + 1];
+            efftabR[j] = efftabR[j + 1];
+          }
+          ret = ST_SUCCESS;
+        }
+        else if (efftabR[e].name)
         {
             efftabR[e].clippedCount = 0;
-            if ((ret = (*efftabR[e].h->start)(&efftabR[e])) == ST_EOF)
+            if ((ret = (*efftabR[e].h->start)(&efftabR[e])) != ST_SUCCESS)
                 break;
         }
     }
--- a/src/speed.c
+++ b/src/speed.c
@@ -110,6 +110,9 @@
 {
     speed_t speed = (speed_t) effp->priv;
 
+    if (speed->factor == 1)
+      return ST_EFF_NULL;
+
     if (speed->factor >= 1.0)
     {
         speed->compression = (int) speed->factor; /* floor */
--- a/src/stretch.c
+++ b/src/stretch.c
@@ -164,6 +164,9 @@
     stretch_t stretch = (stretch_t) effp->priv;
     st_size_t i;
 
+    if (stretch->factor == 1)
+      return ST_EFF_NULL;
+
     /* not necessary. taken care by effect processing? */
     if (effp->outinfo.channels != effp->ininfo.channels)
     {
--- a/src/tone.c
+++ b/src/tone.c
@@ -97,6 +97,9 @@
   double alpha = sin(w0)/2 * sqrt( (A + 1/A)*(1/p->oomph - 1) + 2 );
   double a0;
 
+  if (p->gain == 0)
+    return ST_EFF_NULL;
+
   /* Calculate filter coefficients: */
   p->b0 =    A*( (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha );  /* Numerator. */
   p->b1 =  2*A*( (A-1) - (A+1)*cos(w0)                   );
--- a/src/vol.c
+++ b/src/vol.c
@@ -94,6 +94,9 @@
 {
     vol_t vol = (vol_t) effp->priv;
     
+    if (vol->gain == 1)
+      return ST_EFF_NULL;
+
     if (effp->outinfo.channels != effp->ininfo.channels)
     {
         st_warn("VOL cannot handle different channels (in=%d, out=%d)"