Fix uninitialized variable
[ardour.git] / libs / plugins / a-exp.lv2 / a-exp.c
index d89ce7ad250184621d0bc7d78aa11e9864056ecf..9eca7b5d2fcd28d4913ff167aec3073b8f2575ca 100644 (file)
@@ -27,6 +27,8 @@
 
 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
 
+#define RESET_PEAK_AFTER_SECONDS 3
+
 #define AEXP_URI "urn:ardour:a-exp"
 #define AEXP_STEREO_URI "urn:ardour:a-exp#stereo"
 
 #define isfinite_local isfinite
 #endif
 
+#ifndef FLT_EPSILON
+#  define FLT_EPSILON 1.192093e-07
+#endif
+
+
 typedef enum {
        AEXP_ATTACK = 0,
        AEXP_RELEASE,
@@ -51,6 +58,7 @@ typedef enum {
 
        AEXP_GAINR,
        AEXP_OUTLEVEL,
+       AEXP_INLEVEL,
        AEXP_SIDECHAIN,
        AEXP_ENABLE,
 
@@ -71,6 +79,7 @@ typedef struct {
 
        float* gainr;
        float* outlevel;
+       float* inlevel;
        float* sidechain;
        float* enable;
 
@@ -80,13 +89,13 @@ typedef struct {
        float* output0;
        float* output1;
 
+       uint32_t n_channels;
+
        float srate;
-       float old_yl;
-       float old_y1;
-       float old_yg;
 
        float makeup_gain;
-       float tau;
+
+       bool was_disabled;
 
 #ifdef LV2_EXTENDED
        LV2_Inline_Display_Image_Surface surf;
@@ -104,10 +113,11 @@ typedef struct {
        float v_thresdb;
        float v_gainr;
        float v_makeup;
-       float v_lvl;
-       float v_lv1;
        float v_lvl_in;
        float v_lvl_out;
+
+       float v_peakdb;
+       uint32_t peakdb_samples;
 #endif
 } AExp;
 
@@ -119,6 +129,15 @@ instantiate(const LV2_Descriptor* descriptor,
 {
        AExp* aexp = (AExp*)calloc(1, sizeof(AExp));
 
+       if (!strcmp (descriptor->URI, AEXP_URI)) {
+               aexp->n_channels = 1;
+       } else if (!strcmp (descriptor->URI, AEXP_STEREO_URI)) {
+               aexp->n_channels = 2;
+       } else {
+               free (aexp);
+               return NULL;
+       }
+
        for (int i=0; features[i]; ++i) {
 #ifdef LV2_EXTENDED
                if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
@@ -128,8 +147,6 @@ instantiate(const LV2_Descriptor* descriptor,
        }
 
        aexp->srate = rate;
-       aexp->old_yl=aexp->old_y1=aexp->old_yg=0.f;
-       aexp->tau = (1.0 - exp (-2.f * M_PI * 25.f / aexp->srate));
 #ifdef LV2_EXTENDED
        aexp->need_expose = true;
        aexp->v_lvl_out = -70.f;
@@ -170,6 +187,9 @@ connect_port(LV2_Handle instance,
                case AEXP_OUTLEVEL:
                        aexp->outlevel = (float*)data;
                        break;
+               case AEXP_INLEVEL:
+                       aexp->inlevel = (float*)data;
+                       break;
                case AEXP_SIDECHAIN:
                        aexp->sidechain = (float*)data;
                        break;
@@ -257,185 +277,44 @@ activate(LV2_Handle instance)
 {
        AExp* aexp = (AExp*)instance;
 
-       *(aexp->gainr) = 0.0f;
+       *(aexp->gainr) = 160.0f;
        *(aexp->outlevel) = -45.0f;
-       aexp->old_yl=aexp->old_y1=aexp->old_yg=0.f;
-}
-
-static void
-run_mono(LV2_Handle instance, uint32_t n_samples)
-{
-       AExp* aexp = (AExp*)instance;
-
-       const float* const input = aexp->input0;
-       const float* const sc = aexp->sc;
-       float* const output = aexp->output0;
-
-       float srate = aexp->srate;
-       float width = (6.f * *(aexp->knee)) + 0.01;
-       float cdb=0.f;
-       float attack_coeff = exp(-1000.f/(*(aexp->attack) * srate));
-       float release_coeff = exp(-1000.f/(*(aexp->release) * srate));
-
-       float max = 0.f;
-       float lgaininp = 0.f;
-       float Lgain = 1.f;
-       float Lxg, Lxl, Lyg, Lyl, Ly1;
-       int usesidechain = (*(aexp->sidechain) <= 0.f) ? 0 : 1;
-       uint32_t i;
-       float ingain;
-       float in0;
-       float sc0;
-
-       float ratio = *aexp->ratio;
-       float thresdb = *aexp->thresdb;
-       float makeup = *aexp->makeup;
-       float makeup_target = from_dB(makeup);
-       float makeup_gain = aexp->makeup_gain;
-
-       const float tau = aexp->tau;
-
-       if (*aexp->enable <= 0) {
-               ratio = 1.f;
-               thresdb = 0.f;
-               makeup = 0.f;
-               makeup_target = 1.f;
-       }
-
-#ifdef LV2_EXTENDED
-       if (aexp->v_knee != *aexp->knee) {
-               aexp->v_knee = *aexp->knee;
-               aexp->need_expose = true;
-       }
-
-       if (aexp->v_ratio != ratio) {
-               aexp->v_ratio = ratio;
-               aexp->need_expose = true;
-       }
-
-       if (aexp->v_thresdb != thresdb) {
-               aexp->v_thresdb = thresdb;
-               aexp->need_expose = true;
-       }
-
-       if (aexp->v_makeup != makeup) {
-               aexp->v_makeup = makeup;
-               aexp->need_expose = true;
-       }
-#endif
-
-       float in_peak = 0;
-       aexp->v_gainr = 0.0;
-
-       for (i = 0; i < n_samples; i++) {
-               in0 = input[i];
-               sc0 = sc[i];
-               ingain = usesidechain ? fabs(sc0) : fabs(in0);
-               in_peak = fmaxf (in_peak, ingain);
-               Lyg = 0.f;
-               Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
-               Lxg = sanitize_denormal(Lxg);
-
-               if (2.f*(Lxg-thresdb) < -width) {
-                       Lyg = thresdb + (Lxg-thresdb) * ratio;
-                       Lyg = sanitize_denormal(Lyg);
-               } else if (2.f*(Lxg-thresdb) > width) {
-                       Lyg = Lxg;
-               } else {
-                       Lyg = Lxg + (1.f-ratio)*(Lxg-thresdb-width/2.f)*(Lxg-thresdb-width/2.f)/(2.f*width);
-               }
-
-               Lxl = Lxg - Lyg;
-
-               aexp->old_y1 = sanitize_denormal(aexp->old_y1);
-               aexp->old_yl = sanitize_denormal(aexp->old_yl);
-               Ly1 = fminf(Lxl, release_coeff * aexp->old_y1+(1.f-release_coeff)*Lxl);
-               Lyl = attack_coeff * aexp->old_yl+(1.f-attack_coeff)*Ly1;
-               Ly1 = sanitize_denormal(Ly1);
-               Lyl = sanitize_denormal(Lyl);
-
-               cdb = -Lyl;
-               Lgain = from_dB(cdb);
-
-               *(aexp->gainr) = Lyl;
-               if (Lyl > aexp->v_gainr) {
-                       aexp->v_gainr = Lyl;
-               }
-
-               lgaininp = in0 * Lgain;
-
-               makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
-               output[i] = lgaininp * makeup_gain;
-
-               max = (fabsf(output[i]) > max) ? fabsf(output[i]) : sanitize_denormal(max);
-
-               // TODO re-use local variables on stack
-               // store values back to aexp at the end of the inner-loop
-               aexp->old_yl = Lyl;
-               aexp->old_y1 = Ly1;
-               aexp->old_yg = Lyg;
-       }
-
-       *(aexp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
-       aexp->makeup_gain = makeup_gain;
+       *(aexp->inlevel) = -45.0f;
 
 #ifdef LV2_EXTENDED
-       const float old_v_lv1 = aexp->v_lv1;
-       const float old_v_lvl = aexp->v_lvl;
-       const float tot_rel_c = exp(-1000.f/(*(aexp->release) * srate) * n_samples);
-       const float tot_atk_c = exp(-1000.f/(*(aexp->attack) * srate) * n_samples);
-       aexp->v_lv1 = fmaxf (in_peak, tot_rel_c*old_v_lv1 + (1.f-tot_rel_c)*in_peak);
-       aexp->v_lvl = tot_atk_c*old_v_lvl + (1.f-tot_atk_c)*aexp->v_lv1;
-
-       if (!isfinite_local (aexp->v_lvl)) {
-               aexp->v_lvl = 0.f;
-       }
-       const float v_lvl_in = (aexp->v_lvl < 0.001f) ? -60.f : to_dB(aexp->v_lvl);
-       const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
-       if (fabsf (aexp->v_lvl_out - v_lvl_out) >= 1 || fabsf (aexp->v_lvl_in - v_lvl_in) >= 1) {
-               // >= 1dB difference
-               aexp->need_expose = true;
-               aexp->v_lvl_in = v_lvl_in;
-               const float relax_coef = exp(-(float)n_samples/srate);
-               aexp->v_lvl_out = fmaxf (v_lvl_out, relax_coef*aexp->v_lvl_out + (1.f-relax_coef)*v_lvl_out);
-       }
-       if (aexp->need_expose && aexp->queue_draw) {
-               aexp->need_expose = false;
-               aexp->queue_draw->queue_draw (aexp->queue_draw->handle);
-       }
+       aexp->v_peakdb = -160.f;
+       aexp->peakdb_samples = 0;
 #endif
 }
 
 
 static void
-run_stereo(LV2_Handle instance, uint32_t n_samples)
+run(LV2_Handle instance, uint32_t n_samples)
 {
        AExp* aexp = (AExp*)instance;
 
-       const float* const input0 = aexp->input0;
-       const float* const input1 = aexp->input1;
+       const float* const ins[2] = { aexp->input0, aexp->input1 };
        const float* const sc = aexp->sc;
-       float* const output0 = aexp->output0;
-       float* const output1 = aexp->output1;
+       float* const outs[2] = { aexp->output0, aexp->output1 };
 
        float srate = aexp->srate;
        float width = (6.f * *(aexp->knee)) + 0.01;
-       float cdb=0.f;
        float attack_coeff = exp(-1000.f/(*(aexp->attack) * srate));
        float release_coeff = exp(-1000.f/(*(aexp->release) * srate));
 
-       float max = 0.f;
-       float lgaininp = 0.f;
-       float rgaininp = 0.f;
+       float max_out = 0.f;
        float Lgain = 1.f;
-       float Lxg, Lxl, Lyg, Lyl, Ly1;
+       float Lxg, Lyg;
+       float current_gainr;
+       float old_gainr = *aexp->gainr;
+
        int usesidechain = (*(aexp->sidechain) <= 0.f) ? 0 : 1;
        uint32_t i;
        float ingain;
-       float in0;
-       float in1;
        float sc0;
-       float maxabslr;
+       float maxabs;
+
+       uint32_t n_channels = aexp->n_channels;
 
        float ratio = *aexp->ratio;
        float thresdb = *aexp->thresdb;
@@ -443,13 +322,22 @@ run_stereo(LV2_Handle instance, uint32_t n_samples)
        float makeup_target = from_dB(makeup);
        float makeup_gain = aexp->makeup_gain;
 
-       const float tau = aexp->tau;
+       const float tau = (1.0 - exp (-2.f * M_PI * 25.f / aexp->srate));
 
        if (*aexp->enable <= 0) {
                ratio = 1.f;
                thresdb = 0.f;
                makeup = 0.f;
                makeup_target = 1.f;
+               if (!aexp->was_disabled) {
+                       *aexp->gainr = 0.f;
+                       aexp->was_disabled = true;
+               }
+       } else {
+               if (aexp->was_disabled) {
+                       *aexp->gainr = 160.f;
+                       aexp->was_disabled = false;
+               }
        }
 
 #ifdef LV2_EXTENDED
@@ -474,20 +362,25 @@ run_stereo(LV2_Handle instance, uint32_t n_samples)
        }
 #endif
 
-       float in_peak = 0;
-       aexp->v_gainr = 0.0;
+       float in_peak_db = -160.f;
+       old_gainr = *aexp->gainr;
+       float max_gainr = 0.0;
 
        for (i = 0; i < n_samples; i++) {
-               in0 = input0[i];
-               in1 = input1[i];
+               maxabs = 0.f;
+               for (uint32_t c=0; c<n_channels; ++c) {
+                       maxabs = fmaxf(fabsf(ins[c][i]), maxabs);
+               }
                sc0 = sc[i];
-               maxabslr = fmaxf(fabs(in0), fabs(in1));
-               ingain = usesidechain ? fabs(sc0) : maxabslr;
-               in_peak = fmaxf (in_peak, ingain);
+               ingain = usesidechain ? fabs(sc0) : maxabs;
                Lyg = 0.f;
                Lxg = (ingain==0.f) ? -160.f : to_dB(ingain);
                Lxg = sanitize_denormal(Lxg);
 
+               if (Lxg > in_peak_db) {
+                       in_peak_db = Lxg;
+               }
+
                if (2.f*(Lxg-thresdb) < -width) {
                        Lyg = thresdb + (Lxg-thresdb) * ratio;
                        Lyg = sanitize_denormal(Lyg);
@@ -497,61 +390,74 @@ run_stereo(LV2_Handle instance, uint32_t n_samples)
                        Lyg = Lxg + (1.f-ratio)*(Lxg-thresdb-width/2.f)*(Lxg-thresdb-width/2.f)/(2.f*width);
                }
 
-               Lxl = Lxg - Lyg;
+               current_gainr = Lxg - Lyg;
 
-               aexp->old_y1 = sanitize_denormal(aexp->old_y1);
-               aexp->old_yl = sanitize_denormal(aexp->old_yl);
-               Ly1 = fminf(Lxl, release_coeff * aexp->old_y1+(1.f-release_coeff)*Lxl);
-               Lyl = attack_coeff * aexp->old_yl+(1.f-attack_coeff)*Ly1;
-               Ly1 = sanitize_denormal(Ly1);
-               Lyl = sanitize_denormal(Lyl);
-
-               cdb = -Lyl;
-               Lgain = from_dB(cdb);
+               if (current_gainr > 160.f) {
+                       current_gainr = 160.f;
+               }
 
-               *(aexp->gainr) = Lyl;
-               if (Lyl > aexp->v_gainr) {
-                       aexp->v_gainr = Lyl;
+               if (current_gainr > old_gainr) {
+                       current_gainr = release_coeff*old_gainr + (1.f-release_coeff)*current_gainr;
+               } else if (current_gainr < old_gainr) {
+                       current_gainr = attack_coeff*old_gainr + (1.f-attack_coeff)*current_gainr;
                }
 
-               lgaininp = in0 * Lgain;
-               rgaininp = in1 * Lgain;
+               current_gainr = sanitize_denormal(current_gainr);
+
+               Lgain = from_dB(-current_gainr);
 
-               makeup_gain += tau * (makeup_target - makeup_gain) + 1e-12;
+               old_gainr = current_gainr;
+
+               *(aexp->gainr) = current_gainr;
+               if (current_gainr > max_gainr) {
+                       max_gainr = current_gainr;
+               }
 
-               output0[i] = lgaininp * makeup_gain;
-               output1[i] = rgaininp * makeup_gain;
+               makeup_gain += tau * (makeup_target - makeup_gain);
 
-               max = (fmaxf(fabs(output0[i]), fabs(output1[i])) > max) ? fmaxf(fabs(output0[i]), fabs(output1[i])) : sanitize_denormal(max);
+               for (uint32_t c=0; c<n_channels; ++c) {
+                       float out = ins[c][i] * Lgain * makeup_gain;
+                       outs[c][i] = out;
+                       out = fabsf (out);
+                       if (out > max_out) {
+                               max_out = out;
+                               sanitize_denormal(max_out);
+                       }
+               }
+       }
 
-               // TODO re-use local variables on stack
-               // store values back to aexp at the end of the inner-loop
-               aexp->old_yl = Lyl;
-               aexp->old_y1 = Ly1;
-               aexp->old_yg = Lyg;
+       if (fabsf(tau * (makeup_gain - makeup_target)) < FLT_EPSILON*makeup_gain) {
+               makeup_gain = makeup_target;
        }
 
-       *(aexp->outlevel) = (max < 0.0056f) ? -45.f : to_dB(max);
+       *(aexp->outlevel) = (max_out < 0.0056f) ? -45.f : to_dB(max_out);
+       *(aexp->inlevel) = in_peak_db;
        aexp->makeup_gain = makeup_gain;
 
 #ifdef LV2_EXTENDED
-       const float old_v_lv1 = aexp->v_lv1;
-       const float old_v_lvl = aexp->v_lvl;
-       const float tot_rel_c = exp(-1000.f/(*(aexp->release) * srate) * n_samples);
-       const float tot_atk_c = exp(-1000.f/(*(aexp->attack) * srate) * n_samples);
-       aexp->v_lv1 = fmaxf (in_peak, tot_rel_c*old_v_lv1 + (1.f-tot_rel_c)*in_peak);
-       aexp->v_lvl = tot_atk_c*old_v_lvl + (1.f-tot_atk_c)*aexp->v_lv1;
-       if (!isfinite_local (aexp->v_lvl)) {
-               aexp->v_lvl = 0.f;
+       if (in_peak_db > aexp->v_peakdb) {
+               aexp->v_peakdb = in_peak_db;
+               aexp->peakdb_samples = 0;
+       } else {
+               aexp->peakdb_samples += n_samples;
+               if ((float)aexp->peakdb_samples/aexp->srate > RESET_PEAK_AFTER_SECONDS) {
+                       aexp->v_peakdb = in_peak_db;
+                       aexp->peakdb_samples = 0;
+                       aexp->need_expose = true;
+               }
        }
-       const float v_lvl_in = (aexp->v_lvl < 0.001f) ? -60.f : to_dB(aexp->v_lvl);
-       const float v_lvl_out = (max < 0.001f) ? -60.f : to_dB(max);
-       if (fabsf (aexp->v_lvl_out - v_lvl_out) >= 1 || fabsf (aexp->v_lvl_in - v_lvl_in) >= 1) {
-               // >= 1dB difference
+
+       const float v_lvl_out = (max_out < 0.001f) ? -1600.f : to_dB(max_out);
+       const float v_lvl_in = in_peak_db;
+
+       if (fabsf (aexp->v_lvl_out - v_lvl_out) >= .1 ||
+           fabsf (aexp->v_lvl_in - v_lvl_in) >= .1 ||
+           fabsf (aexp->v_gainr - max_gainr) >= .1) {
+               // >= 0.1dB difference
                aexp->need_expose = true;
                aexp->v_lvl_in = v_lvl_in;
-               const float relax_coef = exp(-2.0*n_samples/srate);
-               aexp->v_lvl_out = fmaxf (v_lvl_out, relax_coef*aexp->v_lvl_out + (1.f-relax_coef)*v_lvl_out);
+               aexp->v_lvl_out = v_lvl_out;
+               aexp->v_gainr = max_gainr;
        }
        if (aexp->need_expose && aexp->queue_draw) {
                aexp->need_expose = false;
@@ -609,6 +515,7 @@ exp_curve (const AExp* self, float xg) {
        return yg;
 }
 
+#include "dynamic_display.c"
 
 static void
 render_inline_full (cairo_t* cr, const AExp* self)
@@ -618,76 +525,26 @@ render_inline_full (cairo_t* cr, const AExp* self)
 
        const float makeup_thres = self->v_thresdb + self->v_makeup;
 
-       // clear background
-       cairo_rectangle (cr, 0, 0, w, h);
-       cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
-       cairo_fill (cr);
-
-       cairo_set_line_width(cr, 1.0);
+       draw_grid (cr, w,h);
 
-       // draw grid 10dB steps
-       const double dash1[] = {1, 2};
-       const double dash2[] = {1, 3};
-       cairo_save (cr);
-       cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
-       cairo_set_dash(cr, dash2, 2, 2);
-       cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
-
-       for (uint32_t d = 1; d < 7; ++d) {
-               const float x = -.5 + floorf (w * (d * 10.f / 70.f));
-               const float y = -.5 + floorf (h * (d * 10.f / 70.f));
-
-               cairo_move_to (cr, x, 0);
-               cairo_line_to (cr, x, h);
-               cairo_stroke (cr);
-
-               cairo_move_to (cr, 0, y);
-               cairo_line_to (cr, w, y);
-               cairo_stroke (cr);
-       }
-       cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
-       cairo_set_dash(cr, dash1, 2, 2);
        if (self->v_thresdb < 0) {
                const float y = -.5 + floorf (h * ((makeup_thres - 10.f) / -70.f));
                cairo_move_to (cr, 0, y);
                cairo_line_to (cr, w, y);
                cairo_stroke (cr);
        }
-       // diagonal unity
-       cairo_move_to (cr, 0, h);
-       cairo_line_to (cr, w, 0);
-       cairo_stroke (cr);
-       cairo_restore (cr);
 
-       { // 0, 0
-               cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
-               const float x = -.5 + floorf (w * (60.f / 70.f));
-               const float y = -.5 + floorf (h * (10.f / 70.f));
-               cairo_move_to (cr, x, 0);
-               cairo_line_to (cr, x, h);
-               cairo_stroke (cr);
-               cairo_move_to (cr, 0, y);
-               cairo_line_to (cr, w, y);
-               cairo_stroke (cr);
-       }
+       draw_GR_bar (cr, w,h, self->v_gainr);
 
-       { // GR
-               const float x = -.5 + floorf (w * (62.5f / 70.f));
-               const float y = -.5 + floorf (h * (10.0f / 70.f));
-               const float wd = floorf (w * (5.f / 70.f));
-               const float ht = floorf (h * (55.f / 70.f));
-               cairo_rectangle (cr, x, y, wd, ht);
-               cairo_fill (cr);
-
-               const float h_gr = fminf (ht, floorf (h * self->v_gainr / 70.f));
-               cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
-               cairo_rectangle (cr, x, y, wd, h_gr);
-               cairo_fill (cr);
-               cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
-               cairo_rectangle (cr, x, y, wd, ht);
-               cairo_set_source_rgba (cr, 0.75, 0.75, 0.75, 1.0);
-               cairo_stroke (cr);
-       }
+       // draw peak input
+       cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
+       cairo_set_line_width(cr, 1.0);
+
+       const float peak_x = w * (1.f - (10.f-self->v_peakdb)/70.f);
+       const float peak_y = fminf (h * (exp_curve (self, self->v_peakdb) - 10.f) / -70.f, h);
+
+       cairo_arc (cr, peak_x, peak_y, 3.f, 0.f, 2.f*M_PI);
+       cairo_fill (cr);
 
        // draw curve
        cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
@@ -739,101 +596,13 @@ render_inline_full (cairo_t* cr, const AExp* self)
 static void
 render_inline_only_bars (cairo_t* cr, const AExp* self)
 {
-       const float w = self->w;
-       const float h = self->h;
-
-       cairo_rectangle (cr, 0, 0, w, h);
-       cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
-       cairo_fill (cr);
-
-
-       cairo_save (cr);
-
-       const float ht = 0.25f * h;
-
-       const float x1 = w*0.05;
-       const float wd = w - 2.0f*x1;
-
-       const float y1 = 0.17*h;
-       const float y2 = h - y1 - ht;
-
-       cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
-
-       cairo_rectangle (cr, x1, y1, wd, ht);
-       cairo_fill (cr);
-
-       cairo_rectangle (cr, x1, y2, wd, ht);
-       cairo_fill (cr);
-
-       cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
-       const float w_gr = (self->v_gainr > 60.f) ? wd : wd * self->v_gainr * (1.f/60.f);
-       cairo_rectangle (cr, x1+wd-w_gr, y2, w_gr, ht);
-       cairo_fill (cr);
-
-       if (self->v_lvl_out > -60.f) {
-               if (self->v_lvl_out > 10.f) {
-                       cairo_set_source_rgba (cr, 0.75, 0.0, 0.0, 1.0);
-               } else if (self->v_lvl_out > 0.f) {
-                       cairo_set_source_rgba (cr, 0.66, 0.66, 0.0, 1.0);
-               } else {
-                       cairo_set_source_rgba (cr, 0.0, 0.66, 0.0, 1.0);
-               }
-               const float w_g = (self->v_lvl_out > 10.f) ? wd : wd * (60.f+self->v_lvl_out) / 70.f;
-               cairo_rectangle (cr, x1, y1, w_g, ht);
-               cairo_fill (cr);
-       }
-
-       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
-
-       const float tck = 0.33*ht;
-
-       cairo_set_line_width (cr, .5);
-
-       for (uint32_t d = 1; d < 7; ++d) {
-               const float x = x1 + (d * wd * (10.f / 70.f));
-
-               cairo_move_to (cr, x, y1);
-               cairo_line_to (cr, x, y1+tck);
-
-               cairo_move_to (cr, x, y1+ht);
-               cairo_line_to (cr, x, y1+ht-tck);
-
-               cairo_move_to (cr, x, y2);
-               cairo_line_to (cr, x, y2+tck);
-
-               cairo_move_to (cr, x, y2+ht);
-               cairo_line_to (cr, x, y2+ht-tck);
-       }
-
-       cairo_stroke (cr);
-
-       const float x_0dB = x1 + wd*(60.f/70.f);
-
-       cairo_move_to (cr, x_0dB, y1);
-       cairo_line_to (cr, x_0dB, y1+ht);
-
-       cairo_rectangle (cr, x1, y1, wd, ht);
-       cairo_rectangle (cr, x1, y2, wd, ht);
-       cairo_stroke (cr);
-
-       cairo_set_line_width (cr, 2.0);
-
-       // visualize threshold
-       const float tr = x1 + wd * (60.f+self->v_thresdb) / 70.f;
-       cairo_set_source_rgba (cr, 0.95, 0.95, 0.0, 1.0);
-       cairo_move_to (cr, tr, y1);
-       cairo_line_to (cr, tr, y1+ht);
-       cairo_stroke (cr);
-
-       // visualize ratio
-       const float reduced_0dB = self->v_thresdb * (1.f - 1.f/self->v_ratio);
-       const float rt = x1 + wd * (60.f+reduced_0dB) / 70.f;
-       cairo_set_source_rgba (cr, 0.95, 0.0, 0.0, 1.0);
-       cairo_move_to (cr, rt, y1);
-       cairo_line_to (cr, rt, y1+ht);
-       cairo_stroke (cr);
+       draw_inline_bars (cr, self->w, self->h,
+                         self->v_thresdb, self->v_ratio,
+                         self->v_peakdb, self->v_gainr,
+                         self->v_lvl_in, self->v_lvl_out);
 }
 
+
 static LV2_Inline_Display_Image_Surface *
 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
 {
@@ -888,7 +657,7 @@ static const LV2_Descriptor descriptor_mono = {
        instantiate,
        connect_mono,
        activate,
-       run_mono,
+       run,
        deactivate,
        cleanup,
        extension_data
@@ -899,7 +668,7 @@ static const LV2_Descriptor descriptor_stereo = {
        instantiate,
        connect_stereo,
        activate,
-       run_stereo,
+       run,
        deactivate,
        cleanup,
        extension_data