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
109 instantiate(const LV2_Descriptor* descriptor,
111 const char* bundle_path,
112 const LV2_Feature* const* features)
114 AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
116 for (int i=0; features[i]; ++i) {
118 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
119 acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
125 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
126 acomp->tau = (1.0 - exp (-2.f * M_PI * 25.f / acomp->srate));
128 acomp->need_expose = true;
131 return (LV2_Handle)acomp;
136 connect_port(LV2_Handle instance,
140 AComp* acomp = (AComp*)instance;
142 switch ((PortIndex)port) {
144 acomp->attack = (float*)data;
147 acomp->release = (float*)data;
150 acomp->knee = (float*)data;
153 acomp->ratio = (float*)data;
155 case ACOMP_THRESHOLD:
156 acomp->thresdb = (float*)data;
159 acomp->makeup = (float*)data;
162 acomp->gainr = (float*)data;
165 acomp->outlevel = (float*)data;
167 case ACOMP_SIDECHAIN:
168 acomp->sidechain = (float*)data;
171 acomp->enable = (float*)data;
179 connect_mono(LV2_Handle instance,
183 AComp* acomp = (AComp*)instance;
184 connect_port (instance, port, data);
186 switch ((PortIndex)port) {
188 acomp->input0 = (float*)data;
191 acomp->sc = (float*)data;
194 acomp->output0 = (float*)data;
202 connect_stereo(LV2_Handle instance,
206 AComp* acomp = (AComp*)instance;
207 connect_port (instance, port, data);
209 switch ((PortIndex)port) {
211 acomp->input0 = (float*)data;
214 acomp->input1 = (float*)data;
217 acomp->sc = (float*)data;
220 acomp->output0 = (float*)data;
223 acomp->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 AComp* acomp = (AComp*)instance;
254 *(acomp->gainr) = 0.0f;
255 *(acomp->outlevel) = -45.0f;
256 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
260 run_mono(LV2_Handle instance, uint32_t n_samples)
262 AComp* acomp = (AComp*)instance;
264 const float* const input = acomp->input0;
265 const float* const sc = acomp->sc;
266 float* const output = acomp->output0;
268 float srate = acomp->srate;
269 float width = (6.f * *(acomp->knee)) + 0.01;
271 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
272 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
275 float lgaininp = 0.f;
277 float Lxg, Lxl, Lyg, Lyl, Ly1;
278 int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
284 float ratio = *acomp->ratio;
285 float thresdb = *acomp->thresdb;
286 float makeup_target = from_dB(*acomp->makeup);
287 float makeup_gain = acomp->makeup_gain;
289 const const float tau = acomp->tau;
291 if (*acomp->enable <= 0) {
298 if (acomp->v_knee != *acomp->knee) {
299 acomp->v_knee = *acomp->knee;
300 acomp->need_expose = true;
303 if (acomp->v_ratio != ratio) {
304 acomp->v_ratio = ratio;
305 acomp->need_expose = true;
308 if (acomp->v_thresdb != thresdb) {
309 acomp->v_thresdb = thresdb;
310 acomp->need_expose = true;
316 for (i = 0; i < n_samples; i++) {
319 ingain = usesidechain ? fabs(sc0) : fabs(in0);
320 in_peak = fmaxf (in_peak, ingain);
322 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
323 Lxg = sanitize_denormal(Lxg);
326 if (2.f*(Lxg-thresdb) < -width) {
328 } else if (2.f*(Lxg-thresdb) > width) {
329 Lyg = thresdb + (Lxg-thresdb)/ratio;
330 Lyg = sanitize_denormal(Lyg);
332 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
337 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
338 acomp->old_yl = sanitize_denormal(acomp->old_yl);
339 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
340 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
341 Ly1 = sanitize_denormal(Ly1);
342 Lyl = sanitize_denormal(Lyl);
345 Lgain = from_dB(cdb);
347 *(acomp->gainr) = Lyl;
349 lgaininp = in0 * Lgain;
351 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
352 output[i] = lgaininp * makeup_gain;
354 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
356 // TODO re-use local variables on stack
357 // store values back to acomp at the end of the inner-loop
363 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
364 acomp->makeup_gain = makeup_gain;
367 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl) + 1e-12; // crude LPF TODO use n_samples/rate TC
368 if (!isfinite_local (acomp->v_lvl)) {
371 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
372 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
373 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
375 acomp->need_expose = true;
376 acomp->v_lvl_in = v_lvl_in;
377 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
379 if (acomp->need_expose && acomp->queue_draw) {
380 acomp->need_expose = false;
381 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
387 run_stereo(LV2_Handle instance, uint32_t n_samples)
389 AComp* acomp = (AComp*)instance;
391 const float* const input0 = acomp->input0;
392 const float* const input1 = acomp->input1;
393 const float* const sc = acomp->sc;
394 float* const output0 = acomp->output0;
395 float* const output1 = acomp->output1;
397 float srate = acomp->srate;
398 float width = (6.f * *(acomp->knee)) + 0.01;
400 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
401 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
404 float lgaininp = 0.f;
405 float rgaininp = 0.f;
407 float Lxg, Lxl, Lyg, Lyl, Ly1;
408 int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
416 float ratio = *acomp->ratio;
417 float thresdb = *acomp->thresdb;
418 float makeup_target = from_dB(*acomp->makeup);
419 float makeup_gain = acomp->makeup_gain;
421 const const float tau = acomp->tau;
423 if (*acomp->enable <= 0) {
430 if (acomp->v_knee != *acomp->knee) {
431 acomp->v_knee = *acomp->knee;
432 acomp->need_expose = true;
435 if (acomp->v_ratio != ratio) {
436 acomp->v_ratio = ratio;
437 acomp->need_expose = true;
440 if (acomp->v_thresdb != thresdb) {
441 acomp->v_thresdb = thresdb;
442 acomp->need_expose = true;
448 for (i = 0; i < n_samples; i++) {
452 maxabslr = fmaxf(fabs(in0), fabs(in1));
453 ingain = usesidechain ? fabs(sc0) : maxabslr;
454 in_peak = fmaxf (in_peak, ingain);
456 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
457 Lxg = sanitize_denormal(Lxg);
460 if (2.f*(Lxg-thresdb) < -width) {
462 } else if (2.f*(Lxg-thresdb) > width) {
463 Lyg = thresdb + (Lxg-thresdb)/ratio;
464 Lyg = sanitize_denormal(Lyg);
466 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
471 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
472 acomp->old_yl = sanitize_denormal(acomp->old_yl);
473 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
474 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
475 Ly1 = sanitize_denormal(Ly1);
476 Lyl = sanitize_denormal(Lyl);
479 Lgain = from_dB(cdb);
481 *(acomp->gainr) = Lyl;
483 lgaininp = in0 * Lgain;
484 rgaininp = in1 * Lgain;
486 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
488 output0[i] = lgaininp * makeup_gain;
489 output1[i] = rgaininp * makeup_gain;
491 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
493 // TODO re-use local variables on stack
494 // store values back to acomp at the end of the inner-loop
500 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
501 acomp->makeup_gain = makeup_gain;
504 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl) + 1e-12; // crude LPF TODO use n_samples/rate TC
505 if (!isfinite_local (acomp->v_lvl)) {
508 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
509 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
510 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
512 acomp->need_expose = true;
513 acomp->v_lvl_in = v_lvl_in;
514 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
516 if (acomp->need_expose && acomp->queue_draw) {
517 acomp->need_expose = false;
518 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
524 deactivate(LV2_Handle instance)
530 cleanup(LV2_Handle instance)
533 AComp* acomp = (AComp*)instance;
534 if (acomp->display) {
535 cairo_surface_destroy (acomp->display);
544 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
549 comp_curve (AComp* self, float xg) {
550 const float knee = self->v_knee;
551 const float ratio = self->v_ratio;
552 const float thresdb = self->v_thresdb;
554 const float width = 6.f * knee + 0.01f;
557 if (2.f * (xg - thresdb) < -width) {
559 } else if (2.f * (xg - thresdb) > width) {
560 yg = thresdb + (xg - thresdb) / ratio;
562 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
567 static LV2_Inline_Display_Image_Surface *
568 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
570 AComp* self = (AComp*)instance;
571 uint32_t h = MIN (w, max_h);
573 if (!self->display || self->w != w || self->h != h) {
574 if (self->display) cairo_surface_destroy(self->display);
575 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
580 cairo_t* cr = cairo_create (self->display);
583 cairo_rectangle (cr, 0, 0, w, h);
584 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
587 cairo_set_line_width(cr, 1.0);
589 // draw grid 10dB steps
590 const double dash1[] = {1, 2};
591 const double dash2[] = {1, 3};
593 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
594 cairo_set_dash(cr, dash2, 2, 2);
595 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
597 for (uint32_t d = 1; d < 6; ++d) {
598 const float x = -.5 + floorf (w * (d * 10.f / 60.f));
599 const float y = -.5 + floorf (h * (d * 10.f / 60.f));
601 cairo_move_to (cr, x, 0);
602 cairo_line_to (cr, x, h);
605 cairo_move_to (cr, 0, y);
606 cairo_line_to (cr, w, y);
609 if (self->v_thresdb < 0) {
610 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
611 const float y = -.5 + floorf (h * (self->v_thresdb / -60.f));
612 cairo_set_dash(cr, dash1, 2, 2);
613 cairo_move_to (cr, 0, y);
614 cairo_line_to (cr, w, y);
616 cairo_move_to (cr, 0, h);
617 cairo_line_to (cr, w, 0);
624 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
625 cairo_move_to (cr, 0, h);
627 for (uint32_t x = 0; x < w; ++x) {
629 const float x_db = 60.f * (-1.f + x / (float)w);
630 const float y_db = comp_curve (self, x_db);
631 const float y = h * (y_db / -60.f);
632 cairo_line_to (cr, x, y);
634 cairo_stroke_preserve (cr);
636 cairo_line_to (cr, w, h);
637 cairo_close_path (cr);
640 // draw signal level & reduction/gradient
641 const float top = comp_curve (self, 0);
642 cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
643 if (top > self->v_thresdb) {
644 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
645 cairo_pattern_add_color_stop_rgba (pat, top / -60.f, 0.8, 0.1, 0.1, 0.5);
647 if (self->v_knee > 0) {
648 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
649 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - self->v_knee) / -60.f), 0.5, 0.5, 0.5, 0.5);
651 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
652 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - .01) / -60.f), 0.5, 0.5, 0.5, 0.5);
654 cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
656 // maybe cut off at x-position?
657 const float x = w * (self->v_lvl_in + 60) / 60.f;
658 //const float y = h * (self->v_lvl_out + 60) / 60.f;
659 cairo_rectangle (cr, 0, h - x, x, h);
660 if (self->v_ratio > 1.0) {
661 cairo_set_source (cr, pat);
663 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
667 cairo_pattern_destroy (pat); // TODO cache pattern
670 // create RGBA surface
672 cairo_surface_flush (self->display);
673 self->surf.width = cairo_image_surface_get_width (self->display);
674 self->surf.height = cairo_image_surface_get_height (self->display);
675 self->surf.stride = cairo_image_surface_get_stride (self->display);
676 self->surf.data = cairo_image_surface_get_data (self->display);
683 extension_data(const char* uri)
686 static const LV2_Inline_Display_Interface display = { render_inline };
687 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
694 static const LV2_Descriptor descriptor_mono = {
705 static const LV2_Descriptor descriptor_stereo = {
717 const LV2_Descriptor*
718 lv2_descriptor(uint32_t index)
722 return &descriptor_mono;
724 return &descriptor_stereo;