File: dyson_compress_1403.so.c

package info (click to toggle)
swh-plugins 0.4.15%2B1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,268 kB
  • ctags: 12,249
  • sloc: ansic: 72,074; sh: 28,183; xml: 12,247; perl: 769; makefile: 212; sed: 16
file content (896 lines) | stat: -rw-r--r-- 25,512 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include "config.h"
#endif

#ifdef ENABLE_NLS
#include <libintl.h>
#endif

#define         _ISOC9X_SOURCE  1
#define         _ISOC99_SOURCE  1
#define         __USE_ISOC99    1
#define         __USE_ISOC9X    1

#include <math.h>

#include "ladspa.h"

#ifdef WIN32
#define _WINDOWS_DLL_EXPORT_ __declspec(dllexport)
int bIsFirstTime = 1; 
void _init(); // forward declaration
#else
#define _WINDOWS_DLL_EXPORT_ 
#endif

#line 10 "dyson_compress_1403.xml"

/*
 * Copyright (c) 1996, John S. Dyson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * This code (easily) runs realtime on a P5-166 w/EDO, Triton-II on FreeBSD.
 *
 * More info/comments: dyson@freebsd.org
 *
 * This program provides compression of a stereo 16bit audio stream,
 * such as that contained by a 16Bit wav file.  Extreme measures have
 * been taken to make the compression as subtile as possible.  One
 * possible purpose for this code would be to master cassette tapes from
 * CD's for playback in automobiles where dynamic range needs to be
 * restricted.
 *
 * Suitably recoded for an embedded DSP, this would make a killer audio
 * compressor for broadcast or recording.  When writing this code, I
 * ignored the issues of roundoff error or trucation -- Pentiums have
 * really nice FP processors :-).
 */

      #include <ladspa-util.h>

      #define MAXLEVEL 0.9f
      #define NFILT 12
      #define NEFILT 17

      /* These filters should filter at least the lowest audio freq */
      #define RLEVELSQ0FILTER .001
      #define RLEVELSQ1FILTER .010
      /* These are the attack time for the rms measurement */
      #define RLEVELSQ0FFILTER .001
      #define RLEVELSQEFILTER .001
    
      #define RMASTERGAIN0FILTER .000003
    
      #define RPEAKGAINFILTER .001

      #define MAXFASTGAIN 3
      #define MAXSLOWGAIN 9

      #define FLOORLEVEL 0.06

      float hardlimit(float value, float knee, float limit) {
        float ab = fabs(value);
        if (ab >= limit) {
          value = value > 0 ? limit : -limit;
        }

        return value;
      }

#define DYSONCOMPRESS_PEAK_LIMIT       0
#define DYSONCOMPRESS_RELEASE_TIME     1
#define DYSONCOMPRESS_CFRATE           2
#define DYSONCOMPRESS_CRATE            3
#define DYSONCOMPRESS_INPUT            4
#define DYSONCOMPRESS_OUTPUT           5

static LADSPA_Descriptor *dysonCompressDescriptor = NULL;

typedef struct {
	LADSPA_Data *peak_limit;
	LADSPA_Data *release_time;
	LADSPA_Data *cfrate;
	LADSPA_Data *crate;
	LADSPA_Data *input;
	LADSPA_Data *output;
	LADSPA_Data *delay;
	float        extra_maxlevel;
	float        lastrgain;
	float        maxgain;
	float        mingain;
	float        ndelay;
	unsigned int ndelayptr;
	int          peaklimitdelay;
	float        rgain;
	float        rlevelsq0;
	float        rlevelsq1;
	LADSPA_Data *rlevelsqe;
	LADSPA_Data *rlevelsqn;
	float        rmastergain0;
	float        rpeakgain0;
	float        rpeakgain1;
	float        rpeaklimitdelay;
	float        sample_rate;
	LADSPA_Data run_adding_gain;
} DysonCompress;

_WINDOWS_DLL_EXPORT_
const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {

#ifdef WIN32
	if (bIsFirstTime) {
		_init();
		bIsFirstTime = 0;
	}
#endif
	switch (index) {
	case 0:
		return dysonCompressDescriptor;
	default:
		return NULL;
	}
}

