Vocoder: bugfixes
[calf.git] / src / modules_limit.cpp
blobdbe50b00f6f57574e84c0aa96e7ca4f846f339eb
1 /* Calf DSP plugin pack
2 * Limiter related plugins
4 * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 #include <limits.h>
22 #include <memory.h>
23 #include <calf/giface.h>
24 #include <calf/audio_fx.h>
25 #include <calf/modules_limit.h>
27 using namespace dsp;
28 using namespace calf_plugins;
30 #define SET_IF_CONNECTED(name) if (params[AM::param_##name] != NULL) *params[AM::param_##name] = name;
32 /**********************************************************************
33 * LIMITER by Christian Holschuh and Markus Schmidt
34 **********************************************************************/
36 limiter_audio_module::limiter_audio_module()
38 is_active = false;
39 srate = 0;
40 asc_led = 0.f;
41 attack_old = -1.f;
42 limit_old = -1.f;
43 oversampling_old = -1;
44 asc_old = true;
47 void limiter_audio_module::activate()
49 is_active = true;
50 // set all filters and strips
51 params_changed();
52 limiter.activate();
55 void limiter_audio_module::deactivate()
57 is_active = false;
58 limiter.deactivate();
60 void limiter_audio_module::set_srates()
62 resampler[0].set_params(srate, *params[param_oversampling], 2);
63 resampler[1].set_params(srate, *params[param_oversampling], 2);
64 limiter.set_sample_rate(srate * *params[param_oversampling]);
66 void limiter_audio_module::params_changed()
68 limiter.set_params(*params[param_limit], *params[param_attack], *params[param_release], 1.f, *params[param_asc], pow(0.5, (*params[param_asc_coeff] - 0.5) * 2 * -1), true);
69 if( *params[param_attack] != attack_old) {
70 attack_old = *params[param_attack];
71 limiter.reset();
73 if(*params[param_limit] != limit_old or *params[param_asc] != asc_old) {
74 asc_old = *params[param_asc];
75 limit_old = *params[param_limit];
76 limiter.reset_asc();
78 if (oversampling_old != *params[param_oversampling]) {
79 oversampling_old = *params[param_oversampling];
80 set_srates();
84 void limiter_audio_module::set_sample_rate(uint32_t sr)
86 srate = sr;
87 int meter[] = {param_meter_inL, param_meter_inR, param_meter_outL, param_meter_outR, -param_att};
88 int clip[] = {param_clip_inL, param_clip_inR, param_clip_outL, param_clip_outR, -1};
89 meters.init(params, meter, clip, 5, srate);
90 set_srates();
93 uint32_t limiter_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask)
95 bool bypass = *params[param_bypass] > 0.5f;
96 numsamples += offset;
97 if(bypass) {
98 // everything bypassed
99 while(offset < numsamples) {
100 outs[0][offset] = ins[0][offset];
101 outs[1][offset] = ins[1][offset];
102 float values[] = {0, 0, 0, 0, 1};
103 meters.process(values);
104 ++offset;
106 asc_led = 0.f;
107 } else {
108 asc_led -= std::min(asc_led, numsamples);
110 while(offset < numsamples) {
111 // cycle through samples
112 float inL = ins[0][offset];
113 float inR = ins[1][offset];
114 // in level
115 inR *= *params[param_level_in];
116 inL *= *params[param_level_in];
117 // out vars
118 float outL = inL;
119 float outR = inR;
121 // upsampling
122 double *samplesL = resampler[0].upsample((double)outL);
123 double *samplesR = resampler[1].upsample((double)outR);
125 float tmpL;
126 float tmpR;
128 // process gain reduction
129 float fickdich[0];
130 for (int i = 0; i < *params[param_oversampling]; i ++) {
131 tmpL = samplesL[i];
132 tmpR = samplesR[i];
133 limiter.process(tmpL, tmpR, fickdich);
134 samplesL[i] = tmpL;
135 samplesR[i] = tmpR;
136 if(limiter.get_asc())
137 asc_led = srate >> 3;
140 // downsampling
141 outL = resampler[0].downsample(samplesL);
142 outR = resampler[1].downsample(samplesR);
144 // should never be used. but hackers are paranoid by default.
145 // so we make shure NOTHING is above limit
146 outL = std::min(std::max(outL, -*params[param_limit]), *params[param_limit]);
147 outR = std::min(std::max(outR, -*params[param_limit]), *params[param_limit]);
149 // autolevel
150 outL /= *params[param_limit];
151 outR /= *params[param_limit];
153 // out level
154 outL *= *params[param_level_out];
155 outR *= *params[param_level_out];
157 // send to output
158 outs[0][offset] = outL;
159 outs[1][offset] = outR;
161 float values[] = {inL, inR, outL, outR, bypass > 0.5 ? (float)1.0 : (float)limiter.get_attenuation()};
162 meters.process (values);
164 // next sample
165 ++offset;
166 } // cycle trough samples
167 } // process (no bypass)
168 meters.fall(numsamples);
169 if (params[param_asc_led] != NULL) *params[param_asc_led] = asc_led;
170 return outputs_mask;
173 /**********************************************************************
174 * MULTIBAND LIMITER by Markus Schmidt and Christian Holschuh
175 **********************************************************************/
177 multibandlimiter_audio_module::multibandlimiter_audio_module()
179 srate = 0;
180 _mode = 0;
181 over = 1;
182 buffer_size = 0;
183 overall_buffer_size = 0;
184 channels = 2;
185 asc_led = 0.f;
186 attack_old = -1.f;
187 oversampling_old = -1.f;
188 limit_old = -1.f;
189 asc_old = true;
190 _sanitize = false;
191 is_active = false;
192 cnt = 0;
194 for(int i = 0; i < strips; i ++) {
195 weight_old[i] = -1.f;
198 crossover.init(2, 4, 44100);
200 multibandlimiter_audio_module::~multibandlimiter_audio_module()
202 free(buffer);
204 void multibandlimiter_audio_module::activate()
206 is_active = true;
207 // set all filters and strips
208 params_changed();
209 // activate all strips
210 for (int j = 0; j < strips; j ++) {
211 strip[j].activate();
212 strip[j].set_multi(true);
213 strip[j].id = j;
215 broadband.activate();
216 pos = 0;
219 void multibandlimiter_audio_module::deactivate()
221 is_active = false;
222 // deactivate all strips
223 for (int j = 0; j < strips; j ++) {
224 strip[j].deactivate();
226 broadband.deactivate();
229 void multibandlimiter_audio_module::params_changed()
231 // determine mute/solo states
232 solo[0] = *params[param_solo0] > 0.f ? true : false;
233 solo[1] = *params[param_solo1] > 0.f ? true : false;
234 solo[2] = *params[param_solo2] > 0.f ? true : false;
235 solo[3] = *params[param_solo3] > 0.f ? true : false;
236 no_solo = (*params[param_solo0] > 0.f ||
237 *params[param_solo1] > 0.f ||
238 *params[param_solo2] > 0.f ||
239 *params[param_solo3] > 0.f) ? false : true;
241 int m = *params[param_mode];
242 if (m != _mode) {
243 _mode = *params[param_mode];
246 crossover.set_mode(_mode + 1);
247 crossover.set_filter(0, *params[param_freq0]);
248 crossover.set_filter(1, *params[param_freq1]);
249 crossover.set_filter(2, *params[param_freq2]);
251 // set the params of all strips
252 float rel;
253 for (int i = 0; i < strips; i++) {
254 rel = *params[param_release] * pow(0.25, *params[param_release0 + i] * -1);
255 rel = (*params[param_minrel] > 0.5) ? std::max(2500 * (1.f / (i ? *params[param_freq0 + i - 1] : 30)), rel) : rel;
256 weight[i] = pow(0.25, *params[param_weight0 + i] * -1);
257 strip[i].set_params(*params[param_limit], *params[param_attack], rel, weight[i], *params[param_asc], pow(0.5, (*params[param_asc_coeff] - 0.5) * 2 * -1), false);
258 *params[param_effrelease0 + i] = rel;
261 // set broadband limiter
262 broadband.set_params(*params[param_limit], *params[param_attack], rel, 1.f, *params[param_asc], pow(0.5, (*params[param_asc_coeff] - 0.5) * 2 * -1));
264 if (over != *params[param_oversampling]) {
265 over = *params[param_oversampling];
266 set_srates();
269 // rebuild multiband buffer
270 if( *params[param_attack] != attack_old or *params[param_oversampling] != oversampling_old) {
271 int bs = (int)(srate * (*params[param_attack] / 1000.f) * channels * *params[param_oversampling]);
272 buffer_size = bs - bs % (channels * (int)*params[param_oversampling]); // buffer size attack rate
273 attack_old = *params[param_attack];
274 oversampling_old = *params[param_oversampling];
275 _sanitize = true;
276 pos = 0;
277 for (int j = 0; j < strips; j ++) {
278 strip[j].reset();
280 broadband.reset();
282 if(*params[param_limit] != limit_old or *params[param_asc] != asc_old or *params[param_weight0] != weight_old[0] or *params[param_weight1] != weight_old[1] or *params[param_weight2] != weight_old[2] or *params[param_weight3] != weight_old[3] ) {
283 asc_old = *params[param_asc];
284 limit_old = *params[param_limit];
285 for (int j = 0; j < strips; j ++) {
286 weight_old[j] = *params[param_weight0 + j];
287 strip[j].reset_asc();
289 broadband.reset_asc();
293 void multibandlimiter_audio_module::set_sample_rate(uint32_t sr)
295 srate = sr;
296 // rebuild buffer
297 overall_buffer_size = (int)(srate * (100.f / 1000.f) * channels * 16) + channels * 16; // buffer size max attack rate
298 buffer = (float*) calloc(overall_buffer_size, sizeof(float));
299 pos = 0;
300 set_srates();
301 int meter[] = {param_meter_inL, param_meter_inR, param_meter_outL, param_meter_outR, -param_att0, -param_att1, -param_att2, -param_att3};
302 int clip[] = {param_clip_inL, param_clip_inR, param_clip_outL, param_clip_outR, -1, -1, -1, -1};
303 meters.init(params, meter, clip, 8, srate);
306 void multibandlimiter_audio_module::set_srates()
308 broadband.set_sample_rate(srate * over);
309 crossover.set_sample_rate(srate);
310 for (int j = 0; j < strips; j ++) {
311 strip[j].set_sample_rate(srate * over);
312 resampler[j][0].set_params(srate, over, 2);
313 resampler[j][1].set_params(srate, over, 2);
317 #define BYPASSED_COMPRESSION(index) \
318 if(params[param_att##index] != NULL) \
319 *params[param_att##index] = 1.0; \
321 #define ACTIVE_COMPRESSION(index) \
322 if(params[param_att##index] != NULL) \
323 *params[param_att##index] = strip[index].get_attenuation(); \
326 uint32_t multibandlimiter_audio_module::process(uint32_t offset, uint32_t numsamples, uint32_t inputs_mask, uint32_t outputs_mask)
328 bool bypass = *params[param_bypass] > 0.5f;
329 numsamples += offset;
330 float batt = 0.f;
331 if(bypass) {
332 // everything bypassed
333 while(offset < numsamples) {
334 outs[0][offset] = ins[0][offset];
335 outs[1][offset] = ins[1][offset];
336 float values[] = {0, 0, 0, 0, 1, 1, 1, 1};
337 meters.process(values);
338 ++offset;
340 asc_led = 0.f;
341 } else {
342 // process all strips
343 asc_led -= std::min(asc_led, numsamples);
344 while(offset < numsamples) {
345 float inL = 0.f; // input
346 float inR = 0.f;
347 float outL = 0.f; // final output
348 float outR = 0.f;
349 float tmpL = 0.f; // used for temporary purposes
350 float tmpR = 0.f;
351 double overL[strips * 16];
352 double overR[strips * 16];
353 double resL[16];
354 double resR[16];
356 bool asc_active = false;
358 // cycle through samples
359 if(!_sanitize) {
360 inL = ins[0][offset];
361 inR = ins[1][offset];
363 // in level
364 inR *= *params[param_level_in];
365 inL *= *params[param_level_in];
367 //if(!(cnt%50)) printf("i: %.5f\n", inL);
369 // process crossover
370 float xin[] = {inL, inR};
371 crossover.process(xin);
373 // cycle over strips
374 for (int i = 0; i < strips; i++) {
375 // upsample
376 double *samplesL = resampler[i][0].upsample((double)crossover.get_value(0, i));
377 double *samplesR = resampler[i][1].upsample((double)crossover.get_value(1, i));
378 // copy to cache
379 memcpy(&overL[i * 16], samplesL, sizeof(double) * over);
380 memcpy(&overR[i * 16], samplesR, sizeof(double) * over);
381 //if(!(cnt%200)) printf("u0: %.5f\n", overL[i*16]);
384 // cycle over upsampled samples
385 for (int o = 0; o < over; o++) {
386 tmpL = 0.f;
387 tmpR = 0.f;
388 resL[o] = 0;
389 resR[o] = 0;
391 // cycle over strips for multiband coefficient
392 for (int i = 0; i < strips; i++) {
393 // sum up for multiband coefficient
394 int p = i * 16 + o;
395 tmpL += ((fabs(overL[p]) > *params[param_limit]) ? *params[param_limit] * (fabs(overL[p]) / overL[p]) : overL[p]) * weight[i];
396 tmpR += ((fabs(overR[p]) > *params[param_limit]) ? *params[param_limit] * (fabs(overR[p]) / overR[p]) : overR[p]) * weight[i];
399 // write multiband coefficient to buffer
400 buffer[pos] = std::min(*params[param_limit] / std::max(fabs(tmpL), fabs(tmpR)), 1.0);
402 // step forward in multiband buffer
403 pos = (pos + channels) % buffer_size;
404 if(pos == 0) _sanitize = false;
406 // limit and add up strips
407 for (int i = 0; i < strips; i++) {
408 int p = i * 16 + o;
409 //if(!(cnt%200) and !o) printf("u1: %.5f\n", overL[p]);
410 // limit
411 tmpL = (float)overL[p];
412 tmpR = (float)overR[p];
413 //if(!(cnt%200)) printf("1: %.5f\n", tmpL);
414 strip[i].process(tmpL, tmpR, buffer);
415 //if(!(cnt%200)) printf("2: %.5f\n\n", tmpL);
416 if (solo[i] || no_solo) {
417 // add
418 resL[o] += (double)tmpL;
419 resR[o] += (double)tmpR;
420 // flash the asc led?
421 asc_active = asc_active || strip[i].get_asc();
425 // process broadband limiter
426 float fickdich[0];
427 tmpL = resL[o];
428 tmpR = resR[o];
429 broadband.process(tmpL, tmpR, fickdich);
430 resL[o] = (double)tmpL;
431 resR[o] = (double)tmpR;
432 asc_active = asc_active || broadband.get_asc();
435 // downsampling
436 outL = (float)resampler[0][0].downsample(resL);
437 outR = (float)resampler[0][1].downsample(resR);
439 //if(!(cnt%50)) printf("o: %.5f %.5f\n\n", outL, resL[0]);
441 // should never be used. but hackers are paranoid by default.
442 // so we make shure NOTHING is above limit
443 outL = std::min(std::max(outL, -*params[param_limit]), *params[param_limit]);
444 outR = std::min(std::max(outR, -*params[param_limit]), *params[param_limit]);
446 // light led
447 if(asc_active) {
448 asc_led = srate >> 3;
451 // autolevel
452 outL /= *params[param_limit];
453 outR /= *params[param_limit];
455 // out level
456 outL *= *params[param_level_out];
457 outR *= *params[param_level_out];
459 // send to output
460 outs[0][offset] = outL;
461 outs[1][offset] = outR;
463 // next sample
464 ++offset;
466 batt = broadband.get_attenuation();
468 float values[] = {inL, inR, outL, outR,
469 bypass > 0.5 ? (float)1.0 : (float)strip[0].get_attenuation() * batt,
470 bypass > 0.5 ? (float)1.0 : (float)strip[1].get_attenuation() * batt,
471 bypass > 0.5 ? (float)1.0 : (float)strip[2].get_attenuation() * batt,
472 bypass > 0.5 ? (float)1.0 : (float)strip[3].get_attenuation() * batt};
473 meters.process(values);
477 //for (int i = 0; i < strips; i++) {
478 //left = crossover.get_value(0, i);
479 //right = crossover.get_value(1, i);
481 //// remember filtered values for limiting
482 //// (we need multiband_coeff before we can call the limiter bands)
483 //_tmpL[i] = left;
484 //_tmpR[i] = right;
486 //// sum up for multiband coefficient
487 //sum_left += ((fabs(left) > *params[param_limit]) ? *params[param_limit] * (fabs(left) / left) : left) * weight[i];
488 //sum_right += ((fabs(right) > *params[param_limit]) ? *params[param_limit] * (fabs(right) / right) : right) * weight[i];
489 //} // process single strip with filter
491 //// write multiband coefficient to buffer
492 //buffer[pos] = std::min(*params[param_limit] / std::max(fabs(sum_left), fabs(sum_right)), 1.0);
494 //for (int i = 0; i < strips; i++) {
495 //// process gain reduction
496 //strip[i].process(_tmpL[i], _tmpR[i], buffer);
497 //// sum up output of limiters
498 //if (solo[i] || no_solo) {
499 //outL += _tmpL[i];
500 //outR += _tmpR[i];
502 //asc_active = asc_active || strip[i].get_asc();
503 //} // process single strip again for limiter
504 //float fickdich[0];
505 //broadband.process(outL, outR, fickdich);
506 //asc_active = asc_active || broadband.get_asc();
508 //// should never be used. but hackers are paranoid by default.
509 //// so we make shure NOTHING is above limit
510 //outL = std::max(outL, -*params[param_limit]);
511 //outL = std::min(outL, *params[param_limit]);
512 //outR = std::max(outR, -*params[param_limit]);
513 //outR = std::min(outR, *params[param_limit]);
515 //if(asc_active) {
516 //asc_led = srate >> 3;
518 cnt++;
519 } // cycle trough samples
520 } // process (no bypass)
521 if (params[param_asc_led] != NULL) *params[param_asc_led] = asc_led;
522 meters.fall(numsamples);
523 return outputs_mask;
526 bool multibandlimiter_audio_module::get_graph(int index, int subindex, int phase, float *data, int points, cairo_iface *context, int *mode) const
528 return crossover.get_graph(subindex, phase, data, points, context, mode);
530 bool multibandlimiter_audio_module::get_layers(int index, int generation, unsigned int &layers) const
532 return crossover.get_layers(index, generation, layers);