Also the stereo version needs to set need_expose
[ardour.git] / libs / plugins / a-comp.lv2 / a-comp.c
1 /* a-comp
2  * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <math.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdbool.h>
19
20 #ifdef LV2_EXTENDED
21 #include <cairo/cairo.h>
22 #include "ardour/lv2_extensions.h"
23 #endif
24
25 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
26
27 #define ACOMP_URI               "urn:ardour:a-comp"
28 #define ACOMP_STEREO_URI        "urn:ardour:a-comp#stereo"
29
30 #ifndef M_PI
31 #  define M_PI 3.14159265358979323846
32 #endif
33
34 #ifdef COMPILER_MSVC
35 #include <float.h>
36 #define isfinite_local(val) (bool)_finite((double)val)
37 #else
38 #define isfinite_local isfinite
39 #endif
40
41 typedef enum {
42         ACOMP_ATTACK = 0,
43         ACOMP_RELEASE,
44         ACOMP_KNEE,
45         ACOMP_RATIO,
46         ACOMP_THRESHOLD,
47         ACOMP_MAKEUP,
48
49         ACOMP_GAINR,
50         ACOMP_OUTLEVEL,
51         ACOMP_SIDECHAIN,
52         ACOMP_ENABLE,
53
54         ACOMP_A0,
55         ACOMP_A1,
56         ACOMP_A2,
57         ACOMP_A3,
58         ACOMP_A4,
59 } PortIndex;
60
61 typedef struct {
62         float* attack;
63         float* release;
64         float* knee;
65         float* ratio;
66         float* thresdb;
67         float* makeup;
68
69         float* gainr;
70         float* outlevel;
71         float* sidechain;
72         float* enable;
73
74         float* input0;
75         float* input1;
76         float* sc;
77         float* output0;
78         float* output1;
79
80         float srate;
81         float old_yl;
82         float old_y1;
83         float old_yg;
84
85         float makeup_gain;
86         float tau;
87
88 #ifdef LV2_EXTENDED
89         LV2_Inline_Display_Image_Surface surf;
90         bool                     need_expose;
91         cairo_surface_t*         display;
92         LV2_Inline_Display*      queue_draw;
93         uint32_t                 w, h;
94
95         /* ports pointers are only valid during run so we'll
96          * have to cache them for the display, besides
97          * we do want to check for changes
98          */
99         float v_knee;
100         float v_ratio;
101         float v_thresdb;
102         float v_makeup;
103         float v_lvl;
104         float v_lvl_in;
105         float v_lvl_out;
106 #endif
107 } AComp;
108
109 static LV2_Handle
110 instantiate(const LV2_Descriptor* descriptor,
111             double rate,
112             const char* bundle_path,
113             const LV2_Feature* const* features)
114 {
115         AComp* acomp = (AComp*)calloc(1, sizeof(AComp));
116
117         for (int i=0; features[i]; ++i) {
118 #ifdef LV2_EXTENDED
119                 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
120                         acomp->queue_draw = (LV2_Inline_Display*) features[i]->data;
121                 }
122 #endif
123         }
124
125         acomp->srate = rate;
126         acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
127         acomp->tau = (1.0 - exp (-2.f * M_PI * 25.f / acomp->srate));
128 #ifdef LV2_EXTENDED
129         acomp->need_expose = true;
130 #endif
131
132         return (LV2_Handle)acomp;
133 }
134
135
136 static void
137 connect_port(LV2_Handle instance,
138              uint32_t port,
139              void* data)
140 {
141         AComp* acomp = (AComp*)instance;
142
143         switch ((PortIndex)port) {
144                 case ACOMP_ATTACK:
145                         acomp->attack = (float*)data;
146                         break;
147                 case ACOMP_RELEASE:
148                         acomp->release = (float*)data;
149                         break;
150                 case ACOMP_KNEE:
151                         acomp->knee = (float*)data;
152                         break;
153                 case ACOMP_RATIO:
154                         acomp->ratio = (float*)data;
155                         break;
156                 case ACOMP_THRESHOLD:
157                         acomp->thresdb = (float*)data;
158                         break;
159                 case ACOMP_MAKEUP:
160                         acomp->makeup = (float*)data;
161                         break;
162                 case ACOMP_GAINR:
163                         acomp->gainr = (float*)data;
164                         break;
165                 case ACOMP_OUTLEVEL:
166                         acomp->outlevel = (float*)data;
167                         break;
168                 case ACOMP_SIDECHAIN:
169                         acomp->sidechain = (float*)data;
170                         break;
171                 case ACOMP_ENABLE:
172                         acomp->enable = (float*)data;
173                         break;
174                 default:
175                         break;
176         }
177 }
178
179 static void
180 connect_mono(LV2_Handle instance,
181              uint32_t port,
182              void* data)
183 {
184         AComp* acomp = (AComp*)instance;
185         connect_port (instance, port, data);
186
187         switch ((PortIndex)port) {
188                 case ACOMP_A0:
189                         acomp->input0 = (float*)data;
190                         break;
191                 case ACOMP_A1:
192                         acomp->sc = (float*)data;
193                         break;
194                 case ACOMP_A2:
195                         acomp->output0 = (float*)data;
196                         break;
197         default:
198                 break;
199         }
200 }
201
202 static void
203 connect_stereo(LV2_Handle instance,
204                uint32_t port,
205                void* data)
206 {
207         AComp* acomp = (AComp*)instance;
208         connect_port (instance, port, data);
209
210         switch ((PortIndex)port) {
211                 case ACOMP_A0:
212                         acomp->input0 = (float*)data;
213                         break;
214                 case ACOMP_A1:
215                         acomp->input1 = (float*)data;
216                         break;
217                 case ACOMP_A2:
218                         acomp->sc = (float*)data;
219                         break;
220                 case ACOMP_A3:
221                         acomp->output0 = (float*)data;
222                         break;
223                 case ACOMP_A4:
224                         acomp->output1 = (float*)data;
225                         break;
226         default:
227                 break;
228         }
229 }
230
231 // Force already-denormal float value to zero
232 static inline float
233 sanitize_denormal(float value) {
234         if (!isnormal(value)) {
235                 value = 0.f;
236         }
237         return value;
238 }
239
240 static inline float
241 from_dB(float gdb) {
242         return (exp(gdb/20.f*log(10.f)));
243 }
244
245 static inline float
246 to_dB(float g) {
247         return (20.f*log10(g));
248 }
249
250 static void
251 activate(LV2_Handle instance)
252 {
253         AComp* acomp = (AComp*)instance;
254
255         *(acomp->gainr) = 0.0f;
256         *(acomp->outlevel) = -45.0f;
257         acomp->old_yl=acomp->old_y1=acomp->old_yg=0.f;
258 }
259
260 static void
261 run_mono(LV2_Handle instance, uint32_t n_samples)
262 {
263         AComp* acomp = (AComp*)instance;
264
265         const float* const input = acomp->input0;
266         const float* const sc = acomp->sc;
267         float* const output = acomp->output0;
268
269         float srate = acomp->srate;
270         float width = (6.f * *(acomp->knee)) + 0.01;
271         float cdb=0.f;
272         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
273         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
274
275         float max = 0.f;
276         float lgaininp = 0.f;
277         float Lgain = 1.f;
278         float Lxg, Lxl, Lyg, Lyl, Ly1;
279         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
280         uint32_t i;
281         float ingain;
282         float in0;
283         float sc0;
284
285         float ratio = *acomp->ratio;
286         float thresdb = *acomp->thresdb;
287         float makeup = *acomp->makeup;
288         float makeup_target = from_dB(makeup);
289         float makeup_gain = acomp->makeup_gain;
290
291         const float tau = acomp->tau;
292
293         if (*acomp->enable <= 0) {
294                 ratio = 1.f;
295                 thresdb = 0.f;
296                 makeup = 0.f;
297                 makeup_target = 1.f;
298         }
299
300 #ifdef LV2_EXTENDED
301         if (acomp->v_knee != *acomp->knee) {
302                 acomp->v_knee = *acomp->knee;
303                 acomp->need_expose = true;
304         }
305
306         if (acomp->v_ratio != ratio) {
307                 acomp->v_ratio = ratio;
308                 acomp->need_expose = true;
309         }
310
311         if (acomp->v_thresdb != thresdb) {
312                 acomp->v_thresdb = thresdb;
313                 acomp->need_expose = true;
314         }
315
316         if (acomp->v_makeup != makeup) {
317                 acomp->v_makeup = makeup;
318                 acomp->need_expose = true;
319         }
320 #endif
321
322         float in_peak = 0;
323
324         for (i = 0; i < n_samples; i++) {
325                 in0 = input[i];
326                 sc0 = sc[i];
327                 ingain = usesidechain ? fabs(sc0) : fabs(in0);
328                 in_peak = fmaxf (in_peak, ingain);
329                 Lyg = 0.f;
330                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
331                 Lxg = sanitize_denormal(Lxg);
332
333
334                 if (2.f*(Lxg-thresdb) < -width) {
335                         Lyg = Lxg;
336                 } else if (2.f*(Lxg-thresdb) > width) {
337                         Lyg = thresdb + (Lxg-thresdb)/ratio;
338                         Lyg = sanitize_denormal(Lyg);
339                 } else {
340                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
341                 }
342
343                 Lxl = Lxg - Lyg;
344
345                 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
346                 acomp->old_yl = sanitize_denormal(acomp->old_yl);
347                 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
348                 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
349                 Ly1 = sanitize_denormal(Ly1);
350                 Lyl = sanitize_denormal(Lyl);
351
352                 cdb = -Lyl;
353                 Lgain = from_dB(cdb);
354
355                 *(acomp->gainr) = Lyl;
356
357                 lgaininp = in0 * Lgain;
358
359                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
360                 output[i] = lgaininp * makeup_gain;
361
362                 max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
363
364                 // TODO re-use local variables on stack
365                 // store values back to acomp at the end of the inner-loop
366                 acomp->old_yl = Lyl;
367                 acomp->old_y1 = Ly1;
368                 acomp->old_yg = Lyg;
369         }
370
371         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
372         acomp->makeup_gain = makeup_gain;
373
374 #ifdef LV2_EXTENDED
375         acomp->v_lvl += .1 * (in_peak - acomp->v_lvl) + 1e-12;  // crude LPF TODO use n_samples/rate TC
376         if (!isfinite_local (acomp->v_lvl)) {
377                 acomp->v_lvl = 0.f;
378         }
379         const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
380         const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
381         if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
382                 // >= 1dB difference
383                 acomp->need_expose = true;
384                 acomp->v_lvl_in = v_lvl_in;
385                 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
386         }
387         if (acomp->need_expose && acomp->queue_draw) {
388                 acomp->need_expose = false;
389                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
390         }
391 #endif
392 }
393
394 static void
395 run_stereo(LV2_Handle instance, uint32_t n_samples)
396 {
397         AComp* acomp = (AComp*)instance;
398
399         const float* const input0 = acomp->input0;
400         const float* const input1 = acomp->input1;
401         const float* const sc = acomp->sc;
402         float* const output0 = acomp->output0;
403         float* const output1 = acomp->output1;
404
405         float srate = acomp->srate;
406         float width = (6.f * *(acomp->knee)) + 0.01;
407         float cdb=0.f;
408         float attack_coeff = exp(-1000.f/(*(acomp->attack) * srate));
409         float release_coeff = exp(-1000.f/(*(acomp->release) * srate));
410
411         float max = 0.f;
412         float lgaininp = 0.f;
413         float rgaininp = 0.f;
414         float Lgain = 1.f;
415         float Lxg, Lxl, Lyg, Lyl, Ly1;
416         int usesidechain = (*(acomp->sidechain) <= 0.f) ? 0 : 1;
417         uint32_t i;
418         float ingain;
419         float in0;
420         float in1;
421         float sc0;
422         float maxabslr;
423
424         float ratio = *acomp->ratio;
425         float thresdb = *acomp->thresdb;
426         float makeup = *acomp->makeup;
427         float makeup_target = from_dB(makeup);
428         float makeup_gain = acomp->makeup_gain;
429
430         const float tau = acomp->tau;
431
432         if (*acomp->enable <= 0) {
433                 ratio = 1.f;
434                 thresdb = 0.f;
435                 makeup_target = 1.f;
436         }
437
438 #ifdef LV2_EXTENDED
439         if (acomp->v_knee != *acomp->knee) {
440                 acomp->v_knee = *acomp->knee;
441                 acomp->need_expose = true;
442         }
443
444         if (acomp->v_ratio != ratio) {
445                 acomp->v_ratio = ratio;
446                 acomp->need_expose = true;
447         }
448
449         if (acomp->v_thresdb != thresdb) {
450                 acomp->v_thresdb = thresdb;
451                 acomp->need_expose = true;
452         }
453
454         if (acomp->v_makeup != makeup) {
455                 acomp->v_makeup = makeup;
456                 acomp->need_expose = true;
457         }
458 #endif
459
460         float in_peak = 0;
461
462         for (i = 0; i < n_samples; i++) {
463                 in0 = input0[i];
464                 in1 = input1[i];
465                 sc0 = sc[i];
466                 maxabslr = fmaxf(fabs(in0), fabs(in1));
467                 ingain = usesidechain ? fabs(sc0) : maxabslr;
468                 in_peak = fmaxf (in_peak, ingain);
469                 Lyg = 0.f;
470                 Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
471                 Lxg = sanitize_denormal(Lxg);
472
473
474                 if (2.f*(Lxg-thresdb) < -width) {
475                         Lyg = Lxg;
476                 } else if (2.f*(Lxg-thresdb) > width) {
477                         Lyg = thresdb + (Lxg-thresdb)/ratio;
478                         Lyg = sanitize_denormal(Lyg);
479                 } else {
480                         Lyg = Lxg + (1.f/ratio-1.f)*(Lxg-thresdb+width/2.f)*(Lxg-thresdb+width/2.f)/(2.f*width);
481                 }
482
483                 Lxl = Lxg - Lyg;
484
485                 acomp->old_y1 = sanitize_denormal(acomp->old_y1);
486                 acomp->old_yl = sanitize_denormal(acomp->old_yl);
487                 Ly1 = fmaxf(Lxl, release_coeff * acomp->old_y1+(1.f-release_coeff)*Lxl);
488                 Lyl = attack_coeff * acomp->old_yl+(1.f-attack_coeff)*Ly1;
489                 Ly1 = sanitize_denormal(Ly1);
490                 Lyl = sanitize_denormal(Lyl);
491
492                 cdb = -Lyl;
493                 Lgain = from_dB(cdb);
494
495                 *(acomp->gainr) = Lyl;
496
497                 lgaininp = in0 * Lgain;
498                 rgaininp = in1 * Lgain;
499
500                 makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
501
502                 output0[i] = lgaininp * makeup_gain;
503                 output1[i] = rgaininp * makeup_gain;
504
505                 max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
506
507                 // TODO re-use local variables on stack
508                 // store values back to acomp at the end of the inner-loop
509                 acomp->old_yl = Lyl;
510                 acomp->old_y1 = Ly1;
511                 acomp->old_yg = Lyg;
512         }
513
514         *(acomp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
515         acomp->makeup_gain = makeup_gain;
516
517 #ifdef LV2_EXTENDED
518         acomp->v_lvl += .1 * (in_peak - acomp->v_lvl) + 1e-12;  // crude LPF TODO use n_samples/rate TC
519         if (!isfinite_local (acomp->v_lvl)) {
520                 acomp->v_lvl = 0.f;
521         }
522         const float v_lvl_in = (acomp->v_lvl < 0.001f) ? -60.f : to_dB(acomp->v_lvl);
523         const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
524         if (fabsf (acomp->v_lvl_out - v_lvl_out) >= 1 || fabsf (acomp->v_lvl_in - v_lvl_in) >= 1) {
525                 // >= 1dB difference
526                 acomp->need_expose = true;
527                 acomp->v_lvl_in = v_lvl_in;
528                 acomp->v_lvl_out = v_lvl_out - to_dB(makeup_gain);
529         }
530         if (acomp->need_expose && acomp->queue_draw) {
531                 acomp->need_expose = false;
532                 acomp->queue_draw->queue_draw (acomp->queue_draw->handle);
533         }
534 #endif
535 }
536
537 static void
538 deactivate(LV2_Handle instance)
539 {
540         activate(instance);
541 }
542
543 static void
544 cleanup(LV2_Handle instance)
545 {
546 #ifdef LV2_EXTENDED
547         AComp* acomp = (AComp*)instance;
548         if (acomp->display) {
549                 cairo_surface_destroy (acomp->display);
550         }
551 #endif
552
553         free(instance);
554 }
555
556
557 #ifndef MIN
558 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
559 #endif
560
561 #ifdef LV2_EXTENDED
562 static float
563 comp_curve (AComp* self, float xg) {
564         const float knee = self->v_knee;
565         const float ratio = self->v_ratio;
566         const float thresdb = self->v_thresdb;
567         const float makeup = self->v_makeup;
568
569         const float width = 6.f * knee + 0.01f;
570         float yg = 0.f;
571
572         if (2.f * (xg - thresdb) < -width) {
573                 yg = xg;
574         } else if (2.f * (xg - thresdb) > width) {
575                 yg = thresdb + (xg - thresdb) / ratio;
576         } else {
577                 yg = xg + (1.f / ratio - 1.f ) * (xg - thresdb + width / 2.f) * (xg - thresdb + width / 2.f) / (2.f * width);
578         }
579
580         yg += makeup;
581
582         return yg;
583 }
584
585 static LV2_Inline_Display_Image_Surface *
586 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
587 {
588         AComp* self = (AComp*)instance;
589         uint32_t h = MIN (w, max_h);
590
591         if (!self->display || self->w != w || self->h != h) {
592                 if (self->display) cairo_surface_destroy(self->display);
593                 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
594                 self->w = w;
595                 self->h = h;
596         }
597
598         cairo_t* cr = cairo_create (self->display);
599
600         // clear background
601         cairo_rectangle (cr, 0, 0, w, h);
602         cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
603         cairo_fill (cr);
604
605         cairo_set_line_width(cr, 1.0);
606
607         // draw grid 10dB steps
608         const double dash1[] = {1, 2};
609         const double dash2[] = {1, 3};
610         cairo_save (cr);
611         cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
612         cairo_set_dash(cr, dash2, 2, 2);
613         cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
614
615         for (uint32_t d = 1; d < 6; ++d) {
616                 const float x = -.5 + floorf (w * (d * 10.f / 60.f));
617                 const float y = -.5 + floorf (h * (d * 10.f / 60.f));
618
619                 cairo_move_to (cr, x, 0);
620                 cairo_line_to (cr, x, h);
621                 cairo_stroke (cr);
622
623                 cairo_move_to (cr, 0, y);
624                 cairo_line_to (cr, w, y);
625                 cairo_stroke (cr);
626         }
627         if (self->v_thresdb < 0) {
628                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
629                 const float y = -.5 + floorf (h * (self->v_thresdb / -60.f));
630                 cairo_set_dash(cr, dash1, 2, 2);
631                 cairo_move_to (cr, 0, y);
632                 cairo_line_to (cr, w, y);
633                 cairo_stroke (cr);
634                 cairo_move_to (cr, 0, h);
635                 cairo_line_to (cr, w, 0);
636                 cairo_stroke (cr);
637         }
638         cairo_restore (cr);
639
640
641         // draw curve
642         cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
643         cairo_move_to (cr, 0, h);
644
645         for (uint32_t x = 0; x < w; ++x) {
646                 // plot -60..0  dB
647                 const float x_db = 60.f * (-1.f + x / (float)w);
648                 const float y_db = comp_curve (self, x_db);
649                 const float y = h * (y_db / -60.f);
650                 cairo_line_to (cr, x, y);
651         }
652         cairo_stroke_preserve (cr);
653
654         cairo_line_to (cr, w, h);
655         cairo_close_path (cr);
656         cairo_clip (cr);
657
658         // draw signal level & reduction/gradient
659         const float top = comp_curve (self, 0);
660         cairo_pattern_t* pat = cairo_pattern_create_linear (0.0, 0.0, 0.0, h);
661         if (top > self->v_thresdb) {
662                 cairo_pattern_add_color_stop_rgba (pat, 0.0, 0.8, 0.1, 0.1, 0.5);
663                 cairo_pattern_add_color_stop_rgba (pat, top / -60.f, 0.8, 0.1, 0.1, 0.5);
664         }
665         if (self->v_knee > 0) {
666                 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
667                 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - self->v_knee) / -60.f), 0.5, 0.5, 0.5, 0.5);
668         } else {
669                 cairo_pattern_add_color_stop_rgba (pat, (self->v_thresdb / -60.f), 0.7, 0.7, 0.2, 0.5);
670                 cairo_pattern_add_color_stop_rgba (pat, ((self->v_thresdb - .01) / -60.f), 0.5, 0.5, 0.5, 0.5);
671         }
672         cairo_pattern_add_color_stop_rgba (pat, 1.0, 0.5, 0.5, 0.5, 0.5);
673
674         // maybe cut off at x-position?
675         const float x = w * (self->v_lvl_in + 60) / 60.f;
676         const float y = x + h*self->v_makeup;
677         cairo_rectangle (cr, 0, h - y, x, y);
678         if (self->v_ratio > 1.0) {
679                 cairo_set_source (cr, pat);
680         } else {
681                 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
682         }
683         cairo_fill (cr);
684
685         cairo_pattern_destroy (pat); // TODO cache pattern
686
687
688         // create RGBA surface
689         cairo_destroy (cr);
690         cairo_surface_flush (self->display);
691         self->surf.width = cairo_image_surface_get_width (self->display);
692         self->surf.height = cairo_image_surface_get_height (self->display);
693         self->surf.stride = cairo_image_surface_get_stride (self->display);
694         self->surf.data = cairo_image_surface_get_data  (self->display);
695
696         return &self->surf;
697 }
698 #endif
699
700 static const void*
701 extension_data(const char* uri)
702 {
703 #ifdef LV2_EXTENDED
704         static const LV2_Inline_Display_Interface display  = { render_inline };
705         if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
706                 return &display;
707         }
708 #endif
709         return NULL;
710 }
711
712 static const LV2_Descriptor descriptor_mono = {
713         ACOMP_URI,
714         instantiate,
715         connect_mono,
716         activate,
717         run_mono,
718         deactivate,
719         cleanup,
720         extension_data
721 };
722
723 static const LV2_Descriptor descriptor_stereo = {
724         ACOMP_STEREO_URI,
725         instantiate,
726         connect_stereo,
727         activate,
728         run_stereo,
729         deactivate,
730         cleanup,
731         extension_data
732 };
733
734 LV2_SYMBOL_EXPORT
735 const LV2_Descriptor*
736 lv2_descriptor(uint32_t index)
737 {
738         switch (index) {
739         case 0:
740                 return &descriptor_mono;
741         case 1:
742                 return &descriptor_stereo;
743         default:
744                 return NULL;
745         }
746 }