1 #include <stdlib.h>
2 #include <string.h>
3 #ifndef WIN32
4 #include "config.h"
5 #endif
6 
7 #ifdef ENABLE_NLS
8 #include <libintl.h>
9 #endif
10 
11 #define         _ISOC9X_SOURCE  1
12 #define         _ISOC99_SOURCE  1
13 #define         __USE_ISOC99    1
14 #define         __USE_ISOC9X    1
15 
16 #include <math.h>
17 
18 #include "ladspa.h"
19 
20 #ifdef WIN32
21 #define _WINDOWS_DLL_EXPORT_ __declspec(dllexport)
22 int bIsFirstTime = 1;
23 static void __attribute__((constructor)) swh_init(); // forward declaration
24 #else
25 #define _WINDOWS_DLL_EXPORT_
26 #endif
27 
28 #line 10 "vocoder_1337.xml"
29 
30 #include "util/iir.h"
31 
32 #define MAX_BANDS                16
33 #define AMPLIFIER                16.0
34 
35 // 0 = formant, 1 = carrier, 2 = output, 3 = output2, 4 = bandcount, 5 = pan
36 #define START_BANDS        6                                /* start of bands level */
37 #define PORT_COUNT        START_BANDS + MAX_BANDS                /* bands level */
38 
39 struct bandpasses
40 {
41   LADSPA_Data c[MAX_BANDS], f[MAX_BANDS], att[MAX_BANDS];
42 
43   LADSPA_Data freq[MAX_BANDS];
44   LADSPA_Data low1[MAX_BANDS], low2[MAX_BANDS];
45   LADSPA_Data mid1[MAX_BANDS], mid2[MAX_BANDS];
46   LADSPA_Data high1[MAX_BANDS], high2[MAX_BANDS];
47   LADSPA_Data y[MAX_BANDS];
48 };
49 
50 void doBandpasses(struct bandpasses *bands, LADSPA_Data sample, int num_bands);
51 
52 struct bands_out{
53   LADSPA_Data decay[MAX_BANDS];
54   LADSPA_Data oldval[MAX_BANDS];
55   LADSPA_Data level[MAX_BANDS];     /* 0.0 - 1.0 level of this output band */
56 };
57 
58 const LADSPA_Data decay_table[] =
59 {
60   1/100.0,
61   1/100.0, 1/100.0, 1/100.0,
62   1/125.0, 1/125.0, 1/125.0,
63   1/166.0, 1/166.0, 1/166.0,
64   1/200.0, 1/200.0, 1/200.0,
65   1/250.0, 1/250.0, 1/250.0
66 };
67 
doBandpasses(struct bandpasses * bands,LADSPA_Data sample,int num_bands)68 void doBandpasses(struct bandpasses *bands, LADSPA_Data sample, int num_bands)
69 {
70   int i;
71   for (i=0; i < num_bands; i++)
72   {
73     bands->high1[i] = sample - bands->f[i] * bands->mid1[i] - bands->low1[i];
74     bands->mid1[i] += bands->high1[i] * bands->c[i];
75     bands->low1[i] += bands->mid1[i];
76 
77     bands->high2[i] = bands->low1[i] - bands->f[i] * bands->mid2[i] - bands->low2[i];
78     bands->mid2[i] += bands->high2[i] * bands->c[i];
79     bands->low2[i] += bands->mid2[i];
80     bands->y[i]     = bands->high2[i] * bands->att[i];
81   }
82 }
83 
84 #define VOCODER_PORT_FORMANT           0
85 #define VOCODER_PORT_CARRIER           1
86 #define VOCODER_PORT_OUTPUT            2
87 #define VOCODER_PORT_OUTPUT2           3
88 #define VOCODER_CTRL_BAND_COUNT        4
89 #define VOCODER_CTRL_PAN               5
90 #define VOCODER_BAND1                  6
91 #define VOCODER_BAND2                  7
92 #define VOCODER_BAND3                  8
93 #define VOCODER_BAND4                  9
94 #define VOCODER_BAND5                  10
95 #define VOCODER_BAND6                  11
96 #define VOCODER_BAND7                  12
97 #define VOCODER_BAND8                  13
98 #define VOCODER_BAND9                  14
99 #define VOCODER_BAND10                 15
100 #define VOCODER_BAND11                 16
101 #define VOCODER_BAND12                 17
102 #define VOCODER_BAND13                 18
103 #define VOCODER_BAND14                 19
104 #define VOCODER_BAND15                 20
105 #define VOCODER_BAND16                 21
106 
107 static LADSPA_Descriptor *vocoderDescriptor = NULL;
108 
109 typedef struct {
110 	LADSPA_Data *port_formant;
111 	LADSPA_Data *port_carrier;
112 	LADSPA_Data *port_output;
113 	LADSPA_Data *port_output2;
114 	LADSPA_Data *ctrl_band_count;
115 	LADSPA_Data *ctrl_pan;
116 	LADSPA_Data *band1;
117 	LADSPA_Data *band2;
118 	LADSPA_Data *band3;
119 	LADSPA_Data *band4;
120 	LADSPA_Data *band5;
121 	LADSPA_Data *band6;
122 	LADSPA_Data *band7;
123 	LADSPA_Data *band8;
124 	LADSPA_Data *band9;
125 	LADSPA_Data *band10;
126 	LADSPA_Data *band11;
127 	LADSPA_Data *band12;
128 	LADSPA_Data *band13;
129 	LADSPA_Data *band14;
130 	LADSPA_Data *band15;
131 	LADSPA_Data *band16;
132 	struct bandpasses bands_carrier;
133 	struct bandpasses bands_formant;
134 	struct bands_out bands_out;
135 	LADSPA_Data *ctrl_band_levels;
136 	float        main_vol;
137 	int          num_bands;
138 	LADSPA_Data  sample_rate;
139 	LADSPA_Data run_adding_gain;
140 } Vocoder;
141 
142 _WINDOWS_DLL_EXPORT_
ladspa_descriptor(unsigned long index)143 const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {
144 
145 #ifdef WIN32
146 	if (bIsFirstTime) {
147 		swh_init();
148 		bIsFirstTime = 0;
149 	}
150 #endif
151 	switch (index) {
152 	case 0:
153 		return vocoderDescriptor;
154 	default:
155 		return NULL;
156 	}
157 }
158 
activateVocoder(LADSPA_Handle instance)159 static void activateVocoder(LADSPA_Handle instance) {
160 	Vocoder *plugin_data = (Vocoder *)instance;
161 	struct bandpasses bands_carrier = plugin_data->bands_carrier;
162 	struct bandpasses bands_formant = plugin_data->bands_formant;
163 	struct bands_out bands_out = plugin_data->bands_out;
164 	LADSPA_Data *ctrl_band_levels = plugin_data->ctrl_band_levels;
165 	float main_vol = plugin_data->main_vol;
166 	int num_bands = plugin_data->num_bands;
167 	LADSPA_Data sample_rate = plugin_data->sample_rate;
168 #line 83 "vocoder_1337.xml"
169 	int i;
170 	for (i = 0; i < MAX_BANDS; i++)
171 	{
172 	  bands_out.oldval[i] = 0.0f;
173 	}
174 	plugin_data->bands_carrier = bands_carrier;
175 	plugin_data->bands_formant = bands_formant;
176 	plugin_data->bands_out = bands_out;
177 	plugin_data->ctrl_band_levels = ctrl_band_levels;
178 	plugin_data->main_vol = main_vol;
179 	plugin_data->num_bands = num_bands;
180 	plugin_data->sample_rate = sample_rate;
181 
182 }
183 
cleanupVocoder(LADSPA_Handle instance)184 static void cleanupVocoder(LADSPA_Handle instance) {
185 #line 92 "vocoder_1337.xml"
186 	Vocoder *plugin_data = (Vocoder *)instance;
187 	free(plugin_data->ctrl_band_levels);
188 	free(instance);
189 }
190 
connectPortVocoder(LADSPA_Handle instance,unsigned long port,LADSPA_Data * data)191 static void connectPortVocoder(
192  LADSPA_Handle instance,
193  unsigned long port,
194  LADSPA_Data *data) {
195 	Vocoder *plugin;
196 
197 	plugin = (Vocoder *)instance;
198 	switch (port) {
199 	case VOCODER_PORT_FORMANT:
200 		plugin->port_formant = data;
201 		break;
202 	case VOCODER_PORT_CARRIER:
203 		plugin->port_carrier = data;
204 		break;
205 	case VOCODER_PORT_OUTPUT:
206 		plugin->port_output = data;
207 		break;
208 	case VOCODER_PORT_OUTPUT2:
209 		plugin->port_output2 = data;
210 		break;
211 	case VOCODER_CTRL_BAND_COUNT:
212 		plugin->ctrl_band_count = data;
213 		break;
214 	case VOCODER_CTRL_PAN:
215 		plugin->ctrl_pan = data;
216 		break;
217 	case VOCODER_BAND1:
218 		plugin->band1 = data;
219 		break;
220 	case VOCODER_BAND2:
221 		plugin->band2 = data;
222 		break;
223 	case VOCODER_BAND3:
224 		plugin->band3 = data;
225 		break;
226 	case VOCODER_BAND4:
227 		plugin->band4 = data;
228 		break;
229 	case VOCODER_BAND5:
230 		plugin->band5 = data;
231 		break;
232 	case VOCODER_BAND6:
233 		plugin->band6 = data;
234 		break;
235 	case VOCODER_BAND7:
236 		plugin->band7 = data;
237 		break;
238 	case VOCODER_BAND8:
239 		plugin->band8 = data;
240 		break;
241 	case VOCODER_BAND9:
242 		plugin->band9 = data;
243 		break;
244 	case VOCODER_BAND10:
245 		plugin->band10 = data;
246 		break;
247 	case VOCODER_BAND11:
248 		plugin->band11 = data;
249 		break;
250 	case VOCODER_BAND12:
251 		plugin->band12 = data;
252 		break;
253 	case VOCODER_BAND13:
254 		plugin->band13 = data;
255 		break;
256 	case VOCODER_BAND14:
257 		plugin->band14 = data;
258 		break;
259 	case VOCODER_BAND15:
260 		plugin->band15 = data;
261 		break;
262 	case VOCODER_BAND16:
263 		plugin->band16 = data;
264 		break;
265 	}
266 }
267 
instantiateVocoder(const LADSPA_Descriptor * descriptor,unsigned long s_rate)268 static LADSPA_Handle instantiateVocoder(
269  const LADSPA_Descriptor *descriptor,
270  unsigned long s_rate) {
271 	Vocoder *plugin_data = (Vocoder *)calloc(1, sizeof(Vocoder));
272 	struct bandpasses bands_carrier;
273 	struct bandpasses bands_formant;
274 	struct bands_out bands_out;
275 	LADSPA_Data *ctrl_band_levels = NULL;
276 	float main_vol;
277 	int num_bands;
278 	LADSPA_Data sample_rate;
279 
280 #line 75 "vocoder_1337.xml"
281 	sample_rate = s_rate;
282 	main_vol = 1.0 * AMPLIFIER;
283 
284 	ctrl_band_levels = malloc(MAX_BANDS * sizeof(LADSPA_Data));
285 	num_bands = -1;
286 
287 	plugin_data->bands_carrier = bands_carrier;
288 	plugin_data->bands_formant = bands_formant;
289 	plugin_data->bands_out = bands_out;
290 	plugin_data->ctrl_band_levels = ctrl_band_levels;
291 	plugin_data->main_vol = main_vol;
292 	plugin_data->num_bands = num_bands;
293 	plugin_data->sample_rate = sample_rate;
294 
295 	return (LADSPA_Handle)plugin_data;
296 }
297 
298 #undef buffer_write
299 #undef RUN_ADDING
300 #undef RUN_REPLACING
301 
302 #define buffer_write(b, v) (b = v)
303 #define RUN_ADDING    0
304 #define RUN_REPLACING 1
305 
runVocoder(LADSPA_Handle instance,unsigned long sample_count)306 static void runVocoder(LADSPA_Handle instance, unsigned long sample_count) {
307 	Vocoder *plugin_data = (Vocoder *)instance;
308 	LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
309 
310 	/* Formant-in (array of floats of length sample_count) */
311 	const LADSPA_Data * const port_formant = plugin_data->port_formant;
312 
313 	/* Carrier-in (array of floats of length sample_count) */
314 	const LADSPA_Data * const port_carrier = plugin_data->port_carrier;
315 
316 	/* Output-out (array of floats of length sample_count) */
317 	LADSPA_Data * const port_output = plugin_data->port_output;
318 
319 	/* Output2-out (array of floats of length sample_count) */
320 	LADSPA_Data * const port_output2 = plugin_data->port_output2;
321 
322 	/* Number of bands (float value) */
323 	const LADSPA_Data ctrl_band_count = *(plugin_data->ctrl_band_count);
324 
325 	/* Left/Right (float value) */
326 	const LADSPA_Data ctrl_pan = *(plugin_data->ctrl_pan);
327 
328 	/* Band 1 Level (float value) */
329 	const LADSPA_Data band1 = *(plugin_data->band1);
330 
331 	/* Band 2 Level (float value) */
332 	const LADSPA_Data band2 = *(plugin_data->band2);
333 
334 	/* Band 3 Level (float value) */
335 	const LADSPA_Data band3 = *(plugin_data->band3);
336 
337 	/* Band 4 Level (float value) */
338 	const LADSPA_Data band4 = *(plugin_data->band4);
339 
340 	/* Band 5 Level (float value) */
341 	const LADSPA_Data band5 = *(plugin_data->band5);
342 
343 	/* Band 6 Level (float value) */
344 	const LADSPA_Data band6 = *(plugin_data->band6);
345 
346 	/* Band 7 Level (float value) */
347 	const LADSPA_Data band7 = *(plugin_data->band7);
348 
349 	/* Band 8 Level (float value) */
350 	const LADSPA_Data band8 = *(plugin_data->band8);
351 
352 	/* Band 9 Level (float value) */
353 	const LADSPA_Data band9 = *(plugin_data->band9);
354 
355 	/* Band 10 Level (float value) */
356 	const LADSPA_Data band10 = *(plugin_data->band10);
357 
358 	/* Band 11 Level (float value) */
359 	const LADSPA_Data band11 = *(plugin_data->band11);
360 
361 	/* Band 12 Level (float value) */
362 	const LADSPA_Data band12 = *(plugin_data->band12);
363 
364 	/* Band 13 Level (float value) */
365 	const LADSPA_Data band13 = *(plugin_data->band13);
366 
367 	/* Band 14 Level (float value) */
368 	const LADSPA_Data band14 = *(plugin_data->band14);
369 
370 	/* Band 15 Level (float value) */
371 	const LADSPA_Data band15 = *(plugin_data->band15);
372 
373 	/* Band 16 Level (float value) */
374 	const LADSPA_Data band16 = *(plugin_data->band16);
375 	struct bandpasses bands_carrier = plugin_data->bands_carrier;
376 	struct bandpasses bands_formant = plugin_data->bands_formant;
377 	struct bands_out bands_out = plugin_data->bands_out;
378 	LADSPA_Data * ctrl_band_levels = plugin_data->ctrl_band_levels;
379 	float main_vol = plugin_data->main_vol;
380 	int num_bands = plugin_data->num_bands;
381 	LADSPA_Data sample_rate = plugin_data->sample_rate;
382 
383 #line 96 "vocoder_1337.xml"
384 	int i, j, numbands, pan;
385 	float a;
386 	LADSPA_Data x, c;
387 	float fl, fr;
388 
389 	// Bind band level controls
390 	plugin_data->ctrl_band_levels[0] = band1;
391 	plugin_data->ctrl_band_levels[1] = band2;
392 	plugin_data->ctrl_band_levels[2] = band3;
393 	plugin_data->ctrl_band_levels[3] = band4;
394 	plugin_data->ctrl_band_levels[4] = band5;
395 	plugin_data->ctrl_band_levels[5] = band6;
396 	plugin_data->ctrl_band_levels[6] = band7;
397 	plugin_data->ctrl_band_levels[7] = band8;
398 	plugin_data->ctrl_band_levels[8] = band9;
399 	plugin_data->ctrl_band_levels[9] = band10;
400 	plugin_data->ctrl_band_levels[10] = band11;
401 	plugin_data->ctrl_band_levels[11] = band12;
402 	plugin_data->ctrl_band_levels[12] = band13;
403 	plugin_data->ctrl_band_levels[13] = band14;
404 	plugin_data->ctrl_band_levels[14] = band15;
405 	plugin_data->ctrl_band_levels[15] = band16;
406 
407 	numbands = (int)(*plugin_data->ctrl_band_count);
408 	if (numbands < 1 || numbands > MAX_BANDS) numbands = MAX_BANDS;
409 
410 	/* initialize bandpass information if num_bands control has changed,
411 	   or on first run */
412 	if (plugin_data->num_bands != numbands)
413 	{
414 	  plugin_data->num_bands = numbands;
415 
416 	  memset(&plugin_data->bands_formant, 0, sizeof(struct bandpasses));
417 	  for(i=0; i < numbands; i++)
418 	  {
419 	    a = 16.0 * i/(double)numbands;  // stretch existing bands
420 
421 	    if (a < 4.0)
422 	      plugin_data->bands_formant.freq[i] = 150 + 420 * a / 4.0;
423 	    else
424 	      plugin_data->bands_formant.freq[i] = 600 * pow (1.23, a - 4.0);
425 
426 	      c = plugin_data->bands_formant.freq[i] * 2 * M_PI / plugin_data->sample_rate;
427 	      plugin_data->bands_formant.c[i] = c * c;
428 
429 	      plugin_data->bands_formant.f[i] = 0.4/c;
430 	      plugin_data->bands_formant.att[i] =
431 	        1/(6.0 + ((exp (plugin_data->bands_formant.freq[i]
432 	                      / plugin_data->sample_rate) - 1) * 10));
433 
434 	      plugin_data->bands_out.decay[i] = decay_table[(int)a];
435 	      plugin_data->bands_out.level[i] =
436 	        CLAMP (plugin_data->ctrl_band_levels[i], 0.0, 1.0);
437 	  }
438 	  memcpy(&plugin_data->bands_carrier,
439 	    &plugin_data->bands_formant, sizeof(struct bandpasses));
440 
441 	}
442 	else                       /* get current values of band level controls */
443 	{
444 	  for (i = 0; i < numbands; i++)
445 	    plugin_data->bands_out.level[i] = CLAMP (plugin_data->ctrl_band_levels[i],
446 	      0.0, 1.0);
447 	}
448 
449 	for (i=0; i < sample_count; i++)
450 	{
451 	  doBandpasses (&(plugin_data->bands_carrier),
452 	    plugin_data->port_carrier[i],
453 	    plugin_data->num_bands);
454 	  doBandpasses (&(plugin_data->bands_formant),
455 	    plugin_data->port_formant[i],
456 	    plugin_data->num_bands);
457 
458 	  LADSPA_Data sample = 0.0;
459 	  for (j=0; j < numbands; j++)
460 	  {
461 	    plugin_data->bands_out.oldval[j] = plugin_data->bands_out.oldval[j]
462 	      + (fabs (plugin_data->bands_formant.y[j])
463 	         - plugin_data->bands_out.oldval[j])
464 	      * plugin_data->bands_out.decay[j];
465 	    x = plugin_data->bands_carrier.y[j] * plugin_data->bands_out.oldval[j];
466 
467 	    sample += x * plugin_data->bands_out.level[j];
468 	  }
469 	  /* treat paning + main volume */
470 	  pan = (int)(*plugin_data->ctrl_pan);
471 	  fl = fr = 1.0f;
472 	  if (pan != 0) { /* no paning, don't compute useless values */
473 	    if (pan > 0) { /* reduce left */
474 	      fl = (100.-pan)/100.;
475 	    } else {
476 	      fr = (100.+pan)/100.;
477 	    }
478 	  }
479 	  /* apply volume and paning */
480 	  plugin_data->port_output[i] = sample * plugin_data->main_vol * fl;
481 	  plugin_data->port_output2[i] = sample * plugin_data->main_vol * fr;
482 	}
483 
484 	// Suppress unused warnings
485 	(void)(sample_rate);
486 	(void)(num_bands);
487 	(void)(main_vol);
488 	(void)(bands_formant);
489 	(void)(bands_carrier);
490 	(void)(bands_out);
491 	(void)(ctrl_band_levels);
492 	(void)(port_formant);
493 	(void)(port_carrier);
494 	(void)(port_output);
495 	(void)(port_output2);
496 	(void)(ctrl_band_count);
497 	(void)(ctrl_pan);
498 	(void)(run_adding_gain);
499 }
500 #undef buffer_write
501 #undef RUN_ADDING
502 #undef RUN_REPLACING
503 
504 #define buffer_write(b, v) (b += (v) * run_adding_gain)
505 #define RUN_ADDING    1
506 #define RUN_REPLACING 0
507 
setRunAddingGainVocoder(LADSPA_Handle instance,LADSPA_Data gain)508 static void setRunAddingGainVocoder(LADSPA_Handle instance, LADSPA_Data gain) {
509 	((Vocoder *)instance)->run_adding_gain = gain;
510 }
511 
runAddingVocoder(LADSPA_Handle instance,unsigned long sample_count)512 static void runAddingVocoder(LADSPA_Handle instance, unsigned long sample_count) {
513 	Vocoder *plugin_data = (Vocoder *)instance;
514 	LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
515 
516 	/* Formant-in (array of floats of length sample_count) */
517 	const LADSPA_Data * const port_formant = plugin_data->port_formant;
518 
519 	/* Carrier-in (array of floats of length sample_count) */
520 	const LADSPA_Data * const port_carrier = plugin_data->port_carrier;
521 
522 	/* Output-out (array of floats of length sample_count) */
523 	LADSPA_Data * const port_output = plugin_data->port_output;
524 
525 	/* Output2-out (array of floats of length sample_count) */
526 	LADSPA_Data * const port_output2 = plugin_data->port_output2;
527 
528 	/* Number of bands (float value) */
529 	const LADSPA_Data ctrl_band_count = *(plugin_data->ctrl_band_count);
530 
531 	/* Left/Right (float value) */
532 	const LADSPA_Data ctrl_pan = *(plugin_data->ctrl_pan);
533 
534 	/* Band 1 Level (float value) */
535 	const LADSPA_Data band1 = *(plugin_data->band1);
536 
537 	/* Band 2 Level (float value) */
538 	const LADSPA_Data band2 = *(plugin_data->band2);
539 
540 	/* Band 3 Level (float value) */
541 	const LADSPA_Data band3 = *(plugin_data->band3);
542 
543 	/* Band 4 Level (float value) */
544 	const LADSPA_Data band4 = *(plugin_data->band4);
545 
546 	/* Band 5 Level (float value) */
547 	const LADSPA_Data band5 = *(plugin_data->band5);
548 
549 	/* Band 6 Level (float value) */
550 	const LADSPA_Data band6 = *(plugin_data->band6);
551 
552 	/* Band 7 Level (float value) */
553 	const LADSPA_Data band7 = *(plugin_data->band7);
554 
555 	/* Band 8 Level (float value) */
556 	const LADSPA_Data band8 = *(plugin_data->band8);
557 
558 	/* Band 9 Level (float value) */
559 	const LADSPA_Data band9 = *(plugin_data->band9);
560 
561 	/* Band 10 Level (float value) */
562 	const LADSPA_Data band10 = *(plugin_data->band10);
563 
564 	/* Band 11 Level (float value) */
565 	const LADSPA_Data band11 = *(plugin_data->band11);
566 
567 	/* Band 12 Level (float value) */
568 	const LADSPA_Data band12 = *(plugin_data->band12);
569 
570 	/* Band 13 Level (float value) */
571 	const LADSPA_Data band13 = *(plugin_data->band13);
572 
573 	/* Band 14 Level (float value) */
574 	const LADSPA_Data band14 = *(plugin_data->band14);
575 
576 	/* Band 15 Level (float value) */
577 	const LADSPA_Data band15 = *(plugin_data->band15);
578 
579 	/* Band 16 Level (float value) */
580 	const LADSPA_Data band16 = *(plugin_data->band16);
581 	struct bandpasses bands_carrier = plugin_data->bands_carrier;
582 	struct bandpasses bands_formant = plugin_data->bands_formant;
583 	struct bands_out bands_out = plugin_data->bands_out;
584 	LADSPA_Data * ctrl_band_levels = plugin_data->ctrl_band_levels;
585 	float main_vol = plugin_data->main_vol;
586 	int num_bands = plugin_data->num_bands;
587 	LADSPA_Data sample_rate = plugin_data->sample_rate;
588 
589 #line 96 "vocoder_1337.xml"
590 	int i, j, numbands, pan;
591 	float a;
592 	LADSPA_Data x, c;
593 	float fl, fr;
594 
595 	// Bind band level controls
596 	plugin_data->ctrl_band_levels[0] = band1;
597 	plugin_data->ctrl_band_levels[1] = band2;
598 	plugin_data->ctrl_band_levels[2] = band3;
599 	plugin_data->ctrl_band_levels[3] = band4;
600 	plugin_data->ctrl_band_levels[4] = band5;
601 	plugin_data->ctrl_band_levels[5] = band6;
602 	plugin_data->ctrl_band_levels[6] = band7;
603 	plugin_data->ctrl_band_levels[7] = band8;
604 	plugin_data->ctrl_band_levels[8] = band9;
605 	plugin_data->ctrl_band_levels[9] = band10;
606 	plugin_data->ctrl_band_levels[10] = band11;
607 	plugin_data->ctrl_band_levels[11] = band12;
608 	plugin_data->ctrl_band_levels[12] = band13;
609 	plugin_data->ctrl_band_levels[13] = band14;
610 	plugin_data->ctrl_band_levels[14] = band15;
611 	plugin_data->ctrl_band_levels[15] = band16;
612 
613 	numbands = (int)(*plugin_data->ctrl_band_count);
614 	if (numbands < 1 || numbands > MAX_BANDS) numbands = MAX_BANDS;
615 
616 	/* initialize bandpass information if num_bands control has changed,
617 	   or on first run */
618 	if (plugin_data->num_bands != numbands)
619 	{
620 	  plugin_data->num_bands = numbands;
621 
622 	  memset(&plugin_data->bands_formant, 0, sizeof(struct bandpasses));
623 	  for(i=0; i < numbands; i++)
624 	  {
625 	    a = 16.0 * i/(double)numbands;  // stretch existing bands
626 
627 	    if (a < 4.0)
628 	      plugin_data->bands_formant.freq[i] = 150 + 420 * a / 4.0;
629 	    else
630 	      plugin_data->bands_formant.freq[i] = 600 * pow (1.23, a - 4.0);
631 
632 	      c = plugin_data->bands_formant.freq[i] * 2 * M_PI / plugin_data->sample_rate;
633 	      plugin_data->bands_formant.c[i] = c * c;
634 
635 	      plugin_data->bands_formant.f[i] = 0.4/c;
636 	      plugin_data->bands_formant.att[i] =
637 	        1/(6.0 + ((exp (plugin_data->bands_formant.freq[i]
638 	                      / plugin_data->sample_rate) - 1) * 10));
639 
640 	      plugin_data->bands_out.decay[i] = decay_table[(int)a];
641 	      plugin_data->bands_out.level[i] =
642 	        CLAMP (plugin_data->ctrl_band_levels[i], 0.0, 1.0);
643 	  }
644 	  memcpy(&plugin_data->bands_carrier,
645 	    &plugin_data->bands_formant, sizeof(struct bandpasses));
646 
647 	}
648 	else                       /* get current values of band level controls */
649 	{
650 	  for (i = 0; i < numbands; i++)
651 	    plugin_data->bands_out.level[i] = CLAMP (plugin_data->ctrl_band_levels[i],
652 	      0.0, 1.0);
653 	}
654 
655 	for (i=0; i < sample_count; i++)
656 	{
657 	  doBandpasses (&(plugin_data->bands_carrier),
658 	    plugin_data->port_carrier[i],
659 	    plugin_data->num_bands);
660 	  doBandpasses (&(plugin_data->bands_formant),
661 	    plugin_data->port_formant[i],
662 	    plugin_data->num_bands);
663 
664 	  LADSPA_Data sample = 0.0;
665 	  for (j=0; j < numbands; j++)
666 	  {
667 	    plugin_data->bands_out.oldval[j] = plugin_data->bands_out.oldval[j]
668 	      + (fabs (plugin_data->bands_formant.y[j])
669 	         - plugin_data->bands_out.oldval[j])
670 	      * plugin_data->bands_out.decay[j];
671 	    x = plugin_data->bands_carrier.y[j] * plugin_data->bands_out.oldval[j];
672 
673 	    sample += x * plugin_data->bands_out.level[j];
674 	  }
675 	  /* treat paning + main volume */
676 	  pan = (int)(*plugin_data->ctrl_pan);
677 	  fl = fr = 1.0f;
678 	  if (pan != 0) { /* no paning, don't compute useless values */
679 	    if (pan > 0) { /* reduce left */
680 	      fl = (100.-pan)/100.;
681 	    } else {
682 	      fr = (100.+pan)/100.;
683 	    }
684 	  }
685 	  /* apply volume and paning */
686 	  plugin_data->port_output[i] = sample * plugin_data->main_vol * fl;
687 	  plugin_data->port_output2[i] = sample * plugin_data->main_vol * fr;
688 	}
689 
690 	// Suppress unused warnings
691 	(void)(sample_rate);
692 	(void)(num_bands);
693 	(void)(main_vol);
694 	(void)(bands_formant);
695 	(void)(bands_carrier);
696 	(void)(bands_out);
697 	(void)(ctrl_band_levels);
698 	(void)(port_formant);
699 	(void)(port_carrier);
700 	(void)(port_output);
701 	(void)(port_output2);
702 	(void)(ctrl_band_count);
703 	(void)(ctrl_pan);
704 	(void)(run_adding_gain);
705 }
706 
swh_init()707 static void __attribute__((constructor)) swh_init() {
708 	char **port_names;
709 	LADSPA_PortDescriptor *port_descriptors;
710 	LADSPA_PortRangeHint *port_range_hints;
711 
712 #ifdef ENABLE_NLS
713 #define D_(s) dgettext(PACKAGE, s)
714 	bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
715 #else
716 #define D_(s) (s)
717 #endif
718 
719 
720 	vocoderDescriptor =
721 	 (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));
722 
723 	if (vocoderDescriptor) {
724 		vocoderDescriptor->UniqueID = 1337;
725 		vocoderDescriptor->Label = "vocoder";
726 		vocoderDescriptor->Properties =
727 		 LADSPA_PROPERTY_HARD_RT_CAPABLE;
728 		vocoderDescriptor->Name =
729 		 D_("Vocoder");
730 		vocoderDescriptor->Maker =
731 		 "Achim Settelmeier <settel-linux@sirlab.de> (adapted by Josh Green and Hexasoft)";
732 		vocoderDescriptor->Copyright =
733 		 "GPL";
734 		vocoderDescriptor->PortCount = 22;
735 
736 		port_descriptors = (LADSPA_PortDescriptor *)calloc(22,
737 		 sizeof(LADSPA_PortDescriptor));
738 		vocoderDescriptor->PortDescriptors =
739 		 (const LADSPA_PortDescriptor *)port_descriptors;
740 
741 		port_range_hints = (LADSPA_PortRangeHint *)calloc(22,
742 		 sizeof(LADSPA_PortRangeHint));
743 		vocoderDescriptor->PortRangeHints =
744 		 (const LADSPA_PortRangeHint *)port_range_hints;
745 
746 		port_names = (char **)calloc(22, sizeof(char*));
747 		vocoderDescriptor->PortNames =
748 		 (const char **)port_names;
749 
750 		/* Parameters for Formant-in */
751 		port_descriptors[VOCODER_PORT_FORMANT] =
752 		 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
753 		port_names[VOCODER_PORT_FORMANT] =
754 		 D_("Formant-in");
755 		port_range_hints[VOCODER_PORT_FORMANT].HintDescriptor = 0;
756 
757 		/* Parameters for Carrier-in */
758 		port_descriptors[VOCODER_PORT_CARRIER] =
759 		 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
760 		port_names[VOCODER_PORT_CARRIER] =
761 		 D_("Carrier-in");
762 		port_range_hints[VOCODER_PORT_CARRIER].HintDescriptor = 0;
763 
764 		/* Parameters for Output-out */
765 		port_descriptors[VOCODER_PORT_OUTPUT] =
766 		 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
767 		port_names[VOCODER_PORT_OUTPUT] =
768 		 D_("Output-out");
769 		port_range_hints[VOCODER_PORT_OUTPUT].HintDescriptor = 0;
770 
771 		/* Parameters for Output2-out */
772 		port_descriptors[VOCODER_PORT_OUTPUT2] =
773 		 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
774 		port_names[VOCODER_PORT_OUTPUT2] =
775 		 D_("Output2-out");
776 		port_range_hints[VOCODER_PORT_OUTPUT2].HintDescriptor = 0;
777 
778 		/* Parameters for Number of bands */
779 		port_descriptors[VOCODER_CTRL_BAND_COUNT] =
780 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
781 		port_names[VOCODER_CTRL_BAND_COUNT] =
782 		 D_("Number of bands");
783 		port_range_hints[VOCODER_CTRL_BAND_COUNT].HintDescriptor =
784 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_INTEGER;
785 		port_range_hints[VOCODER_CTRL_BAND_COUNT].LowerBound = 1;
786 		port_range_hints[VOCODER_CTRL_BAND_COUNT].UpperBound = MAX_BANDS;
787 
788 		/* Parameters for Left/Right */
789 		port_descriptors[VOCODER_CTRL_PAN] =
790 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
791 		port_names[VOCODER_CTRL_PAN] =
792 		 D_("Left/Right");
793 		port_range_hints[VOCODER_CTRL_PAN].HintDescriptor =
794 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_INTEGER;
795 		port_range_hints[VOCODER_CTRL_PAN].LowerBound = -100;
796 		port_range_hints[VOCODER_CTRL_PAN].UpperBound = +100;
797 
798 		/* Parameters for Band 1 Level */
799 		port_descriptors[VOCODER_BAND1] =
800 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
801 		port_names[VOCODER_BAND1] =
802 		 D_("Band 1 Level");
803 		port_range_hints[VOCODER_BAND1].HintDescriptor =
804 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
805 		port_range_hints[VOCODER_BAND1].LowerBound = 0;
806 		port_range_hints[VOCODER_BAND1].UpperBound = 1;
807 
808 		/* Parameters for Band 2 Level */
809 		port_descriptors[VOCODER_BAND2] =
810 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
811 		port_names[VOCODER_BAND2] =
812 		 D_("Band 2 Level");
813 		port_range_hints[VOCODER_BAND2].HintDescriptor =
814 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
815 		port_range_hints[VOCODER_BAND2].LowerBound = 0;
816 		port_range_hints[VOCODER_BAND2].UpperBound = 1;
817 
818 		/* Parameters for Band 3 Level */
819 		port_descriptors[VOCODER_BAND3] =
820 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
821 		port_names[VOCODER_BAND3] =
822 		 D_("Band 3 Level");
823 		port_range_hints[VOCODER_BAND3].HintDescriptor =
824 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
825 		port_range_hints[VOCODER_BAND3].LowerBound = 0;
826 		port_range_hints[VOCODER_BAND3].UpperBound = 1;
827 
828 		/* Parameters for Band 4 Level */
829 		port_descriptors[VOCODER_BAND4] =
830 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
831 		port_names[VOCODER_BAND4] =
832 		 D_("Band 4 Level");
833 		port_range_hints[VOCODER_BAND4].HintDescriptor =
834 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
835 		port_range_hints[VOCODER_BAND4].LowerBound = 0;
836 		port_range_hints[VOCODER_BAND4].UpperBound = 1;
837 
838 		/* Parameters for Band 5 Level */
839 		port_descriptors[VOCODER_BAND5] =
840 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
841 		port_names[VOCODER_BAND5] =
842 		 D_("Band 5 Level");
843 		port_range_hints[VOCODER_BAND5].HintDescriptor =
844 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
845 		port_range_hints[VOCODER_BAND5].LowerBound = 0;
846 		port_range_hints[VOCODER_BAND5].UpperBound = 1;
847 
848 		/* Parameters for Band 6 Level */
849 		port_descriptors[VOCODER_BAND6] =
850 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
851 		port_names[VOCODER_BAND6] =
852 		 D_("Band 6 Level");
853 		port_range_hints[VOCODER_BAND6].HintDescriptor =
854 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
855 		port_range_hints[VOCODER_BAND6].LowerBound = 0;
856 		port_range_hints[VOCODER_BAND6].UpperBound = 1;
857 
858 		/* Parameters for Band 7 Level */
859 		port_descriptors[VOCODER_BAND7] =
860 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
861 		port_names[VOCODER_BAND7] =
862 		 D_("Band 7 Level");
863 		port_range_hints[VOCODER_BAND7].HintDescriptor =
864 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
865 		port_range_hints[VOCODER_BAND7].LowerBound = 0;
866 		port_range_hints[VOCODER_BAND7].UpperBound = 1;
867 
868 		/* Parameters for Band 8 Level */
869 		port_descriptors[VOCODER_BAND8] =
870 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
871 		port_names[VOCODER_BAND8] =
872 		 D_("Band 8 Level");
873 		port_range_hints[VOCODER_BAND8].HintDescriptor =
874 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
875 		port_range_hints[VOCODER_BAND8].LowerBound = 0;
876 		port_range_hints[VOCODER_BAND8].UpperBound = 1;
877 
878 		/* Parameters for Band 9 Level */
879 		port_descriptors[VOCODER_BAND9] =
880 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
881 		port_names[VOCODER_BAND9] =
882 		 D_("Band 9 Level");
883 		port_range_hints[VOCODER_BAND9].HintDescriptor =
884 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
885 		port_range_hints[VOCODER_BAND9].LowerBound = 0;
886 		port_range_hints[VOCODER_BAND9].UpperBound = 1;
887 
888 		/* Parameters for Band 10 Level */
889 		port_descriptors[VOCODER_BAND10] =
890 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
891 		port_names[VOCODER_BAND10] =
892 		 D_("Band 10 Level");
893 		port_range_hints[VOCODER_BAND10].HintDescriptor =
894 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
895 		port_range_hints[VOCODER_BAND10].LowerBound = 0;
896 		port_range_hints[VOCODER_BAND10].UpperBound = 1;
897 
898 		/* Parameters for Band 11 Level */
899 		port_descriptors[VOCODER_BAND11] =
900 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
901 		port_names[VOCODER_BAND11] =
902 		 D_("Band 11 Level");
903 		port_range_hints[VOCODER_BAND11].HintDescriptor =
904 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
905 		port_range_hints[VOCODER_BAND11].LowerBound = 0;
906 		port_range_hints[VOCODER_BAND11].UpperBound = 1;
907 
908 		/* Parameters for Band 12 Level */
909 		port_descriptors[VOCODER_BAND12] =
910 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
911 		port_names[VOCODER_BAND12] =
912 		 D_("Band 12 Level");
913 		port_range_hints[VOCODER_BAND12].HintDescriptor =
914 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
915 		port_range_hints[VOCODER_BAND12].LowerBound = 0;
916 		port_range_hints[VOCODER_BAND12].UpperBound = 1;
917 
918 		/* Parameters for Band 13 Level */
919 		port_descriptors[VOCODER_BAND13] =
920 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
921 		port_names[VOCODER_BAND13] =
922 		 D_("Band 13 Level");
923 		port_range_hints[VOCODER_BAND13].HintDescriptor =
924 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
925 		port_range_hints[VOCODER_BAND13].LowerBound = 0;
926 		port_range_hints[VOCODER_BAND13].UpperBound = 1;
927 
928 		/* Parameters for Band 14 Level */
929 		port_descriptors[VOCODER_BAND14] =
930 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
931 		port_names[VOCODER_BAND14] =
932 		 D_("Band 14 Level");
933 		port_range_hints[VOCODER_BAND14].HintDescriptor =
934 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
935 		port_range_hints[VOCODER_BAND14].LowerBound = 0;
936 		port_range_hints[VOCODER_BAND14].UpperBound = 1;
937 
938 		/* Parameters for Band 15 Level */
939 		port_descriptors[VOCODER_BAND15] =
940 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
941 		port_names[VOCODER_BAND15] =
942 		 D_("Band 15 Level");
943 		port_range_hints[VOCODER_BAND15].HintDescriptor =
944 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
945 		port_range_hints[VOCODER_BAND15].LowerBound = 0;
946 		port_range_hints[VOCODER_BAND15].UpperBound = 1;
947 
948 		/* Parameters for Band 16 Level */
949 		port_descriptors[VOCODER_BAND16] =
950 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
951 		port_names[VOCODER_BAND16] =
952 		 D_("Band 16 Level");
953 		port_range_hints[VOCODER_BAND16].HintDescriptor =
954 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE;
955 		port_range_hints[VOCODER_BAND16].LowerBound = 0;
956 		port_range_hints[VOCODER_BAND16].UpperBound = 1;
957 
958 		vocoderDescriptor->activate = activateVocoder;
959 		vocoderDescriptor->cleanup = cleanupVocoder;
960 		vocoderDescriptor->connect_port = connectPortVocoder;
961 		vocoderDescriptor->deactivate = NULL;
962 		vocoderDescriptor->instantiate = instantiateVocoder;
963 		vocoderDescriptor->run = runVocoder;
964 		vocoderDescriptor->run_adding = runAddingVocoder;
965 		vocoderDescriptor->set_run_adding_gain = setRunAddingGainVocoder;
966 	}
967 }
968 
swh_fini()969 static void __attribute__((destructor)) swh_fini() {
970 	if (vocoderDescriptor) {
971 		free((LADSPA_PortDescriptor *)vocoderDescriptor->PortDescriptors);
972 		free((char **)vocoderDescriptor->PortNames);
973 		free((LADSPA_PortRangeHint *)vocoderDescriptor->PortRangeHints);
974 		free(vocoderDescriptor);
975 	}
976 	vocoderDescriptor = NULL;
977 
978 }
979