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