<?xml version="1.0"?>
<!DOCTYPE ladspa SYSTEM "ladspa-swh.dtd">
<?xml-stylesheet href="ladspa.css" type="text/css"?>

<ladspa>
  <global>
    <meta name="maker" value="Steve Harris &lt;steve@plugin.org.uk&gt;"/>
    <meta name="copyright" value="GPL"/>
    <meta name="properties" value="HARD_RT_CAPABLE"/>
    <code><![CDATA[
      #include <ladspa-util.h>

      #define HARMONICS 11
      #define STAGES 2

      static float cd_lut[STAGES][HARMONICS];

      /* Calculate Chebychev coefficents from partial magnitudes, adapted from
       * example in Num. Rec. */
      void chebpc(float c[], float d[])
      {
          int k, j;
          float sv, dd[HARMONICS];
      
          for (j = 0; j < HARMONICS; j++) {
              d[j] = dd[j] = 0.0;
          }
      
          d[0] = c[HARMONICS - 1];
      
          for (j = HARMONICS - 2; j >= 1; j--) {
              for (k = HARMONICS - j; k >= 1; k--) {
                  sv = d[k];
                  d[k] = 2.0 * d[k - 1] - dd[k];
                  dd[k] = sv;
              }
              sv = d[0];
              d[0] = -dd[0] + c[j];
              dd[0] = sv;
          }
      
          for (j = HARMONICS - 1; j >= 1; j--) {
              d[j] = d[j - 1] - dd[j];
          }
          d[0] = -dd[0] + 0.5 * c[0];
      }

    ]]></code>
  </global>

  <plugin label="chebstortion" id="1430" class="DistortionPlugin">
    <name>Chebyshev distortion</name>
    <p>This is an interesting distortion effect that is seeded from incoming
signal envelope. As the level of the signal rises more and more harmonics will
for added to the output signal.</p>
    <p>The distortion control sets the sensitivity of the input.</p>
    <p>The effect eveolved from some experiments between Tim Goetze and myself,
apptempting to emulate valve based guitar amp distortion. This was one of the
failures, but it still makes an interesting noise.</p>

    <callback event="instantiate"><![CDATA[
      unsigned int i;

      cd_lut[0][0] = 0.0f;
      cd_lut[0][1] = 1.0f;
      for (i=2; i<HARMONICS; i++) {
        cd_lut[0][i] = 0.0f;
      }
      cd_lut[1][0] = 0.0f;
      cd_lut[1][1] = 1.0f;
      for (i=2; i<HARMONICS; i++) {
        cd_lut[1][i] = 1.0f/(float)i;
      }

      itm1 = 0.0f;
      otm1 = 0.0f;
      env = 0.0f;
      count = 0;
    ]]></callback>

    <callback event="activate"><![CDATA[
      itm1 = 0.0f;
      otm1 = 0.0f;
      env = 0.0f;
      count = 0;
    ]]></callback>

    <callback event="run"><![CDATA[
      unsigned long pos, i;
      float p[HARMONICS], interp[HARMONICS];

      for (pos = 0; pos < sample_count; pos++) {
        const float x = input[pos];
        const float a = fabs(input[pos]);
	float y;

	if (a > env) {
		env = env * 0.9f + a * 0.1f;
	} else {
		env = env * 0.97f + a * 0.03f;
	}

	if (count-- == 0) {
	  for (i=0; i<HARMONICS; i++) {
	    interp[i] = cd_lut[0][i] * (1.0f - env * dist) +
                        cd_lut[1][i] * env * dist;
	  }
	  chebpc(interp, p);

	  count = 4;
	}

        // Evaluate the polynomial using Horner's Rule
	y = p[0] + (p[1] + (p[2] + (p[3] + (p[4] + (p[5] + (p[6] + (p[7] +
            (p[8] + (p[9] + p[10] * x) * x) * x) * x) * x) * x) * x) * x) *
            x) * x;

	// DC offset remove (odd harmonics cause DC offset)
        otm1 = 0.999f * otm1 + y - itm1;
        itm1 = y;

        buffer_write(output[pos], otm1);
      }

      plugin_data->itm1 = itm1;
      plugin_data->otm1 = otm1;
      plugin_data->env = env;
      plugin_data->count = count;
    ]]></callback>

    <port label="dist" dir="input" type="control" hint="default_0">
      <name>Distortion</name>
      <range min="0" max="3"/>
    </port>

    <port label="input" dir="input" type="audio">
      <name>Input</name>
      <range min="-1" max="+1"/>
    </port>

    <port label="output" dir="output" type="audio">
      <name>Output</name>
      <range min="-1" max="+1"/>
    </port>

    <instance-data label="itm1" type="float" />
    <instance-data label="otm1" type="float" />
    <instance-data label="env" type="float" />
    <instance-data label="count" type="unsigned int" />
  </plugin>
</ladspa>