static void activateDysonCompress(LADSPA_Handle instance) {
	DysonCompress *plugin_data = (DysonCompress *)instance;
	LADSPA_Data *delay = plugin_data->delay;
	float extra_maxlevel = plugin_data->extra_maxlevel;
	float lastrgain = plugin_data->lastrgain;
	float maxgain = plugin_data->maxgain;
	float mingain = plugin_data->mingain;
	float ndelay = plugin_data->ndelay;
	unsigned int ndelayptr = plugin_data->ndelayptr;
	int peaklimitdelay = plugin_data->peaklimitdelay;
	float rgain = plugin_data->rgain;
	float rlevelsq0 = plugin_data->rlevelsq0;
	float rlevelsq1 = plugin_data->rlevelsq1;
	LADSPA_Data *rlevelsqe = plugin_data->rlevelsqe;
	LADSPA_Data *rlevelsqn = plugin_data->rlevelsqn;
	float rmastergain0 = plugin_data->rmastergain0;
	float rpeakgain0 = plugin_data->rpeakgain0;
	float rpeakgain1 = plugin_data->rpeakgain1;
	float rpeaklimitdelay = plugin_data->rpeaklimitdelay;
	float sample_rate = plugin_data->sample_rate;
#line 105 "dyson_compress_1403.xml"
	unsigned int i;

	for (i=0; i<ndelay; i++) {
	  delay[i] = 0;
	}
	for (i=0; i<NFILT + 1; i++) {
	  rlevelsqn[i] = 0;
	}
	for (i=0; i<NEFILT + 1; i++) {
	  rlevelsqe[i] = 0;
	}

	mingain = 10000;
	maxgain = 0;

	rpeaklimitdelay = 2500;
    
	rgain = rmastergain0 = 1.0;
	rlevelsq0 = 0;
	rlevelsq1 = 0;
    
	rpeakgain0 = 1.0;
	rpeakgain1 = 1.0;
	rpeaklimitdelay = 0;
	ndelayptr = 0;
	lastrgain = 1.0;

	extra_maxlevel = 0.0f;
	peaklimitdelay = 0;
	plugin_data->delay = delay;
	plugin_data->extra_maxlevel = extra_maxlevel;
	plugin_data->lastrgain = lastrgain;
	plugin_data->maxgain = maxgain;
	plugin_data->mingain = mingain;
	plugin_data->ndelay = ndelay;
	plugin_data->ndelayptr = ndelayptr;
	plugin_data->peaklimitdelay = peaklimitdelay;
	plugin_data->rgain = rgain;
	plugin_data->rlevelsq0 = rlevelsq0;
	plugin_data->rlevelsq1 = rlevelsq1;
	plugin_data->rlevelsqe = rlevelsqe;
	plugin_data->rlevelsqn = rlevelsqn;
	plugin_data->rmastergain0 = rmastergain0;
	plugin_data->rpeakgain0 = rpeakgain0;
	plugin_data->rpeakgain1 = rpeakgain1;
	plugin_data->rpeaklimitdelay = rpeaklimitdelay;
	plugin_data->sample_rate = sample_rate;

}

static void cleanupDysonCompress(LADSPA_Handle instance) {
#line 137 "dyson_compress_1403.xml"
	DysonCompress *plugin_data = (DysonCompress *)instance;
	free(plugin_data->delay);
	free(plugin_data->rlevelsqn);
	free(plugin_data->rlevelsqe);
	free(instance);
}

static void connectPortDysonCompress(
 LADSPA_Handle instance,
 unsigned long port,
 LADSPA_Data *data) {
	DysonCompress *plugin;

	plugin = (DysonCompress *)instance;
	switch (port) {
	case DYSONCOMPRESS_PEAK_LIMIT:
		plugin->peak_limit = data;
		break;
	case DYSONCOMPRESS_RELEASE_TIME:
		plugin->release_time = data;
		break;
	case DYSONCOMPRESS_CFRATE:
		plugin->cfrate = data;
		break;
	case DYSONCOMPRESS_CRATE:
		plugin->crate = data;
		break;
	case DYSONCOMPRESS_INPUT:
		plugin->input = data;
		break;
	case DYSONCOMPRESS_OUTPUT:
		plugin->output = data;
		break;
	}
}

