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
111 instantiate(const LV2_Descriptor* descriptor,
113 const char* bundle_path,
114 const LV2_Feature* const* features)
116 AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
118 for (int i=0; features[i]; ++i) {
120 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
121 acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
127 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
128 acomp->tau = (1.0 - exp (-2.f * M_PI * 25.f / acomp->srate));
130 acomp->need_expose = true;
133 return (LV2_Handle)acomp;
138 connect_port(LV2_Handle instance,
142 AComp* acomp = (AComp*)instance;
144 switch ((PortIndex)port) {
146 acomp->attack = (float*)data;
149 acomp->release = (float*)data;
152 acomp->knee = (float*)data;
155 acomp->ratio = (float*)data;
157 case ACOMP_THRESHOLD:
158 acomp->thresdb = (float*)data;
161 acomp->makeup = (float*)data;
164 acomp->gainr = (float*)data;
167 acomp->outlevel = (float*)data;
169 case ACOMP_SIDECHAIN:
170 acomp->sidechain = (float*)data;
173 acomp->enable = (float*)data;
181 connect_mono(LV2_Handle instance,
185 AComp* acomp = (AComp*)instance;
186 connect_port (instance, port, data);
188 switch ((PortIndex)port) {
190 acomp->input0 = (float*)data;
193 acomp->sc = (float*)data;
196 acomp->output0 = (float*)data;
204 connect_stereo(LV2_Handle instance,
208 AComp* acomp = (AComp*)instance;
209 connect_port (instance, port, data);
211 switch ((PortIndex)port) {
213 acomp->input0 = (float*)data;
216 acomp->input1 = (float*)data;
219 acomp->sc = (float*)data;
222 acomp->output0 = (float*)data;
225 acomp->output1 = (float*)data;
232 // Force already-denormal float value to zero
234 sanitize_denormal(float value) {
235 if (!isnormal(value)) {
243 return (exp(gdb/20.f*log(10.f)));
248 return (20.f*log10(g));
252 activate(LV2_Handle instance)
254 AComp* acomp = (AComp*)instance;
256 *(acomp->gainr) = 0.0f;
257 *(acomp->outlevel) = -45.0f;
258 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
262 run_mono(LV2_Handle instance, uint32_t n_samples)
264 AComp* acomp = (AComp*)instance;
266 const float* const input = acomp->input0;
267 const float* const sc = acomp->sc;
268 float* const output = acomp->output0;
270 float srate = acomp->srate;
271 float width = (6.f * *(acomp->knee)) + 0.01;
273 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
274 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
277 float lgaininp = 0.f;
279 float Lxg, Lxl, Lyg, Lyl, Ly1;
280 int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
286 float ratio = *acomp->ratio;
287 float thresdb = *acomp->thresdb;
288 float makeup = *acomp->makeup;
289 float makeup_target = from_dB(makeup);
290 float makeup_gain = acomp->makeup_gain;
292 const float tau = acomp->tau;
294 if (*acomp->enable <= 0) {
302 if (acomp->v_knee != *acomp->knee) {
303 acomp->v_knee = *acomp->knee;
304 acomp->need_expose = true;
307 if (acomp->v_ratio != ratio) {
308 acomp->v_ratio = ratio;
309 acomp->need_expose = true;
312 if (acomp->v_thresdb != thresdb) {
313 acomp->v_thresdb = thresdb;
314 acomp->need_expose = true;
317 if (acomp->v_makeup != makeup) {
318 acomp->v_makeup = makeup;
319 acomp->need_expose = true;
324 acomp->v_gainr = 0.0;
326 for (i = 0; i < n_samples; i++) {
329 ingain = usesidechain ? fabs(sc0) : fabs(in0);
330 in_peak = fmaxf (in_peak, ingain);
332 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
333 Lxg = sanitize_denormal(Lxg);
336 if (2.f*(Lxg-thresdb) < -width) {
338 } else if (2.f*(Lxg-thresdb) > width) {
339 Lyg = thresdb + (Lxg-thresdb)/ratio;
340 Lyg = sanitize_denormal(Lyg);
342 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
347 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
348 acomp->old_yl = sanitize_denormal(acomp->old_yl);
349 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
350 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
351 Ly1 = sanitize_denormal(Ly1);
352 Lyl = sanitize_denormal(Lyl);
355 Lgain = from_dB(cdb);
357 *(acomp->gainr) = Lyl;
358 if (Lyl > acomp->v_gainr) {
359 acomp->v_gainr = Lyl;
362 lgaininp = in0 * Lgain;
364 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
365 output[i] = lgaininp * makeup_gain;
367 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
369 // TODO re-use local variables on stack
370 // store values back to acomp at the end of the inner-loop
376 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
377 acomp->makeup_gain = makeup_gain;
380 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl) + 1e-12; // crude LPF TODO use n_samples/rate TC
381 if (!isfinite_local (acomp->v_lvl)) {
384 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
385 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
386 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
388 acomp->need_expose = true;
389 acomp->v_lvl_in = v_lvl_in;
390 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
392 if (acomp->need_expose && acomp->queue_draw) {
393 acomp->need_expose = false;
394 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
400 run_stereo(LV2_Handle instance, uint32_t n_samples)
402 AComp* acomp = (AComp*)instance;
404 const float* const input0 = acomp->input0;
405 const float* const input1 = acomp->input1;
406 const float* const sc = acomp->sc;
407 float* const output0 = acomp->output0;
408 float* const output1 = acomp->output1;
410 float srate = acomp->srate;
411 float width = (6.f * *(acomp->knee)) + 0.01;
413 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
414 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
417 float lgaininp = 0.f;
418 float rgaininp = 0.f;
420 float Lxg, Lxl, Lyg, Lyl, Ly1;
421 int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
429 float ratio = *acomp->ratio;
430 float thresdb = *acomp->thresdb;
431 float makeup = *acomp->makeup;
432 float makeup_target = from_dB(makeup);
433 float makeup_gain = acomp->makeup_gain;
435 const float tau = acomp->tau;
437 if (*acomp->enable <= 0) {
445 if (acomp->v_knee != *acomp->knee) {
446 acomp->v_knee = *acomp->knee;
447 acomp->need_expose = true;
450 if (acomp->v_ratio != ratio) {
451 acomp->v_ratio = ratio;
452 acomp->need_expose = true;
455 if (acomp->v_thresdb != thresdb) {
456 acomp->v_thresdb = thresdb;
457 acomp->need_expose = true;
460 if (acomp->v_makeup != makeup) {
461 acomp->v_makeup = makeup;
462 acomp->need_expose = true;
467 acomp->v_gainr = 0.0;
469 for (i = 0; i < n_samples; i++) {
473 maxabslr = fmaxf(fabs(in0), fabs(in1));
474 ingain = usesidechain ? fabs(sc0) : maxabslr;
475 in_peak = fmaxf (in_peak, ingain);
477 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
478 Lxg = sanitize_denormal(Lxg);
481 if (2.f*(Lxg-thresdb) < -width) {
483 } else if (2.f*(Lxg-thresdb) > width) {
484 Lyg = thresdb + (Lxg-thresdb)/ratio;
485 Lyg = sanitize_denormal(Lyg);
487 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
492 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
493 acomp->old_yl = sanitize_denormal(acomp->old_yl);
494 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
495 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
496 Ly1 = sanitize_denormal(Ly1);
497 Lyl = sanitize_denormal(Lyl);
500 Lgain = from_dB(cdb);
502 *(acomp->gainr) = Lyl;
503 if (Lyl > acomp->v_gainr) {
504 acomp->v_gainr = Lyl;
507 lgaininp = in0 * Lgain;
508 rgaininp = in1 * Lgain;
510 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
512 output0[i] = lgaininp * makeup_gain;
513 output1[i] = rgaininp * makeup_gain;
515 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
517 // TODO re-use local variables on stack
518 // store values back to acomp at the end of the inner-loop
524 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
525 acomp->makeup_gain = makeup_gain;
528 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl) + 1e-12; // crude LPF TODO use n_samples/rate TC
529 if (!isfinite_local (acomp->v_lvl)) {
532 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
533 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
534 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
536 acomp->need_expose = true;
537 acomp->v_lvl_in = v_lvl_in;
538 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
540 if (acomp->need_expose && acomp->queue_draw) {
541 acomp->need_expose = false;
542 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
548 deactivate(LV2_Handle instance)
554 cleanup(LV2_Handle instance)
557 AComp* acomp = (AComp*)instance;
558 if (acomp->display) {
559 cairo_surface_destroy (acomp->display);
568 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
573 comp_curve (AComp* self, float xg) {
574 const float knee = self->v_knee;
575 const float ratio = self->v_ratio;
576 const float thresdb = self->v_thresdb;
577 const float makeup = self->v_makeup;
579 const float width = 6.f * knee + 0.01f;
582 if (2.f * (xg - thresdb) < -width) {
584 } else if (2.f * (xg - thresdb) > width) {
585 yg = thresdb + (xg - thresdb) / ratio;
587 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
595 static LV2_Inline_Display_Image_Surface *
596 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
598 AComp* self = (AComp*)instance;
599 uint32_t h = MIN (w, max_h);
601 const float makeup_thres = self->v_thresdb + self->v_makeup;
603 if (!self->display || self->w != w || self->h != h) {
604 if (self->display) cairo_surface_destroy(self->display);
605 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
610 cairo_t* cr = cairo_create (self->display);
613 cairo_rectangle (cr, 0, 0, w, h);
614 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
617 cairo_set_line_width(cr, 1.0);
619 // draw grid 10dB steps
620 const double dash1[] = {1, 2};
621 const double dash2[] = {1, 3};
623 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
624 cairo_set_dash(cr, dash2, 2, 2);
625 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
627 for (uint32_t d = 1; d < 7; ++d) {
628 const float x = -.5 + floorf (w * (d * 10.f / 70.f));
629 const float y = -.5 + floorf (h * (d * 10.f / 70.f));
631 cairo_move_to (cr, x, 0);
632 cairo_line_to (cr, x, h);
635 cairo_move_to (cr, 0, y);
636 cairo_line_to (cr, w, y);
639 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
640 cairo_set_dash(cr, dash1, 2, 2);
641 if (self->v_thresdb < 0) {
642 const float y = -.5 + floorf (h * ((makeup_thres - 10.f) / -70.f));
643 cairo_move_to (cr, 0, y);
644 cairo_line_to (cr, w, y);
648 cairo_move_to (cr, 0, h);
649 cairo_line_to (cr, w, 0);
654 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
655 const float x = -.5 + floorf (w * (60.f / 70.f));
656 const float y = -.5 + floorf (h * (10.f / 70.f));
657 cairo_move_to (cr, x, 0);
658 cairo_line_to (cr, x, h);
660 cairo_move_to (cr, 0, y);
661 cairo_line_to (cr, w, y);
666 const float x = -.5 + floorf (w * (62.5f / 70.f));
667 const float y = -.5 + floorf (h * (10.0f / 70.f));
668 const float wd = floorf (w * (5.f / 70.f));
669 const float ht = floorf (h * (55.f / 70.f));
670 cairo_rectangle (cr, x, y, wd, ht);
673 const float h_gr = fminf (ht, floorf (h * self->v_gainr / 70.f));
674 cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
675 cairo_rectangle (cr, x, y, wd, h_gr);
677 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
678 cairo_rectangle (cr, x, y, wd, ht);
679 cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 1.0);
684 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
685 cairo_move_to (cr, 0, h);
687 for (uint32_t x = 0; x < w; ++x) {
689 const float x_db = 70.f * (-1.f + x / (float)w) + 10.f;
690 const float y_db = comp_curve (self, x_db) - 10.f;
691 const float y = h * (y_db / -70.f);
692 cairo_line_to (cr, x, y);
694 cairo_stroke_preserve (cr);
696 cairo_line_to (cr, w, h);
697 cairo_close_path (cr);
700 // draw signal level & reduction/gradient
701 const float top = comp_curve (self, 0) - 10.f;
702 cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
703 if (top > makeup_thres - 10.f) {
704 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
705 cairo_pattern_add_color_stop_rgba (pat, top / -70.f, 0.8, 0.1, 0.1, 0.5);
707 if (self->v_knee > 0) {
708 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres -10.f) / -70.f), 0.7, 0.7, 0.2, 0.5);
709 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - self->v_knee - 10.f) / -70.f), 0.5, 0.5, 0.5, 0.5);
711 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.f)/ -70.f), 0.7, 0.7, 0.2, 0.5);
712 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.01f) / -70.f), 0.5, 0.5, 0.5, 0.5);
714 cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
716 // maybe cut off at x-position?
717 const float x = w * (self->v_lvl_in + 60) / 70.f;
718 const float y = x + h*self->v_makeup;
719 cairo_rectangle (cr, 0, h - y, x, y);
720 if (self->v_ratio > 1.0) {
721 cairo_set_source (cr, pat);
723 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
727 cairo_pattern_destroy (pat); // TODO cache pattern
730 // create RGBA surface
732 cairo_surface_flush (self->display);
733 self->surf.width = cairo_image_surface_get_width (self->display);
734 self->surf.height = cairo_image_surface_get_height (self->display);
735 self->surf.stride = cairo_image_surface_get_stride (self->display);
736 self->surf.data = cairo_image_surface_get_data (self->display);
743 extension_data(const char* uri)
746 static const LV2_Inline_Display_Interface display = { render_inline };
747 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
754 static const LV2_Descriptor descriptor_mono = {
765 static const LV2_Descriptor descriptor_stereo = {
777 const LV2_Descriptor*
778 lv2_descriptor(uint32_t index)
782 return &descriptor_mono;
784 return &descriptor_stereo;