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"
70 LV2_Inline_Display_Image_Surface surf;
71 cairo_surface_t* display;
72 LV2_Inline_Display* queue_draw;
75 /* ports pointers are only valid during run so we'll
76 * have to cache them for the display, besides
77 * we do want to check for changes
89 instantiate(const LV2_Descriptor* descriptor,
91 const char* bundle_path,
92 const LV2_Feature* const* features)
94 AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
96 for (int i=0; features[i]; ++i) {
98 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
99 acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
105 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
106 acomp->need_expose = true;
108 return (LV2_Handle)acomp;
112 connect_port(LV2_Handle instance,
116 AComp* acomp = (AComp*)instance;
118 switch ((PortIndex)port) {
120 acomp->attack = (float*)data;
123 acomp->release = (float*)data;
126 acomp->knee = (float*)data;
129 acomp->ratio = (float*)data;
131 case ACOMP_THRESHOLD:
132 acomp->thresdb = (float*)data;
135 acomp->makeup = (float*)data;
138 acomp->gainr = (float*)data;
141 acomp->outlevel = (float*)data;
143 case ACOMP_SIDECHAIN:
144 acomp->sidechain = (float*)data;
147 acomp->input0 = (float*)data;
150 acomp->input1 = (float*)data;
153 acomp->output = (float*)data;
158 // Force already-denormal float value to zero
160 sanitize_denormal(float value) {
161 if (!isnormal(value)) {
169 return (exp(gdb/20.f*log(10.f)));
174 return (20.f*log10(g));
178 activate(LV2_Handle instance)
180 AComp* acomp = (AComp*)instance;
182 *(acomp->gainr) = 0.0f;
183 *(acomp->outlevel) = -45.0f;
184 acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
188 run(LV2_Handle instance, uint32_t n_samples)
190 AComp* acomp = (AComp*)instance;
192 const float* const input0 = acomp->input0;
193 const float* const input1 = acomp->input1;
194 float* const output = acomp->output;
196 float srate = acomp->srate;
197 float width = (6.f * *(acomp->knee)) + 0.01;
199 float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
200 float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
203 float lgaininp = 0.f;
205 float Lxg, Lxl, Lyg, Lyl, Ly1;
206 int usesidechain = (*(acomp->sidechain) < 0.5) ? 0 : 1;
211 float ratio = *(acomp->ratio);
212 float thresdb = *(acomp->thresdb);
215 if (acomp->v_knee != *acomp->knee) {
216 acomp->v_knee = *acomp->knee;
217 acomp->need_expose = true;
220 if (acomp->v_ratio != *acomp->ratio) {
221 acomp->v_ratio = *acomp->ratio;
222 acomp->need_expose = true;
225 if (acomp->v_thresdb != *acomp->thresdb) {
226 acomp->v_thresdb = *acomp->thresdb;
227 acomp->need_expose = true;
233 for (i = 0; i < n_samples; i++) {
236 ingain = usesidechain ? in1 : in0;
238 Lxg = (ingain==0.f) ? -160.f : to_dB(fabs(ingain));
239 Lxg = sanitize_denormal(Lxg);
241 Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
243 if (2.f*(Lxg-thresdb) < -width) {
246 Lyg = thresdb + (Lxg-thresdb)/ratio;
247 Lyg = sanitize_denormal(Lyg);
252 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
253 acomp->old_yl = sanitize_denormal(acomp->old_yl);
254 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
255 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
256 Ly1 = sanitize_denormal(Ly1);
257 Lyl = sanitize_denormal(Lyl);
260 Lgain = from_dB(cdb);
262 *(acomp->gainr) = Lyl;
267 lgaininp = in0 * Lgain;
268 output[i] = lgaininp * from_dB(*(acomp->makeup));
270 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
272 // TODO re-use local variables on stack
273 // store values back to acomp at the end of the inner-loop
279 *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
282 acomp->v_lvl += .1 * (in_peak - acomp->v_lvl); // crude LPF TODO use n_samples/rate TC
283 const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
284 const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
285 if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
287 acomp->need_expose = true;
288 acomp->v_lvl_in = v_lvl_in;
289 acomp->v_lvl_out = v_lvl_out - *acomp->makeup;
291 if (acomp->need_expose && acomp->queue_draw) {
292 acomp->need_expose = false;
293 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
299 deactivate(LV2_Handle instance)
305 cleanup(LV2_Handle instance)
308 AComp* acomp = (AComp*)instance;
309 if (acomp->display) {
310 cairo_surface_destroy (acomp->display);
319 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
324 comp_curve (AComp* self, float xg) {
325 const float knee = self->v_knee;
326 const float ratio = self->v_ratio;
327 const float thresdb = self->v_thresdb;
329 const float width = 6.f * knee + 0.01f;
332 if (2.f * (xg - thresdb) < -width) {
334 } else if (2.f * fabs (xg - thresdb) <= width) {
335 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
336 } else if (2.f * (xg - thresdb) > width) {
337 yg = thresdb + (xg - thresdb) / ratio;
342 static LV2_Inline_Display_Image_Surface *
343 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
345 AComp* self = (AComp*)instance;
346 uint32_t h = MIN (w, max_h);
348 if (!self->display || self->w != w || self->h != h) {
349 if (self->display) cairo_surface_destroy(self->display);
350 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
355 cairo_t* cr = cairo_create (self->display);
358 cairo_rectangle (cr, 0, 0, w, h);
359 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
362 cairo_set_line_width(cr, 1.0);
364 // draw grid 10dB steps
365 const double dash1[] = {1, 2};
366 const double dash2[] = {1, 3};
368 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
369 cairo_set_dash(cr, dash2, 2, 2);
370 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
372 for (uint32_t d = 1; d < 6; ++d) {
373 const float x = -.5 + floorf (w * (d * 10.f / 60.f));
374 const float y = -.5 + floorf (h * (d * 10.f / 60.f));
376 cairo_move_to (cr, x, 0);
377 cairo_line_to (cr, x, h);
380 cairo_move_to (cr, 0, y);
381 cairo_line_to (cr, w, y);
384 if (self->v_thresdb < 0) {
385 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
386 const float y = -.5 + floorf (h * (self->v_thresdb / -60.f));
387 cairo_set_dash(cr, dash1, 2, 2);
388 cairo_move_to (cr, 0, y);
389 cairo_line_to (cr, w, y);
391 cairo_move_to (cr, 0, h);
392 cairo_line_to (cr, w, 0);
399 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
400 cairo_move_to (cr, 0, h);
402 for (uint32_t x = 0; x < w; ++x) {
404 const float x_db = 60.f * (-1.f + x / (float)w);
405 const float y_db = comp_curve (self, x_db);
406 const float y = h * (y_db / -60.f);
407 cairo_line_to (cr, x, y);
409 cairo_stroke_preserve (cr);
411 cairo_line_to (cr, w, h);
412 cairo_close_path (cr);
415 // draw signal level & reduction/gradient
416 const float top = comp_curve (self, 0);
417 cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
418 if (top > self->v_thresdb) {
419 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
420 cairo_pattern_add_color_stop_rgba (pat, top / -60.f, 0.8, 0.1, 0.1, 0.5);
422 if (self->v_knee > 0) {
423 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
424 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - self->v_knee) / -60.f), 0.5, 0.5, 0.5, 0.5);
426 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
427 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - .01) / -60.f), 0.5, 0.5, 0.5, 0.5);
429 cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
431 // maybe cut off at x-position?
432 const float x = w * (self->v_lvl_in + 60) / 60.f;
433 const float y = h * (self->v_lvl_out + 60) / 60.f;
434 cairo_rectangle (cr, 0, h - y, x, y);
435 if (self->v_ratio > 1.0) {
436 cairo_set_source (cr, pat);
438 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
442 cairo_pattern_destroy (pat); // TODO cache pattern
445 // create RGBA surface
447 cairo_surface_flush (self->display);
448 self->surf.width = cairo_image_surface_get_width (self->display);
449 self->surf.height = cairo_image_surface_get_height (self->display);
450 self->surf.stride = cairo_image_surface_get_stride (self->display);
451 self->surf.data = cairo_image_surface_get_data (self->display);
458 extension_data(const char* uri)
460 static const LV2_Inline_Display_Interface display = { render_inline };
461 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
467 static const LV2_Descriptor descriptor = {
479 const LV2_Descriptor*
480 lv2_descriptor(uint32_t index)