static LADSPA_Handle instantiateDysonCompress(
 const LADSPA_Descriptor *descriptor,
 unsigned long s_rate) {
	DysonCompress *plugin_data = (DysonCompress *)malloc(sizeof(DysonCompress));
	LADSPA_Data *delay = NULL;
	float extra_maxlevel;
	float lastrgain;
	float maxgain;
	float mingain;
	float ndelay;
	unsigned int ndelayptr;
	int peaklimitdelay;
	float rgain;
	float rlevelsq0;
	float rlevelsq1;
	LADSPA_Data *rlevelsqe = NULL;
	LADSPA_Data *rlevelsqn = NULL;
	float rmastergain0;
	float rpeakgain0;
	float rpeakgain1;
	float rpeaklimitdelay;
	float sample_rate;

#line 78 "dyson_compress_1403.xml"
	sample_rate = (float)s_rate;

	mingain = 10000;
	maxgain = 0;

	rpeaklimitdelay = 2500;
    
	rgain = rmastergain0 = 1.0;
	rlevelsq0 = 0;
	rlevelsq1 = 0;
	ndelay = (int)(1.0 / RLEVELSQ0FFILTER);

	delay = calloc(ndelay, sizeof(LADSPA_Data));
	rlevelsqn = calloc(NFILT + 1, sizeof(float));
	rlevelsqe = calloc(NEFILT + 1, sizeof(float));
    
	rpeakgain0 = 1.0;
	rpeakgain1 = 1.0;
	rpeaklimitdelay = 0;
	ndelayptr = 0;
	lastrgain = 1.0;

	extra_maxlevel = 0.0f;
	peaklimitdelay = 0;

	plugin_data->delay = delay;
	plugin_data->extra_maxlevel = extra_maxlevel;
	plugin_data->lastrgain = lastrgain;
	plugin_data->maxgain = maxgain;
	plugin_data->mingain = mingain;
	plugin_data->ndelay = ndelay;
	plugin_data->ndelayptr = ndelayptr;
	plugin_data->peaklimitdelay = peaklimitdelay;
	plugin_data->rgain = rgain;
	plugin_data->rlevelsq0 = rlevelsq0;
	plugin_data->rlevelsq1 = rlevelsq1;
	plugin_data->rlevelsqe = rlevelsqe;
	plugin_data->rlevelsqn = rlevelsqn;
	plugin_data->rmastergain0 = rmastergain0;
	plugin_data->rpeakgain0 = rpeakgain0;
	plugin_data->rpeakgain1 = rpeakgain1;
	plugin_data->rpeaklimitdelay = rpeaklimitdelay;
	plugin_data->sample_rate = sample_rate;

	return (LADSPA_Handle)plugin_data;
}

#undef buffer_write
#undef RUN_ADDING
#undef RUN_REPLACING

#define buffer_write(b, v) (b = v)
#define RUN_ADDING    0
#define RUN_REPLACING 1

