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"
48 ACOMP_STEREO_ATTACK = 0,
52 ACOMP_STEREO_THRESHOLD,
56 ACOMP_STEREO_OUTLEVEL,
57 ACOMP_STEREO_SIDECHAIN,
90 LV2_Inline_Display_Image_Surface surf;
92 cairo_surface_t* display;
93 LV2_Inline_Display* queue_draw;
96 /* ports pointers are only valid during run so we'll
97 * have to cache them for the display, besides
98 * 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 AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
117 for (int i=0; features[i]; ++i) {
119 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
120 acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
126 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
128 acomp->need_expose = true;
131 return (LV2_Handle)acomp;
136 connect_port_mono(LV2_Handle instance,
140 AComp* acomp = (AComp*)instance;
142 switch ((PortIndexMono)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->input0 = (float*)data;
174 acomp->sc = (float*)data;
177 acomp->output0 = (float*)data;
183 connect_port_stereo(LV2_Handle instance,
187 AComp* acomp = (AComp*)instance;
189 switch ((PortIndexStereo)port) {
190 case ACOMP_STEREO_ATTACK:
191 acomp->attack = (float*)data;
193 case ACOMP_STEREO_RELEASE:
194 acomp->release = (float*)data;
196 case ACOMP_STEREO_KNEE:
197 acomp->knee = (float*)data;
199 case ACOMP_STEREO_RATIO:
200 acomp->ratio = (float*)data;
202 case ACOMP_STEREO_THRESHOLD:
203 acomp->thresdb = (float*)data;
205 case ACOMP_STEREO_MAKEUP:
206 acomp->makeup = (float*)data;
208 case ACOMP_STEREO_GAINR:
209 acomp->gainr = (float*)data;
211 case ACOMP_STEREO_OUTLEVEL:
212 acomp->outlevel = (float*)data;
214 case ACOMP_STEREO_SIDECHAIN:
215 acomp->sidechain = (float*)data;
217 case ACOMP_STEREO_INPUT0:
218 acomp->input0 = (float*)data;
220 case ACOMP_STEREO_INPUT1:
221 acomp->input1 = (float*)data;
223 case ACOMP_STEREO_SC:
224 acomp->sc = (float*)data;
226 case ACOMP_STEREO_OUTPUT0:
227 acomp->output0 = (float*)data;
229 case ACOMP_STEREO_OUTPUT1:
230 acomp->output1 = (float*)data;
235 // Force already-denormal float value to zero
237 sanitize_denormal(float value) {
238 if (!isnormal(value)) {
246 return (exp(gdb/20.f*log(10.f)));
251 return (20.f*log10(g));
255 activate(LV2_Handle instance)
257 AComp* acomp = (AComp*)instance;
259 *(acomp->gainr) = 0.0f;
260 *(acomp->outlevel) = -45.0f;
261 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
265 run_mono(LV2_Handle instance, uint32_t n_samples)
267 AComp* acomp = (AComp*)instance;
269 const float* const input = acomp->input0;
270 const float* const sc = acomp->sc;
271 float* const output = acomp->output0;
273 float srate = acomp->srate;
274 float width = (6.f * *(acomp->knee)) + 0.01;
276 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
277 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
280 float lgaininp = 0.f;
282 float Lxg, Lxl, Lyg, Lyl, Ly1;
283 int usesidechain = (*(acomp->sidechain) < 0.5) ? 0 : 1;
288 float ratio = *(acomp->ratio);
289 float thresdb = *(acomp->thresdb);
292 if (acomp->v_knee != *acomp->knee) {
293 acomp->v_knee = *acomp->knee;
294 acomp->need_expose = true;
297 if (acomp->v_ratio != *acomp->ratio) {
298 acomp->v_ratio = *acomp->ratio;
299 acomp->need_expose = true;
302 if (acomp->v_thresdb != *acomp->thresdb) {
303 acomp->v_thresdb = *acomp->thresdb;
304 acomp->need_expose = true;
310 for (i = 0; i < n_samples; i++) {
313 ingain = usesidechain ? fabs(sc0) : fabs(in0);
314 in_peak = fmaxf (in_peak, ingain);
316 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
317 Lxg = sanitize_denormal(Lxg);
320 if (2.f*(Lxg-thresdb) < -width) {
322 } else if (2.f*(Lxg-thresdb) > width) {
323 Lyg = thresdb + (Lxg-thresdb)/ratio;
324 Lyg = sanitize_denormal(Lyg);
326 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
331 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
332 acomp->old_yl = sanitize_denormal(acomp->old_yl);
333 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
334 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
335 Ly1 = sanitize_denormal(Ly1);
336 Lyl = sanitize_denormal(Lyl);
339 Lgain = from_dB(cdb);
341 *(acomp->gainr) = Lyl;
343 lgaininp = in0 * Lgain;
344 output[i] = lgaininp * from_dB(*(acomp->makeup));
346 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
348 // TODO re-use local variables on stack
349 // store values back to acomp at the end of the inner-loop
355 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
358 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl); // crude LPF TODO use n_samples/rate TC
359 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
360 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
361 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
363 acomp->need_expose = true;
364 acomp->v_lvl_in = v_lvl_in;
365 acomp->v_lvl_out = v_lvl_out - *acomp->makeup;
367 if (acomp->need_expose && acomp->queue_draw) {
368 acomp->need_expose = false;
369 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
375 run_stereo(LV2_Handle instance, uint32_t n_samples)
377 AComp* acomp = (AComp*)instance;
379 const float* const input0 = acomp->input0;
380 const float* const input1 = acomp->input1;
381 const float* const sc = acomp->sc;
382 float* const output0 = acomp->output0;
383 float* const output1 = acomp->output1;
385 float srate = acomp->srate;
386 float width = (6.f * *(acomp->knee)) + 0.01;
388 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
389 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
392 float lgaininp = 0.f;
393 float rgaininp = 0.f;
395 float Lxg, Lxl, Lyg, Lyl, Ly1;
396 int usesidechain = (*(acomp->sidechain) < 0.5) ? 0 : 1;
403 float ratio = *(acomp->ratio);
404 float thresdb = *(acomp->thresdb);
407 if (acomp->v_knee != *acomp->knee) {
408 acomp->v_knee = *acomp->knee;
409 acomp->need_expose = true;
412 if (acomp->v_ratio != *acomp->ratio) {
413 acomp->v_ratio = *acomp->ratio;
414 acomp->need_expose = true;
417 if (acomp->v_thresdb != *acomp->thresdb) {
418 acomp->v_thresdb = *acomp->thresdb;
419 acomp->need_expose = true;
425 for (i = 0; i < n_samples; i++) {
429 maxabslr = fmaxf(fabs(in0), fabs(in1));
430 ingain = usesidechain ? fabs(sc0) : maxabslr;
431 in_peak = fmaxf (in_peak, ingain);
433 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
434 Lxg = sanitize_denormal(Lxg);
437 if (2.f*(Lxg-thresdb) < -width) {
439 } else if (2.f*(Lxg-thresdb) > width) {
440 Lyg = thresdb + (Lxg-thresdb)/ratio;
441 Lyg = sanitize_denormal(Lyg);
443 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
448 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
449 acomp->old_yl = sanitize_denormal(acomp->old_yl);
450 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
451 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
452 Ly1 = sanitize_denormal(Ly1);
453 Lyl = sanitize_denormal(Lyl);
456 Lgain = from_dB(cdb);
458 *(acomp->gainr) = Lyl;
460 lgaininp = in0 * Lgain;
461 rgaininp = in1 * Lgain;
462 output0[i] = lgaininp * from_dB(*(acomp->makeup));
463 output1[i] = rgaininp * from_dB(*(acomp->makeup));
465 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
467 // TODO re-use local variables on stack
468 // store values back to acomp at the end of the inner-loop
474 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
477 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl); // crude LPF TODO use n_samples/rate TC
478 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
479 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
480 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
482 acomp->need_expose = true;
483 acomp->v_lvl_in = v_lvl_in;
484 acomp->v_lvl_out = v_lvl_out - *acomp->makeup;
486 if (acomp->need_expose && acomp->queue_draw) {
487 acomp->need_expose = false;
488 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
494 deactivate(LV2_Handle instance)
500 cleanup(LV2_Handle instance)
503 AComp* acomp = (AComp*)instance;
504 if (acomp->display) {
505 cairo_surface_destroy (acomp->display);
514 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
519 comp_curve (AComp* self, float xg) {
520 const float knee = self->v_knee;
521 const float ratio = self->v_ratio;
522 const float thresdb = self->v_thresdb;
524 const float width = 6.f * knee + 0.01f;
527 if (2.f * (xg - thresdb) < -width) {
529 } else if (2.f * (xg - thresdb) > width) {
530 yg = thresdb + (xg - thresdb) / ratio;
532 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
537 static LV2_Inline_Display_Image_Surface *
538 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
540 AComp* self = (AComp*)instance;
541 uint32_t h = MIN (w, max_h);
543 if (!self->display || self->w != w || self->h != h) {
544 if (self->display) cairo_surface_destroy(self->display);
545 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
550 cairo_t* cr = cairo_create (self->display);
553 cairo_rectangle (cr, 0, 0, w, h);
554 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
557 cairo_set_line_width(cr, 1.0);
559 // draw grid 10dB steps
560 const double dash1[] = {1, 2};
561 const double dash2[] = {1, 3};
563 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
564 cairo_set_dash(cr, dash2, 2, 2);
565 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
567 for (uint32_t d = 1; d < 6; ++d) {
568 const float x = -.5 + floorf (w * (d * 10.f / 60.f));
569 const float y = -.5 + floorf (h * (d * 10.f / 60.f));
571 cairo_move_to (cr, x, 0);
572 cairo_line_to (cr, x, h);
575 cairo_move_to (cr, 0, y);
576 cairo_line_to (cr, w, y);
579 if (self->v_thresdb < 0) {
580 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
581 const float y = -.5 + floorf (h * (self->v_thresdb / -60.f));
582 cairo_set_dash(cr, dash1, 2, 2);
583 cairo_move_to (cr, 0, y);
584 cairo_line_to (cr, w, y);
586 cairo_move_to (cr, 0, h);
587 cairo_line_to (cr, w, 0);
594 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
595 cairo_move_to (cr, 0, h);
597 for (uint32_t x = 0; x < w; ++x) {
599 const float x_db = 60.f * (-1.f + x / (float)w);
600 const float y_db = comp_curve (self, x_db);
601 const float y = h * (y_db / -60.f);
602 cairo_line_to (cr, x, y);
604 cairo_stroke_preserve (cr);
606 cairo_line_to (cr, w, h);
607 cairo_close_path (cr);
610 // draw signal level & reduction/gradient
611 const float top = comp_curve (self, 0);
612 cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
613 if (top > self->v_thresdb) {
614 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
615 cairo_pattern_add_color_stop_rgba (pat, top / -60.f, 0.8, 0.1, 0.1, 0.5);
617 if (self->v_knee > 0) {
618 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
619 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - self->v_knee) / -60.f), 0.5, 0.5, 0.5, 0.5);
621 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
622 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - .01) / -60.f), 0.5, 0.5, 0.5, 0.5);
624 cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
626 // maybe cut off at x-position?
627 const float x = w * (self->v_lvl_in + 60) / 60.f;
628 //const float y = h * (self->v_lvl_out + 60) / 60.f;
629 cairo_rectangle (cr, 0, h - x, x, h);
630 if (self->v_ratio > 1.0) {
631 cairo_set_source (cr, pat);
633 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
637 cairo_pattern_destroy (pat); // TODO cache pattern
640 // create RGBA surface
642 cairo_surface_flush (self->display);
643 self->surf.width = cairo_image_surface_get_width (self->display);
644 self->surf.height = cairo_image_surface_get_height (self->display);
645 self->surf.stride = cairo_image_surface_get_stride (self->display);
646 self->surf.data = cairo_image_surface_get_data (self->display);
653 extension_data(const char* uri)
656 static const LV2_Inline_Display_Interface display = { render_inline };
657 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
664 static const LV2_Descriptor descriptor_mono = {
675 static const LV2_Descriptor descriptor_stereo = {
687 const LV2_Descriptor*
688 lv2_descriptor(uint32_t index)
692 return &descriptor_mono;
694 return &descriptor_stereo;