2 * Copyright (C) 2017 Johannes Mueller <github@johannes-mueller.org>
3 * based on a-comp (C) 2016 Damien Zammit <damien@zamaudio.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
24 #include <cairo/cairo.h>
25 #include "ardour/lv2_extensions.h"
28 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
30 #define AEXP_URI "urn:ardour:a-exp"
31 #define AEXP_STEREO_URI "urn:ardour:a-exp#stereo"
34 # define M_PI 3.14159265358979323846
39 #define isfinite_local(val) (bool)_finite((double)val)
41 #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
110 instantiate(const LV2_Descriptor* descriptor,
112 const char* bundle_path,
113 const LV2_Feature* const* features)
115 AExp* aexp = (AExp*)calloc(1, sizeof(AExp));
117 for (int i=0; features[i]; ++i) {
119 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
120 aexp->queue_draw = (LV2_Inline_Display*) features[i]->data;
126 aexp->tau = (1.0 - exp (-2.f * M_PI * 25.f / aexp->srate));
128 aexp->need_expose = true;
129 aexp->v_lvl_out = -70.f;
132 return (LV2_Handle)aexp;
136 connect_port(LV2_Handle instance,
140 AExp* aexp = (AExp*)instance;
142 switch ((PortIndex)port) {
144 aexp->attack = (float*)data;
147 aexp->release = (float*)data;
150 aexp->knee = (float*)data;
153 aexp->ratio = (float*)data;
156 aexp->thresdb = (float*)data;
159 aexp->makeup = (float*)data;
162 aexp->gainr = (float*)data;
165 aexp->outlevel = (float*)data;
168 aexp->sidechain = (float*)data;
171 aexp->enable = (float*)data;
179 connect_mono(LV2_Handle instance,
183 AExp* aexp = (AExp*)instance;
184 connect_port (instance, port, data);
186 switch ((PortIndex)port) {
188 aexp->input0 = (float*)data;
191 aexp->sc = (float*)data;
194 aexp->output0 = (float*)data;
202 connect_stereo(LV2_Handle instance,
206 AExp* aexp = (AExp*)instance;
207 connect_port (instance, port, data);
209 switch ((PortIndex)port) {
211 aexp->input0 = (float*)data;
214 aexp->input1 = (float*)data;
217 aexp->sc = (float*)data;
220 aexp->output0 = (float*)data;
223 aexp->output1 = (float*)data;
230 // Force already-denormal float value to zero
232 sanitize_denormal(float value) {
233 if (!isnormal(value)) {
241 return (exp(gdb/20.f*log(10.f)));
246 return (20.f*log10(g));
250 activate(LV2_Handle instance)
252 AExp* aexp = (AExp*)instance;
254 *(aexp->gainr) = 160.0f;
255 *(aexp->outlevel) = -45.0f;
259 run_mono(LV2_Handle instance, uint32_t n_samples)
261 AExp* aexp = (AExp*)instance;
263 const float* const input = aexp->input0;
264 const float* const sc = aexp->sc;
265 float* const output = aexp->output0;
267 float srate = aexp->srate;
268 float width = (6.f * *(aexp->knee)) + 0.01;
269 float attack_coeff = exp(-1000.f/(*(aexp->attack) * srate));
270 float release_coeff = exp(-1000.f/(*(aexp->release) * srate));
273 float lgaininp = 0.f;
277 float old_gainr = *aexp->gainr;
279 int usesidechain = (*(aexp->sidechain) <= 0.f) ? 0 : 1;
285 float ratio = *aexp->ratio;
286 float thresdb = *aexp->thresdb;
287 float makeup = *aexp->makeup;
288 float makeup_target = from_dB(makeup);
289 float makeup_gain = aexp->makeup_gain;
291 const float tau = aexp->tau;
293 if (*aexp->enable <= 0) {
301 if (aexp->v_knee != *aexp->knee) {
302 aexp->v_knee = *aexp->knee;
303 aexp->need_expose = true;
306 if (aexp->v_ratio != ratio) {
307 aexp->v_ratio = ratio;
308 aexp->need_expose = true;
311 if (aexp->v_thresdb != thresdb) {
312 aexp->v_thresdb = thresdb;
313 aexp->need_expose = true;
316 if (aexp->v_makeup != makeup) {
317 aexp->v_makeup = makeup;
318 aexp->need_expose = true;
324 for (i = 0; i < n_samples; i++) {
327 ingain = usesidechain ? fabs(sc0) : fabs(in0);
329 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
330 Lxg = sanitize_denormal(Lxg);
332 if (2.f*(Lxg-thresdb) < -width) {
333 Lyg = thresdb + (Lxg-thresdb) * ratio;
334 Lyg = sanitize_denormal(Lyg);
335 } else if (2.f*(Lxg-thresdb) > width) {
338 Lyg = Lxg + (1.f-ratio)*(Lxg-thresdb-width/2.f)*(Lxg-thresdb-width/2.f)/(2.f*width);
341 current_gainr = Lxg - Lyg;
343 if (current_gainr > old_gainr) {
344 current_gainr = release_coeff*old_gainr + (1.f-release_coeff)*current_gainr;
345 } else if (current_gainr < old_gainr) {
346 current_gainr = attack_coeff*old_gainr + (1.f-attack_coeff)*current_gainr;
349 current_gainr = sanitize_denormal(current_gainr);
351 Lgain = from_dB(-current_gainr);
353 old_gainr = current_gainr;
355 if (current_gainr > aexp->v_gainr) {
356 aexp->v_gainr = current_gainr;
359 lgaininp = in0 * Lgain;
361 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
362 output[i] = lgaininp * makeup_gain;
364 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
367 *(aexp->gainr) = current_gainr;
368 *(aexp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
369 aexp->makeup_gain = makeup_gain;
372 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
373 const float v_lvl_in = v_lvl_out > thresdb ?
375 (aexp->v_lvl_out - thresdb * (1.f-ratio))/ratio;
377 if (fabsf (aexp->v_lvl_out - v_lvl_out) >= 1 || fabsf (aexp->v_lvl_in - v_lvl_in) >= 1) {
379 aexp->need_expose = true;
380 aexp->v_lvl_in = v_lvl_in;
381 const float relax_coef = exp(-(float)n_samples/srate);
382 aexp->v_lvl_out = fmaxf (v_lvl_out, relax_coef*aexp->v_lvl_out + (1.f-relax_coef)*v_lvl_out);
384 if (aexp->need_expose && aexp->queue_draw) {
385 aexp->need_expose = false;
386 aexp->queue_draw->queue_draw (aexp->queue_draw->handle);
393 run_stereo(LV2_Handle instance, uint32_t n_samples)
395 AExp* aexp = (AExp*)instance;
397 const float* const input0 = aexp->input0;
398 const float* const input1 = aexp->input1;
399 const float* const sc = aexp->sc;
400 float* const output0 = aexp->output0;
401 float* const output1 = aexp->output1;
403 float srate = aexp->srate;
404 float width = (6.f * *(aexp->knee)) + 0.01;
405 float attack_coeff = exp(-1000.f/(*(aexp->attack) * srate));
406 float release_coeff = exp(-1000.f/(*(aexp->release) * srate));
409 float lgaininp = 0.f;
410 float rgaininp = 0.f;
414 float old_gainr = *aexp->gainr;
416 int usesidechain = (*(aexp->sidechain) <= 0.f) ? 0 : 1;
424 float ratio = *aexp->ratio;
425 float thresdb = *aexp->thresdb;
426 float makeup = *aexp->makeup;
427 float makeup_target = from_dB(makeup);
428 float makeup_gain = aexp->makeup_gain;
430 const float tau = aexp->tau;
432 if (*aexp->enable <= 0) {
440 if (aexp->v_knee != *aexp->knee) {
441 aexp->v_knee = *aexp->knee;
442 aexp->need_expose = true;
445 if (aexp->v_ratio != ratio) {
446 aexp->v_ratio = ratio;
447 aexp->need_expose = true;
450 if (aexp->v_thresdb != thresdb) {
451 aexp->v_thresdb = thresdb;
452 aexp->need_expose = true;
455 if (aexp->v_makeup != makeup) {
456 aexp->v_makeup = makeup;
457 aexp->need_expose = true;
463 for (i = 0; i < n_samples; i++) {
467 maxabslr = fmaxf(fabs(in0), fabs(in1));
468 ingain = usesidechain ? fabs(sc0) : maxabslr;
470 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
471 Lxg = sanitize_denormal(Lxg);
473 if (2.f*(Lxg-thresdb) < -width) {
474 Lyg = thresdb + (Lxg-thresdb) * ratio;
475 Lyg = sanitize_denormal(Lyg);
476 } else if (2.f*(Lxg-thresdb) > width) {
479 Lyg = Lxg + (1.f-ratio)*(Lxg-thresdb-width/2.f)*(Lxg-thresdb-width/2.f)/(2.f*width);
482 current_gainr = Lxg - Lyg;
484 if (current_gainr > old_gainr) {
485 current_gainr = release_coeff*old_gainr + (1.f-release_coeff)*current_gainr;
486 } else if (current_gainr < old_gainr) {
487 current_gainr = attack_coeff*old_gainr + (1.f-attack_coeff)*current_gainr;
490 current_gainr = sanitize_denormal(current_gainr);
492 Lgain = from_dB(-current_gainr);
494 old_gainr = current_gainr;
496 if (current_gainr > aexp->v_gainr) {
497 aexp->v_gainr = current_gainr;
500 lgaininp = in0 * Lgain;
501 rgaininp = in1 * Lgain;
503 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
505 output0[i] = lgaininp * makeup_gain;
506 output1[i] = rgaininp * makeup_gain;
508 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
511 *(aexp->gainr) = current_gainr;
512 *(aexp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
513 aexp->makeup_gain = makeup_gain;
516 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
517 const float v_lvl_in = v_lvl_out > thresdb ?
519 (aexp->v_lvl_out - thresdb * (1.f-ratio))/ratio;
521 if (fabsf (aexp->v_lvl_out - v_lvl_out) >= 1 || fabsf (aexp->v_lvl_in - v_lvl_in) >= 1) {
523 aexp->need_expose = true;
524 aexp->v_lvl_in = v_lvl_in;
525 const float relax_coef = exp(-2.0*n_samples/srate);
526 aexp->v_lvl_out = fmaxf (v_lvl_out, relax_coef*aexp->v_lvl_out + (1.f-relax_coef)*v_lvl_out);
528 if (aexp->need_expose && aexp->queue_draw) {
529 aexp->need_expose = false;
530 aexp->queue_draw->queue_draw (aexp->queue_draw->handle);
537 deactivate(LV2_Handle instance)
543 cleanup(LV2_Handle instance)
546 AExp* aexp = (AExp*)instance;
548 cairo_surface_destroy (aexp->display);
557 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
562 exp_curve (const AExp* self, float xg) {
563 const float knee = self->v_knee;
564 const float ratio = self->v_ratio;
565 const float thresdb = self->v_thresdb;
566 const float makeup = self->v_makeup;
568 const float width = 6.f * knee + 0.01f;
571 if (2.f * (xg - thresdb) < -width) {
572 yg = thresdb + (xg - thresdb) * ratio;
573 } else if (2.f * (xg - thresdb) > width) {
576 yg = xg + (1.f - ratio) * (xg - thresdb - width / 2.f) * (xg - thresdb - width / 2.f) / (2.f * width);
586 render_inline_full (cairo_t* cr, const AExp* self)
588 const float w = self->w;
589 const float h = self->h;
591 const float makeup_thres = self->v_thresdb + self->v_makeup;
594 cairo_rectangle (cr, 0, 0, w, h);
595 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
598 cairo_set_line_width(cr, 1.0);
600 // draw grid 10dB steps
601 const double dash1[] = {1, 2};
602 const double dash2[] = {1, 3};
604 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
605 cairo_set_dash(cr, dash2, 2, 2);
606 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
608 for (uint32_t d = 1; d < 7; ++d) {
609 const float x = -.5 + floorf (w * (d * 10.f / 70.f));
610 const float y = -.5 + floorf (h * (d * 10.f / 70.f));
612 cairo_move_to (cr, x, 0);
613 cairo_line_to (cr, x, h);
616 cairo_move_to (cr, 0, y);
617 cairo_line_to (cr, w, y);
620 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
621 cairo_set_dash(cr, dash1, 2, 2);
622 if (self->v_thresdb < 0) {
623 const float y = -.5 + floorf (h * ((makeup_thres - 10.f) / -70.f));
624 cairo_move_to (cr, 0, y);
625 cairo_line_to (cr, w, y);
629 cairo_move_to (cr, 0, h);
630 cairo_line_to (cr, w, 0);
635 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
636 const float x = -.5 + floorf (w * (60.f / 70.f));
637 const float y = -.5 + floorf (h * (10.f / 70.f));
638 cairo_move_to (cr, x, 0);
639 cairo_line_to (cr, x, h);
641 cairo_move_to (cr, 0, y);
642 cairo_line_to (cr, w, y);
647 const float x = -.5 + floorf (w * (62.5f / 70.f));
648 const float y = -.5 + floorf (h * (10.0f / 70.f));
649 const float wd = floorf (w * (5.f / 70.f));
650 const float ht = floorf (h * (55.f / 70.f));
651 cairo_rectangle (cr, x, y, wd, ht);
654 const float h_gr = fminf (ht, floorf (h * self->v_gainr / 70.f));
655 cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
656 cairo_rectangle (cr, x, y, wd, h_gr);
658 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
659 cairo_rectangle (cr, x, y, wd, ht);
660 cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 1.0);
665 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
666 cairo_move_to (cr, 0, h);
668 for (uint32_t x = 0; x < w; ++x) {
670 const float x_db = 70.f * (-1.f + x / (float)w) + 10.f;
671 const float y_db = exp_curve (self, x_db) - 10.f;
672 const float y = h * (y_db / -70.f);
673 cairo_line_to (cr, x, y);
675 cairo_stroke_preserve (cr);
677 cairo_line_to (cr, w, h);
678 cairo_close_path (cr);
681 // draw signal level & reduction/gradient
682 const float top = exp_curve (self, 0) - 10.f;
683 cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
684 if (top > makeup_thres - 10.f) {
685 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
686 cairo_pattern_add_color_stop_rgba (pat, top / -70.f, 0.8, 0.1, 0.1, 0.5);
688 if (self->v_knee > 0) {
689 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres -10.f) / -70.f), 0.7, 0.7, 0.2, 0.5);
690 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - self->v_knee - 10.f) / -70.f), 0.5, 0.5, 0.5, 0.5);
692 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.f)/ -70.f), 0.7, 0.7, 0.2, 0.5);
693 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.01f) / -70.f), 0.5, 0.5, 0.5, 0.5);
695 cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
697 // maybe cut off at x-position?
698 const float x = w * (self->v_lvl_in + 60) / 70.f;
699 const float y = x + h*self->v_makeup;
700 cairo_rectangle (cr, 0, h - y, x, y);
701 if (self->v_ratio > 1.0) {
702 cairo_set_source (cr, pat);
704 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
708 cairo_pattern_destroy (pat); // TODO cache pattern
712 render_inline_only_bars (cairo_t* cr, const AExp* self)
714 const float w = self->w;
715 const float h = self->h;
717 cairo_rectangle (cr, 0, 0, w, h);
718 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
724 const float ht = 0.25f * h;
726 const float x1 = w*0.05;
727 const float wd = w - 2.0f*x1;
729 const float y1 = 0.17*h;
730 const float y2 = h - y1 - ht;
732 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
734 cairo_rectangle (cr, x1, y1, wd, ht);
737 cairo_rectangle (cr, x1, y2, wd, ht);
740 cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
741 const float w_gr = (self->v_gainr > 60.f) ? wd : wd * self->v_gainr * (1.f/60.f);
742 cairo_rectangle (cr, x1+wd-w_gr, y2, w_gr, ht);
745 if (self->v_lvl_out > -60.f) {
746 if (self->v_lvl_out > 10.f) {
747 cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
748 } else if (self->v_lvl_out > 0.f) {
749 cairo_set_source_rgba (cr, 0.66, 0.66, 0.0, 1.0);
751 cairo_set_source_rgba (cr, 0.0, 0.66, 0.0, 1.0);
753 const float w_g = (self->v_lvl_out > 10.f) ? wd : wd * (60.f+self->v_lvl_out) / 70.f;
754 cairo_rectangle (cr, x1, y1, w_g, ht);
758 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
760 const float tck = 0.33*ht;
762 cairo_set_line_width (cr, .5);
764 for (uint32_t d = 1; d < 7; ++d) {
765 const float x = x1 + (d * wd * (10.f / 70.f));
767 cairo_move_to (cr, x, y1);
768 cairo_line_to (cr, x, y1+tck);
770 cairo_move_to (cr, x, y1+ht);
771 cairo_line_to (cr, x, y1+ht-tck);
773 cairo_move_to (cr, x, y2);
774 cairo_line_to (cr, x, y2+tck);
776 cairo_move_to (cr, x, y2+ht);
777 cairo_line_to (cr, x, y2+ht-tck);
782 const float x_0dB = x1 + wd*(60.f/70.f);
784 cairo_move_to (cr, x_0dB, y1);
785 cairo_line_to (cr, x_0dB, y1+ht);
787 cairo_rectangle (cr, x1, y1, wd, ht);
788 cairo_rectangle (cr, x1, y2, wd, ht);
791 cairo_set_line_width (cr, 2.0);
793 // visualize threshold
794 const float tr = x1 + wd * (60.f+self->v_thresdb) / 70.f;
795 cairo_set_source_rgba (cr, 0.95, 0.95, 0.0, 1.0);
796 cairo_move_to (cr, tr, y1);
797 cairo_line_to (cr, tr, y1+ht);
801 const float reduced_0dB = self->v_thresdb * (1.f - 1.f/self->v_ratio);
802 const float rt = x1 + wd * (60.f+reduced_0dB) / 70.f;
803 cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
804 cairo_move_to (cr, rt, y1);
805 cairo_line_to (cr, rt, y1+ht);
809 static LV2_Inline_Display_Image_Surface *
810 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
812 AExp* self = (AExp*)instance;
814 uint32_t h = MIN (w, max_h);
819 if (!self->display || self->w != w || self->h != h) {
820 if (self->display) cairo_surface_destroy(self->display);
821 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
826 cairo_t* cr = cairo_create (self->display);
829 render_inline_full (cr, self);
831 render_inline_only_bars (cr, self);
836 cairo_surface_flush (self->display);
837 self->surf.width = cairo_image_surface_get_width (self->display);
838 self->surf.height = cairo_image_surface_get_height (self->display);
839 self->surf.stride = cairo_image_surface_get_stride (self->display);
840 self->surf.data = cairo_image_surface_get_data (self->display);
847 extension_data(const char* uri)
850 static const LV2_Inline_Display_Interface display = { render_inline };
851 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
858 static const LV2_Descriptor descriptor_mono = {
869 static const LV2_Descriptor descriptor_stereo = {
881 const LV2_Descriptor*
882 lv2_descriptor(uint32_t index)
886 return &descriptor_mono;
888 return &descriptor_stereo;