static void runDysonCompress(LADSPA_Handle instance, unsigned long sample_count) {
	DysonCompress *plugin_data = (DysonCompress *)instance;

	/* Peak limit (dB) (float value) */
	const LADSPA_Data peak_limit = *(plugin_data->peak_limit);

	/* Release time (s) (float value) */
	const LADSPA_Data release_time = *(plugin_data->release_time);

	/* Fast compression ratio (float value) */
	const LADSPA_Data cfrate = *(plugin_data->cfrate);

	/* Compression ratio (float value) */
	const LADSPA_Data crate = *(plugin_data->crate);

	/* Input (array of floats of length sample_count) */
	const LADSPA_Data * const input = plugin_data->input;

	/* Output (array of floats of length sample_count) */
	LADSPA_Data * const output = plugin_data->output;
	LADSPA_Data * delay = plugin_data->delay;
	float extra_maxlevel = plugin_data->extra_maxlevel;
	float lastrgain = plugin_data->lastrgain;
	float maxgain = plugin_data->maxgain;
	float mingain = plugin_data->mingain;
	float ndelay = plugin_data->ndelay;
	unsigned int ndelayptr = plugin_data->ndelayptr;
	int peaklimitdelay = plugin_data->peaklimitdelay;
	float rgain = plugin_data->rgain;
	float rlevelsq0 = plugin_data->rlevelsq0;
	float rlevelsq1 = plugin_data->rlevelsq1;
	LADSPA_Data * rlevelsqe = plugin_data->rlevelsqe;
	LADSPA_Data * rlevelsqn = plugin_data->rlevelsqn;
	float rmastergain0 = plugin_data->rmastergain0;
	float rpeakgain0 = plugin_data->rpeakgain0;
	float rpeakgain1 = plugin_data->rpeakgain1;
	float rpeaklimitdelay = plugin_data->rpeaklimitdelay;
	float sample_rate = plugin_data->sample_rate;

#line 143 "dyson_compress_1403.xml"
	unsigned long pos;
	float targetlevel = MAXLEVEL * DB_CO(peak_limit);
	float rgainfilter = 1.0f / (release_time * sample_rate);
	float fastgaincompressionratio = cfrate;
	float compressionratio = crate;
	float efilt;
	float levelsqe;
	float gain;
	float tgain;
	float d;
	float fastgain;
	float qgain;
	float tslowgain;
	float slowgain;
	float npeakgain;
	float new;
	float nrgain;
	float ngain;
	float ngsq;
	float tnrgain;
	float sqrtrpeakgain;
	float totalgain;
	unsigned int i;

	for (pos = 0; pos < sample_count; pos++) {
	  // Ergh! this was originally meant to track a stereo signal
	  float levelsq0 = 2.0f * (input[pos] * input[pos]);

	  delay[ndelayptr] = input[pos];
	  ndelayptr++;

	  if (ndelayptr >= ndelay) {
	    ndelayptr = 0;
	  }

	  if (levelsq0 > rlevelsq0) {
	    rlevelsq0 = (levelsq0 * RLEVELSQ0FFILTER) +
	     rlevelsq0 * (1 - RLEVELSQ0FFILTER);
	  } else {
	    rlevelsq0 = (levelsq0 * RLEVELSQ0FILTER) +
	     rlevelsq0 * (1 - RLEVELSQ0FILTER);
	  }

	  if (rlevelsq0 <= FLOORLEVEL * FLOORLEVEL) {
	    goto skipagc;
	  }

	  if (rlevelsq0 > rlevelsq1) {
	    rlevelsq1 = rlevelsq0;
	  } else {
	    rlevelsq1 = rlevelsq0 * RLEVELSQ1FILTER +
	      rlevelsq1 * (1 - RLEVELSQ1FILTER);
	  }

	  rlevelsqn[0] = rlevelsq1;
	  for(i = 0; i < NFILT-1; i++) {
	    if (rlevelsqn[i] > rlevelsqn[i+1])
	      rlevelsqn[i+1] = rlevelsqn[i];
	    else
	      rlevelsqn[i+1] = rlevelsqn[i] * RLEVELSQ1FILTER +
	        rlevelsqn[i+1] * (1 - RLEVELSQ1FILTER);
	  }

	  efilt = RLEVELSQEFILTER;
	  levelsqe = rlevelsqe[0] = rlevelsqn[NFILT-1];
	  for(i = 0; i < NEFILT-1; i++) {
	    rlevelsqe[i+1] = rlevelsqe[i] * efilt +
	      rlevelsqe[i+1] * (1.0 - efilt);
	    if (rlevelsqe[i+1] > levelsqe)
	      levelsqe = rlevelsqe[i+1];
	    efilt *= 1.0f / 1.5f;
	  }

	  gain = targetlevel / sqrt(levelsqe);
	  if (compressionratio < 0.99f) {
	    if (compressionratio == 0.50f)
	      gain = sqrt(gain);
	    else
	      gain = f_exp(log(gain) * compressionratio);
	  }

	  if (gain < rgain)
	    rgain = gain * RLEVELSQEFILTER/2 +
	      rgain * (1 - RLEVELSQEFILTER/2);
	  else
	    rgain = gain * rgainfilter +
	      rgain * (1 - rgainfilter);

	  lastrgain = rgain;
	  if ( gain < lastrgain)
	    lastrgain = gain;

	skipagc:;

	  tgain = lastrgain;
    
	  d = delay[ndelayptr];
    
	  fastgain = tgain;
	  if (fastgain > MAXFASTGAIN)
	    fastgain = MAXFASTGAIN;
    
	  if (fastgain < 0.0001)
	    fastgain = 0.0001;

	  qgain = f_exp(log(fastgain) * fastgaincompressionratio);

	  tslowgain = tgain / qgain;
	  if (tslowgain > MAXSLOWGAIN)
	    tslowgain = MAXSLOWGAIN;
	  if (tslowgain < rmastergain0)
	    rmastergain0 = tslowgain;
	  else
	    rmastergain0 = tslowgain * RMASTERGAIN0FILTER +
	      (1 - RMASTERGAIN0FILTER) * rmastergain0;
    
	  slowgain = rmastergain0;
	  npeakgain = slowgain * qgain;
    
	  new = d * npeakgain;
	  if (fabs(new) >= MAXLEVEL)
	    nrgain = MAXLEVEL / fabs(new);
	  else
	    nrgain = 1.0;
    
	  ngain = nrgain;
    
	  ngsq = ngain * ngain;
	  if (ngsq <= rpeakgain0) {
	    rpeakgain0 = ngsq /* * 0.50 + rpeakgain0 * 0.50 */;
	    rpeaklimitdelay = peaklimitdelay;
	  } else if (rpeaklimitdelay == 0) {
	    if (nrgain > 1.0)
	      tnrgain = 1.0;
	    else
	      tnrgain = nrgain;
	    rpeakgain0 = tnrgain * RPEAKGAINFILTER +
	      (1.0 - RPEAKGAINFILTER) * rpeakgain0;
	  }

	  if (rpeakgain0 <= rpeakgain1) {
	    rpeakgain1 = rpeakgain0;
	    rpeaklimitdelay = peaklimitdelay;
	  } else if (rpeaklimitdelay == 0) {
	    rpeakgain1 = RPEAKGAINFILTER * rpeakgain0 +
	      (1.0 - RPEAKGAINFILTER) * rpeakgain1;
	  } else {
	    --rpeaklimitdelay;
	  }

	  sqrtrpeakgain = sqrt(rpeakgain1);
	  totalgain = npeakgain * sqrtrpeakgain;
    
	  buffer_write(output[pos], new * sqrtrpeakgain);
    
	  if (totalgain > maxgain)
	    maxgain = totalgain;
	  if (totalgain < mingain)
	    mingain = totalgain;
	  if (output[pos] > extra_maxlevel)
	    extra_maxlevel = output[pos];
	}

	plugin_data->ndelayptr = ndelayptr;
	plugin_data->rlevelsq0 = rlevelsq0;
	plugin_data->rlevelsq1 = rlevelsq1;
	plugin_data->mingain = mingain;
	plugin_data->maxgain = maxgain;
	plugin_data->rpeaklimitdelay = rpeaklimitdelay;
	plugin_data->rgain = rgain;
	plugin_data->rmastergain0 = rmastergain0;
	plugin_data->rpeakgain0 = rpeakgain0;
	plugin_data->rpeakgain1 = rpeakgain1;
	plugin_data->lastrgain = lastrgain;
	plugin_data->extra_maxlevel = extra_maxlevel;
}
#undef buffer_write
#undef RUN_ADDING
#undef RUN_REPLACING

