bb282f962c6d92660448637e131ba92b84026f6c
[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_lv1;
106         float v_lvl_in;
107         float v_lvl_out;
108 #endif
109 } AComp;
110
111 static LV2_Handle
112 instantiate(const LV2_Descriptor* descriptor,
113             double rate,
114             const char* bundle_path,
115             const LV2_Feature* const* features)
116 {
117         AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
118
119         for (int i=0; features[i]; ++i) {
120 #ifdef LV2_EXTENDED
121                 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
122                         acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
123                 }
124 #endif
125         }
126
127         acomp->srate = rate;
128         acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
129         acomp->tau = (1.0 - exp (-2.f * M_PI * 25.f / acomp->srate));
130 #ifdef LV2_EXTENDED
131         acomp->need_expose = true;
132 #endif
133
134         return (LV2_Handle)acomp;
135 }
136
137
138 static void
139 connect_port(LV2_Handle instance,
140              uint32_t port,
141              void* data)
142 {
143         AComp* acomp = (AComp*)instance;
144
145         switch ((PortIndex)port) {
146                 case ACOMP_ATTACK:
147                         acomp->attack = (float*)data;
148                         break;
149                 case ACOMP_RELEASE:
150                         acomp->release = (float*)data;
151                         break;
152                 case ACOMP_KNEE:
153                         acomp->knee = (float*)data;
154                         break;
155                 case ACOMP_RATIO:
156                         acomp->ratio = (float*)data;
157                         break;
158                 case ACOMP_THRESHOLD:
159                         acomp->thresdb = (float*)data;
160                         break;
161                 case ACOMP_MAKEUP:
162                         acomp->makeup = (float*)data;
163                         break;
164                 case ACOMP_GAINR:
165                         acomp->gainr = (float*)data;
166                         break;
167                 case ACOMP_OUTLEVEL:
168                         acomp->outlevel = (float*)data;
169                         break;
170                 case ACOMP_SIDECHAIN:
171                         acomp->sidechain = (float*)data;
172                         break;
173                 case ACOMP_ENABLE:
174                         acomp->enable = (float*)data;
175                         break;
176                 default:
177                         break;
178         }
179 }
180
181 static void
182 connect_mono(LV2_Handle instance,
183              uint32_t port,
184              void* data)
185 {
186         AComp* acomp = (AComp*)instance;
187         connect_port (instance, port, data);
188
189         switch ((PortIndex)port) {
190                 case ACOMP_A0:
191                         acomp->input0 = (float*)data;
192                         break;
193                 case ACOMP_A1:
194                         acomp->sc = (float*)data;
195                         break;
196                 case ACOMP_A2:
197                         acomp->output0 = (float*)data;
198                         break;
199         default:
200                 break;
201         }
202 }
203
204 static void
205 connect_stereo(LV2_Handle instance,
206                uint32_t port,
207                void* data)
208 {
209         AComp* acomp = (AComp*)instance;
210         connect_port (instance, port, data);
211
212         switch ((PortIndex)port) {
213                 case ACOMP_A0:
214                         acomp->input0 = (float*)data;
215                         break;
216                 case ACOMP_A1:
217                         acomp->input1 = (float*)data;
218                         break;
219                 case ACOMP_A2:
220                         acomp->sc = (float*)data;
221                         break;
222                 case ACOMP_A3:
223                         acomp->output0 = (float*)data;
224                         break;
225                 case ACOMP_A4:
226                         acomp->output1 = (float*)data;
227                         break;
228         default:
229                 break;
230         }
231 }
232
233 // Force already-denormal float value to zero
234 static inline float
235 sanitize_denormal(float value) {
236         if (!isnormal(value)) {
237                 value = 0.f;
238         }
239         return value;
240 }
241
242 static inline float
243 from_dB(float gdb) {
244         return (exp(gdb/20.f*log(10.f)));
245 }
246
247 static inline float
248 to_dB(float g) {
249         return (20.f*log10(g));
250 }
251
252 static void
253 activate(LV2_Handle instance)
254 {
255         AComp* acomp = (AComp*)instance;
256
257         *(acomp->gainr) = 0.0f;
258         *(acomp->outlevel) = -45.0f;
259         acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
260 }
261
262 static void
263 run_mono(LV2_Handle instance, uint32_t n_samples)
264 {
265         AComp* acomp = (AComp*)instance;
266
267         const float* const input = acomp->input0;
268         const float* const sc = acomp->sc;
269         float* const output = acomp->output0;
270
271         float srate = acomp->srate;
272         float width = (6.f * *(acomp->knee)) + 0.01;
273         float cdb=0.f;
274         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
275         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
276
277         float max = 0.f;
278         float lgaininp = 0.f;
279         float Lgain = 1.f;
280         float Lxg, Lxl, Lyg, Lyl, Ly1;
281         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
282         uint32_t i;
283         float ingain;
284         float in0;
285         float sc0;
286
287         float ratio = *acomp->ratio;
288         float thresdb = *acomp->thresdb;
289         float makeup = *acomp->makeup;
290         float makeup_target = from_dB(makeup);
291         float makeup_gain = acomp->makeup_gain;
292
293         const float tau = acomp->tau;
294
295         if (*acomp->enable <= 0) {
296                 ratio = 1.f;
297                 thresdb = 0.f;
298                 makeup = 0.f;
299                 makeup_target = 1.f;
300         }
301
302 #ifdef LV2_EXTENDED
303         if (acomp->v_knee != *acomp->knee) {
304                 acomp->v_knee = *acomp->knee;
305                 acomp->need_expose = true;
306         }
307
308         if (acomp->v_ratio != ratio) {
309                 acomp->v_ratio = ratio;
310                 acomp->need_expose = true;
311         }
312
313         if (acomp->v_thresdb != thresdb) {
314                 acomp->v_thresdb = thresdb;
315                 acomp->need_expose = true;
316         }
317
318         if (acomp->v_makeup != makeup) {
319                 acomp->v_makeup = makeup;
320                 acomp->need_expose = true;
321         }
322 #endif
323
324         float in_peak = 0;
325         acomp->v_gainr = 0.0;
326
327         for (i = 0; i < n_samples; i++) {
328                 in0 = input[i];
329                 sc0 = sc[i];
330                 ingain = usesidechain ? fabs(sc0) : fabs(in0);
331                 in_peak = fmaxf (in_peak, ingain);
332                 Lyg = 0.f;
333                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
334                 Lxg = sanitize_denormal(Lxg);
335
336
337                 if (2.f*(Lxg-thresdb) < -width) {
338                         Lyg = Lxg;
339                 } else if (2.f*(Lxg-thresdb) > width) {
340                         Lyg = thresdb + (Lxg-thresdb)/ratio;
341                         Lyg = sanitize_denormal(Lyg);
342                 } else {
343                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
344                 }
345
346                 Lxl = Lxg - Lyg;
347
348                 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
349                 acomp->old_yl = sanitize_denormal(acomp->old_yl);
350                 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
351                 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
352                 Ly1 = sanitize_denormal(Ly1);
353                 Lyl = sanitize_denormal(Lyl);
354
355                 cdb = -Lyl;
356                 Lgain = from_dB(cdb);
357
358                 *(acomp->gainr) = Lyl;
359                 if (Lyl > acomp->v_gainr) {
360                         acomp->v_gainr = Lyl;
361                 }
362
363                 lgaininp = in0 * Lgain;
364
365                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
366                 output[i] = lgaininp * makeup_gain;
367
368                 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
369
370                 // TODO re-use local variables on stack
371                 // store values back to acomp at the end of the inner-loop
372                 acomp->old_yl = Lyl;
373                 acomp->old_y1 = Ly1;
374                 acomp->old_yg = Lyg;
375         }
376
377         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
378         acomp->makeup_gain = makeup_gain;
379
380 #ifdef LV2_EXTENDED
381         const float old_v_lv1 = acomp->v_lv1;
382         const float old_v_lvl = acomp->v_lvl;
383         const float tot_rel_c = exp(-1000.f/(*(acomp->release) * srate) * n_samples);
384         const float tot_atk_c = exp(-1000.f/(*(acomp->attack) * srate) * n_samples);
385         acomp->v_lv1 = fmaxf (in_peak, tot_rel_c*old_v_lv1 + (1.f-tot_rel_c)*in_peak);
386         acomp->v_lvl = tot_atk_c*old_v_lvl + (1.f-tot_atk_c)*acomp->v_lv1;
387
388         if (!isfinite_local (acomp->v_lvl)) {
389                 acomp->v_lvl = 0.f;
390         }
391         const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
392         const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
393         if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
394                 // >= 1dB difference
395                 acomp->need_expose = true;
396                 acomp->v_lvl_in = v_lvl_in;
397                 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
398         }
399         if (acomp->need_expose && acomp->queue_draw) {
400                 acomp->need_expose = false;
401                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
402         }
403 #endif
404 }
405
406 static void
407 run_stereo(LV2_Handle instance, uint32_t n_samples)
408 {
409         AComp* acomp = (AComp*)instance;
410
411         const float* const input0 = acomp->input0;
412         const float* const input1 = acomp->input1;
413         const float* const sc = acomp->sc;
414         float* const output0 = acomp->output0;
415         float* const output1 = acomp->output1;
416
417         float srate = acomp->srate;
418         float width = (6.f * *(acomp->knee)) + 0.01;
419         float cdb=0.f;
420         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
421         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
422
423         float max = 0.f;
424         float lgaininp = 0.f;
425         float rgaininp = 0.f;
426         float Lgain = 1.f;
427         float Lxg, Lxl, Lyg, Lyl, Ly1;
428         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
429         uint32_t i;
430         float ingain;
431         float in0;
432         float in1;
433         float sc0;
434         float maxabslr;
435
436         float ratio = *acomp->ratio;
437         float thresdb = *acomp->thresdb;
438         float makeup = *acomp->makeup;
439         float makeup_target = from_dB(makeup);
440         float makeup_gain = acomp->makeup_gain;
441
442         const float tau = acomp->tau;
443
444         if (*acomp->enable <= 0) {
445                 ratio = 1.f;
446                 thresdb = 0.f;
447                 makeup = 0.f;
448                 makeup_target = 1.f;
449         }
450
451 #ifdef LV2_EXTENDED
452         if (acomp->v_knee != *acomp->knee) {
453                 acomp->v_knee = *acomp->knee;
454                 acomp->need_expose = true;
455         }
456
457         if (acomp->v_ratio != ratio) {
458                 acomp->v_ratio = ratio;
459                 acomp->need_expose = true;
460         }
461
462         if (acomp->v_thresdb != thresdb) {
463                 acomp->v_thresdb = thresdb;
464                 acomp->need_expose = true;
465         }
466
467         if (acomp->v_makeup != makeup) {
468                 acomp->v_makeup = makeup;
469                 acomp->need_expose = true;
470         }
471 #endif
472
473         float in_peak = 0;
474         acomp->v_gainr = 0.0;
475
476         for (i = 0; i < n_samples; i++) {
477                 in0 = input0[i];
478                 in1 = input1[i];
479                 sc0 = sc[i];
480                 maxabslr = fmaxf(fabs(in0), fabs(in1));
481                 ingain = usesidechain ? fabs(sc0) : maxabslr;
482                 in_peak = fmaxf (in_peak, ingain);
483                 Lyg = 0.f;
484                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
485                 Lxg = sanitize_denormal(Lxg);
486
487
488                 if (2.f*(Lxg-thresdb) < -width) {
489                         Lyg = Lxg;
490                 } else if (2.f*(Lxg-thresdb) > width) {
491                         Lyg = thresdb + (Lxg-thresdb)/ratio;
492                         Lyg = sanitize_denormal(Lyg);
493                 } else {
494                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
495                 }
496
497                 Lxl = Lxg - Lyg;
498
499                 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
500                 acomp->old_yl = sanitize_denormal(acomp->old_yl);
501                 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
502                 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
503                 Ly1 = sanitize_denormal(Ly1);
504                 Lyl = sanitize_denormal(Lyl);
505
506                 cdb = -Lyl;
507                 Lgain = from_dB(cdb);
508
509                 *(acomp->gainr) = Lyl;
510                 if (Lyl > acomp->v_gainr) {
511                         acomp->v_gainr = Lyl;
512                 }
513
514                 lgaininp = in0 * Lgain;
515                 rgaininp = in1 * Lgain;
516
517                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
518
519                 output0[i] = lgaininp * makeup_gain;
520                 output1[i] = rgaininp * makeup_gain;
521
522                 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
523
524                 // TODO re-use local variables on stack
525                 // store values back to acomp at the end of the inner-loop
526                 acomp->old_yl = Lyl;
527                 acomp->old_y1 = Ly1;
528                 acomp->old_yg = Lyg;
529         }
530
531         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
532         acomp->makeup_gain = makeup_gain;
533
534 #ifdef LV2_EXTENDED
535         const float old_v_lv1 = acomp->v_lv1;
536         const float old_v_lvl = acomp->v_lvl;
537         const float tot_rel_c = exp(-1000.f/(*(acomp->release) * srate) * n_samples);
538         const float tot_atk_c = exp(-1000.f/(*(acomp->attack) * srate) * n_samples);
539         acomp->v_lv1 = fmaxf (in_peak, tot_rel_c*old_v_lv1 + (1.f-tot_rel_c)*in_peak);
540         acomp->v_lvl = tot_atk_c*old_v_lvl + (1.f-tot_atk_c)*acomp->v_lv1;
541         if (!isfinite_local (acomp->v_lvl)) {
542                 acomp->v_lvl = 0.f;
543         }
544         const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
545         const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
546         if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
547                 // >= 1dB difference
548                 acomp->need_expose = true;
549                 acomp->v_lvl_in = v_lvl_in;
550                 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
551         }
552         if (acomp->need_expose && acomp->queue_draw) {
553                 acomp->need_expose = false;
554                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
555         }
556 #endif
557 }
558
559 static void
560 deactivate(LV2_Handle instance)
561 {
562         activate(instance);
563 }
564
565 static void
566 cleanup(LV2_Handle instance)
567 {
568 #ifdef LV2_EXTENDED
569         AComp* acomp = (AComp*)instance;
570         if (acomp->display) {
571                 cairo_surface_destroy (acomp->display);
572         }
573 #endif
574
575         free(instance);
576 }
577
578
579 #ifndef MIN
580 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
581 #endif
582
583 #ifdef LV2_EXTENDED
584 static float
585 comp_curve (AComp* self, float xg) {
586         const float knee = self->v_knee;
587         const float ratio = self->v_ratio;
588         const float thresdb = self->v_thresdb;
589         const float makeup = self->v_makeup;
590
591         const float width = 6.f * knee + 0.01f;
592         float yg = 0.f;
593
594         if (2.f * (xg - thresdb) < -width) {
595                 yg = xg;
596         } else if (2.f * (xg - thresdb) > width) {
597                 yg = thresdb + (xg - thresdb) / ratio;
598         } else {
599                 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
600         }
601
602         yg += makeup;
603
604         return yg;
605 }
606
607 static LV2_Inline_Display_Image_Surface *
608 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
609 {
610         AComp* self = (AComp*)instance;
611         uint32_t h = MIN (w, max_h);
612
613         const float makeup_thres = self->v_thresdb + self->v_makeup;
614
615         if (!self->display || self->w != w || self->h != h) {
616                 if (self->display) cairo_surface_destroy(self->display);
617                 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
618                 self->w = w;
619                 self->h = h;
620         }
621
622         cairo_t* cr = cairo_create (self->display);
623
624         // clear background
625         cairo_rectangle (cr, 0, 0, w, h);
626         cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
627         cairo_fill (cr);
628
629         cairo_set_line_width(cr, 1.0);
630
631         // draw grid 10dB steps
632         const double dash1[] = {1, 2};
633         const double dash2[] = {1, 3};
634         cairo_save (cr);
635         cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
636         cairo_set_dash(cr, dash2, 2, 2);
637         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
638
639         for (uint32_t d = 1; d < 7; ++d) {
640                 const float x = -.5 + floorf (w * (d * 10.f / 70.f));
641                 const float y = -.5 + floorf (h * (d * 10.f / 70.f));
642
643                 cairo_move_to (cr, x, 0);
644                 cairo_line_to (cr, x, h);
645                 cairo_stroke (cr);
646
647                 cairo_move_to (cr, 0, y);
648                 cairo_line_to (cr, w, y);
649                 cairo_stroke (cr);
650         }
651         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
652         cairo_set_dash(cr, dash1, 2, 2);
653         if (self->v_thresdb < 0) {
654                 const float y = -.5 + floorf (h * ((makeup_thres - 10.f) / -70.f));
655                 cairo_move_to (cr, 0, y);
656                 cairo_line_to (cr, w, y);
657                 cairo_stroke (cr);
658         }
659         // diagonal unity
660         cairo_move_to (cr, 0, h);
661         cairo_line_to (cr, w, 0);
662         cairo_stroke (cr);
663         cairo_restore (cr);
664
665         { // 0, 0
666                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
667                 const float x = -.5 + floorf (w * (60.f / 70.f));
668                 const float y = -.5 + floorf (h * (10.f / 70.f));
669                 cairo_move_to (cr, x, 0);
670                 cairo_line_to (cr, x, h);
671                 cairo_stroke (cr);
672                 cairo_move_to (cr, 0, y);
673                 cairo_line_to (cr, w, y);
674                 cairo_stroke (cr);
675         }
676
677         { // GR
678                 const float x = -.5 + floorf (w * (62.5f / 70.f));
679                 const float y = -.5 + floorf (h * (10.0f / 70.f));
680                 const float wd = floorf (w * (5.f / 70.f));
681                 const float ht = floorf (h * (55.f / 70.f));
682                 cairo_rectangle (cr, x, y, wd, ht);
683                 cairo_fill (cr);
684
685                 const float h_gr = fminf (ht, floorf (h * self->v_gainr / 70.f));
686                 cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
687                 cairo_rectangle (cr, x, y, wd, h_gr);
688                 cairo_fill (cr);
689                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
690                 cairo_rectangle (cr, x, y, wd, ht);
691                 cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 1.0);
692                 cairo_stroke (cr);
693         }
694
695         // draw curve
696         cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
697         cairo_move_to (cr, 0, h);
698
699         for (uint32_t x = 0; x < w; ++x) {
700                 // plot -60..+10  dB
701                 const float x_db = 70.f * (-1.f + x / (float)w) + 10.f;
702                 const float y_db = comp_curve (self, x_db) - 10.f;
703                 const float y = h * (y_db / -70.f);
704                 cairo_line_to (cr, x, y);
705         }
706         cairo_stroke_preserve (cr);
707
708         cairo_line_to (cr, w, h);
709         cairo_close_path (cr);
710         cairo_clip (cr);
711
712         // draw signal level & reduction/gradient
713         const float top = comp_curve (self, 0) - 10.f;
714         cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
715         if (top > makeup_thres - 10.f) {
716                 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
717                 cairo_pattern_add_color_stop_rgba (pat, top / -70.f, 0.8, 0.1, 0.1, 0.5);
718         }
719         if (self->v_knee > 0) {
720                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres -10.f) / -70.f), 0.7, 0.7, 0.2, 0.5);
721                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - self->v_knee - 10.f) / -70.f), 0.5, 0.5, 0.5, 0.5);
722         } else {
723                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.f)/ -70.f), 0.7, 0.7, 0.2, 0.5);
724                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.01f) / -70.f), 0.5, 0.5, 0.5, 0.5);
725         }
726         cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
727
728         // maybe cut off at x-position?
729         const float x = w * (self->v_lvl_in + 60) / 70.f;
730         const float y = x + h*self->v_makeup;
731         cairo_rectangle (cr, 0, h - y, x, y);
732         if (self->v_ratio > 1.0) {
733                 cairo_set_source (cr, pat);
734         } else {
735                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
736         }
737         cairo_fill (cr);
738
739         cairo_pattern_destroy (pat); // TODO cache pattern
740
741
742         // create RGBA surface
743         cairo_destroy (cr);
744         cairo_surface_flush (self->display);
745         self->surf.width = cairo_image_surface_get_width (self->display);
746         self->surf.height = cairo_image_surface_get_height (self->display);
747         self->surf.stride = cairo_image_surface_get_stride (self->display);
748         self->surf.data = cairo_image_surface_get_data  (self->display);
749
750         return &self->surf;
751 }
752 #endif
753
754 static const void*
755 extension_data(const char* uri)
756 {
757 #ifdef LV2_EXTENDED
758         static const LV2_Inline_Display_Interface display  = { render_inline };
759         if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
760                 return &display;
761         }
762 #endif
763         return NULL;
764 }
765
766 static const LV2_Descriptor descriptor_mono = {
767         ACOMP_URI,
768         instantiate,
769         connect_mono,
770         activate,
771         run_mono,
772         deactivate,
773         cleanup,
774         extension_data
775 };
776
777 static const LV2_Descriptor descriptor_stereo = {
778         ACOMP_STEREO_URI,
779         instantiate,
780         connect_stereo,
781         activate,
782         run_stereo,
783         deactivate,
784         cleanup,
785         extension_data
786 };
787
788 LV2_SYMBOL_EXPORT
789 const LV2_Descriptor*
790 lv2_descriptor(uint32_t index)
791 {
792         switch (index) {
793         case 0:
794                 return &descriptor_mono;
795         case 1:
796                 return &descriptor_stereo;
797         default:
798                 return NULL;
799         }
800 }