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