#define buffer_write(b, v) (b += (v) * run_adding_gain)
#define RUN_ADDING    1
#define RUN_REPLACING 0

static void setRunAddingGainDysonCompress(LADSPA_Handle instance, LADSPA_Data gain) {
	((DysonCompress *)instance)->run_adding_gain = gain;
}

static void runAddingDysonCompress(LADSPA_Handle instance, unsigned long sample_count) {
	DysonCompress *plugin_data = (DysonCompress *)instance;
	LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;

	/* Peak limit (dB) (float value) */
	const LADSPA_Data peak_limit = *(plugin_data->peak_limit);

	/* Release time (s) (float value) */
	const LADSPA_Data release_time = *(plugin_data->release_time);

	/* Fast compression ratio (float value) */
	const LADSPA_Data cfrate = *(plugin_data->cfrate);

	/* Compression ratio (float value) */
	const LADSPA_Data crate = *(plugin_data->crate);

	/* Input (array of floats of length sample_count) */
	const LADSPA_Data * const input = plugin_data->input;

	/* Output (array of floats of length sample_count) */
	LADSPA_Data * const output = plugin_data->output;
	LADSPA_Data * delay = plugin_data->delay;
	float extra_maxlevel = plugin_data->extra_maxlevel;
	float lastrgain = plugin_data->lastrgain;
	float maxgain = plugin_data->maxgain;
	float mingain = plugin_data->mingain;
	float ndelay = plugin_data->ndelay;
	unsigned int ndelayptr = plugin_data->ndelayptr;
	int peaklimitdelay = plugin_data->peaklimitdelay;
	float rgain = plugin_data->rgain;
	float rlevelsq0 = plugin_data->rlevelsq0;
	float rlevelsq1 = plugin_data->rlevelsq1;
	LADSPA_Data * rlevelsqe = plugin_data->rlevelsqe;
	LADSPA_Data * rlevelsqn = plugin_data->rlevelsqn;
	float rmastergain0 = plugin_data->rmastergain0;
	float rpeakgain0 = plugin_data->rpeakgain0;
	float rpeakgain1 = plugin_data->rpeakgain1;
	float rpeaklimitdelay = plugin_data->rpeaklimitdelay;
	float sample_rate = plugin_data->sample_rate;

#line 143 "dyson_compress_1403.xml"
	unsigned long pos;
	float targetlevel = MAXLEVEL * DB_CO(peak_limit);
	float rgainfilter = 1.0f / (release_time * sample_rate);
	float fastgaincompressionratio = cfrate;
	float compressionratio = crate;
	float efilt;
	float levelsqe;
	float gain;
	float tgain;
	float d;
	float fastgain;
	float qgain;
	float tslowgain;
	float slowgain;
	float npeakgain;
	float new;
	float nrgain;
	float ngain;
	float ngsq;
	float tnrgain;
	float sqrtrpeakgain;
	float totalgain;
	unsigned int i;

	for (pos = 0; pos < sample_count; pos++) {
	  // Ergh! this was originally meant to track a stereo signal
	  float levelsq0 = 2.0f * (input[pos] * input[pos]);

	  delay[ndelayptr] = input[pos];
	  ndelayptr++;

	  if (ndelayptr >= ndelay) {
	    ndelayptr = 0;
	  }

	  if (levelsq0 > rlevelsq0) {
	    rlevelsq0 = (levelsq0 * RLEVELSQ0FFILTER) +
	     rlevelsq0 * (1 - RLEVELSQ0FFILTER);
	  } else {
	    rlevelsq0 = (levelsq0 * RLEVELSQ0FILTER) +
	     rlevelsq0 * (1 - RLEVELSQ0FILTER);
	  }

	  if (rlevelsq0 <= FLOORLEVEL * FLOORLEVEL) {
	    goto skipagc;
	  }

	  if (rlevelsq0 > rlevelsq1) {
	    rlevelsq1 = rlevelsq0;
	  } else {
	    rlevelsq1 = rlevelsq0 * RLEVELSQ1FILTER +
	      rlevelsq1 * (1 - RLEVELSQ1FILTER);
	  }

	  rlevelsqn[0] = rlevelsq1;
	  for(i = 0; i < NFILT-1; i++) {
	    if (rlevelsqn[i] > rlevelsqn[i+1])
	      rlevelsqn[i+1] = rlevelsqn[i];
	    else
	      rlevelsqn[i+1] = rlevelsqn[i] * RLEVELSQ1FILTER +
	        rlevelsqn[i+1] * (1 - RLEVELSQ1FILTER);
	  }

	  efilt = RLEVELSQEFILTER;
	  levelsqe = rlevelsqe[0] = rlevelsqn[NFILT-1];
	  for(i = 0; i < NEFILT-1; i++) {
	    rlevelsqe[i+1] = rlevelsqe[i] * efilt +
	      rlevelsqe[i+1] * (1.0 - efilt);
	    if (rlevelsqe[i+1] > levelsqe)
	      levelsqe = rlevelsqe[i+1];
	    efilt *= 1.0f / 1.5f;
	  }

	  gain = targetlevel / sqrt(levelsqe);
	  if (compressionratio < 0.99f) {
	    if (compressionratio == 0.50f)
	      gain = sqrt(gain);
	    else
	      gain = f_exp(log(gain) * compressionratio);
	  }

	  if (gain < rgain)
	    rgain = gain * RLEVELSQEFILTER/2 +
	      rgain * (1 - RLEVELSQEFILTER/2);
	  else
	    rgain = gain * rgainfilter +
	      rgain * (1 - rgainfilter);

	  lastrgain = rgain;
	  if ( gain < lastrgain)
	    lastrgain = gain;

	skipagc:;

	  tgain = lastrgain;
    
	  d = delay[ndelayptr];
    
	  fastgain = tgain;
	  if (fastgain > MAXFASTGAIN)
	    fastgain = MAXFASTGAIN;
    
	  if (fastgain < 0.0001)
	    fastgain = 0.0001;

	  qgain = f_exp(log(fastgain) * fastgaincompressionratio);

	  tslowgain = tgain / qgain;
	  if (tslowgain > MAXSLOWGAIN)
	    tslowgain = MAXSLOWGAIN;
	  if (tslowgain < rmastergain0)
	    rmastergain0 = tslowgain;
	  else
	    rmastergain0 = tslowgain * RMASTERGAIN0FILTER +
	      (1 - RMASTERGAIN0FILTER) * rmastergain0;
    
	  slowgain = rmastergain0;
	  npeakgain = slowgain * qgain;
    
	  new = d * npeakgain;
	  if (fabs(new) >= MAXLEVEL)
	    nrgain = MAXLEVEL / fabs(new);
	  else
	    nrgain = 1.0;
    
	  ngain = nrgain;
    
	  ngsq = ngain * ngain;
	  if (ngsq <= rpeakgain0) {
	    rpeakgain0 = ngsq /* * 0.50 + rpeakgain0 * 0.50 */;
	    rpeaklimitdelay = peaklimitdelay;
	  } else if (rpeaklimitdelay == 0) {
	    if (nrgain > 1.0)
	      tnrgain = 1.0;
	    else
	      tnrgain = nrgain;
	    rpeakgain0 = tnrgain * RPEAKGAINFILTER +
	      (1.0 - RPEAKGAINFILTER) * rpeakgain0;
	  }

	  if (rpeakgain0 <= rpeakgain1) {
	    rpeakgain1 = rpeakgain0;
	    rpeaklimitdelay = peaklimitdelay;
	  } else if (rpeaklimitdelay == 0) {
	    rpeakgain1 = RPEAKGAINFILTER * rpeakgain0 +
	      (1.0 - RPEAKGAINFILTER) * rpeakgain1;
	  } else {
	    --rpeaklimitdelay;
	  }

	  sqrtrpeakgain = sqrt(rpeakgain1);
	  totalgain = npeakgain * sqrtrpeakgain;
    
	  buffer_write(output[pos], new * sqrtrpeakgain);
    
	  if (totalgain > maxgain)
	    maxgain = totalgain;
	  if (totalgain < mingain)
	    mingain = totalgain;
	  if (output[pos] > extra_maxlevel)
	    extra_maxlevel = output[pos];
	}

	plugin_data->ndelayptr = ndelayptr;
	plugin_data->rlevelsq0 = rlevelsq0;
	plugin_data->rlevelsq1 = rlevelsq1;
	plugin_data->mingain = mingain;
	plugin_data->maxgain = maxgain;
	plugin_data->rpeaklimitdelay = rpeaklimitdelay;
	plugin_data->rgain = rgain;
	plugin_data->rmastergain0 = rmastergain0;
	plugin_data->rpeakgain0 = rpeakgain0;
	plugin_data->rpeakgain1 = rpeakgain1;
	plugin_data->lastrgain = lastrgain;
	plugin_data->extra_maxlevel = extra_maxlevel;
}

