-
Notifications
You must be signed in to change notification settings - Fork 52
/
dyson_compress_1403.xml
371 lines (310 loc) · 11 KB
/
dyson_compress_1403.xml
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
<?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 <[email protected]>"/>
<meta name="copyright" value="GPL"/>
<meta name="properties" value="HARD_RT_CAPABLE"/>
<code><![CDATA[
/*
* 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: [email protected]
*
* 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;
}
]]></code>
</global>
<plugin label="dysonCompress" id="1403" class="CompressorPlugin">
<name>Dyson compressor</name>
<callback event="instantiate"><![CDATA[
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;
]]></callback>
<callback event="activate"><![CDATA[
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;
]]></callback>
<callback event="cleanup"><![CDATA[
free(plugin_data->delay);
free(plugin_data->rlevelsqn);
free(plugin_data->rlevelsqe);
]]></callback>
<callback event="run"><![CDATA[
unsigned long pos;
float targetlevel = MAXLEVEL * DB_CO(peak_limit);
float rgainfilter = 1.0f / (fmaxf(release_time, 0.0000001f) * 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;
]]></callback>
<port label="peak_limit" dir="input" type="control" hint="default_0">
<name>Peak limit (dB)</name>
<p>Controls the desired limit of the output signal in dB's.</p>
<range min="-30" max="0"/>
</port>
<port label="release_time" dir="input" type="control" hint="default_low">
<name>Release time (s)</name>
<p>Controls the time taken for the compressor to relax its gain control over the input signal.</p>
<range min="0" max="1"/>
</port>
<port label="cfrate" dir="input" type="control" hint="default_middle">
<name>Fast compression ratio</name>
<p>I have no clear idea what this controls.</p>
<range min="0" max="1"/>
</port>
<port label="crate" dir="input" type="control" hint="default_middle">
<name>Compression ratio</name>
<p>I have no clear idea what this controls.</p>
<range min="0" max="1"/>
</port>
<port label="input" dir="input" type="audio">
<name>Input</name>
</port>
<port label="output" dir="output" type="audio">
<name>Output</name>
</port>
<instance-data label="sample_rate" type="float"/>
<instance-data label="mingain" type="float"/>
<instance-data label="maxgain" type="float"/>
<instance-data label="rpeaklimitdelay" type="float"/>
<instance-data label="rgain" type="float"/>
<instance-data label="rlevelsq0" type="float"/>
<instance-data label="rlevelsq1" type="float"/>
<instance-data label="ndelay" type="float"/>
<instance-data label="delay" type="LADSPA_Data *"/>
<instance-data label="rlevelsqn" type="LADSPA_Data *"/>
<instance-data label="rlevelsqe" type="LADSPA_Data *"/>
<instance-data label="rmastergain0" type="float"/>
<instance-data label="rpeakgain0" type="float"/>
<instance-data label="rpeakgain1" type="float"/>
<instance-data label="peaklimitdelay" type="int"/>
<instance-data label="ndelayptr" type="unsigned int"/>
<instance-data label="lastrgain" type="float"/>
<instance-data label="extra_maxlevel" type="float"/>
</plugin>
</ladspa>