forked from swh/ladspa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookahead_limiter_const_1906.xml
204 lines (168 loc) · 6.11 KB
/
lookahead_limiter_const_1906.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
<?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>
#include "ladspa-util.h"
#include "util/db.h"
/* Minimum buffer size in seconds */
#define BUFFER_TIME 0.15f
</code>
</global>
<plugin label="lookaheadLimiterConst" id="1906" class="LimiterPlugin">
<name>Lookahead limiter (fixed latency)</name>
<p>A lookahead limiter - similar to the original Lookahead Limiter, but
with a constant latency of around 150ms and a reduacued maximum lookahead
time.</p>
<callback event="instantiate"><![CDATA[
buffer_len = 4096;
buffer_pos = 0;
fs = s_rate;
db_init();
/* Find size for power-of-two interleaved delay buffer */
while(buffer_len < s_rate * BUFFER_TIME) {
buffer_len *= 2;
}
buffer_mask = buffer_len * 2 - 1;
buffer = calloc(buffer_len * 2, sizeof(LADSPA_Data));
amp_buffer = calloc(buffer_len, sizeof(float));
peak = 0.0f;
peak_dist = 1;
atten = 0.0f;
last_delay = -1.0f;
]]></callback>
<callback event="activate"><![CDATA[
int i;
memset(buffer, 0, buffer_len * 2 * sizeof(float));
for (i=0; i<buffer_len; i++) amp_buffer[i] = 1.0f;
buffer_pos = 0;
peak = 0.0f;
peak_dist = 1;
atten = 0.0f;
last_delay = -1.0f;
]]></callback>
<callback event="run"><![CDATA[
unsigned long pos;
const float max = DB_CO(limit);
float sig, gain;
float delay = last_delay;
float delay_delta;
float a, b;
if (delay < 0.0f) {
delay = delay_s * fs;
delay_delta = 0.0f;
} else {
delay_delta = (delay_s * fs - last_delay) / (sample_count - 1);
}
for (pos = 0; pos < sample_count; pos++) {
delay += delay_delta;
buffer[(buffer_pos * 2) & buffer_mask] = in_1[pos];
buffer[(buffer_pos * 2 + 1) & buffer_mask] = in_2[pos];
a = fabs(buffer[((buffer_pos + f_round(delay)) * 2) & buffer_mask]);
b = fabs(buffer[((buffer_pos + f_round(delay)) * 2 + 1) & buffer_mask]);
sig = a > b ? a : b;
/* XXX
sig = fabs(in_1[pos]) > fabs(in_2[pos]) ? fabs(in_1[pos]) :
fabs(in_2[pos]);
*/
if (sig > max) {
const float rel = lin2db(sig) - limit;
if (rel / delay > peak / (float)peak_dist) {
peak_dist = delay;
peak = rel;
}
}
/* Incremenatlly approach the correct attenuation for the next peak */
atten -= (atten - peak) / (float)(peak_dist + 1);
if (peak_dist-- == 0) {
peak_dist = f_round(delay);
peak = 0.0f;
}
/* Cacluate the apropriate gain reduction and write it back into the
* buffer */
gain = amp_buffer[(buffer_pos - f_round(delay)) & (buffer_len - 1)];
amp_buffer[(buffer_pos - f_round(delay)) & (buffer_len - 1)] =
1.0f / db2lin(atten);
gain=1.0f / db2lin(atten);
buffer_write(out_1[pos], buffer[(2 * (buffer_pos + 1)) &
buffer_mask] * gain);
buffer_write(out_2[pos], buffer[(2 * (buffer_pos + 1)+1) &
buffer_mask] * gain);
/* Ensure that the signal really can't be over the limit */
#if 0
XXX FIXME XXX
if (out_1[pos] < -max) {
buffer_write(out_1[pos], -max);
} else if (out_1[pos] > max) {
buffer_write(out_1[pos], max);
}
if (out_2[pos] < -max) {
buffer_write(out_2[pos], -max);
} else if (out_2[pos] > max) {
buffer_write(out_2[pos], max);
}
#endif
buffer_pos++;
}
plugin_data->buffer_pos = buffer_pos;
plugin_data->peak = peak;
plugin_data->peak_dist = peak_dist;
plugin_data->atten = atten;
plugin_data->last_delay = delay;
*(plugin_data->attenuation) = atten;
*(plugin_data->latency) = buffer_len - 1;
]]></callback>
<callback event="cleanup"><![CDATA[
free(plugin_data->buffer);
free(plugin_data->amp_buffer);
]]></callback>
<port label="limit" dir="input" type="control" hint="default_0">
<name>Limit (dB)</name>
<p>The maximum output amplitude. Peaks over this level will be attenuated as smoothly as possible to bring them as close as possible to this level.</p>
<range min="-20" max="0"/>
</port>
<port label="delay_s" dir="input" type="control" hint="default_middle">
<name>Lookahead time (s)</name>
<p>The lookahead time used by the lookahead predictor. The longer the time the smoother the limiting will be, but will tend to make the changes in dynamic range more obvious.</p>
<range min="0.001" max="0.15"/>
</port>
<port label="attenuation" dir="output" type="control">
<name>Attenuation (dB)</name>
<p>The current limiting attenuation of the signal coming out of the delay
buffer.</p>
<range min="0" max="12"/>
</port>
<port label="in_1" dir="input" type="audio">
<name>Input 1</name>
</port>
<port label="in_2" dir="input" type="audio">
<name>Input 2</name>
</port>
<port label="out_1" dir="output" type="audio">
<name>Output 1</name>
</port>
<port label="out_2" dir="output" type="audio">
<name>Output 2</name>
</port>
<port label="latency" dir="output" type="control">
<name>latency</name>
</port>
<instance-data label="buffer" type="LADSPA_Data *" />
<instance-data label="amp_buffer" type="float *" />
<instance-data label="buffer_len" type="unsigned int" />
<instance-data label="buffer_mask" type="unsigned int" />
<instance-data label="buffer_pos" type="unsigned int" />
<instance-data label="fs" type="unsigned int" />
<!-- running value for the attenuation -->
<instance-data label="atten" type="float" />
<!-- the next peak, relative to the limit -->
<instance-data label="peak" type="float" />
<!-- the number of sample until the next peak -->
<instance-data label="peak_dist" type="unsigned int" />
<instance-data label="last_delay" type="float" />
</plugin>
</ladspa>