void _init() {
	char **port_names;
	LADSPA_PortDescriptor *port_descriptors;
	LADSPA_PortRangeHint *port_range_hints;

#ifdef ENABLE_NLS
#define D_(s) dgettext(PACKAGE, s)
	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
#else
#define D_(s) (s)
#endif


	dysonCompressDescriptor =
	 (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));

	if (dysonCompressDescriptor) {
		dysonCompressDescriptor->UniqueID = 1403;
		dysonCompressDescriptor->Label = "dysonCompress";
		dysonCompressDescriptor->Properties =
		 LADSPA_PROPERTY_HARD_RT_CAPABLE;
		dysonCompressDescriptor->Name =
		 D_("Dyson compressor");
		dysonCompressDescriptor->Maker =
		 "Steve Harris <steve@plugin.org.uk>";
		dysonCompressDescriptor->Copyright =
		 "GPL";
		dysonCompressDescriptor->PortCount = 6;

		port_descriptors = (LADSPA_PortDescriptor *)calloc(6,
		 sizeof(LADSPA_PortDescriptor));
		dysonCompressDescriptor->PortDescriptors =
		 (const LADSPA_PortDescriptor *)port_descriptors;

		port_range_hints = (LADSPA_PortRangeHint *)calloc(6,
		 sizeof(LADSPA_PortRangeHint));
		dysonCompressDescriptor->PortRangeHints =
		 (const LADSPA_PortRangeHint *)port_range_hints;

		port_names = (char **)calloc(6, sizeof(char*));
		dysonCompressDescriptor->PortNames =
		 (const char **)port_names;

		/* Parameters for Peak limit (dB) */
		port_descriptors[DYSONCOMPRESS_PEAK_LIMIT] =
		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
		port_names[DYSONCOMPRESS_PEAK_LIMIT] =
		 D_("Peak limit (dB)");
		port_range_hints[DYSONCOMPRESS_PEAK_LIMIT].HintDescriptor =
		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_0;
		port_range_hints[DYSONCOMPRESS_PEAK_LIMIT].LowerBound = -30;
		port_range_hints[DYSONCOMPRESS_PEAK_LIMIT].UpperBound = 0;

		/* Parameters for Release time (s) */
		port_descriptors[DYSONCOMPRESS_RELEASE_TIME] =
		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
		port_names[DYSONCOMPRESS_RELEASE_TIME] =
		 D_("Release time (s)");
		port_range_hints[DYSONCOMPRESS_RELEASE_TIME].HintDescriptor =
		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_LOW;
		port_range_hints[DYSONCOMPRESS_RELEASE_TIME].LowerBound = 0;
		port_range_hints[DYSONCOMPRESS_RELEASE_TIME].UpperBound = 1;

		/* Parameters for Fast compression ratio */
		port_descriptors[DYSONCOMPRESS_CFRATE] =
		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
		port_names[DYSONCOMPRESS_CFRATE] =
		 D_("Fast compression ratio");
		port_range_hints[DYSONCOMPRESS_CFRATE].HintDescriptor =
		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_MIDDLE;
		port_range_hints[DYSONCOMPRESS_CFRATE].LowerBound = 0;
		port_range_hints[DYSONCOMPRESS_CFRATE].UpperBound = 1;

		/* Parameters for Compression ratio */
		port_descriptors[DYSONCOMPRESS_CRATE] =
		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
		port_names[DYSONCOMPRESS_CRATE] =
		 D_("Compression ratio");
		port_range_hints[DYSONCOMPRESS_CRATE].HintDescriptor =
		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_MIDDLE;
		port_range_hints[DYSONCOMPRESS_CRATE].LowerBound = 0;
		port_range_hints[DYSONCOMPRESS_CRATE].UpperBound = 1;

		/* Parameters for Input */
		port_descriptors[DYSONCOMPRESS_INPUT] =
		 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
		port_names[DYSONCOMPRESS_INPUT] =
		 D_("Input");
		port_range_hints[DYSONCOMPRESS_INPUT].HintDescriptor = 0;

		/* Parameters for Output */
		port_descriptors[DYSONCOMPRESS_OUTPUT] =
		 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
		port_names[DYSONCOMPRESS_OUTPUT] =
		 D_("Output");
		port_range_hints[DYSONCOMPRESS_OUTPUT].HintDescriptor = 0;

		dysonCompressDescriptor->activate = activateDysonCompress;
		dysonCompressDescriptor->cleanup = cleanupDysonCompress;
		dysonCompressDescriptor->connect_port = connectPortDysonCompress;
		dysonCompressDescriptor->deactivate = NULL;
		dysonCompressDescriptor->instantiate = instantiateDysonCompress;
		dysonCompressDescriptor->run = runDysonCompress;
		dysonCompressDescriptor->run_adding = runAddingDysonCompress;
		dysonCompressDescriptor->set_run_adding_gain = setRunAddingGainDysonCompress;
	}
}

void _fini() {
	if (dysonCompressDescriptor) {
		free((LADSPA_PortDescriptor *)dysonCompressDescriptor->PortDescriptors);
		free((char **)dysonCompressDescriptor->PortNames);
		free((LADSPA_PortRangeHint *)dysonCompressDescriptor->PortRangeHints);
		free(dysonCompressDescriptor);
	}

}