1<?xml version="1.0"?>
2<!DOCTYPE ladspa SYSTEM "ladspa-swh.dtd">
3<?xml-stylesheet href="ladspa.css" type="text/css"?>
4
5<ladspa>
6  <global>
7    <meta name="maker" value="Steve Harris &lt;steve@plugin.org.uk&gt;"/>
8    <meta name="copyright" value="GPL"/>
9    <meta name="properties" value="HARD_RT_CAPABLE"/>
10    <code><![CDATA[
11      #define HARMONICS 11
12
13      /* Calculate Chebychev coefficents from partial magnitudes, adapted from
14       * example in Num. Rec. */
15      void chebpc(float c[], float d[])
16      {
17          int k, j;
18          float sv, dd[HARMONICS];
19
20          for (j = 0; j < HARMONICS; j++) {
21              d[j] = dd[j] = 0.0;
22          }
23
24          d[0] = c[HARMONICS - 1];
25
26          for (j = HARMONICS - 2; j >= 1; j--) {
27              for (k = HARMONICS - j; k >= 1; k--) {
28                  sv = d[k];
29                  d[k] = 2.0 * d[k - 1] - dd[k];
30                  dd[k] = sv;
31              }
32              sv = d[0];
33              d[0] = -dd[0] + c[j];
34              dd[0] = sv;
35          }
36
37          for (j = HARMONICS - 1; j >= 1; j--) {
38              d[j] = d[j - 1] - dd[j];
39          }
40          d[0] = -dd[0] + 0.5 * c[0];
41      }
42    ]]></code>
43  </global>
44
45  <plugin label="harmonicGen" id="1220" class="GeneratorPlugin">
46    <name>Harmonic generator</name>
47    <p>\subsubsection{What does it do?}</p>
48    <p>Allows you to add harmonics and remove the fundamental from any audio signal.</p>
49    <p>\subsubsection{Known bugs}</p>
50    <p>There is no bandwith limiting filter on the output, so it is easy to create excessively high frequency harmonics that could cause aliasing problems. In practice this doesn't seem to be a serious problem however.</p>
51    <p>\subsubsection{Examples}</p>
52    <p>There are many interesting effects you can achieve with sinewaves, one example is producing bandlimited squarewaves from sinewaves. To do this set the parameters to 1, 0, -0.3333, 0, 0.2, 0, -0.14285, 0, 0.11111.</p>
53    <p>To get a triangle like signal use 1, 0, -0.3333, 0, -0.2, 0, -0.14285, 0, -0.11111.</p>
54
55    <callback event="activate"><![CDATA[
56      itm1 = 0.0f;
57      otm1 = 0.0f;
58    ]]></callback>
59
60    <callback event="run"><![CDATA[
61      unsigned long pos, i;
62      float mag_fix;
63      float mag[HARMONICS] = {0.0f, mag_1, mag_2, mag_3, mag_4, mag_5, mag_6,
64                              mag_7, mag_8, mag_9, mag_10};
65      float p[HARMONICS];
66
67      // Normalise magnitudes
68      mag_fix = (fabs(mag_1) + fabs(mag_2) + fabs(mag_3) + fabs(mag_4) +
69                 fabs(mag_5) + fabs(mag_6) + fabs(mag_7) + fabs(mag_8) +
70                 fabs(mag_9) + fabs(mag_10));
71      if (mag_fix < 1.0f) {
72        mag_fix = 1.0f;
73      } else {
74        mag_fix = 1.0f / mag_fix;
75      }
76      for (i=0; i<HARMONICS; i++) {
77        mag[i] *= mag_fix;
78      }
79
80      // Calculate polynomial coefficients, using Chebychev aproximation
81      chebpc(mag, p);
82
83      for (pos = 0; pos < sample_count; pos++) {
84        float x = input[pos], y;
85
86        // Calculate the polynomial using Horner's Rule
87	y = p[0] + (p[1] + (p[2] + (p[3] + (p[4] + (p[5] + (p[6] + (p[7] +
88            (p[8] + (p[9] + p[10] * x) * x) * x) * x) * x) * x) * x) * x) *
89            x) * x;
90
91	// DC offset remove (odd harmonics cause DC offset)
92        otm1 = 0.999f * otm1 + y - itm1;
93        itm1 = y;
94
95        buffer_write(output[pos], otm1);
96      }
97
98      plugin_data->itm1 = itm1;
99      plugin_data->otm1 = otm1;
100    ]]></callback>
101
102    <port label="mag_1" dir="input" type="control" hint="default_1">
103      <name>Fundamental magnitude</name>
104      <p>The amplitude of the fundamental of the signal, reduce it to 0 to remove the base signal altogether, or -1 to phase invert it.</p>
105      <range min="-1" max="+1"/>
106    </port>
107
108    <port label="mag_2" dir="input" type="control" hint="default_0">
109      <name>2nd harmonic magnitude</name>
110      <p>The 2nd harmonic, its frequency is twice the frequency of the fundamental.</p>
111      <p>Even harmonics add a distorted feel to the sound, valve (tube) amplifiers introduce distortions at all the harmonics.</p>
112      <range min="-1" max="+1"/>
113    </port>
114
115    <port label="mag_3" dir="input" type="control" hint="default_0">
116      <name>3rd harmonic magnitude</name>
117      <p>The 3rd harmonic, its frequency is three times the frequency of the fundamental.</p>
118      <p>Transistor amplifiers only introduce distortion into the odd harmonics.</p>
119      <range min="-1" max="+1"/>
120    </port>
121
122    <port label="mag_4" dir="input" type="control" hint="default_0">
123      <name>4th harmonic magnitude</name>
124      <range min="-1" max="+1"/>
125    </port>
126
127    <port label="mag_5" dir="input" type="control" hint="default_0">
128      <name>5th harmonic magnitude</name>
129      <range min="-1" max="+1"/>
130    </port>
131
132    <port label="mag_6" dir="input" type="control" hint="default_0">
133      <name>6th harmonic magnitude</name>
134      <range min="-1" max="+1"/>
135    </port>
136
137    <port label="mag_7" dir="input" type="control" hint="default_0">
138      <name>7th harmonic magnitude</name>
139      <range min="-1" max="+1"/>
140    </port>
141
142    <port label="mag_8" dir="input" type="control" hint="default_0">
143      <name>8th harmonic magnitude</name>
144      <range min="-1" max="+1"/>
145    </port>
146
147    <port label="mag_9" dir="input" type="control" hint="default_0">
148      <name>9th harmonic magnitude</name>
149      <range min="-1" max="+1"/>
150    </port>
151
152    <port label="mag_10" dir="input" type="control" hint="default_0">
153      <name>10th harmonic magnitude</name>
154      <range min="-1" max="+1"/>
155    </port>
156
157    <port label="input" dir="input" type="audio">
158      <name>Input</name>
159      <range min="-1" max="+1"/>
160    </port>
161
162    <port label="output" dir="output" type="audio">
163      <name>Output</name>
164      <range min="-1" max="+1"/>
165    </port>
166
167    <instance-data label="itm1" type="float" />
168    <instance-data label="otm1" type="float" />
169  </plugin>
170</ladspa>
171