shithub: sox

Download patch

ref: ca7934099cd76a88f18a4626eebd36b1f080d111
parent: b3cb872a0070c189552eafd8a51f4d74998eeb44
author: robs <robs>
date: Sat Nov 1 08:49:49 EDT 2008

Fix 'phaser' clicks and overflows

--- a/ChangeLog
+++ b/ChangeLog
@@ -65,6 +65,7 @@
   o Fix rare crash with `rate' effect.  (robs)
   o Fix [2190767] `norm' under-amplifying in some cases.  (George Yohng)
   o Fix [2007062] Earwax effect can overflow; should clip. (robs)
+  o Fix `phaser' clicks and overflows.  (robs)
   o Trim will now skip past 2G point correctly. (cbagwell)
   o Improved handling of speed changes in the effects chain.  (robs)
 
--- a/src/phaser.c
+++ b/src/phaser.c
@@ -1,15 +1,11 @@
-/* August 24, 1998
- * Copyright (C) 1998 Juergen Mueller And Sundry Contributors
+/* Effect: phaser     Copyright (C) 1998 Juergen Mueller And Sundry Contributors
+ *
  * This source code is freely redistributable and may be used for
  * any purpose.  This copyright notice must be maintained.
  * Juergen Mueller And Sundry Contributors are not responsible for
  * the consequences of using this software.
- */
-
-/*
- *      Phaser effect.
  *
- * Flow diagram scheme:
+ * Flow diagram scheme:                                          August 24, 1998
  *
  *        * gain-in  +---+                     * gain-out
  * ibuff ----------->|   |----------------------------------> obuff
@@ -25,7 +21,6 @@
  *                     | Delay control |<-----| modulation speed |
  *                     +---------------+      +------------------+
  *
- *
  * The delay is controled by a sine or triangle modulation.
  *
  * Usage:
@@ -32,239 +27,118 @@
  *   phaser gain-in gain-out delay decay speed [ -s | -t ]
  *
  * Where:
- *   gain-in, decay :  0.0 ... 1.0      volume
- *   gain-out :  0.0 ...      volume
- *   delay :  0.0 ... 5.0 msec
- *   speed :  0.1 ... 2.0 Hz       modulation
- *   -s : modulation by sine (default)
- *   -t : modulation by triangle
+ *   gain-in, decay : 0.0 .. 1.0             volume
+ *   gain-out       : 0.0 ..                 volume
+ *   delay          : 0.0 .. 5.0 msec
+ *   speed          : 0.1 .. 2.0 Hz          modulation speed
+ *   -s             : modulation by sine     (default)
+ *   -t             : modulation by triangle
  *
  * Note:
- *   when decay is close to 1.0, the samples may begin clipping or the output
- *   can saturate!
- *
- * Hint:
- *   in-gain < ( 1 - decay * decay )
- *   1 / out-gain > gain-in / ( 1 - decay )
- *
-*/
-
-/*
- * libSoX phaser effect file.
+ *   When decay is close to 1.0, the samples may begin clipping or the output
+ *   can saturate!  Hint:
+ *     in-gain < (1 - decay * decay)
+ *     1 / out-gain > gain-in / (1 - decay)
  */
 
 #include "sox_i.h"
-
-#include <stdlib.h> /* Harmless, and prototypes atof() etc. --dgc */
 #include <string.h>
 
-#define MOD_SINE        0
-#define MOD_TRIANGLE    1
-
-/* Private data for SKEL file */
 typedef struct {
-        int     modulation;
-        int     counter;
-        int     phase;
-        double  *phaserbuf;
-        float   in_gain, out_gain;
-        float   delay, decay;
-        float   speed;
-        size_t length;
-        int     *lookup_tab;
-        size_t maxsamples, fade_out;
+  double     in_gain, out_gain, delay_ms, decay, mod_speed;
+  lsx_wave_t mod_type;
+
+  int        * mod_buf;
+  size_t     mod_buf_len;
+  int        mod_pos;
+            
+  double     * delay_buf;
+  size_t     delay_buf_len;
+  int        delay_pos;
 } priv_t;
 
-/*
- * Process options
- */
-static int sox_phaser_getopts(sox_effect_t * effp, int n, char **argv)
+static int getopts(sox_effect_t * effp, int argc, char * * argv)
 {
-        priv_t * phaser = (priv_t *) effp->priv;
+  priv_t * p = (priv_t *) effp->priv;
+  char chars[2];
 
-        if (!((n == 5) || (n == 6)))
-          return lsx_usage(effp);
+  /* Set non-zero defaults: */
+  p->in_gain   = .4;
+  p->out_gain  = .74;
+  p->delay_ms  = 3.;
+  p->decay     = .4;
+  p->mod_speed = .5;
 
-        sscanf(argv[0], "%f", &phaser->in_gain);
-        sscanf(argv[1], "%f", &phaser->out_gain);
-        sscanf(argv[2], "%f", &phaser->delay);
-        sscanf(argv[3], "%f", &phaser->decay);
-        sscanf(argv[4], "%f", &phaser->speed);
-        phaser->modulation = MOD_SINE;
-        if ( n == 6 ) {
-                if ( !strcmp(argv[5], "-s"))
-                        phaser->modulation = MOD_SINE;
-                else if ( ! strcmp(argv[5], "-t"))
-                        phaser->modulation = MOD_TRIANGLE;
-                else
-                  return lsx_usage(effp);
-        }
-        return (SOX_SUCCESS);
-}
+  do { /* break-able block */
+    NUMERIC_PARAMETER(in_gain  , .0, 1)
+    NUMERIC_PARAMETER(out_gain , .0, 1e9)
+    NUMERIC_PARAMETER(delay_ms , .0, 5)
+    NUMERIC_PARAMETER(decay    , .0, .99)
+    NUMERIC_PARAMETER(mod_speed, .1, 2)
+  } while (0);
 
-/*
- * Prepare for processing.
- */
-static int sox_phaser_start(sox_effect_t * effp)
-{
-        priv_t * phaser = (priv_t *) effp->priv;
-        unsigned int i;
+  if (argc && sscanf(*argv, "-%1[st]%c", chars, chars + 1) == 1) {
+    p->mod_type = *chars == 's'? SOX_WAVE_SINE : SOX_WAVE_TRIANGLE;
+    --argc, ++argv;
+  }
 
-        phaser->maxsamples = phaser->delay * effp->in_signal.rate / 1000.0;
+  if (p->in_gain > (1 - p->decay * p->decay))
+    lsx_warn("warning: gain-in might cause clipping");
+  if (p->in_gain / (1 - p->decay) > 1 / p->out_gain)
+    lsx_warn("warning: gain-out might cause clipping");
 
-        if ( phaser->delay < 0.0 )
-        {
-            lsx_fail("phaser: delay must be positive!");
-            return (SOX_EOF);
-        }
-        if ( phaser->delay > 5.0 )
-        {
-            lsx_fail("phaser: delay must be less than 5.0 msec!");
-            return (SOX_EOF);
-        }
-        if ( phaser->speed < 0.1 )
-        {
-            lsx_fail("phaser: speed must be more than 0.1 Hz!");
-            return (SOX_EOF);
-        }
-        if ( phaser->speed > 2.0 )
-        {
-            lsx_fail("phaser: speed must be less than 2.0 Hz!");
-            return (SOX_EOF);
-        }
-        if ( phaser->decay < 0.0 )
-        {
-            lsx_fail("phaser: decay must be positive!" );
-            return (SOX_EOF);
-        }
-        if ( phaser->decay >= 1.0 )
-        {
-            lsx_fail("phaser: decay must be less that 1.0!" );
-            return (SOX_EOF);
-        }
-        /* Be nice and check the hint with warning, if... */
-        if ( phaser->in_gain > ( 1.0 - phaser->decay * phaser->decay ) )
-                lsx_warn("phaser: warning >>> gain-in can cause saturation or clipping of output <<<");
-        if ( phaser->in_gain / ( 1.0 - phaser->decay ) > 1.0 / phaser->out_gain )
-                lsx_warn("phaser: warning >>> gain-out can cause saturation or clipping of output <<<");
-
-        phaser->length = effp->in_signal.rate / phaser->speed;
-        phaser->phaserbuf = lsx_malloc(sizeof (double) * phaser->maxsamples);
-        for ( i = 0; i < phaser->maxsamples; i++ )
-                phaser->phaserbuf[i] = 0.0;
-        phaser->lookup_tab = lsx_malloc(sizeof (int) * phaser->length);
-
-        if (phaser->modulation == MOD_SINE)
-          lsx_generate_wave_table(SOX_WAVE_SINE, SOX_INT, phaser->lookup_tab, (size_t)
-              phaser->length, 0., (double)(phaser->maxsamples - 1), 0.);
-        else
-          lsx_generate_wave_table(SOX_WAVE_TRIANGLE, SOX_INT, phaser->lookup_tab, (size_t)
-              phaser->length, 0., (double)(2 * (phaser->maxsamples - 1)), 3 * M_PI_2);
-        phaser->counter = 0;
-        phaser->phase = 0;
-        phaser->fade_out = phaser->maxsamples;
-        return (SOX_SUCCESS);
+  return argc? lsx_usage(effp) : SOX_SUCCESS;
 }
 
-/*
- * Processed signed long samples from ibuf to obuf.
- * Return number of samples processed.
- */
-static int sox_phaser_flow(sox_effect_t * effp, const sox_sample_t *ibuf, sox_sample_t *obuf,
-                   size_t *isamp, size_t *osamp)
+static int start(sox_effect_t * effp)
 {
-        priv_t * phaser = (priv_t *) effp->priv;
-        double d_in, d_out;
-        sox_sample_t out;
-        size_t len = min(*isamp, *osamp);
-        *isamp = *osamp = len;
+  priv_t * p = (priv_t *) effp->priv;
 
-        while (len--) {
-                /* Store delays as 24-bit signed longs */
-                d_in = (double) *ibuf++ / 256;
-                /* Compute output first */
-                d_in = d_in * phaser->in_gain;
-                d_in += phaser->phaserbuf[(phaser->maxsamples +
-        phaser->counter - phaser->lookup_tab[phaser->phase]) %
-        phaser->maxsamples] * phaser->decay * -1.0;
-                /* Adjust the output volume and size to 24 bit */
-                d_out = d_in * phaser->out_gain;
-                out = SOX_24BIT_CLIP_COUNT((sox_sample_t) d_out, effp->clips);
-                *obuf++ = out * 256;
-                /* Mix decay of delay and input */
-                phaser->phaserbuf[phaser->counter] = d_in;
-                phaser->counter =
-                        ( phaser->counter + 1 ) % phaser->maxsamples;
-                phaser->phase  = ( phaser->phase + 1 ) % phaser->length;
-        }
-        /* processed all samples */
-        return (SOX_SUCCESS);
+  p->delay_buf_len = p->delay_ms * .001 * effp->in_signal.rate + .5;
+  p->delay_buf = lsx_calloc(p->delay_buf_len, sizeof(*p->delay_buf));
+
+  p->mod_buf_len = effp->in_signal.rate / p->mod_speed + .5;
+  p->mod_buf = lsx_malloc(p->mod_buf_len * sizeof(*p->mod_buf));
+  lsx_generate_wave_table(p->mod_type, SOX_INT, p->mod_buf, p->mod_buf_len,
+      1., (double)p->delay_buf_len, M_PI_2);
+
+  p->delay_pos = p->mod_pos = 0;
+  return SOX_SUCCESS;
 }
 
-/*
- * Drain out reverb lines.
- */
-static int sox_phaser_drain(sox_effect_t * effp, sox_sample_t *obuf, size_t *osamp)
+static int flow(sox_effect_t * effp, const sox_sample_t *ibuf,
+    sox_sample_t *obuf, size_t *isamp, size_t *osamp)
 {
-        priv_t * phaser = (priv_t *) effp->priv;
-        size_t done;
+  priv_t * p = (priv_t *) effp->priv;
+  size_t len = *isamp = *osamp = min(*isamp, *osamp);
 
-        double d_in, d_out;
-        sox_sample_t out;
+  while (len--) {
+    double d = *ibuf++ * p->in_gain + p->delay_buf[
+      (p->delay_pos + p->mod_buf[p->mod_pos]) % p->delay_buf_len] * p->decay;
+    p->mod_pos = (p->mod_pos + 1) % p->mod_buf_len;
+    
+    p->delay_pos = (p->delay_pos + 1) % p->delay_buf_len;
+    p->delay_buf[p->delay_pos] = d;
 
-        done = 0;
-        while ( ( done < *osamp ) && ( done < phaser->fade_out ) ) {
-                d_in = 0;
-                d_out = 0;
-                /* Compute output first */
-                d_in += phaser->phaserbuf[(phaser->maxsamples +
-        phaser->counter - phaser->lookup_tab[phaser->phase]) %
-        phaser->maxsamples] * phaser->decay * -1.0;
-                /* Adjust the output volume and size to 24 bit */
-                d_out = d_in * phaser->out_gain;
-                out = SOX_24BIT_CLIP_COUNT((sox_sample_t) d_out, effp->clips);
-                *obuf++ = out * 256;
-                /* Mix decay of delay and input */
-                phaser->phaserbuf[phaser->counter] = d_in;
-                phaser->counter =
-                        ( phaser->counter + 1 ) % phaser->maxsamples;
-                phaser->phase  = ( phaser->phase + 1 ) % phaser->length;
-                done++;
-                phaser->fade_out--;
-        }
-        /* samples played, it remains */
-        *osamp = done;
-        if (phaser->fade_out == 0)
-            return SOX_EOF;
-        else
-            return SOX_SUCCESS;
+    *obuf++ = SOX_ROUND_CLIP_COUNT(d * p->out_gain, effp->clips);
+  }
+  return SOX_SUCCESS;
 }
 
-/*
- * Clean up phaser effect.
- */
-static int sox_phaser_stop(sox_effect_t * effp)
+static int stop(sox_effect_t * effp)
 {
-        priv_t * phaser = (priv_t *) effp->priv;
+  priv_t * p = (priv_t *) effp->priv;
 
-        free(phaser->phaserbuf);
-        free(phaser->lookup_tab);
-        return (SOX_SUCCESS);
+  free(p->delay_buf);
+  free(p->mod_buf);
+  return SOX_SUCCESS;
 }
 
-static sox_effect_handler_t sox_phaser_effect = {
-  "phaser",
-  "gain-in gain-out delay decay speed [ -s | -t ]",
-  SOX_EFF_LENGTH,
-  sox_phaser_getopts,
-  sox_phaser_start,
-  sox_phaser_flow,
-  sox_phaser_drain,
-  sox_phaser_stop,
-  NULL, sizeof(priv_t)
-};
-
-const sox_effect_handler_t *sox_phaser_effect_fn(void)
+sox_effect_handler_t const * sox_phaser_effect_fn(void)
 {
-    return &sox_phaser_effect;
+  static sox_effect_handler_t handler = {
+    "phaser", "gain-in gain-out delay decay speed [ -s | -t ]",
+    SOX_EFF_LENGTH, getopts, start, flow, NULL, stop, NULL, sizeof(priv_t)
+  };
+  return &handler;
 }