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
|
<?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 <steve@plugin.org.uk>"/>
<meta name="copyright" value="GPL"/>
<meta name="properties" value="HARD_RT_CAPABLE"/>
<code>
#include "ladspa-util.h"
//#define DEBUG
#define NUM_CHUNKS 16
#define BUFFER_TIME 0.0053
#ifdef DEBUG
#include "stdio.h"
#endif
</code>
</global>
<plugin label="fastLookaheadLimiter" id="1913" class="LimiterPlugin">
<name>Fast Lookahead limiter</name>
<p>This is a limiter with an attack time of 5ms. It adds just over 5ms of
lantecy to the input signal, but it guatantees that there will be no signals
over the limit, and tries to get the minimum ammount of distortion.</p>
<callback event="instantiate"><![CDATA[
fs = s_rate;
buffer_len = 128;
buffer_pos = 0;
/* Find size for power-of-two interleaved delay buffer */
while(buffer_len < fs * BUFFER_TIME * 2) {
buffer_len *= 2;
}
buffer = calloc(buffer_len, sizeof(float));
delay = (int)(0.005 * fs);
chunk_pos = 0;
chunk_num = 0;
/* find a chunk size (in smaples) thats roughly 0.5ms */
chunk_size = s_rate / 2000;
chunks = calloc(NUM_CHUNKS, sizeof(float));
peak = 0.0f;
atten = 1.0f;
atten_lp = 1.0f;
delta = 0.0f;
]]></callback>
<callback event="activate"><![CDATA[
memset(buffer, 0, NUM_CHUNKS * sizeof(float));
chunk_pos = 0;
chunk_num = 0;
peak = 0.0f;
atten = 1.0f;
atten_lp = 1.0f;
delta = 0.0f;
]]></callback>
<callback event="run"><![CDATA[
unsigned long pos;
const float max = DB_CO(limit);
const float trim = DB_CO(ingain);
float sig;
unsigned int i;
#ifdef DEBUG
float clip = 0.0, clipp = 0.0;
int clipc = 0;
#endif
for (pos = 0; pos < sample_count; pos++) {
if (chunk_pos++ == chunk_size) {
/* we've got a full chunk */
delta = (1.0f - atten) / (fs * release);
round_to_zero(&delta);
for (i=0; i<10; i++) {
const int p = (chunk_num - 9 + i) & (NUM_CHUNKS - 1);
const float this_delta = (max / chunks[p] - atten) /
((float)(i) * fs * 0.0005f + 1.0f);
if (this_delta < delta) {
delta = this_delta;
}
}
chunks[chunk_num++ & (NUM_CHUNKS - 1)] = peak;
peak = 0.0f;
chunk_pos = 0;
}
buffer[(buffer_pos * 2) & (buffer_len - 1)] = in_1[pos] * trim
+ 1.0e-30;
buffer[(buffer_pos * 2 + 1) & (buffer_len - 1)] = in_2[pos] * trim
+ 1.0e-30;
sig = fabs(in_1[pos]) > fabs(in_2[pos]) ? fabs(in_1[pos]) :
fabs(in_2[pos]);
sig += 1.0e-30;
if (sig * trim > peak) {
peak = sig * trim;
}
//round_to_zero(&peak);
//round_to_zero(&sig);
atten += delta;
atten_lp = atten * 0.1f + atten_lp * 0.9f;
//round_to_zero(&atten_lp);
if (delta > 0.0f && atten > 1.0f) {
atten = 1.0f;
delta = 0.0f;
}
buffer_write(out_1[pos], buffer[(buffer_pos * 2 - delay * 2) &
(buffer_len - 1)] * atten_lp);
buffer_write(out_2[pos], buffer[(buffer_pos * 2 - delay * 2 + 1) &
(buffer_len - 1)] * atten_lp);
round_to_zero(&out_1[pos]);
round_to_zero(&out_2[pos]);
if (out_1[pos] < -max) {
#ifdef DEBUG
clip += 20.0*log10(out_1[pos] / -max);
clipc++;
if (fabs(out_1[pos] - max) > clipp) {
clipp = fabs(out_1[pos] / -max);
}
#endif
buffer_write(out_1[pos], -max);
} else if (out_1[pos] > max) {
#ifdef DEBUG
clip += 20.0*log10(out_1[pos] / max);
clipc++;
if (fabs(out_1[pos] - max) > clipp) {
clipp = fabs(out_1[pos] / max);
}
#endif
buffer_write(out_1[pos], max);
}
if (out_2[pos] < -max) {
#ifdef DEBUG
clip += 20.0*log10(out_2[pos] / -max);
clipc++;
if (fabs(out_2[pos] - max) > clipp) {
clipp = fabs(out_2[pos] / -max);
}
#endif
buffer_write(out_2[pos], -max);
} else if (out_2[pos] > max) {
#ifdef DEBUG
clip += 20.0*log10(out_2[pos] / max);
clipc++;
if (fabs(out_2[pos] - max) > clipp) {
clipp = fabs(out_2[pos] / max);
}
#endif
buffer_write(out_2[pos], max);
}
buffer_pos++;
}
#ifdef DEBUG
if (clipc > 0) {
printf("%d overs: %fdB avg, %fdB peak\n", clipc, clip/(float)clipc, 20.0*log10(clipp));
}
#endif
plugin_data->buffer_pos = buffer_pos;
plugin_data->peak = peak;
plugin_data->atten = atten;
plugin_data->atten_lp = atten_lp;
plugin_data->chunk_pos = chunk_pos;
plugin_data->chunk_num = chunk_num;
*(plugin_data->attenuation) = -CO_DB(atten);
*(plugin_data->latency) = delay;
]]></callback>
<callback event="cleanup"><![CDATA[
free(plugin_data->buffer);
]]></callback>
<port label="ingain" dir="input" type="control" hint="default_0">
<name>Input gain (dB)</name>
<p>Gain that is applied to the input stage. Can be used to trim gain to
bring it roughly under the limit or to push the signal against the limit.</p>
<range min="-20" max="20"/>
</port>
<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="release" dir="input" type="control" hint="default_low">
<name>Release time (s)</name>
<p>The time taken for the limiters attenuation to return to 0 dB's</p>
<range min="0.01" max="2.0"/>
</port>
<port label="attenuation" dir="output" type="control">
<name>Attenuation (dB)</name>
<p>The current attenuation of the signal coming out of the delay buffer.</p>
<range min="0" max="70"/>
</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="buffer_len" type="unsigned int" />
<instance-data label="buffer_pos" type="unsigned int" />
<instance-data label="fs" type="unsigned int" />
<instance-data label="atten" type="float" />
<instance-data label="atten_lp" type="float" />
<instance-data label="peak" type="float" />
<instance-data label="delta" type="float" />
<instance-data label="delay" type="unsigned int" />
<instance-data label="chunk_num" type="unsigned int" />
<instance-data label="chunk_pos" type="unsigned int" />
<instance-data label="chunk_size" type="unsigned int" />
<instance-data label="chunks" type="float *" />
</plugin>
</ladspa>
|