a-comp: Fix noise floor - asymptotics
[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
87 #ifdef LV2_EXTENDED
88         LV2_Inline_Display_Image_Surface surf;
89         bool                     need_expose;
90         cairo_surface_t*         display;
91         LV2_Inline_Display*      queue_draw;
92         uint32_t                 w, h;
93
94         /* ports pointers are only valid during run so we'll
95          * have to cache them for the display, besides
96          * we do want to check for changes
97          */
98         float v_knee;
99         float v_ratio;
100         float v_thresdb;
101         float v_gainr;
102         float v_makeup;
103         float v_lvl;
104         float v_lv1;
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->makeup_gain = 1.f;
129 #ifdef LV2_EXTENDED
130         acomp->need_expose = true;
131         acomp->v_lvl_out = -70.f;
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 = (1.0 - exp (-2.f * M_PI * n_samples * 25.f / acomp->srate));
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                 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         if ( fabsf(makeup_target - makeup_gain) < 1e-6 ) {
377                 makeup_gain = 1.0;
378         } else {
379                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
380         }
381
382         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
383         acomp->makeup_gain = makeup_gain;
384
385 #ifdef LV2_EXTENDED
386         const float old_v_lv1 = acomp->v_lv1;
387         const float old_v_lvl = acomp->v_lvl;
388         const float tot_rel_c = exp(-1000.f/(*(acomp->release) * srate) * n_samples);
389         const float tot_atk_c = exp(-1000.f/(*(acomp->attack) * srate) * n_samples);
390         acomp->v_lv1 = fmaxf (in_peak, tot_rel_c*old_v_lv1 + (1.f-tot_rel_c)*in_peak);
391         acomp->v_lvl = tot_atk_c*old_v_lvl + (1.f-tot_atk_c)*acomp->v_lv1;
392
393         if (!isfinite_local (acomp->v_lvl)) {
394                 acomp->v_lvl = 0.f;
395         }
396         const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
397         const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
398         if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
399                 // >= 1dB difference
400                 acomp->need_expose = true;
401                 acomp->v_lvl_in = v_lvl_in;
402                 const float relax_coef = exp(-(float)n_samples/srate);
403                 acomp->v_lvl_out = fmaxf (v_lvl_out, relax_coef*acomp->v_lvl_out + (1.f-relax_coef)*v_lvl_out);
404         }
405         if (acomp->need_expose && acomp->queue_draw) {
406                 acomp->need_expose = false;
407                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
408         }
409 #endif
410 }
411
412 static void
413 run_stereo(LV2_Handle instance, uint32_t n_samples)
414 {
415         AComp* acomp = (AComp*)instance;
416
417         const float* const input0 = acomp->input0;
418         const float* const input1 = acomp->input1;
419         const float* const sc = acomp->sc;
420         float* const output0 = acomp->output0;
421         float* const output1 = acomp->output1;
422
423         float srate = acomp->srate;
424         float width = (6.f * *(acomp->knee)) + 0.01;
425         float cdb=0.f;
426         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
427         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
428
429         float max = 0.f;
430         float lgaininp = 0.f;
431         float rgaininp = 0.f;
432         float Lgain = 1.f;
433         float Lxg, Lxl, Lyg, Lyl, Ly1;
434         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
435         uint32_t i;
436         float ingain;
437         float in0;
438         float in1;
439         float sc0;
440         float maxabslr;
441
442         float ratio = *acomp->ratio;
443         float thresdb = *acomp->thresdb;
444         float makeup = *acomp->makeup;
445         float makeup_target = from_dB(makeup);
446         float makeup_gain = acomp->makeup_gain;
447
448         const float tau = (1.0 - exp (-2.f * M_PI * n_samples * 25.f / acomp->srate));
449
450         if (*acomp->enable <= 0) {
451                 ratio = 1.f;
452                 thresdb = 0.f;
453                 makeup = 0.f;
454                 makeup_target = 1.f;
455         }
456
457 #ifdef LV2_EXTENDED
458         if (acomp->v_knee != *acomp->knee) {
459                 acomp->v_knee = *acomp->knee;
460                 acomp->need_expose = true;
461         }
462
463         if (acomp->v_ratio != ratio) {
464                 acomp->v_ratio = ratio;
465                 acomp->need_expose = true;
466         }
467
468         if (acomp->v_thresdb != thresdb) {
469                 acomp->v_thresdb = thresdb;
470                 acomp->need_expose = true;
471         }
472
473         if (acomp->v_makeup != makeup) {
474                 acomp->v_makeup = makeup;
475                 acomp->need_expose = true;
476         }
477 #endif
478
479         float in_peak = 0;
480         acomp->v_gainr = 0.0;
481
482         for (i = 0; i < n_samples; i++) {
483                 in0 = input0[i];
484                 in1 = input1[i];
485                 sc0 = sc[i];
486                 maxabslr = fmaxf(fabs(in0), fabs(in1));
487                 ingain = usesidechain ? fabs(sc0) : maxabslr;
488                 in_peak = fmaxf (in_peak, ingain);
489                 Lyg = 0.f;
490                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
491                 Lxg = sanitize_denormal(Lxg);
492
493
494                 if (2.f*(Lxg-thresdb) < -width) {
495                         Lyg = Lxg;
496                 } else if (2.f*(Lxg-thresdb) > width) {
497                         Lyg = thresdb + (Lxg-thresdb)/ratio;
498                         Lyg = sanitize_denormal(Lyg);
499                 } else {
500                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
501                 }
502
503                 Lxl = Lxg - Lyg;
504
505                 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
506                 acomp->old_yl = sanitize_denormal(acomp->old_yl);
507                 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
508                 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
509                 Ly1 = sanitize_denormal(Ly1);
510                 Lyl = sanitize_denormal(Lyl);
511
512                 cdb = -Lyl;
513                 Lgain = from_dB(cdb);
514
515                 *(acomp->gainr) = Lyl;
516                 if (Lyl > acomp->v_gainr) {
517                         acomp->v_gainr = Lyl;
518                 }
519
520                 lgaininp = in0 * Lgain;
521                 rgaininp = in1 * Lgain;
522
523                 output0[i] = lgaininp * makeup_gain;
524                 output1[i] = rgaininp * makeup_gain;
525
526                 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
527
528                 // TODO re-use local variables on stack
529                 // store values back to acomp at the end of the inner-loop
530                 acomp->old_yl = Lyl;
531                 acomp->old_y1 = Ly1;
532                 acomp->old_yg = Lyg;
533         }
534
535         if ( fabsf(makeup_target - makeup_gain) < 1e-6 ) {
536                 makeup_gain = 1.0;
537         } else {
538                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
539         }
540
541         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
542         acomp->makeup_gain = makeup_gain;
543
544 #ifdef LV2_EXTENDED
545         const float old_v_lv1 = acomp->v_lv1;
546         const float old_v_lvl = acomp->v_lvl;
547         const float tot_rel_c = exp(-1000.f/(*(acomp->release) * srate) * n_samples);
548         const float tot_atk_c = exp(-1000.f/(*(acomp->attack) * srate) * n_samples);
549         acomp->v_lv1 = fmaxf (in_peak, tot_rel_c*old_v_lv1 + (1.f-tot_rel_c)*in_peak);
550         acomp->v_lvl = tot_atk_c*old_v_lvl + (1.f-tot_atk_c)*acomp->v_lv1;
551         if (!isfinite_local (acomp->v_lvl)) {
552                 acomp->v_lvl = 0.f;
553         }
554         const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
555         const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
556         if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
557                 // >= 1dB difference
558                 acomp->need_expose = true;
559                 acomp->v_lvl_in = v_lvl_in;
560                 const float relax_coef = exp(-2.0*n_samples/srate);
561                 acomp->v_lvl_out = fmaxf (v_lvl_out, relax_coef*acomp->v_lvl_out + (1.f-relax_coef)*v_lvl_out);
562         }
563         if (acomp->need_expose && acomp->queue_draw) {
564                 acomp->need_expose = false;
565                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
566         }
567 #endif
568 }
569
570 static void
571 deactivate(LV2_Handle instance)
572 {
573         activate(instance);
574 }
575
576 static void
577 cleanup(LV2_Handle instance)
578 {
579 #ifdef LV2_EXTENDED
580         AComp* acomp = (AComp*)instance;
581         if (acomp->display) {
582                 cairo_surface_destroy (acomp->display);
583         }
584 #endif
585
586         free(instance);
587 }
588
589
590 #ifndef MIN
591 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
592 #endif
593
594 #ifdef LV2_EXTENDED
595 static float
596 comp_curve (const AComp* self, float xg) {
597         const float knee = self->v_knee;
598         const float ratio = self->v_ratio;
599         const float thresdb = self->v_thresdb;
600         const float makeup = self->v_makeup;
601
602         const float width = 6.f * knee + 0.01f;
603         float yg = 0.f;
604
605         if (2.f * (xg - thresdb) < -width) {
606                 yg = xg;
607         } else if (2.f * (xg - thresdb) > width) {
608                 yg = thresdb + (xg - thresdb) / ratio;
609         } else {
610                 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
611         }
612
613         yg += makeup;
614
615         return yg;
616 }
617
618 static void
619 render_inline_full (cairo_t* cr, const AComp* self)
620 {
621         const float w = self->w;
622         const float h = self->h;
623
624         const float makeup_thres = self->v_thresdb + self->v_makeup;
625
626         // clear background
627         cairo_rectangle (cr, 0, 0, w, h);
628         cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
629         cairo_fill (cr);
630
631         cairo_set_line_width(cr, 1.0);
632
633         // draw grid 10dB steps
634         const double dash1[] = {1, 2};
635         const double dash2[] = {1, 3};
636         cairo_save (cr);
637         cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
638         cairo_set_dash(cr, dash2, 2, 2);
639         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
640
641         for (uint32_t d = 1; d < 7; ++d) {
642                 const float x = -.5 + floorf (w * (d * 10.f / 70.f));
643                 const float y = -.5 + floorf (h * (d * 10.f / 70.f));
644
645                 cairo_move_to (cr, x, 0);
646                 cairo_line_to (cr, x, h);
647                 cairo_stroke (cr);
648
649                 cairo_move_to (cr, 0, y);
650                 cairo_line_to (cr, w, y);
651                 cairo_stroke (cr);
652         }
653         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
654         cairo_set_dash(cr, dash1, 2, 2);
655         if (self->v_thresdb < 0) {
656                 const float y = -.5 + floorf (h * ((makeup_thres - 10.f) / -70.f));
657                 cairo_move_to (cr, 0, y);
658                 cairo_line_to (cr, w, y);
659                 cairo_stroke (cr);
660         }
661         // diagonal unity
662         cairo_move_to (cr, 0, h);
663         cairo_line_to (cr, w, 0);
664         cairo_stroke (cr);
665         cairo_restore (cr);
666
667         { // 0, 0
668                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
669                 const float x = -.5 + floorf (w * (60.f / 70.f));
670                 const float y = -.5 + floorf (h * (10.f / 70.f));
671                 cairo_move_to (cr, x, 0);
672                 cairo_line_to (cr, x, h);
673                 cairo_stroke (cr);
674                 cairo_move_to (cr, 0, y);
675                 cairo_line_to (cr, w, y);
676                 cairo_stroke (cr);
677         }
678
679         { // GR
680                 const float x = -.5 + floorf (w * (62.5f / 70.f));
681                 const float y = -.5 + floorf (h * (10.0f / 70.f));
682                 const float wd = floorf (w * (5.f / 70.f));
683                 const float ht = floorf (h * (55.f / 70.f));
684                 cairo_rectangle (cr, x, y, wd, ht);
685                 cairo_fill (cr);
686
687                 const float h_gr = fminf (ht, floorf (h * self->v_gainr / 70.f));
688                 cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
689                 cairo_rectangle (cr, x, y, wd, h_gr);
690                 cairo_fill (cr);
691                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
692                 cairo_rectangle (cr, x, y, wd, ht);
693                 cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 1.0);
694                 cairo_stroke (cr);
695         }
696
697         // draw curve
698         cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
699         cairo_move_to (cr, 0, h);
700
701         for (uint32_t x = 0; x < w; ++x) {
702                 // plot -60..+10  dB
703                 const float x_db = 70.f * (-1.f + x / (float)w) + 10.f;
704                 const float y_db = comp_curve (self, x_db) - 10.f;
705                 const float y = h * (y_db / -70.f);
706                 cairo_line_to (cr, x, y);
707         }
708         cairo_stroke_preserve (cr);
709
710         cairo_line_to (cr, w, h);
711         cairo_close_path (cr);
712         cairo_clip (cr);
713
714         // draw signal level & reduction/gradient
715         const float top = comp_curve (self, 0) - 10.f;
716         cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
717         if (top > makeup_thres - 10.f) {
718                 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
719                 cairo_pattern_add_color_stop_rgba (pat, top / -70.f, 0.8, 0.1, 0.1, 0.5);
720         }
721         if (self->v_knee > 0) {
722                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres -10.f) / -70.f), 0.7, 0.7, 0.2, 0.5);
723                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - self->v_knee - 10.f) / -70.f), 0.5, 0.5, 0.5, 0.5);
724         } else {
725                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.f)/ -70.f), 0.7, 0.7, 0.2, 0.5);
726                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.01f) / -70.f), 0.5, 0.5, 0.5, 0.5);
727         }
728         cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
729
730         // maybe cut off at x-position?
731         const float x = w * (self->v_lvl_in + 60) / 70.f;
732         const float y = x + h*self->v_makeup;
733         cairo_rectangle (cr, 0, h - y, x, y);
734         if (self->v_ratio > 1.0) {
735                 cairo_set_source (cr, pat);
736         } else {
737                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
738         }
739         cairo_fill (cr);
740
741         cairo_pattern_destroy (pat); // TODO cache pattern
742 }
743
744 static void
745 render_inline_only_bars (cairo_t* cr, const AComp* self)
746 {
747         const float w = self->w;
748         const float h = self->h;
749
750         cairo_rectangle (cr, 0, 0, w, h);
751         cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
752         cairo_fill (cr);
753
754
755         cairo_save (cr);
756
757         const float ht = 0.25f * h;
758
759         const float x1 = w*0.05;
760         const float wd = w - 2.0f*x1;
761
762         const float y1 = 0.17*h;
763         const float y2 = h - y1 - ht;
764
765         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
766
767         cairo_rectangle (cr, x1, y1, wd, ht);
768         cairo_fill (cr);
769
770         cairo_rectangle (cr, x1, y2, wd, ht);
771         cairo_fill (cr);
772
773         cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
774         const float w_gr = (self->v_gainr > 60.f) ? wd : wd * self->v_gainr * (1.f/60.f);
775         cairo_rectangle (cr, x1+wd-w_gr, y2, w_gr, ht);
776         cairo_fill (cr);
777
778         if (self->v_lvl_out > -60.f) {
779                 if (self->v_lvl_out > 10.f) {
780                         cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
781                 } else if (self->v_lvl_out > 0.f) {
782                         cairo_set_source_rgba (cr, 0.66, 0.66, 0.0, 1.0);
783                 } else {
784                         cairo_set_source_rgba (cr, 0.0, 0.66, 0.0, 1.0);
785                 }
786                 const float w_g = (self->v_lvl_out > 10.f) ? wd : wd * (60.f+self->v_lvl_out) / 70.f;
787                 cairo_rectangle (cr, x1, y1, w_g, ht);
788                 cairo_fill (cr);
789         }
790
791         cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
792
793         const float tck = 0.33*ht;
794
795         cairo_set_line_width (cr, .5);
796
797         for (uint32_t d = 1; d < 7; ++d) {
798                 const float x = x1 + (d * wd * (10.f / 70.f));
799
800                 cairo_move_to (cr, x, y1);
801                 cairo_line_to (cr, x, y1+tck);
802
803                 cairo_move_to (cr, x, y1+ht);
804                 cairo_line_to (cr, x, y1+ht-tck);
805
806                 cairo_move_to (cr, x, y2);
807                 cairo_line_to (cr, x, y2+tck);
808
809                 cairo_move_to (cr, x, y2+ht);
810                 cairo_line_to (cr, x, y2+ht-tck);
811         }
812
813         cairo_stroke (cr);
814
815         const float x_0dB = x1 + wd*(60.f/70.f);
816
817         cairo_move_to (cr, x_0dB, y1);
818         cairo_line_to (cr, x_0dB, y1+ht);
819
820         cairo_rectangle (cr, x1, y1, wd, ht);
821         cairo_rectangle (cr, x1, y2, wd, ht);
822         cairo_stroke (cr);
823
824         cairo_set_line_width (cr, 2.0);
825
826         // visualize threshold
827         const float tr = x1 + wd * (60.f+self->v_thresdb) / 70.f;
828         cairo_set_source_rgba (cr, 0.95, 0.95, 0.0, 1.0);
829         cairo_move_to (cr, tr, y1);
830         cairo_line_to (cr, tr, y1+ht);
831         cairo_stroke (cr);
832
833         // visualize ratio
834         const float reduced_0dB = self->v_thresdb * (1.f - 1.f/self->v_ratio);
835         const float rt = x1 + wd * (60.f+reduced_0dB) / 70.f;
836         cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
837         cairo_move_to (cr, rt, y1);
838         cairo_line_to (cr, rt, y1+ht);
839         cairo_stroke (cr);
840 }
841
842 static LV2_Inline_Display_Image_Surface *
843 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
844 {
845         AComp* self = (AComp*)instance;
846
847         uint32_t h = MIN (w, max_h);
848         if (w < 200) {
849                 h = 40;
850         }
851
852         if (!self->display || self->w != w || self->h != h) {
853                 if (self->display) cairo_surface_destroy(self->display);
854                 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
855                 self->w = w;
856                 self->h = h;
857         }
858
859         cairo_t* cr = cairo_create (self->display);
860
861         if (w >= 200) {
862                 render_inline_full (cr, self);
863         } else {
864                 render_inline_only_bars (cr, self);
865         }
866
867         cairo_destroy (cr);
868
869         cairo_surface_flush (self->display);
870         self->surf.width = cairo_image_surface_get_width (self->display);
871         self->surf.height = cairo_image_surface_get_height (self->display);
872         self->surf.stride = cairo_image_surface_get_stride (self->display);
873         self->surf.data = cairo_image_surface_get_data  (self->display);
874
875         return &self->surf;
876 }
877 #endif
878
879 static const void*
880 extension_data(const char* uri)
881 {
882 #ifdef LV2_EXTENDED
883         static const LV2_Inline_Display_Interface display  = { render_inline };
884         if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
885                 return &display;
886         }
887 #endif
888         return NULL;
889 }
890
891 static const LV2_Descriptor descriptor_mono = {
892         ACOMP_URI,
893         instantiate,
894         connect_mono,
895         activate,
896         run_mono,
897         deactivate,
898         cleanup,
899         extension_data
900 };
901
902 static const LV2_Descriptor descriptor_stereo = {
903         ACOMP_STEREO_URI,
904         instantiate,
905         connect_stereo,
906         activate,
907         run_stereo,
908         deactivate,
909         cleanup,
910         extension_data
911 };
912
913 LV2_SYMBOL_EXPORT
914 const LV2_Descriptor*
915 lv2_descriptor(uint32_t index)
916 {
917         switch (index) {
918         case 0:
919                 return &descriptor_mono;
920         case 1:
921                 return &descriptor_stereo;
922         default:
923                 return NULL;
924         }
925 }