2 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include <cairo/cairo.h>
22 #include "ardour/lv2_extensions.h"
25 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
27 #define ACOMP_URI "urn:ardour:a-comp"
28 #define ACOMP_STEREO_URI "urn:ardour:a-comp#stereo"
31 # define M_PI 3.14159265358979323846
82 LV2_Inline_Display_Image_Surface surf;
84 cairo_surface_t* display;
85 LV2_Inline_Display* queue_draw;
88 /* ports pointers are only valid during run so we'll
89 * have to cache them for the display, besides
90 * we do want to check for changes
102 instantiate(const LV2_Descriptor* descriptor,
104 const char* bundle_path,
105 const LV2_Feature* const* features)
107 AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
109 for (int i=0; features[i]; ++i) {
111 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
112 acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
118 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
119 acomp->tau = (1.0 - exp (-2.f * M_PI * 25.f / acomp->srate));
121 acomp->need_expose = true;
124 return (LV2_Handle)acomp;
129 connect_port(LV2_Handle instance,
133 AComp* acomp = (AComp*)instance;
135 switch ((PortIndex)port) {
137 acomp->attack = (float*)data;
140 acomp->release = (float*)data;
143 acomp->knee = (float*)data;
146 acomp->ratio = (float*)data;
148 case ACOMP_THRESHOLD:
149 acomp->thresdb = (float*)data;
152 acomp->makeup = (float*)data;
155 acomp->gainr = (float*)data;
158 acomp->outlevel = (float*)data;
160 case ACOMP_SIDECHAIN:
161 acomp->sidechain = (float*)data;
164 acomp->enable = (float*)data;
167 acomp->input0 = (float*)data;
170 acomp->sc = (float*)data;
173 acomp->output0 = (float*)data;
175 case ACOMP_STEREO_INPUT:
176 acomp->input1 = (float*)data;
178 case ACOMP_STEREO_OUTPUT:
179 acomp->output1 = (float*)data;
184 // Force already-denormal float value to zero
186 sanitize_denormal(float value) {
187 if (!isnormal(value)) {
195 return (exp(gdb/20.f*log(10.f)));
200 return (20.f*log10(g));
204 activate(LV2_Handle instance)
206 AComp* acomp = (AComp*)instance;
208 *(acomp->gainr) = 0.0f;
209 *(acomp->outlevel) = -45.0f;
210 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
214 run_mono(LV2_Handle instance, uint32_t n_samples)
216 AComp* acomp = (AComp*)instance;
218 const float* const input = acomp->input0;
219 const float* const sc = acomp->sc;
220 float* const output = acomp->output0;
222 float srate = acomp->srate;
223 float width = (6.f * *(acomp->knee)) + 0.01;
225 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
226 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
229 float lgaininp = 0.f;
231 float Lxg, Lxl, Lyg, Lyl, Ly1;
232 int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
238 float ratio = *acomp->ratio;
239 float thresdb = *acomp->thresdb;
240 float makeup_target = from_dB(*acomp->makeup);
241 float makeup_gain = acomp->makeup_gain;
243 const const float tau = acomp->tau;
245 if (*acomp->enable <= 0) {
252 if (acomp->v_knee != *acomp->knee) {
253 acomp->v_knee = *acomp->knee;
254 acomp->need_expose = true;
257 if (acomp->v_ratio != ratio) {
258 acomp->v_ratio = ratio;
259 acomp->need_expose = true;
262 if (acomp->v_thresdb != thresdb) {
263 acomp->v_thresdb = thresdb;
264 acomp->need_expose = true;
270 for (i = 0; i < n_samples; i++) {
273 ingain = usesidechain ? fabs(sc0) : fabs(in0);
274 in_peak = fmaxf (in_peak, ingain);
276 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
277 Lxg = sanitize_denormal(Lxg);
280 if (2.f*(Lxg-thresdb) < -width) {
282 } else if (2.f*(Lxg-thresdb) > width) {
283 Lyg = thresdb + (Lxg-thresdb)/ratio;
284 Lyg = sanitize_denormal(Lyg);
286 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
291 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
292 acomp->old_yl = sanitize_denormal(acomp->old_yl);
293 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
294 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
295 Ly1 = sanitize_denormal(Ly1);
296 Lyl = sanitize_denormal(Lyl);
299 Lgain = from_dB(cdb);
301 *(acomp->gainr) = Lyl;
303 lgaininp = in0 * Lgain;
305 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
306 output[i] = lgaininp * makeup_gain;
308 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
310 // TODO re-use local variables on stack
311 // store values back to acomp at the end of the inner-loop
317 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
318 acomp->makeup_gain = makeup_gain;
321 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl); // crude LPF TODO use n_samples/rate TC
322 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
323 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
324 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
326 acomp->need_expose = true;
327 acomp->v_lvl_in = v_lvl_in;
328 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
330 if (acomp->need_expose && acomp->queue_draw) {
331 acomp->need_expose = false;
332 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
338 run_stereo(LV2_Handle instance, uint32_t n_samples)
340 AComp* acomp = (AComp*)instance;
342 const float* const input0 = acomp->input0;
343 const float* const input1 = acomp->input1;
344 const float* const sc = acomp->sc;
345 float* const output0 = acomp->output0;
346 float* const output1 = acomp->output1;
348 float srate = acomp->srate;
349 float width = (6.f * *(acomp->knee)) + 0.01;
351 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
352 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
355 float lgaininp = 0.f;
356 float rgaininp = 0.f;
358 float Lxg, Lxl, Lyg, Lyl, Ly1;
359 int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
367 float ratio = *acomp->ratio;
368 float thresdb = *acomp->thresdb;
369 float makeup_target = from_dB(*acomp->makeup);
370 float makeup_gain = acomp->makeup_gain;
372 const const float tau = acomp->tau;
374 if (*acomp->enable <= 0) {
381 if (acomp->v_knee != *acomp->knee) {
382 acomp->v_knee = *acomp->knee;
383 acomp->need_expose = true;
386 if (acomp->v_ratio != ratio) {
387 acomp->v_ratio = ratio;
388 acomp->need_expose = true;
391 if (acomp->v_thresdb != thresdb) {
392 acomp->v_thresdb = thresdb;
393 acomp->need_expose = true;
399 for (i = 0; i < n_samples; i++) {
403 maxabslr = fmaxf(fabs(in0), fabs(in1));
404 ingain = usesidechain ? fabs(sc0) : maxabslr;
405 in_peak = fmaxf (in_peak, ingain);
407 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
408 Lxg = sanitize_denormal(Lxg);
411 if (2.f*(Lxg-thresdb) < -width) {
413 } else if (2.f*(Lxg-thresdb) > width) {
414 Lyg = thresdb + (Lxg-thresdb)/ratio;
415 Lyg = sanitize_denormal(Lyg);
417 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
422 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
423 acomp->old_yl = sanitize_denormal(acomp->old_yl);
424 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
425 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
426 Ly1 = sanitize_denormal(Ly1);
427 Lyl = sanitize_denormal(Lyl);
430 Lgain = from_dB(cdb);
432 *(acomp->gainr) = Lyl;
434 lgaininp = in0 * Lgain;
435 rgaininp = in1 * Lgain;
437 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
439 output0[i] = lgaininp * makeup_gain;
440 output1[i] = rgaininp * makeup_gain;
442 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
444 // TODO re-use local variables on stack
445 // store values back to acomp at the end of the inner-loop
451 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
452 acomp->makeup_gain = makeup_gain;
455 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl); // crude LPF TODO use n_samples/rate TC
456 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
457 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
458 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
460 acomp->need_expose = true;
461 acomp->v_lvl_in = v_lvl_in;
462 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
464 if (acomp->need_expose && acomp->queue_draw) {
465 acomp->need_expose = false;
466 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
472 deactivate(LV2_Handle instance)
478 cleanup(LV2_Handle instance)
481 AComp* acomp = (AComp*)instance;
482 if (acomp->display) {
483 cairo_surface_destroy (acomp->display);
492 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
497 comp_curve (AComp* self, float xg) {
498 const float knee = self->v_knee;
499 const float ratio = self->v_ratio;
500 const float thresdb = self->v_thresdb;
502 const float width = 6.f * knee + 0.01f;
505 if (2.f * (xg - thresdb) < -width) {
507 } else if (2.f * (xg - thresdb) > width) {
508 yg = thresdb + (xg - thresdb) / ratio;
510 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
515 static LV2_Inline_Display_Image_Surface *
516 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
518 AComp* self = (AComp*)instance;
519 uint32_t h = MIN (w, max_h);
521 if (!self->display || self->w != w || self->h != h) {
522 if (self->display) cairo_surface_destroy(self->display);
523 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
528 cairo_t* cr = cairo_create (self->display);
531 cairo_rectangle (cr, 0, 0, w, h);
532 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
535 cairo_set_line_width(cr, 1.0);
537 // draw grid 10dB steps
538 const double dash1[] = {1, 2};
539 const double dash2[] = {1, 3};
541 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
542 cairo_set_dash(cr, dash2, 2, 2);
543 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
545 for (uint32_t d = 1; d < 6; ++d) {
546 const float x = -.5 + floorf (w * (d * 10.f / 60.f));
547 const float y = -.5 + floorf (h * (d * 10.f / 60.f));
549 cairo_move_to (cr, x, 0);
550 cairo_line_to (cr, x, h);
553 cairo_move_to (cr, 0, y);
554 cairo_line_to (cr, w, y);
557 if (self->v_thresdb < 0) {
558 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
559 const float y = -.5 + floorf (h * (self->v_thresdb / -60.f));
560 cairo_set_dash(cr, dash1, 2, 2);
561 cairo_move_to (cr, 0, y);
562 cairo_line_to (cr, w, y);
564 cairo_move_to (cr, 0, h);
565 cairo_line_to (cr, w, 0);
572 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
573 cairo_move_to (cr, 0, h);
575 for (uint32_t x = 0; x < w; ++x) {
577 const float x_db = 60.f * (-1.f + x / (float)w);
578 const float y_db = comp_curve (self, x_db);
579 const float y = h * (y_db / -60.f);
580 cairo_line_to (cr, x, y);
582 cairo_stroke_preserve (cr);
584 cairo_line_to (cr, w, h);
585 cairo_close_path (cr);
588 // draw signal level & reduction/gradient
589 const float top = comp_curve (self, 0);
590 cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
591 if (top > self->v_thresdb) {
592 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
593 cairo_pattern_add_color_stop_rgba (pat, top / -60.f, 0.8, 0.1, 0.1, 0.5);
595 if (self->v_knee > 0) {
596 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
597 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - self->v_knee) / -60.f), 0.5, 0.5, 0.5, 0.5);
599 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
600 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - .01) / -60.f), 0.5, 0.5, 0.5, 0.5);
602 cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
604 // maybe cut off at x-position?
605 const float x = w * (self->v_lvl_in + 60) / 60.f;
606 //const float y = h * (self->v_lvl_out + 60) / 60.f;
607 cairo_rectangle (cr, 0, h - x, x, h);
608 if (self->v_ratio > 1.0) {
609 cairo_set_source (cr, pat);
611 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
615 cairo_pattern_destroy (pat); // TODO cache pattern
618 // create RGBA surface
620 cairo_surface_flush (self->display);
621 self->surf.width = cairo_image_surface_get_width (self->display);
622 self->surf.height = cairo_image_surface_get_height (self->display);
623 self->surf.stride = cairo_image_surface_get_stride (self->display);
624 self->surf.data = cairo_image_surface_get_data (self->display);
631 extension_data(const char* uri)
634 static const LV2_Inline_Display_Interface display = { render_inline };
635 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
642 static const LV2_Descriptor descriptor_mono = {
653 static const LV2_Descriptor descriptor_stereo = {
665 const LV2_Descriptor*
666 lv2_descriptor(uint32_t index)
670 return &descriptor_mono;
672 return &descriptor_stereo;