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