615646d943ca55560a0f9da0fd97eb8286290d7b
[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 #ifdef COMPILER_MSVC
35 #include <float.h>
36 #define isfinite_local(val) (bool)_finite((double)val)
37 #else
38 #define isfinite_local isfinite
39 #endif
40
41 typedef enum {
42         ACOMP_ATTACK = 0,
43         ACOMP_RELEASE,
44         ACOMP_KNEE,
45         ACOMP_RATIO,
46         ACOMP_THRESHOLD,
47         ACOMP_MAKEUP,
48
49         ACOMP_GAINR,
50         ACOMP_OUTLEVEL,
51         ACOMP_SIDECHAIN,
52         ACOMP_ENABLE,
53
54         ACOMP_A0,
55         ACOMP_A1,
56         ACOMP_A2,
57         ACOMP_A3,
58         ACOMP_A4,
59 } PortIndex;
60
61 typedef struct {
62         float* attack;
63         float* release;
64         float* knee;
65         float* ratio;
66         float* thresdb;
67         float* makeup;
68
69         float* gainr;
70         float* outlevel;
71         float* sidechain;
72         float* enable;
73
74         float* input0;
75         float* input1;
76         float* sc;
77         float* output0;
78         float* output1;
79
80         float srate;
81         float old_yl;
82         float old_y1;
83         float old_yg;
84
85         float makeup_gain;
86         float tau;
87
88 #ifdef LV2_EXTENDED
89         LV2_Inline_Display_Image_Surface surf;
90         bool                     need_expose;
91         cairo_surface_t*         display;
92         LV2_Inline_Display*      queue_draw;
93         uint32_t                 w, h;
94
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
98          */
99         float v_knee;
100         float v_ratio;
101         float v_thresdb;
102         float v_gainr;
103         float v_makeup;
104         float v_lvl;
105         float v_lvl_in;
106         float v_lvl_out;
107 #endif
108 } AComp;
109
110 static LV2_Handle
111 instantiate(const LV2_Descriptor* descriptor,
112             double rate,
113             const char* bundle_path,
114             const LV2_Feature* const* features)
115 {
116         AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
117
118         for (int i=0; features[i]; ++i) {
119 #ifdef LV2_EXTENDED
120                 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
121                         acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
122                 }
123 #endif
124         }
125
126         acomp->srate = rate;
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));
129 #ifdef LV2_EXTENDED
130         acomp->need_expose = true;
131 #endif
132
133         return (LV2_Handle)acomp;
134 }
135
136
137 static void
138 connect_port(LV2_Handle instance,
139              uint32_t port,
140              void* data)
141 {
142         AComp* acomp = (AComp*)instance;
143
144         switch ((PortIndex)port) {
145                 case ACOMP_ATTACK:
146                         acomp->attack = (float*)data;
147                         break;
148                 case ACOMP_RELEASE:
149                         acomp->release = (float*)data;
150                         break;
151                 case ACOMP_KNEE:
152                         acomp->knee = (float*)data;
153                         break;
154                 case ACOMP_RATIO:
155                         acomp->ratio = (float*)data;
156                         break;
157                 case ACOMP_THRESHOLD:
158                         acomp->thresdb = (float*)data;
159                         break;
160                 case ACOMP_MAKEUP:
161                         acomp->makeup = (float*)data;
162                         break;
163                 case ACOMP_GAINR:
164                         acomp->gainr = (float*)data;
165                         break;
166                 case ACOMP_OUTLEVEL:
167                         acomp->outlevel = (float*)data;
168                         break;
169                 case ACOMP_SIDECHAIN:
170                         acomp->sidechain = (float*)data;
171                         break;
172                 case ACOMP_ENABLE:
173                         acomp->enable = (float*)data;
174                         break;
175                 default:
176                         break;
177         }
178 }
179
180 static void
181 connect_mono(LV2_Handle instance,
182              uint32_t port,
183              void* data)
184 {
185         AComp* acomp = (AComp*)instance;
186         connect_port (instance, port, data);
187
188         switch ((PortIndex)port) {
189                 case ACOMP_A0:
190                         acomp->input0 = (float*)data;
191                         break;
192                 case ACOMP_A1:
193                         acomp->sc = (float*)data;
194                         break;
195                 case ACOMP_A2:
196                         acomp->output0 = (float*)data;
197                         break;
198         default:
199                 break;
200         }
201 }
202
203 static void
204 connect_stereo(LV2_Handle instance,
205                uint32_t port,
206                void* data)
207 {
208         AComp* acomp = (AComp*)instance;
209         connect_port (instance, port, data);
210
211         switch ((PortIndex)port) {
212                 case ACOMP_A0:
213                         acomp->input0 = (float*)data;
214                         break;
215                 case ACOMP_A1:
216                         acomp->input1 = (float*)data;
217                         break;
218                 case ACOMP_A2:
219                         acomp->sc = (float*)data;
220                         break;
221                 case ACOMP_A3:
222                         acomp->output0 = (float*)data;
223                         break;
224                 case ACOMP_A4:
225                         acomp->output1 = (float*)data;
226                         break;
227         default:
228                 break;
229         }
230 }
231
232 // Force already-denormal float value to zero
233 static inline float
234 sanitize_denormal(float value) {
235         if (!isnormal(value)) {
236                 value = 0.f;
237         }
238         return value;
239 }
240
241 static inline float
242 from_dB(float gdb) {
243         return (exp(gdb/20.f*log(10.f)));
244 }
245
246 static inline float
247 to_dB(float g) {
248         return (20.f*log10(g));
249 }
250
251 static void
252 activate(LV2_Handle instance)
253 {
254         AComp* acomp = (AComp*)instance;
255
256         *(acomp->gainr) = 0.0f;
257         *(acomp->outlevel) = -45.0f;
258         acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
259 }
260
261 static void
262 run_mono(LV2_Handle instance, uint32_t n_samples)
263 {
264         AComp* acomp = (AComp*)instance;
265
266         const float* const input = acomp->input0;
267         const float* const sc = acomp->sc;
268         float* const output = acomp->output0;
269
270         float srate = acomp->srate;
271         float width = (6.f * *(acomp->knee)) + 0.01;
272         float cdb=0.f;
273         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
274         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
275
276         float max = 0.f;
277         float lgaininp = 0.f;
278         float Lgain = 1.f;
279         float Lxg, Lxl, Lyg, Lyl, Ly1;
280         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
281         uint32_t i;
282         float ingain;
283         float in0;
284         float sc0;
285
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;
291
292         const float tau = acomp->tau;
293
294         if (*acomp->enable <= 0) {
295                 ratio = 1.f;
296                 thresdb = 0.f;
297                 makeup = 0.f;
298                 makeup_target = 1.f;
299         }
300
301 #ifdef LV2_EXTENDED
302         if (acomp->v_knee != *acomp->knee) {
303                 acomp->v_knee = *acomp->knee;
304                 acomp->need_expose = true;
305         }
306
307         if (acomp->v_ratio != ratio) {
308                 acomp->v_ratio = ratio;
309                 acomp->need_expose = true;
310         }
311
312         if (acomp->v_thresdb != thresdb) {
313                 acomp->v_thresdb = thresdb;
314                 acomp->need_expose = true;
315         }
316
317         if (acomp->v_makeup != makeup) {
318                 acomp->v_makeup = makeup;
319                 acomp->need_expose = true;
320         }
321 #endif
322
323         float in_peak = 0;
324         acomp->v_gainr = 0.0;
325
326         for (i = 0; i < n_samples; i++) {
327                 in0 = input[i];
328                 sc0 = sc[i];
329                 ingain = usesidechain ? fabs(sc0) : fabs(in0);
330                 in_peak = fmaxf (in_peak, ingain);
331                 Lyg = 0.f;
332                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
333                 Lxg = sanitize_denormal(Lxg);
334
335
336                 if (2.f*(Lxg-thresdb) < -width) {
337                         Lyg = Lxg;
338                 } else if (2.f*(Lxg-thresdb) > width) {
339                         Lyg = thresdb + (Lxg-thresdb)/ratio;
340                         Lyg = sanitize_denormal(Lyg);
341                 } else {
342                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
343                 }
344
345                 Lxl = Lxg - Lyg;
346
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);
353
354                 cdb = -Lyl;
355                 Lgain = from_dB(cdb);
356
357                 *(acomp->gainr) = Lyl;
358                 if (Lyl > acomp->v_gainr) {
359                         acomp->v_gainr = Lyl;
360                 }
361
362                 lgaininp = in0 * Lgain;
363
364                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
365                 output[i] = lgaininp * makeup_gain;
366
367                 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
368
369                 // TODO re-use local variables on stack
370                 // store values back to acomp at the end of the inner-loop
371                 acomp->old_yl = Lyl;
372                 acomp->old_y1 = Ly1;
373                 acomp->old_yg = Lyg;
374         }
375
376         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
377         acomp->makeup_gain = makeup_gain;
378
379 #ifdef LV2_EXTENDED
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)) {
382                 acomp->v_lvl = 0.f;
383         }
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) {
387                 // >= 1dB difference
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);
391         }
392         if (acomp->need_expose && acomp->queue_draw) {
393                 acomp->need_expose = false;
394                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
395         }
396 #endif
397 }
398
399 static void
400 run_stereo(LV2_Handle instance, uint32_t n_samples)
401 {
402         AComp* acomp = (AComp*)instance;
403
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;
409
410         float srate = acomp->srate;
411         float width = (6.f * *(acomp->knee)) + 0.01;
412         float cdb=0.f;
413         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
414         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
415
416         float max = 0.f;
417         float lgaininp = 0.f;
418         float rgaininp = 0.f;
419         float Lgain = 1.f;
420         float Lxg, Lxl, Lyg, Lyl, Ly1;
421         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
422         uint32_t i;
423         float ingain;
424         float in0;
425         float in1;
426         float sc0;
427         float maxabslr;
428
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;
434
435         const float tau = acomp->tau;
436
437         if (*acomp->enable <= 0) {
438                 ratio = 1.f;
439                 thresdb = 0.f;
440                 makeup = 0.f;
441                 makeup_target = 1.f;
442         }
443
444 #ifdef LV2_EXTENDED
445         if (acomp->v_knee != *acomp->knee) {
446                 acomp->v_knee = *acomp->knee;
447                 acomp->need_expose = true;
448         }
449
450         if (acomp->v_ratio != ratio) {
451                 acomp->v_ratio = ratio;
452                 acomp->need_expose = true;
453         }
454
455         if (acomp->v_thresdb != thresdb) {
456                 acomp->v_thresdb = thresdb;
457                 acomp->need_expose = true;
458         }
459
460         if (acomp->v_makeup != makeup) {
461                 acomp->v_makeup = makeup;
462                 acomp->need_expose = true;
463         }
464 #endif
465
466         float in_peak = 0;
467         acomp->v_gainr = 0.0;
468
469         for (i = 0; i < n_samples; i++) {
470                 in0 = input0[i];
471                 in1 = input1[i];
472                 sc0 = sc[i];
473                 maxabslr = fmaxf(fabs(in0), fabs(in1));
474                 ingain = usesidechain ? fabs(sc0) : maxabslr;
475                 in_peak = fmaxf (in_peak, ingain);
476                 Lyg = 0.f;
477                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
478                 Lxg = sanitize_denormal(Lxg);
479
480
481                 if (2.f*(Lxg-thresdb) < -width) {
482                         Lyg = Lxg;
483                 } else if (2.f*(Lxg-thresdb) > width) {
484                         Lyg = thresdb + (Lxg-thresdb)/ratio;
485                         Lyg = sanitize_denormal(Lyg);
486                 } else {
487                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
488                 }
489
490                 Lxl = Lxg - Lyg;
491
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);
498
499                 cdb = -Lyl;
500                 Lgain = from_dB(cdb);
501
502                 *(acomp->gainr) = Lyl;
503                 if (Lyl > acomp->v_gainr) {
504                         acomp->v_gainr = Lyl;
505                 }
506
507                 lgaininp = in0 * Lgain;
508                 rgaininp = in1 * Lgain;
509
510                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
511
512                 output0[i] = lgaininp * makeup_gain;
513                 output1[i] = rgaininp * makeup_gain;
514
515                 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
516
517                 // TODO re-use local variables on stack
518                 // store values back to acomp at the end of the inner-loop
519                 acomp->old_yl = Lyl;
520                 acomp->old_y1 = Ly1;
521                 acomp->old_yg = Lyg;
522         }
523
524         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
525         acomp->makeup_gain = makeup_gain;
526
527 #ifdef LV2_EXTENDED
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)) {
530                 acomp->v_lvl = 0.f;
531         }
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) {
535                 // >= 1dB difference
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);
539         }
540         if (acomp->need_expose && acomp->queue_draw) {
541                 acomp->need_expose = false;
542                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
543         }
544 #endif
545 }
546
547 static void
548 deactivate(LV2_Handle instance)
549 {
550         activate(instance);
551 }
552
553 static void
554 cleanup(LV2_Handle instance)
555 {
556 #ifdef LV2_EXTENDED
557         AComp* acomp = (AComp*)instance;
558         if (acomp->display) {
559                 cairo_surface_destroy (acomp->display);
560         }
561 #endif
562
563         free(instance);
564 }
565
566
567 #ifndef MIN
568 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
569 #endif
570
571 #ifdef LV2_EXTENDED
572 static float
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;
578
579         const float width = 6.f * knee + 0.01f;
580         float yg = 0.f;
581
582         if (2.f * (xg - thresdb) < -width) {
583                 yg = xg;
584         } else if (2.f * (xg - thresdb) > width) {
585                 yg = thresdb + (xg - thresdb) / ratio;
586         } else {
587                 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
588         }
589
590         yg += makeup;
591
592         return yg;
593 }
594
595 static LV2_Inline_Display_Image_Surface *
596 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
597 {
598         AComp* self = (AComp*)instance;
599         uint32_t h = MIN (w, max_h);
600
601         const float makeup_thres = self->v_thresdb + self->v_makeup;
602
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);
606                 self->w = w;
607                 self->h = h;
608         }
609
610         cairo_t* cr = cairo_create (self->display);
611
612         // clear background
613         cairo_rectangle (cr, 0, 0, w, h);
614         cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
615         cairo_fill (cr);
616
617         cairo_set_line_width(cr, 1.0);
618
619         // draw grid 10dB steps
620         const double dash1[] = {1, 2};
621         const double dash2[] = {1, 3};
622         cairo_save (cr);
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);
626
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));
630
631                 cairo_move_to (cr, x, 0);
632                 cairo_line_to (cr, x, h);
633                 cairo_stroke (cr);
634
635                 cairo_move_to (cr, 0, y);
636                 cairo_line_to (cr, w, y);
637                 cairo_stroke (cr);
638         }
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);
645                 cairo_stroke (cr);
646         }
647         // diagonal unity
648         cairo_move_to (cr, 0, h);
649         cairo_line_to (cr, w, 0);
650         cairo_stroke (cr);
651         cairo_restore (cr);
652
653         { // 0, 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);
659                 cairo_stroke (cr);
660                 cairo_move_to (cr, 0, y);
661                 cairo_line_to (cr, w, y);
662                 cairo_stroke (cr);
663         }
664
665         { // GR
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);
671                 cairo_fill (cr);
672
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);
676                 cairo_fill (cr);
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);
680                 cairo_stroke (cr);
681         }
682
683         // draw curve
684         cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
685         cairo_move_to (cr, 0, h);
686
687         for (uint32_t x = 0; x < w; ++x) {
688                 // plot -60..+10  dB
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);
693         }
694         cairo_stroke_preserve (cr);
695
696         cairo_line_to (cr, w, h);
697         cairo_close_path (cr);
698         cairo_clip (cr);
699
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);
706         }
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);
710         } else {
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);
713         }
714         cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
715
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);
722         } else {
723                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
724         }
725         cairo_fill (cr);
726
727         cairo_pattern_destroy (pat); // TODO cache pattern
728
729
730         // create RGBA surface
731         cairo_destroy (cr);
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);
737
738         return &self->surf;
739 }
740 #endif
741
742 static const void*
743 extension_data(const char* uri)
744 {
745 #ifdef LV2_EXTENDED
746         static const LV2_Inline_Display_Interface display  = { render_inline };
747         if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
748                 return &display;
749         }
750 #endif
751         return NULL;
752 }
753
754 static const LV2_Descriptor descriptor_mono = {
755         ACOMP_URI,
756         instantiate,
757         connect_mono,
758         activate,
759         run_mono,
760         deactivate,
761         cleanup,
762         extension_data
763 };
764
765 static const LV2_Descriptor descriptor_stereo = {
766         ACOMP_STEREO_URI,
767         instantiate,
768         connect_stereo,
769         activate,
770         run_stereo,
771         deactivate,
772         cleanup,
773         extension_data
774 };
775
776 LV2_SYMBOL_EXPORT
777 const LV2_Descriptor*
778 lv2_descriptor(uint32_t index)
779 {
780         switch (index) {
781         case 0:
782                 return &descriptor_mono;
783         case 1:
784                 return &descriptor_stereo;
785         default:
786                 return NULL;
787         }
788 }