d3a29a689c4972aba4a6c12260dbbe93fdc77761
[ardour.git] / libs / plugins / a-exp.lv2 / a-exp.c
1 /* a-exp
2  * Copyright (C) 2017 Johannes Mueller <github@johannes-mueller.org>
3  * based on a-comp (C) 2016 Damien Zammit <damien@zamaudio.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16
17
18 #include <math.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdbool.h>
22
23 #ifdef LV2_EXTENDED
24 #include <cairo/cairo.h>
25 #include "ardour/lv2_extensions.h"
26 #endif
27
28 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
29
30 #define RESET_PEAK_AFTER_SECONDS 3
31
32 #define AEXP_URI "urn:ardour:a-exp"
33 #define AEXP_STEREO_URI "urn:ardour:a-exp#stereo"
34
35 #ifndef M_PI
36 #  define M_PI 3.14159265358979323846
37 #endif
38
39 #ifdef COMPILER_MSVC
40 #include <float.h>
41 #define isfinite_local(val) (bool)_finite((double)val)
42 #else
43 #define isfinite_local isfinite
44 #endif
45
46 typedef enum {
47         AEXP_ATTACK = 0,
48         AEXP_RELEASE,
49         AEXP_KNEE,
50         AEXP_RATIO,
51         AEXP_THRESHOLD,
52         AEXP_MAKEUP,
53
54         AEXP_GAINR,
55         AEXP_OUTLEVEL,
56         AEXP_INLEVEL,
57         AEXP_SIDECHAIN,
58         AEXP_ENABLE,
59
60         AEXP_A0,
61         AEXP_A1,
62         AEXP_A2,
63         AEXP_A3,
64         AEXP_A4,
65 } PortIndex;
66
67 typedef struct {
68         float* attack;
69         float* release;
70         float* knee;
71         float* ratio;
72         float* thresdb;
73         float* makeup;
74
75         float* gainr;
76         float* outlevel;
77         float* inlevel;
78         float* sidechain;
79         float* enable;
80
81         float* input0;
82         float* input1;
83         float* sc;
84         float* output0;
85         float* output1;
86
87         float srate;
88
89         float makeup_gain;
90         float tau;
91
92         bool was_disabled;
93
94 #ifdef LV2_EXTENDED
95         LV2_Inline_Display_Image_Surface surf;
96         bool                     need_expose;
97         cairo_surface_t*         display;
98         LV2_Inline_Display*      queue_draw;
99         uint32_t                 w, h;
100
101         /* ports pointers are only valid during run so we'll
102          * have to cache them for the display, besides
103          * we do want to check for changes
104          */
105         float v_knee;
106         float v_ratio;
107         float v_thresdb;
108         float v_gainr;
109         float v_makeup;
110         float v_lvl_in;
111         float v_lvl_out;
112
113         float v_peakdb;
114         uint32_t peakdb_samples;
115 #endif
116 } AExp;
117
118 static LV2_Handle
119 instantiate(const LV2_Descriptor* descriptor,
120             double rate,
121             const char* bundle_path,
122             const LV2_Feature* const* features)
123 {
124         AExp* aexp = (AExp*)calloc(1, sizeof(AExp));
125
126         for (int i=0; features[i]; ++i) {
127 #ifdef LV2_EXTENDED
128                 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
129                         aexp->queue_draw = (LV2_Inline_Display*) features[i]->data;
130                 }
131 #endif
132         }
133
134         aexp->srate = rate;
135         aexp->tau = (1.0 - exp (-2.f * M_PI * 25.f / aexp->srate));
136 #ifdef LV2_EXTENDED
137         aexp->need_expose = true;
138         aexp->v_lvl_out = -70.f;
139 #endif
140
141         return (LV2_Handle)aexp;
142 }
143
144 static void
145 connect_port(LV2_Handle instance,
146              uint32_t port,
147              void* data)
148 {
149         AExp* aexp = (AExp*)instance;
150
151         switch ((PortIndex)port) {
152                 case AEXP_ATTACK:
153                         aexp->attack = (float*)data;
154                         break;
155                 case AEXP_RELEASE:
156                         aexp->release = (float*)data;
157                         break;
158                 case AEXP_KNEE:
159                         aexp->knee = (float*)data;
160                         break;
161                 case AEXP_RATIO:
162                         aexp->ratio = (float*)data;
163                         break;
164                 case AEXP_THRESHOLD:
165                         aexp->thresdb = (float*)data;
166                         break;
167                 case AEXP_MAKEUP:
168                         aexp->makeup = (float*)data;
169                         break;
170                 case AEXP_GAINR:
171                         aexp->gainr = (float*)data;
172                         break;
173                 case AEXP_OUTLEVEL:
174                         aexp->outlevel = (float*)data;
175                         break;
176                 case AEXP_INLEVEL:
177                         aexp->inlevel = (float*)data;
178                         break;
179                 case AEXP_SIDECHAIN:
180                         aexp->sidechain = (float*)data;
181                         break;
182                 case AEXP_ENABLE:
183                         aexp->enable = (float*)data;
184                         break;
185                 default:
186                         break;
187         }
188 }
189
190 static void
191 connect_mono(LV2_Handle instance,
192              uint32_t port,
193              void* data)
194 {
195         AExp* aexp = (AExp*)instance;
196         connect_port (instance, port, data);
197
198         switch ((PortIndex)port) {
199                 case AEXP_A0:
200                         aexp->input0 = (float*)data;
201                         break;
202                 case AEXP_A1:
203                         aexp->sc = (float*)data;
204                         break;
205                 case AEXP_A2:
206                         aexp->output0 = (float*)data;
207                         break;
208         default:
209                 break;
210         }
211 }
212
213 static void
214 connect_stereo(LV2_Handle instance,
215                uint32_t port,
216                void* data)
217 {
218         AExp* aexp = (AExp*)instance;
219         connect_port (instance, port, data);
220
221         switch ((PortIndex)port) {
222                 case AEXP_A0:
223                         aexp->input0 = (float*)data;
224                         break;
225                 case AEXP_A1:
226                         aexp->input1 = (float*)data;
227                         break;
228                 case AEXP_A2:
229                         aexp->sc = (float*)data;
230                         break;
231                 case AEXP_A3:
232                         aexp->output0 = (float*)data;
233                         break;
234                 case AEXP_A4:
235                         aexp->output1 = (float*)data;
236                         break;
237         default:
238                 break;
239         }
240 }
241
242 // Force already-denormal float value to zero
243 static inline float
244 sanitize_denormal(float value) {
245         if (!isnormal(value)) {
246                 value = 0.f;
247         }
248         return value;
249 }
250
251 static inline float
252 from_dB(float gdb) {
253         return (exp(gdb/20.f*log(10.f)));
254 }
255
256 static inline float
257 to_dB(float g) {
258         return (20.f*log10(g));
259 }
260
261 static void
262 activate(LV2_Handle instance)
263 {
264         AExp* aexp = (AExp*)instance;
265
266         *(aexp->gainr) = 160.0f;
267         *(aexp->outlevel) = -45.0f;
268         *(aexp->inlevel) = -45.0f;
269
270 #ifdef LV2_EXTENDED
271         aexp->v_peakdb = -160.f;
272         aexp->peakdb_samples = 0;
273 #endif
274 }
275
276 static void
277 run_mono(LV2_Handle instance, uint32_t n_samples)
278 {
279         AExp* aexp = (AExp*)instance;
280
281         const float* const input = aexp->input0;
282         const float* const sc = aexp->sc;
283         float* const output = aexp->output0;
284
285         float srate = aexp->srate;
286         float width = (6.f * *(aexp->knee)) + 0.01;
287         float attack_coeff = exp(-1000.f/(*(aexp->attack) * srate));
288         float release_coeff = exp(-1000.f/(*(aexp->release) * srate));
289
290         float max = 0.f;
291         float lgaininp = 0.f;
292         float Lgain = 1.f;
293         float Lxg, Lyg;
294         float current_gainr;
295         float old_gainr;
296
297         int usesidechain = (*(aexp->sidechain) <= 0.f) ? 0 : 1;
298         uint32_t i;
299         float ingain;
300         float in0;
301         float sc0;
302
303         float ratio = *aexp->ratio;
304         float thresdb = *aexp->thresdb;
305         float makeup = *aexp->makeup;
306         float makeup_target = from_dB(makeup);
307         float makeup_gain = aexp->makeup_gain;
308
309         const float tau = aexp->tau;
310
311         if (*aexp->enable <= 0) {
312                 ratio = 1.f;
313                 thresdb = 0.f;
314                 makeup = 0.f;
315                 makeup_target = 1.f;
316                 if (!aexp->was_disabled) {
317                         *aexp->gainr = 0.f;
318                         aexp->was_disabled = true;
319                 }
320         } else {
321                 if (aexp->was_disabled) {
322                         *aexp->gainr = 160.f;
323                         aexp->was_disabled = false;
324                 }
325         }
326
327 #ifdef LV2_EXTENDED
328         if (aexp->v_knee != *aexp->knee) {
329                 aexp->v_knee = *aexp->knee;
330                 aexp->need_expose = true;
331         }
332
333         if (aexp->v_ratio != ratio) {
334                 aexp->v_ratio = ratio;
335                 aexp->need_expose = true;
336         }
337
338         if (aexp->v_thresdb != thresdb) {
339                 aexp->v_thresdb = thresdb;
340                 aexp->need_expose = true;
341         }
342
343         if (aexp->v_makeup != makeup) {
344                 aexp->v_makeup = makeup;
345                 aexp->need_expose = true;
346         }
347 #endif
348         float in_peak_db = -160.f;
349         old_gainr = *aexp->gainr;
350         float max_gainr = 0.0;
351
352         for (i = 0; i < n_samples; i++) {
353                 in0 = input[i];
354                 sc0 = sc[i];
355                 ingain = usesidechain ? fabs(sc0) : fabs(in0);
356                 Lyg = 0.f;
357                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
358                 Lxg = sanitize_denormal(Lxg);
359
360                 if (Lxg > in_peak_db) {
361                         in_peak_db = Lxg;
362                 }
363
364                 if (2.f*(Lxg-thresdb) < -width) {
365                         Lyg = thresdb + (Lxg-thresdb) * ratio;
366                         Lyg = sanitize_denormal(Lyg);
367                 } else if (2.f*(Lxg-thresdb) > width) {
368                         Lyg = Lxg;
369                 } else {
370                         Lyg = Lxg + (1.f-ratio)*(Lxg-thresdb-width/2.f)*(Lxg-thresdb-width/2.f)/(2.f*width);
371                 }
372
373                 current_gainr = Lxg - Lyg;
374
375                 if (current_gainr > old_gainr) {
376                         current_gainr = release_coeff*old_gainr + (1.f-release_coeff)*current_gainr;
377                 } else if (current_gainr < old_gainr) {
378                         current_gainr = attack_coeff*old_gainr + (1.f-attack_coeff)*current_gainr;
379                 }
380
381                 current_gainr = sanitize_denormal(current_gainr);
382
383                 Lgain = from_dB(-current_gainr);
384
385                 old_gainr = current_gainr;
386
387                 *(aexp->gainr) = current_gainr;
388                 if (current_gainr > max_gainr) {
389                         max_gainr = current_gainr;
390                 }
391
392                 lgaininp = in0 * Lgain;
393
394                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
395                 output[i] = lgaininp * makeup_gain;
396
397                 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
398         }
399
400         *(aexp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
401         *(aexp->inlevel) = in_peak_db;
402         aexp->makeup_gain = makeup_gain;
403
404 #ifdef LV2_EXTENDED
405         if (in_peak_db > aexp->v_peakdb) {
406                 aexp->v_peakdb = in_peak_db;
407                 aexp->peakdb_samples = 0;
408         } else {
409                 aexp->peakdb_samples += n_samples;
410                 if ((float)aexp->peakdb_samples/aexp->srate > RESET_PEAK_AFTER_SECONDS) {
411                         aexp->v_peakdb = in_peak_db;
412                         aexp->peakdb_samples = 0;
413                         aexp->need_expose = true;
414                 }
415         }
416
417         const float v_lvl_out = (max < 0.001f) ? -1600.f : to_dB(max);
418         const float v_lvl_in = in_peak_db;
419
420         if (fabsf (aexp->v_lvl_out - v_lvl_out) >= .1 ||
421             fabsf (aexp->v_lvl_in - v_lvl_in) >= .1 ||
422             fabsf (aexp->v_gainr - max_gainr) >= .1) {
423                 // >= 0.1dB difference
424                 aexp->need_expose = true;
425                 aexp->v_lvl_in = v_lvl_in;
426                 aexp->v_lvl_out = v_lvl_out;
427                 aexp->v_gainr = max_gainr;
428         }
429         if (aexp->need_expose && aexp->queue_draw) {
430                 aexp->need_expose = false;
431                 aexp->queue_draw->queue_draw (aexp->queue_draw->handle);
432         }
433 #endif
434 }
435
436
437 static void
438 run_stereo(LV2_Handle instance, uint32_t n_samples)
439 {
440         AExp* aexp = (AExp*)instance;
441
442         const float* const input0 = aexp->input0;
443         const float* const input1 = aexp->input1;
444         const float* const sc = aexp->sc;
445         float* const output0 = aexp->output0;
446         float* const output1 = aexp->output1;
447
448         float srate = aexp->srate;
449         float width = (6.f * *(aexp->knee)) + 0.01;
450         float attack_coeff = exp(-1000.f/(*(aexp->attack) * srate));
451         float release_coeff = exp(-1000.f/(*(aexp->release) * srate));
452
453         float max = 0.f;
454         float lgaininp = 0.f;
455         float rgaininp = 0.f;
456         float Lgain = 1.f;
457         float Lxg, Lyg;
458         float current_gainr;
459         float old_gainr = *aexp->gainr;
460
461         int usesidechain = (*(aexp->sidechain) <= 0.f) ? 0 : 1;
462         uint32_t i;
463         float ingain;
464         float in0;
465         float in1;
466         float sc0;
467         float maxabslr;
468
469         float ratio = *aexp->ratio;
470         float thresdb = *aexp->thresdb;
471         float makeup = *aexp->makeup;
472         float makeup_target = from_dB(makeup);
473         float makeup_gain = aexp->makeup_gain;
474
475         const float tau = aexp->tau;
476
477         if (*aexp->enable <= 0) {
478                 ratio = 1.f;
479                 thresdb = 0.f;
480                 makeup = 0.f;
481                 makeup_target = 1.f;
482                 if (!aexp->was_disabled) {
483                         *aexp->gainr = 0.f;
484                         aexp->was_disabled = true;
485                 }
486         } else {
487                 if (aexp->was_disabled) {
488                         *aexp->gainr = 160.f;
489                         aexp->was_disabled = false;
490                 }
491         }
492
493 #ifdef LV2_EXTENDED
494         if (aexp->v_knee != *aexp->knee) {
495                 aexp->v_knee = *aexp->knee;
496                 aexp->need_expose = true;
497         }
498
499         if (aexp->v_ratio != ratio) {
500                 aexp->v_ratio = ratio;
501                 aexp->need_expose = true;
502         }
503
504         if (aexp->v_thresdb != thresdb) {
505                 aexp->v_thresdb = thresdb;
506                 aexp->need_expose = true;
507         }
508
509         if (aexp->v_makeup != makeup) {
510                 aexp->v_makeup = makeup;
511                 aexp->need_expose = true;
512         }
513 #endif
514
515         float in_peak_db = -160.f;
516         old_gainr = *aexp->gainr;
517         float max_gainr = 0.0;
518
519         for (i = 0; i < n_samples; i++) {
520                 in0 = input0[i];
521                 in1 = input1[i];
522                 sc0 = sc[i];
523                 maxabslr = fmaxf(fabs(in0), fabs(in1));
524                 ingain = usesidechain ? fabs(sc0) : maxabslr;
525                 Lyg = 0.f;
526                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
527                 Lxg = sanitize_denormal(Lxg);
528
529                 if (Lxg > in_peak_db) {
530                         in_peak_db = Lxg;
531                 }
532
533                 if (2.f*(Lxg-thresdb) < -width) {
534                         Lyg = thresdb + (Lxg-thresdb) * ratio;
535                         Lyg = sanitize_denormal(Lyg);
536                 } else if (2.f*(Lxg-thresdb) > width) {
537                         Lyg = Lxg;
538                 } else {
539                         Lyg = Lxg + (1.f-ratio)*(Lxg-thresdb-width/2.f)*(Lxg-thresdb-width/2.f)/(2.f*width);
540                 }
541
542                 current_gainr = Lxg - Lyg;
543
544                 if (current_gainr > old_gainr) {
545                         current_gainr = release_coeff*old_gainr + (1.f-release_coeff)*current_gainr;
546                 } else if (current_gainr < old_gainr) {
547                         current_gainr = attack_coeff*old_gainr + (1.f-attack_coeff)*current_gainr;
548                 }
549
550                 current_gainr = sanitize_denormal(current_gainr);
551
552                 Lgain = from_dB(-current_gainr);
553
554                 old_gainr = current_gainr;
555
556                 *(aexp->gainr) = current_gainr;
557                 if (current_gainr > max_gainr) {
558                         max_gainr = current_gainr;
559                 }
560
561                 lgaininp = in0 * Lgain;
562                 rgaininp = in1 * Lgain;
563
564                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
565
566                 output0[i] = lgaininp * makeup_gain;
567                 output1[i] = rgaininp * makeup_gain;
568
569                 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
570         }
571
572         *(aexp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
573         *(aexp->inlevel) = in_peak_db;
574         aexp->makeup_gain = makeup_gain;
575
576 #ifdef LV2_EXTENDED
577         if (in_peak_db > aexp->v_peakdb) {
578                 aexp->v_peakdb = in_peak_db;
579                 aexp->peakdb_samples = 0;
580         } else {
581                 aexp->peakdb_samples += n_samples;
582                 if ((float)aexp->peakdb_samples/aexp->srate > RESET_PEAK_AFTER_SECONDS) {
583                         aexp->v_peakdb = in_peak_db;
584                         aexp->peakdb_samples = 0;
585                         aexp->need_expose = true;
586                 }
587         }
588
589         const float v_lvl_out = (max < 0.001f) ? -1600.f : to_dB(max);
590         const float v_lvl_in = in_peak_db;
591
592         if (fabsf (aexp->v_lvl_out - v_lvl_out) >= .1 ||
593             fabsf (aexp->v_lvl_in - v_lvl_in) >= .1 ||
594             fabsf (aexp->v_gainr - max_gainr) >= .1) {
595                 // >= 0.1dB difference
596                 aexp->need_expose = true;
597                 aexp->v_lvl_in = v_lvl_in;
598                 aexp->v_lvl_out = v_lvl_out;
599                 aexp->v_gainr = max_gainr;
600         }
601         if (aexp->need_expose && aexp->queue_draw) {
602                 aexp->need_expose = false;
603                 aexp->queue_draw->queue_draw (aexp->queue_draw->handle);
604         }
605 #endif
606 }
607
608
609 static void
610 deactivate(LV2_Handle instance)
611 {
612         activate(instance);
613 }
614
615 static void
616 cleanup(LV2_Handle instance)
617 {
618 #ifdef LV2_EXTENDED
619         AExp* aexp = (AExp*)instance;
620         if (aexp->display) {
621                 cairo_surface_destroy (aexp->display);
622         }
623 #endif
624
625         free(instance);
626 }
627
628
629 #ifndef MIN
630 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
631 #endif
632
633 #ifdef LV2_EXTENDED
634 static float
635 exp_curve (const AExp* self, float xg) {
636         const float knee = self->v_knee;
637         const float ratio = self->v_ratio;
638         const float thresdb = self->v_thresdb;
639         const float makeup = self->v_makeup;
640
641         const float width = 6.f * knee + 0.01f;
642         float yg = 0.f;
643
644         if (2.f * (xg - thresdb) < -width) {
645                 yg = thresdb + (xg - thresdb) * ratio;
646         } else if (2.f * (xg - thresdb) > width) {
647                 yg = xg;
648         } else {
649                 yg = xg + (1.f - ratio) * (xg - thresdb - width / 2.f) * (xg - thresdb - width / 2.f) / (2.f * width);
650         }
651
652         yg += makeup;
653
654         return yg;
655 }
656
657
658 static void
659 render_inline_full (cairo_t* cr, const AExp* self)
660 {
661         const float w = self->w;
662         const float h = self->h;
663
664         const float makeup_thres = self->v_thresdb + self->v_makeup;
665
666         // clear background
667         cairo_rectangle (cr, 0, 0, w, h);
668         cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
669         cairo_fill (cr);
670
671         cairo_set_line_width(cr, 1.0);
672
673         // draw grid 10dB steps
674         const double dash1[] = {1, 2};
675         const double dash2[] = {1, 3};
676         cairo_save (cr);
677         cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
678         cairo_set_dash(cr, dash2, 2, 2);
679         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
680
681         for (uint32_t d = 1; d < 7; ++d) {
682                 const float x = -.5 + floorf (w * (d * 10.f / 70.f));
683                 const float y = -.5 + floorf (h * (d * 10.f / 70.f));
684
685                 cairo_move_to (cr, x, 0);
686                 cairo_line_to (cr, x, h);
687                 cairo_stroke (cr);
688
689                 cairo_move_to (cr, 0, y);
690                 cairo_line_to (cr, w, y);
691                 cairo_stroke (cr);
692         }
693         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
694         cairo_set_dash(cr, dash1, 2, 2);
695         if (self->v_thresdb < 0) {
696                 const float y = -.5 + floorf (h * ((makeup_thres - 10.f) / -70.f));
697                 cairo_move_to (cr, 0, y);
698                 cairo_line_to (cr, w, y);
699                 cairo_stroke (cr);
700         }
701         // diagonal unity
702         cairo_move_to (cr, 0, h);
703         cairo_line_to (cr, w, 0);
704         cairo_stroke (cr);
705         cairo_restore (cr);
706
707         { // 0, 0
708                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
709                 const float x = -.5 + floorf (w * (60.f / 70.f));
710                 const float y = -.5 + floorf (h * (10.f / 70.f));
711                 cairo_move_to (cr, x, 0);
712                 cairo_line_to (cr, x, h);
713                 cairo_stroke (cr);
714                 cairo_move_to (cr, 0, y);
715                 cairo_line_to (cr, w, y);
716                 cairo_stroke (cr);
717         }
718
719         { // GR
720                 const float x = -.5 + floorf (w * (62.5f / 70.f));
721                 const float y = -.5 + floorf (h * (10.0f / 70.f));
722                 const float wd = floorf (w * (5.f / 70.f));
723                 const float ht = floorf (h * (55.f / 70.f));
724                 cairo_rectangle (cr, x, y, wd, ht);
725                 cairo_fill (cr);
726
727                 const float h_gr = fminf (ht, floorf (h * self->v_gainr / 70.f));
728                 cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
729                 cairo_rectangle (cr, x, y, wd, h_gr);
730                 cairo_fill (cr);
731                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
732                 cairo_rectangle (cr, x, y, wd, ht);
733                 cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 1.0);
734                 cairo_stroke (cr);
735         }
736
737         // draw curve
738         cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
739         cairo_move_to (cr, 0, h);
740
741         for (uint32_t x = 0; x < w; ++x) {
742                 // plot -60..+10  dB
743                 const float x_db = 70.f * (-1.f + x / (float)w) + 10.f;
744                 const float y_db = exp_curve (self, x_db) - 10.f;
745                 const float y = h * (y_db / -70.f);
746                 cairo_line_to (cr, x, y);
747         }
748         cairo_stroke_preserve (cr);
749
750         cairo_line_to (cr, w, h);
751         cairo_close_path (cr);
752         cairo_clip (cr);
753
754         // draw signal level & reduction/gradient
755         const float top = exp_curve (self, 0) - 10.f;
756         cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
757         if (top > makeup_thres - 10.f) {
758                 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
759                 cairo_pattern_add_color_stop_rgba (pat, top / -70.f, 0.8, 0.1, 0.1, 0.5);
760         }
761         if (self->v_knee > 0) {
762                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres -10.f) / -70.f), 0.7, 0.7, 0.2, 0.5);
763                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - self->v_knee - 10.f) / -70.f), 0.5, 0.5, 0.5, 0.5);
764         } else {
765                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.f)/ -70.f), 0.7, 0.7, 0.2, 0.5);
766                 cairo_pattern_add_color_stop_rgba (pat, ((makeup_thres - 10.01f) / -70.f), 0.5, 0.5, 0.5, 0.5);
767         }
768         cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
769
770         // maybe cut off at x-position?
771         const float x = w * (self->v_lvl_in + 60) / 70.f;
772         const float y = x + h*self->v_makeup;
773         cairo_rectangle (cr, 0, h - y, x, y);
774         if (self->v_ratio > 1.0) {
775                 cairo_set_source (cr, pat);
776         } else {
777                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
778         }
779         cairo_fill (cr);
780
781         // draw peak input
782         cairo_set_source_rgba (cr, .9f, .9f, .9f, 1.0);
783         cairo_set_line_width(cr, 1.0);
784
785         const float peak_x = w * (1.f - (10.f-self->v_peakdb)/70.f);
786         const float peak_y = h * (exp_curve (self, self->v_peakdb) - 10.f) / -70.f;
787
788         cairo_move_to(cr, peak_x, h);
789         cairo_line_to(cr, peak_x, peak_y);
790         cairo_stroke (cr);
791
792         cairo_pattern_destroy (pat); // TODO cache pattern
793 }
794
795 static void
796 render_inline_only_bars (cairo_t* cr, const AExp* self)
797 {
798         const float w = self->w;
799         const float h = self->h;
800
801         cairo_rectangle (cr, 0, 0, w, h);
802         cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
803         cairo_fill (cr);
804
805
806         cairo_save (cr);
807
808         const float ht = 0.25f * h;
809
810         const float x1 = w*0.05;
811         const float wd = w - 2.0f*x1;
812
813         const float y1 = 0.17*h;
814         const float y2 = h - y1 - ht;
815
816         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
817
818         cairo_rectangle (cr, x1, y1, wd, ht);
819         cairo_fill (cr);
820
821         cairo_rectangle (cr, x1, y2, wd, ht);
822         cairo_fill (cr);
823
824         cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
825         const float w_gr = (self->v_gainr > 60.f) ? wd : wd * self->v_gainr * (1.f/60.f);
826         cairo_rectangle (cr, x1+wd-w_gr, y2, w_gr, ht);
827         cairo_fill (cr);
828
829         if (self->v_lvl_in > -60.f) {
830                 if (self->v_lvl_out > 10.f) {
831                         cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
832                 } else if (self->v_lvl_out > 0.f) {
833                         cairo_set_source_rgba (cr, 0.66, 0.66, 0.0, 1.0);
834                 } else {
835                         cairo_set_source_rgba (cr, 0.0, 0.66, 0.0, 1.0);
836                 }
837                 const float w_g = (self->v_lvl_in > 10.f) ? wd : wd * (60.f+self->v_lvl_in) / 70.f;
838                 cairo_rectangle (cr, x1, y1, w_g, ht);
839                 cairo_fill (cr);
840         }
841
842         cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
843
844         const float tck = 0.33*ht;
845
846         cairo_set_line_width (cr, .5);
847
848         for (uint32_t d = 1; d < 7; ++d) {
849                 const float x = x1 + (d * wd * (10.f / 70.f));
850
851                 cairo_move_to (cr, x, y1);
852                 cairo_line_to (cr, x, y1+tck);
853
854                 cairo_move_to (cr, x, y1+ht);
855                 cairo_line_to (cr, x, y1+ht-tck);
856
857                 cairo_move_to (cr, x, y2);
858                 cairo_line_to (cr, x, y2+tck);
859
860                 cairo_move_to (cr, x, y2+ht);
861                 cairo_line_to (cr, x, y2+ht-tck);
862         }
863
864         cairo_stroke (cr);
865
866         const float x_0dB = x1 + wd*(60.f/70.f);
867
868         cairo_move_to (cr, x_0dB, y1);
869         cairo_line_to (cr, x_0dB, y1+ht);
870
871         cairo_rectangle (cr, x1, y1, wd, ht);
872         cairo_rectangle (cr, x1, y2, wd, ht);
873         cairo_stroke (cr);
874
875         cairo_set_line_width (cr, 2.0);
876
877         // visualize threshold
878         const float tr = x1 + wd * (60.f+self->v_thresdb) / 70.f;
879         cairo_set_source_rgba (cr, 0.95, 0.95, 0.0, 1.0);
880         cairo_move_to (cr, tr, y1);
881         cairo_line_to (cr, tr, y1+ht);
882         cairo_stroke (cr);
883
884         // visualize ratio
885         const float reduced_0dB = self->v_thresdb * (1.f - 1.f/self->v_ratio);
886         const float rt = x1 + wd * (60.f+reduced_0dB) / 70.f;
887         cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
888         cairo_move_to (cr, rt, y1);
889         cairo_line_to (cr, rt, y1+ht);
890         cairo_stroke (cr);
891
892         // visualize in peak
893         if (self->v_peakdb > -60.f) {
894                 cairo_set_source_rgba (cr, 0.0, 1.0, 0.0, 1.0);
895                 const float pk = (self->v_peakdb > 10.f) ? x1+wd : wd * (60.f+self->v_peakdb) / 70.f;
896                 cairo_move_to (cr, pk, y1);
897                 cairo_line_to (cr, pk, y1+ht);
898                 cairo_stroke (cr);
899         }
900 }
901
902
903 static LV2_Inline_Display_Image_Surface *
904 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
905 {
906         AExp* self = (AExp*)instance;
907
908         uint32_t h = MIN (w, max_h);
909         if (w < 200) {
910                 h = 40;
911         }
912
913         if (!self->display || self->w != w || self->h != h) {
914                 if (self->display) cairo_surface_destroy(self->display);
915                 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
916                 self->w = w;
917                 self->h = h;
918         }
919
920         cairo_t* cr = cairo_create (self->display);
921
922         if (w >= 200) {
923                 render_inline_full (cr, self);
924         } else {
925                 render_inline_only_bars (cr, self);
926         }
927
928         cairo_destroy (cr);
929
930         cairo_surface_flush (self->display);
931         self->surf.width = cairo_image_surface_get_width (self->display);
932         self->surf.height = cairo_image_surface_get_height (self->display);
933         self->surf.stride = cairo_image_surface_get_stride (self->display);
934         self->surf.data = cairo_image_surface_get_data  (self->display);
935
936         return &self->surf;
937 }
938 #endif
939
940 static const void*
941 extension_data(const char* uri)
942 {
943 #ifdef LV2_EXTENDED
944         static const LV2_Inline_Display_Interface display  = { render_inline };
945         if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
946                 return &display;
947         }
948 #endif
949         return NULL;
950 }
951
952 static const LV2_Descriptor descriptor_mono = {
953         AEXP_URI,
954         instantiate,
955         connect_mono,
956         activate,
957         run_mono,
958         deactivate,
959         cleanup,
960         extension_data
961 };
962
963 static const LV2_Descriptor descriptor_stereo = {
964         AEXP_STEREO_URI,
965         instantiate,
966         connect_stereo,
967         activate,
968         run_stereo,
969         deactivate,
970         cleanup,
971         extension_data
972 };
973
974 LV2_SYMBOL_EXPORT
975 const LV2_Descriptor*
976 lv2_descriptor(uint32_t index)
977 {
978         switch (index) {
979         case 0:
980                 return &descriptor_mono;
981         case 1:
982                 return &descriptor_stereo;
983         default:
984                 return NULL;
985         }
986 }