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
23 #include <calf/giface.h>
24 #include <calf/audio_fx.h>
25 #include <calf/modules_limit.h>
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()
43 oversampling_old
= -1;
47 void limiter_audio_module::activate()
50 // set all filters and strips
55 void limiter_audio_module::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
];
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
];
78 if (oversampling_old
!= *params
[param_oversampling
]) {
79 oversampling_old
= *params
[param_oversampling
];
84 void limiter_audio_module::set_sample_rate(uint32_t 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
);
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
;
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
);
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
];
115 inR
*= *params
[param_level_in
];
116 inL
*= *params
[param_level_in
];
122 double *samplesL
= resampler
[0].upsample((double)outL
);
123 double *samplesR
= resampler
[1].upsample((double)outR
);
128 // process gain reduction
130 for (int i
= 0; i
< *params
[param_oversampling
]; i
++) {
133 limiter
.process(tmpL
, tmpR
, fickdich
);
136 if(limiter
.get_asc())
137 asc_led
= srate
>> 3;
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
]);
150 outL
/= *params
[param_limit
];
151 outR
/= *params
[param_limit
];
154 outL
*= *params
[param_level_out
];
155 outR
*= *params
[param_level_out
];
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
);
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
;
173 /**********************************************************************
174 * MULTIBAND LIMITER by Markus Schmidt and Christian Holschuh
175 **********************************************************************/
177 multibandlimiter_audio_module::multibandlimiter_audio_module()
183 overall_buffer_size
= 0;
187 oversampling_old
= -1.f
;
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()
204 void multibandlimiter_audio_module::activate()
207 // set all filters and strips
209 // activate all strips
210 for (int j
= 0; j
< strips
; j
++) {
212 strip
[j
].set_multi(true);
215 broadband
.activate();
219 void multibandlimiter_audio_module::deactivate()
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
];
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
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
];
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
];
277 for (int j
= 0; j
< strips
; j
++) {
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
)
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));
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
;
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
);
342 // process all strips
343 asc_led
-= std::min(asc_led
, numsamples
);
344 while(offset
< numsamples
) {
345 float inL
= 0.f
; // input
347 float outL
= 0.f
; // final output
349 float tmpL
= 0.f
; // used for temporary purposes
351 double overL
[strips
* 16];
352 double overR
[strips
* 16];
356 bool asc_active
= false;
358 // cycle through samples
360 inL
= ins
[0][offset
];
361 inR
= ins
[1][offset
];
364 inR
*= *params
[param_level_in
];
365 inL
*= *params
[param_level_in
];
367 //if(!(cnt%50)) printf("i: %.5f\n", inL);
370 float xin
[] = {inL
, inR
};
371 crossover
.process(xin
);
374 for (int i
= 0; i
< strips
; i
++) {
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
));
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
++) {
391 // cycle over strips for multiband coefficient
392 for (int i
= 0; i
< strips
; i
++) {
393 // sum up for multiband coefficient
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
++) {
409 //if(!(cnt%200) and !o) printf("u1: %.5f\n", overL[p]);
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
) {
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
429 broadband
.process(tmpL
, tmpR
, fickdich
);
430 resL
[o
] = (double)tmpL
;
431 resR
[o
] = (double)tmpR
;
432 asc_active
= asc_active
|| broadband
.get_asc();
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
]);
448 asc_led
= srate
>> 3;
452 outL
/= *params
[param_limit
];
453 outR
/= *params
[param_limit
];
456 outL
*= *params
[param_level_out
];
457 outR
*= *params
[param_level_out
];
460 outs
[0][offset
] = outL
;
461 outs
[1][offset
] = outR
;
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)
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) {
502 //asc_active = asc_active || strip[i].get_asc();
503 //} // process single strip again for limiter
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]);
516 //asc_led = srate >> 3;
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
);
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
);