Prepare a-comp for click-free bypass/enable
[ardour.git] / libs / plugins / a-comp.lv2 / a-comp.c
1 /* a-comp
2  * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
3  *
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.
8  *
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.
13  */
14
15 #include <math.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdbool.h>
19
20 #ifdef LV2_EXTENDED
21 #include <cairo/cairo.h>
22 #include "ardour/lv2_extensions.h"
23 #endif
24
25 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
26
27 #define ACOMP_URI               "urn:ardour:a-comp"
28 #define ACOMP_STEREO_URI        "urn:ardour:a-comp#stereo"
29
30 #ifndef M_PI
31 #  define M_PI 3.14159265358979323846
32 #endif
33
34 typedef enum {
35         ACOMP_ATTACK = 0,
36         ACOMP_RELEASE,
37         ACOMP_KNEE,
38         ACOMP_RATIO,
39         ACOMP_THRESHOLD,
40         ACOMP_MAKEUP,
41
42         ACOMP_GAINR,
43         ACOMP_OUTLEVEL,
44         ACOMP_SIDECHAIN,
45         ACOMP_ENABLE,
46
47         ACOMP_INPUT,
48         ACOMP_SC,
49         ACOMP_OUTPUT,
50         ACOMP_STEREO_INPUT,
51         ACOMP_STEREO_OUTPUT,
52 } PortIndex;
53
54 typedef struct {
55         float* attack;
56         float* release;
57         float* knee;
58         float* ratio;
59         float* thresdb;
60         float* makeup;
61
62         float* gainr;
63         float* outlevel;
64         float* sidechain;
65         float* enable;
66
67         float* input0;
68         float* input1;
69         float* sc;
70         float* output0;
71         float* output1;
72
73         float srate;
74         float old_yl;
75         float old_y1;
76         float old_yg;
77
78         float makeup_gain;
79         float tau;
80
81 #ifdef LV2_EXTENDED
82         LV2_Inline_Display_Image_Surface surf;
83         bool                     need_expose;
84         cairo_surface_t*         display;
85         LV2_Inline_Display*      queue_draw;
86         uint32_t                 w, h;
87
88         /* ports pointers are only valid during run so we'll
89          * have to cache them for the display, besides
90          * we do want to check for changes
91          */
92         float v_knee;
93         float v_ratio;
94         float v_thresdb;
95         float v_lvl;
96         float v_lvl_in;
97         float v_lvl_out;
98 #endif
99 } AComp;
100
101 static LV2_Handle
102 instantiate(const LV2_Descriptor* descriptor,
103             double rate,
104             const char* bundle_path,
105             const LV2_Feature* const* features)
106 {
107         AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
108
109         for (int i=0; features[i]; ++i) {
110 #ifdef LV2_EXTENDED
111                 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
112                         acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
113                 }
114 #endif
115         }
116
117         acomp->srate = rate;
118         acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
119         acomp->tau = (1.0 - exp (-2.f * M_PI * 25.f / acomp->srate));
120 #ifdef LV2_EXTENDED
121         acomp->need_expose = true;
122 #endif
123
124         return (LV2_Handle)acomp;
125 }
126
127
128 static void
129 connect_port(LV2_Handle instance,
130              uint32_t port,
131              void* data)
132 {
133         AComp* acomp = (AComp*)instance;
134
135         switch ((PortIndex)port) {
136         case ACOMP_ATTACK:
137                 acomp->attack = (float*)data;
138                 break;
139         case ACOMP_RELEASE:
140                 acomp->release = (float*)data;
141                 break;
142         case ACOMP_KNEE:
143                 acomp->knee = (float*)data;
144                 break;
145         case ACOMP_RATIO:
146                 acomp->ratio = (float*)data;
147                 break;
148         case ACOMP_THRESHOLD:
149                 acomp->thresdb = (float*)data;
150                 break;
151         case ACOMP_MAKEUP:
152                 acomp->makeup = (float*)data;
153                 break;
154         case ACOMP_GAINR:
155                 acomp->gainr = (float*)data;
156                 break;
157         case ACOMP_OUTLEVEL:
158                 acomp->outlevel = (float*)data;
159                 break;
160         case ACOMP_SIDECHAIN:
161                 acomp->sidechain = (float*)data;
162                 break;
163         case ACOMP_ENABLE:
164                 acomp->enable = (float*)data;
165                 break;
166         case ACOMP_INPUT:
167                 acomp->input0 = (float*)data;
168                 break;
169         case ACOMP_SC:
170                 acomp->sc = (float*)data;
171                 break;
172         case ACOMP_OUTPUT:
173                 acomp->output0 = (float*)data;
174                 break;
175         case ACOMP_STEREO_INPUT:
176                 acomp->input1 = (float*)data;
177                 break;
178         case ACOMP_STEREO_OUTPUT:
179                 acomp->output1 = (float*)data;
180                 break;
181         }
182 }
183
184 // Force already-denormal float value to zero
185 static inline float
186 sanitize_denormal(float value) {
187         if (!isnormal(value)) {
188                 value = 0.f;
189         }
190         return value;
191 }
192
193 static inline float
194 from_dB(float gdb) {
195         return (exp(gdb/20.f*log(10.f)));
196 }
197
198 static inline float
199 to_dB(float g) {
200         return (20.f*log10(g));
201 }
202
203 static void
204 activate(LV2_Handle instance)
205 {
206         AComp* acomp = (AComp*)instance;
207
208         *(acomp->gainr) = 0.0f;
209         *(acomp->outlevel) = -45.0f;
210         acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
211 }
212
213 static void
214 run_mono(LV2_Handle instance, uint32_t n_samples)
215 {
216         AComp* acomp = (AComp*)instance;
217
218         const float* const input = acomp->input0;
219         const float* const sc = acomp->sc;
220         float* const output = acomp->output0;
221
222         float srate = acomp->srate;
223         float width = (6.f * *(acomp->knee)) + 0.01;
224         float cdb=0.f;
225         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
226         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
227
228         float max = 0.f;
229         float lgaininp = 0.f;
230         float Lgain = 1.f;
231         float Lxg, Lxl, Lyg, Lyl, Ly1;
232         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
233         uint32_t i;
234         float ingain;
235         float in0;
236         float sc0;
237
238         float ratio = *acomp->ratio;
239         float thresdb = *acomp->thresdb;
240         float makeup_target = from_dB(*acomp->makeup);
241         float makeup_gain = acomp->makeup_gain;
242
243         const const float tau = acomp->tau;
244
245         if (*acomp->enable <= 0) {
246                 ratio = 1.f;
247                 thresdb = 0.f;
248                 makeup_target = 1.f;
249         }
250
251 #ifdef LV2_EXTENDED
252         if (acomp->v_knee != *acomp->knee) {
253                 acomp->v_knee = *acomp->knee;
254                 acomp->need_expose = true;
255         }
256
257         if (acomp->v_ratio != ratio) {
258                 acomp->v_ratio = ratio;
259                 acomp->need_expose = true;
260         }
261
262         if (acomp->v_thresdb != thresdb) {
263                 acomp->v_thresdb = thresdb;
264                 acomp->need_expose = true;
265         }
266 #endif
267
268         float in_peak = 0;
269
270         for (i = 0; i < n_samples; i++) {
271                 in0 = input[i];
272                 sc0 = sc[i];
273                 ingain = usesidechain ? fabs(sc0) : fabs(in0);
274                 in_peak = fmaxf (in_peak, ingain);
275                 Lyg = 0.f;
276                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
277                 Lxg = sanitize_denormal(Lxg);
278
279
280                 if (2.f*(Lxg-thresdb) < -width) {
281                         Lyg = Lxg;
282                 } else if (2.f*(Lxg-thresdb) > width) {
283                         Lyg = thresdb + (Lxg-thresdb)/ratio;
284                         Lyg = sanitize_denormal(Lyg);
285                 } else {
286                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
287                 }
288
289                 Lxl = Lxg - Lyg;
290
291                 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
292                 acomp->old_yl = sanitize_denormal(acomp->old_yl);
293                 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
294                 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
295                 Ly1 = sanitize_denormal(Ly1);
296                 Lyl = sanitize_denormal(Lyl);
297
298                 cdb = -Lyl;
299                 Lgain = from_dB(cdb);
300
301                 *(acomp->gainr) = Lyl;
302
303                 lgaininp = in0 * Lgain;
304
305                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
306                 output[i] = lgaininp * makeup_gain;
307
308                 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
309
310                 // TODO re-use local variables on stack
311                 // store values back to acomp at the end of the inner-loop
312                 acomp->old_yl = Lyl;
313                 acomp->old_y1 = Ly1;
314                 acomp->old_yg = Lyg;
315         }
316
317         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
318         acomp->makeup_gain = makeup_gain;
319
320 #ifdef LV2_EXTENDED
321         acomp->v_lvl += .1 * (in_peak - acomp->v_lvl);  // crude LPF TODO use n_samples/rate TC
322         const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
323         const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
324         if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
325                 // >= 1dB difference
326                 acomp->need_expose = true;
327                 acomp->v_lvl_in = v_lvl_in;
328                 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
329         }
330         if (acomp->need_expose && acomp->queue_draw) {
331                 acomp->need_expose = false;
332                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
333         }
334 #endif
335 }
336
337 static void
338 run_stereo(LV2_Handle instance, uint32_t n_samples)
339 {
340         AComp* acomp = (AComp*)instance;
341
342         const float* const input0 = acomp->input0;
343         const float* const input1 = acomp->input1;
344         const float* const sc = acomp->sc;
345         float* const output0 = acomp->output0;
346         float* const output1 = acomp->output1;
347
348         float srate = acomp->srate;
349         float width = (6.f * *(acomp->knee)) + 0.01;
350         float cdb=0.f;
351         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
352         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
353
354         float max = 0.f;
355         float lgaininp = 0.f;
356         float rgaininp = 0.f;
357         float Lgain = 1.f;
358         float Lxg, Lxl, Lyg, Lyl, Ly1;
359         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
360         uint32_t i;
361         float ingain;
362         float in0;
363         float in1;
364         float sc0;
365         float maxabslr;
366
367         float ratio = *acomp->ratio;
368         float thresdb = *acomp->thresdb;
369         float makeup_target = from_dB(*acomp->makeup);
370         float makeup_gain = acomp->makeup_gain;
371
372         const const float tau = acomp->tau;
373
374         if (*acomp->enable <= 0) {
375                 ratio = 1.f;
376                 thresdb = 0.f;
377                 makeup_target = 1.f;
378         }
379
380 #ifdef LV2_EXTENDED
381         if (acomp->v_knee != *acomp->knee) {
382                 acomp->v_knee = *acomp->knee;
383                 acomp->need_expose = true;
384         }
385
386         if (acomp->v_ratio != ratio) {
387                 acomp->v_ratio = ratio;
388                 acomp->need_expose = true;
389         }
390
391         if (acomp->v_thresdb != thresdb) {
392                 acomp->v_thresdb = thresdb;
393                 acomp->need_expose = true;
394         }
395 #endif
396
397         float in_peak = 0;
398
399         for (i = 0; i < n_samples; i++) {
400                 in0 = input0[i];
401                 in1 = input1[i];
402                 sc0 = sc[i];
403                 maxabslr = fmaxf(fabs(in0), fabs(in1));
404                 ingain = usesidechain ? fabs(sc0) : maxabslr;
405                 in_peak = fmaxf (in_peak, ingain);
406                 Lyg = 0.f;
407                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
408                 Lxg = sanitize_denormal(Lxg);
409
410
411                 if (2.f*(Lxg-thresdb) < -width) {
412                         Lyg = Lxg;
413                 } else if (2.f*(Lxg-thresdb) > width) {
414                         Lyg = thresdb + (Lxg-thresdb)/ratio;
415                         Lyg = sanitize_denormal(Lyg);
416                 } else {
417                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
418                 }
419
420                 Lxl = Lxg - Lyg;
421
422                 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
423                 acomp->old_yl = sanitize_denormal(acomp->old_yl);
424                 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
425                 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
426                 Ly1 = sanitize_denormal(Ly1);
427                 Lyl = sanitize_denormal(Lyl);
428
429                 cdb = -Lyl;
430                 Lgain = from_dB(cdb);
431
432                 *(acomp->gainr) = Lyl;
433
434                 lgaininp = in0 * Lgain;
435                 rgaininp = in1 * Lgain;
436
437                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
438
439                 output0[i] = lgaininp * makeup_gain;
440                 output1[i] = rgaininp * makeup_gain;
441
442                 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
443
444                 // TODO re-use local variables on stack
445                 // store values back to acomp at the end of the inner-loop
446                 acomp->old_yl = Lyl;
447                 acomp->old_y1 = Ly1;
448                 acomp->old_yg = Lyg;
449         }
450
451         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
452         acomp->makeup_gain = makeup_gain;
453
454 #ifdef LV2_EXTENDED
455         acomp->v_lvl += .1 * (in_peak - acomp->v_lvl);  // crude LPF TODO use n_samples/rate TC
456         const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
457         const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
458         if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
459                 // >= 1dB difference
460                 acomp->need_expose = true;
461                 acomp->v_lvl_in = v_lvl_in;
462                 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
463         }
464         if (acomp->need_expose && acomp->queue_draw) {
465                 acomp->need_expose = false;
466                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
467         }
468 #endif
469 }
470
471 static void
472 deactivate(LV2_Handle instance)
473 {
474         activate(instance);
475 }
476
477 static void
478 cleanup(LV2_Handle instance)
479 {
480 #ifdef LV2_EXTENDED
481         AComp* acomp = (AComp*)instance;
482         if (acomp->display) {
483                 cairo_surface_destroy (acomp->display);
484         }
485 #endif
486
487         free(instance);
488 }
489
490
491 #ifndef MIN
492 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
493 #endif
494
495 #ifdef LV2_EXTENDED
496 static float
497 comp_curve (AComp* self, float xg) {
498         const float knee = self->v_knee;
499         const float ratio = self->v_ratio;
500         const float thresdb = self->v_thresdb;
501
502         const float width = 6.f * knee + 0.01f;
503         float yg = 0.f;
504
505         if (2.f * (xg - thresdb) < -width) {
506                 yg = xg;
507         } else if (2.f * (xg - thresdb) > width) {
508                 yg = thresdb + (xg - thresdb) / ratio;
509         } else {
510                 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
511         }
512         return yg;
513 }
514
515 static LV2_Inline_Display_Image_Surface *
516 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
517 {
518         AComp* self = (AComp*)instance;
519         uint32_t h = MIN (w, max_h);
520
521         if (!self->display || self->w != w || self->h != h) {
522                 if (self->display) cairo_surface_destroy(self->display);
523                 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
524                 self->w = w;
525                 self->h = h;
526         }
527
528         cairo_t* cr = cairo_create (self->display);
529
530         // clear background
531         cairo_rectangle (cr, 0, 0, w, h);
532         cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
533         cairo_fill (cr);
534
535         cairo_set_line_width(cr, 1.0);
536
537         // draw grid 10dB steps
538         const double dash1[] = {1, 2};
539         const double dash2[] = {1, 3};
540         cairo_save (cr);
541         cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
542         cairo_set_dash(cr, dash2, 2, 2);
543         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
544
545         for (uint32_t d = 1; d < 6; ++d) {
546                 const float x = -.5 + floorf (w * (d * 10.f / 60.f));
547                 const float y = -.5 + floorf (h * (d * 10.f / 60.f));
548
549                 cairo_move_to (cr, x, 0);
550                 cairo_line_to (cr, x, h);
551                 cairo_stroke (cr);
552
553                 cairo_move_to (cr, 0, y);
554                 cairo_line_to (cr, w, y);
555                 cairo_stroke (cr);
556         }
557         if (self->v_thresdb < 0) {
558                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
559                 const float y = -.5 + floorf (h * (self->v_thresdb / -60.f));
560                 cairo_set_dash(cr, dash1, 2, 2);
561                 cairo_move_to (cr, 0, y);
562                 cairo_line_to (cr, w, y);
563                 cairo_stroke (cr);
564                 cairo_move_to (cr, 0, h);
565                 cairo_line_to (cr, w, 0);
566                 cairo_stroke (cr);
567         }
568         cairo_restore (cr);
569
570
571         // draw curve
572         cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
573         cairo_move_to (cr, 0, h);
574
575         for (uint32_t x = 0; x < w; ++x) {
576                 // plot -60..0  dB
577                 const float x_db = 60.f * (-1.f + x / (float)w);
578                 const float y_db = comp_curve (self, x_db);
579                 const float y = h * (y_db / -60.f);
580                 cairo_line_to (cr, x, y);
581         }
582         cairo_stroke_preserve (cr);
583
584         cairo_line_to (cr, w, h);
585         cairo_close_path (cr);
586         cairo_clip (cr);
587
588         // draw signal level & reduction/gradient
589         const float top = comp_curve (self, 0);
590         cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
591         if (top > self->v_thresdb) {
592                 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
593                 cairo_pattern_add_color_stop_rgba (pat, top / -60.f, 0.8, 0.1, 0.1, 0.5);
594         }
595         if (self->v_knee > 0) {
596                 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
597                 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - self->v_knee) / -60.f), 0.5, 0.5, 0.5, 0.5);
598         } else {
599                 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
600                 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - .01) / -60.f), 0.5, 0.5, 0.5, 0.5);
601         }
602         cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
603
604         // maybe cut off at x-position?
605         const float x = w * (self->v_lvl_in + 60) / 60.f;
606         //const float y = h * (self->v_lvl_out + 60) / 60.f;
607         cairo_rectangle (cr, 0, h - x, x, h);
608         if (self->v_ratio > 1.0) {
609                 cairo_set_source (cr, pat);
610         } else {
611                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
612         }
613         cairo_fill (cr);
614
615         cairo_pattern_destroy (pat); // TODO cache pattern
616
617
618         // create RGBA surface
619         cairo_destroy (cr);
620         cairo_surface_flush (self->display);
621         self->surf.width = cairo_image_surface_get_width (self->display);
622         self->surf.height = cairo_image_surface_get_height (self->display);
623         self->surf.stride = cairo_image_surface_get_stride (self->display);
624         self->surf.data = cairo_image_surface_get_data  (self->display);
625
626         return &self->surf;
627 }
628 #endif
629
630 static const void*
631 extension_data(const char* uri)
632 {
633 #ifdef LV2_EXTENDED
634         static const LV2_Inline_Display_Interface display  = { render_inline };
635         if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
636                 return &display;
637         }
638 #endif
639         return NULL;
640 }
641
642 static const LV2_Descriptor descriptor_mono = {
643         ACOMP_URI,
644         instantiate,
645         connect_port,
646         activate,
647         run_mono,
648         deactivate,
649         cleanup,
650         extension_data
651 };
652
653 static const LV2_Descriptor descriptor_stereo = {
654         ACOMP_STEREO_URI,
655         instantiate,
656         connect_port,
657         activate,
658         run_stereo,
659         deactivate,
660         cleanup,
661         extension_data
662 };
663
664 LV2_SYMBOL_EXPORT
665 const LV2_Descriptor*
666 lv2_descriptor(uint32_t index)
667 {
668         switch (index) {
669         case 0:
670                 return &descriptor_mono;
671         case 1:
672                 return &descriptor_stereo;
673         default:
674                 return NULL;
675         }
676 }