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
36 #define isfinite_local(val) (bool)_finite((double)val)
38 #define isfinite_local isfinite
89 LV2_Inline_Display_Image_Surface surf;
91 cairo_surface_t* display;
92 LV2_Inline_Display* queue_draw;
95 /* ports pointers are only valid during run so we'll
96 * have to cache them for the display, besides
97 * we do want to check for changes
112 instantiate(const LV2_Descriptor* descriptor,
114 const char* bundle_path,
115 const LV2_Feature* const* features)
117 AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
119 for (int i=0; features[i]; ++i) {
121 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
122 acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
128 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
129 acomp->tau = (1.0 - exp (-2.f * M_PI * 25.f / acomp->srate));
131 acomp->need_expose = true;
134 return (LV2_Handle)acomp;
139 connect_port(LV2_Handle instance,
143 AComp* acomp = (AComp*)instance;
145 switch ((PortIndex)port) {
147 acomp->attack = (float*)data;
150 acomp->release = (float*)data;
153 acomp->knee = (float*)data;
156 acomp->ratio = (float*)data;
158 case ACOMP_THRESHOLD:
159 acomp->thresdb = (float*)data;
162 acomp->makeup = (float*)data;
165 acomp->gainr = (float*)data;
168 acomp->outlevel = (float*)data;
170 case ACOMP_SIDECHAIN:
171 acomp->sidechain = (float*)data;
174 acomp->enable = (float*)data;
182 connect_mono(LV2_Handle instance,
186 AComp* acomp = (AComp*)instance;
187 connect_port (instance, port, data);
189 switch ((PortIndex)port) {
191 acomp->input0 = (float*)data;
194 acomp->sc = (float*)data;
197 acomp->output0 = (float*)data;
205 connect_stereo(LV2_Handle instance,
209 AComp* acomp = (AComp*)instance;
210 connect_port (instance, port, data);
212 switch ((PortIndex)port) {
214 acomp->input0 = (float*)data;
217 acomp->input1 = (float*)data;
220 acomp->sc = (float*)data;
223 acomp->output0 = (float*)data;
226 acomp->output1 = (float*)data;
233 // Force already-denormal float value to zero
235 sanitize_denormal(float value) {
236 if (!isnormal(value)) {
244 return (exp(gdb/20.f*log(10.f)));
249 return (20.f*log10(g));
253 activate(LV2_Handle instance)
255 AComp* acomp = (AComp*)instance;
257 *(acomp->gainr) = 0.0f;
258 *(acomp->outlevel) = -45.0f;
259 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
263 run_mono(LV2_Handle instance, uint32_t n_samples)
265 AComp* acomp = (AComp*)instance;
267 const float* const input = acomp->input0;
268 const float* const sc = acomp->sc;
269 float* const output = acomp->output0;
271 float srate = acomp->srate;
272 float width = (6.f * *(acomp->knee)) + 0.01;
274 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
275 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
278 float lgaininp = 0.f;
280 float Lxg, Lxl, Lyg, Lyl, Ly1;
281 int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
287 float ratio = *acomp->ratio;
288 float thresdb = *acomp->thresdb;
289 float makeup = *acomp->makeup;
290 float makeup_target = from_dB(makeup);
291 float makeup_gain = acomp->makeup_gain;
293 const float tau = acomp->tau;
295 if (*acomp->enable <= 0) {
303 if (acomp->v_knee != *acomp->knee) {
304 acomp->v_knee = *acomp->knee;
305 acomp->need_expose = true;
308 if (acomp->v_ratio != ratio) {
309 acomp->v_ratio = ratio;
310 acomp->need_expose = true;
313 if (acomp->v_thresdb != thresdb) {
314 acomp->v_thresdb = thresdb;
315 acomp->need_expose = true;
318 if (acomp->v_makeup != makeup) {
319 acomp->v_makeup = makeup;
320 acomp->need_expose = true;
325 acomp->v_gainr = 0.0;
327 for (i = 0; i < n_samples; i++) {
330 ingain = usesidechain ? fabs(sc0) : fabs(in0);
331 in_peak = fmaxf (in_peak, ingain);
333 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
334 Lxg = sanitize_denormal(Lxg);
337 if (2.f*(Lxg-thresdb) < -width) {
339 } else if (2.f*(Lxg-thresdb) > width) {
340 Lyg = thresdb + (Lxg-thresdb)/ratio;
341 Lyg = sanitize_denormal(Lyg);
343 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
348 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
349 acomp->old_yl = sanitize_denormal(acomp->old_yl);
350 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
351 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
352 Ly1 = sanitize_denormal(Ly1);
353 Lyl = sanitize_denormal(Lyl);
356 Lgain = from_dB(cdb);
358 *(acomp->gainr) = Lyl;
359 if (Lyl > acomp->v_gainr) {
360 acomp->v_gainr = Lyl;
363 lgaininp = in0 * Lgain;
365 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
366 output[i] = lgaininp * makeup_gain;
368 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
370 // TODO re-use local variables on stack
371 // store values back to acomp at the end of the inner-loop
377 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
378 acomp->makeup_gain = makeup_gain;
381 const float old_v_lv1 = acomp->v_lv1;
382 const float old_v_lvl = acomp->v_lvl;
383 const float tot_rel_c = exp(-1000.f/(*(acomp->release) * srate) * n_samples);
384 const float tot_atk_c = exp(-1000.f/(*(acomp->attack) * srate) * n_samples);
385 acomp->v_lv1 = fmaxf (in_peak, tot_rel_c*old_v_lv1 + (1.f-tot_rel_c)*in_peak);
386 acomp->v_lvl = tot_atk_c*old_v_lvl + (1.f-tot_atk_c)*acomp->v_lv1;
388 if (!isfinite_local (acomp->v_lvl)) {
391 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
392 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
393 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
395 acomp->need_expose = true;
396 acomp->v_lvl_in = v_lvl_in;
397 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
399 if (acomp->need_expose && acomp->queue_draw) {
400 acomp->need_expose = false;
401 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
407 run_stereo(LV2_Handle instance, uint32_t n_samples)
409 AComp* acomp = (AComp*)instance;
411 const float* const input0 = acomp->input0;
412 const float* const input1 = acomp->input1;
413 const float* const sc = acomp->sc;
414 float* const output0 = acomp->output0;
415 float* const output1 = acomp->output1;
417 float srate = acomp->srate;
418 float width = (6.f * *(acomp->knee)) + 0.01;
420 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
421 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
424 float lgaininp = 0.f;
425 float rgaininp = 0.f;
427 float Lxg, Lxl, Lyg, Lyl, Ly1;
428 int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
436 float ratio = *acomp->ratio;
437 float thresdb = *acomp->thresdb;
438 float makeup = *acomp->makeup;
439 float makeup_target = from_dB(makeup);
440 float makeup_gain = acomp->makeup_gain;
442 const float tau = acomp->tau;
444 if (*acomp->enable <= 0) {
452 if (acomp->v_knee != *acomp->knee) {
453 acomp->v_knee = *acomp->knee;
454 acomp->need_expose = true;
457 if (acomp->v_ratio != ratio) {
458 acomp->v_ratio = ratio;
459 acomp->need_expose = true;
462 if (acomp->v_thresdb != thresdb) {
463 acomp->v_thresdb = thresdb;
464 acomp->need_expose = true;
467 if (acomp->v_makeup != makeup) {
468 acomp->v_makeup = makeup;
469 acomp->need_expose = true;
474 acomp->v_gainr = 0.0;
476 for (i = 0; i < n_samples; i++) {
480 maxabslr = fmaxf(fabs(in0), fabs(in1));
481 ingain = usesidechain ? fabs(sc0) : maxabslr;
482 in_peak = fmaxf (in_peak, ingain);
484 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
485 Lxg = sanitize_denormal(Lxg);
488 if (2.f*(Lxg-thresdb) < -width) {
490 } else if (2.f*(Lxg-thresdb) > width) {
491 Lyg = thresdb + (Lxg-thresdb)/ratio;
492 Lyg = sanitize_denormal(Lyg);
494 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
499 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
500 acomp->old_yl = sanitize_denormal(acomp->old_yl);
501 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
502 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
503 Ly1 = sanitize_denormal(Ly1);
504 Lyl = sanitize_denormal(Lyl);
507 Lgain = from_dB(cdb);
509 *(acomp->gainr) = Lyl;
510 if (Lyl > acomp->v_gainr) {
511 acomp->v_gainr = Lyl;
514 lgaininp = in0 * Lgain;
515 rgaininp = in1 * Lgain;
517 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
519 output0[i] = lgaininp * makeup_gain;
520 output1[i] = rgaininp * makeup_gain;
522 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
524 // TODO re-use local variables on stack
525 // store values back to acomp at the end of the inner-loop
531 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
532 acomp->makeup_gain = makeup_gain;
535 const float old_v_lv1 = acomp->v_lv1;
536 const float old_v_lvl = acomp->v_lvl;
537 const float tot_rel_c = exp(-1000.f/(*(acomp->release) * srate) * n_samples);
538 const float tot_atk_c = exp(-1000.f/(*(acomp->attack) * srate) * n_samples);
539 acomp->v_lv1 = fmaxf (in_peak, tot_rel_c*old_v_lv1 + (1.f-tot_rel_c)*in_peak);
540 acomp->v_lvl = tot_atk_c*old_v_lvl + (1.f-tot_atk_c)*acomp->v_lv1;
541 if (!isfinite_local (acomp->v_lvl)) {
544 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
545 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
546 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
548 acomp->need_expose = true;
549 acomp->v_lvl_in = v_lvl_in;
550 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
552 if (acomp->need_expose && acomp->queue_draw) {
553 acomp->need_expose = false;
554 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
560 deactivate(LV2_Handle instance)
566 cleanup(LV2_Handle instance)
569 AComp* acomp = (AComp*)instance;
570 if (acomp->display) {
571 cairo_surface_destroy (acomp->display);
580 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
585 comp_curve (AComp* self, float xg) {
586 const float knee = self->v_knee;
587 const float ratio = self->v_ratio;
588 const float thresdb = self->v_thresdb;
589 const float makeup = self->v_makeup;
591 const float width = 6.f * knee + 0.01f;
594 if (2.f * (xg - thresdb) < -width) {
596 } else if (2.f * (xg - thresdb) > width) {
597 yg = thresdb + (xg - thresdb) / ratio;
599 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
607 static LV2_Inline_Display_Image_Surface *
608 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
610 AComp* self = (AComp*)instance;
611 uint32_t h = MIN (w, max_h);
613 const float makeup_thres = self->v_thresdb + self->v_makeup;
615 if (!self->display || self->w != w || self->h != h) {
616 if (self->display) cairo_surface_destroy(self->display);
617 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
622 cairo_t* cr = cairo_create (self->display);
625 cairo_rectangle (cr, 0, 0, w, h);
626 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
629 cairo_set_line_width(cr, 1.0);
631 // draw grid 10dB steps
632 const double dash1[] = {1, 2};
633 const double dash2[] = {1, 3};
635 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
636 cairo_set_dash(cr, dash2, 2, 2);
637 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
639 for (uint32_t d = 1; d < 7; ++d) {
640 const float x = -.5 + floorf (w * (d * 10.f / 70.f));
641 const float y = -.5 + floorf (h * (d * 10.f / 70.f));
643 cairo_move_to (cr, x, 0);
644 cairo_line_to (cr, x, h);
647 cairo_move_to (cr, 0, y);
648 cairo_line_to (cr, w, y);
651 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
652 cairo_set_dash(cr, dash1, 2, 2);
653 if (self->v_thresdb < 0) {
654 const float y = -.5 + floorf (h * ((makeup_thres - 10.f) / -70.f));
655 cairo_move_to (cr, 0, y);
656 cairo_line_to (cr, w, y);
660 cairo_move_to (cr, 0, h);
661 cairo_line_to (cr, w, 0);
666 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
667 const float x = -.5 + floorf (w * (60.f / 70.f));
668 const float y = -.5 + floorf (h * (10.f / 70.f));
669 cairo_move_to (cr, x, 0);
670 cairo_line_to (cr, x, h);
672 cairo_move_to (cr, 0, y);
673 cairo_line_to (cr, w, y);
678 const float x = -.5 + floorf (w * (62.5f / 70.f));
679 const float y = -.5 + floorf (h * (10.0f / 70.f));
680 const float wd = floorf (w * (5.f / 70.f));
681 const float ht = floorf (h * (55.f / 70.f));
682 cairo_rectangle (cr, x, y, wd, ht);
685 const float h_gr = fminf (ht, floorf (h * self->v_gainr / 70.f));
686 cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
687 cairo_rectangle (cr, x, y, wd, h_gr);
689 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
690 cairo_rectangle (cr, x, y, wd, ht);
691 cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 1.0);
696 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
697 cairo_move_to (cr, 0, h);
699 for (uint32_t x = 0; x < w; ++x) {
701 const float x_db = 70.f * (-1.f + x / (float)w) + 10.f;
702 const float y_db = comp_curve (self, x_db) - 10.f;
703 const float y = h * (y_db / -70.f);
704 cairo_line_to (cr, x, y);
706 cairo_stroke_preserve (cr);
708 cairo_line_to (cr, w, h);
709 cairo_close_path (cr);
712 // draw signal level & reduction/gradient
713 const float top = comp_curve (self, 0) - 10.f;
714 cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
715 if (top > makeup_thres - 10.f) {
716 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
717 cairo_pattern_add_color_stop_rgba (pat, top / -70.f, 0.8, 0.1, 0.1, 0.5);
719 if (self->v_knee > 0) {
720 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres -10.f) / -70.f), 0.7, 0.7, 0.2, 0.5);
721 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - self->v_knee - 10.f) / -70.f), 0.5, 0.5, 0.5, 0.5);
723 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.f)/ -70.f), 0.7, 0.7, 0.2, 0.5);
724 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.01f) / -70.f), 0.5, 0.5, 0.5, 0.5);
726 cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
728 // maybe cut off at x-position?
729 const float x = w * (self->v_lvl_in + 60) / 70.f;
730 const float y = x + h*self->v_makeup;
731 cairo_rectangle (cr, 0, h - y, x, y);
732 if (self->v_ratio > 1.0) {
733 cairo_set_source (cr, pat);
735 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
739 cairo_pattern_destroy (pat); // TODO cache pattern
742 // create RGBA surface
744 cairo_surface_flush (self->display);
745 self->surf.width = cairo_image_surface_get_width (self->display);
746 self->surf.height = cairo_image_surface_get_height (self->display);
747 self->surf.stride = cairo_image_surface_get_stride (self->display);
748 self->surf.data = cairo_image_surface_get_data (self->display);
755 extension_data(const char* uri)
758 static const LV2_Inline_Display_Interface display = { render_inline };
759 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
766 static const LV2_Descriptor descriptor_mono = {
777 static const LV2_Descriptor descriptor_stereo = {
789 const LV2_Descriptor*
790 lv2_descriptor(uint32_t index)
794 return &descriptor_mono;
796 return &descriptor_stereo;