a-EQ: Revert one of the previous changes
[ardour.git] / libs / plugins / a-eq.lv2 / a-eq.c
index a539c908258b7ef7c22340e6d5fe00dc8c4ebee4..b0851f2120f73bc3d47829414c19973f3f310ab4 100644 (file)
 #include <stdbool.h>
 #include <stdio.h>
 
+#ifdef COMPILER_MSVC
+#include <float.h>
+#define isfinite_local(val) (bool)_finite((double)val)
+#else
+#define isfinite_local isfinite
+#endif
+
 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
 
 #ifdef LV2_EXTENDED
@@ -32,8 +39,6 @@
 
 #define AEQ_URI        "urn:ardour:a-eq"
 #define BANDS  6
-#define SMALL  0.0001f
-
 #ifndef MIN
 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
 #endif
@@ -62,6 +67,7 @@ typedef enum {
        AEQ_FILTOG3,
        AEQ_FILTOG4,
        AEQ_FILTOGH,
+       AEQ_ENABLE,
        AEQ_INPUT,
        AEQ_OUTPUT,
 } PortIndex;
@@ -77,8 +83,8 @@ from_dB(double gdb) {
 }
 
 static inline bool
-is_eq(float a, float b) {
-       return (fabsf(a - b) < SMALL);
+is_eq(float a, float b, float small) {
+       return (fabsf(a - b) < small);
 }
 
 struct linear_svf {
@@ -93,14 +99,23 @@ static void linear_svf_reset(struct linear_svf *self)
        self->s[0] = self->s[1] = 0.0;
 }
 
+static void linear_svf_protect(struct linear_svf *self)
+{
+       if (!isfinite_local (self->s[0]) || !isfinite_local (self->s[1])) {
+               linear_svf_reset (self);
+       }
+}
+
 typedef struct {
        float* f0[BANDS];
        float* g[BANDS];
        float* bw[BANDS];
        float* filtog[BANDS];
        float* master;
+       float* enable;
 
        float srate;
+       float tau;
 
        float* input;
        float* output;
@@ -109,7 +124,6 @@ typedef struct {
        float v_g[BANDS];
        float v_bw[BANDS];
        float v_f0[BANDS];
-       float v_filtog[BANDS];
        float v_master;
 
        bool need_expose;
@@ -130,6 +144,7 @@ instantiate(const LV2_Descriptor* descriptor,
 {
        Aeq* aeq = (Aeq*)calloc(1, sizeof(Aeq));
        aeq->srate = rate;
+       aeq->tau = 1.0 - expf (-2.f * M_PI * 64.f * 25.f / aeq->srate); // 25Hz time constant @ 64fpp
 
 #ifdef LV2_EXTENDED
        for (int i=0; features[i]; ++i) {
@@ -158,6 +173,9 @@ connect_port(LV2_Handle instance,
        Aeq* aeq = (Aeq*)instance;
 
        switch ((PortIndex)port) {
+       case AEQ_ENABLE:
+               aeq->enable = (float*)data;
+               break;
        case AEQ_FREQL:
                aeq->f0[0] = (float*)data;
                break;
@@ -252,7 +270,7 @@ activate(LV2_Handle instance)
 static void linear_svf_set_peq(struct linear_svf *self, float gdb, float sample_rate, float cutoff, float bandwidth)
 {
        double f0 = (double)cutoff;
-       double q = (double)pow(2.0, 1.0 / bandwidth) / (pow(2.0, bandwidth) - 1.0);
+       double q = (double)pow(2.0, 0.5 * bandwidth) / (pow(2.0, bandwidth) - 1.0);
        double sr = (double)sample_rate;
        double A = pow(10.0, gdb/40.0);
 
@@ -353,51 +371,75 @@ run(LV2_Handle instance, uint32_t n_samples)
        const float* const input = aeq->input;
        float* const output = aeq->output;
 
-       float in0, out;
-       uint32_t i, j;
+       const float tau = aeq->tau;
+       uint32_t offset = 0;
 
-       // 15Hz time constant
-       const float tau = (1.0 - exp(-2.0 * M_PI * n_samples * 15. / aeq->srate));
+       const float target_gain = *aeq->enable <= 0 ? 0 : *aeq->master; // dB
 
-       for (i = 0; i < n_samples; i++) {
-               in0 = input[i];
-               out = in0;
-               for (j = 0; j < BANDS; j++) {
-                       out = run_linear_svf(&aeq->v_filter[j], out);
-               }
-               output[i] = out * from_dB(*(aeq->master));
-       }
+       while (n_samples > 0) {
+               uint32_t block = n_samples;
+               bool any_changed = false;
 
-       for (i = 0; i < BANDS; i++) {
-               if (!is_eq(aeq->v_filtog[i], *aeq->filtog[i])) {
-                       aeq->v_filtog[i] = *(aeq->filtog[i]);
+               if (!is_eq(aeq->v_master, target_gain, 0.1)) {
+                       aeq->v_master += tau * (target_gain - aeq->v_master);
+                       any_changed = true;
+               } else {
+                       aeq->v_master = target_gain;
                }
-               if (!is_eq(aeq->v_f0[i], *aeq->f0[i])) {
-                       aeq->v_f0[i] += tau * (*aeq->f0[i] - aeq->v_f0[i]);
-                       aeq->need_expose = true;
-               }
-               if (aeq->v_filtog[i] < 0.5) {
-                       if (!is_eq(aeq->v_g[i], 0.f)) {
-                               aeq->v_g[i] += tau * (0.0 - aeq->v_g[i]);
-                               aeq->need_expose = true;
+
+               for (int i = 0; i < BANDS; ++i) {
+                       bool changed = false;
+
+                       if (!is_eq(aeq->v_f0[i], *aeq->f0[i], 0.1)) {
+                               aeq->v_f0[i] += tau * (*aeq->f0[i] - aeq->v_f0[i]);
+                               changed = true;
                        }
-               } else if (aeq->v_filtog[i] >= 0.5) {
-                       if (!is_eq(aeq->v_g[i], *aeq->g[i])) {
-                               aeq->v_g[i] += tau * (*aeq->g[i] - aeq->v_g[i]);
-                               aeq->need_expose = true;
+
+                       if (*aeq->filtog[i] <= 0 || *aeq->enable <= 0) {
+                               if (!is_eq(aeq->v_g[i], 0.f, 0.05)) {
+                                       aeq->v_g[i] += tau * (0.0 - aeq->v_g[i]);
+                                       changed = true;
+                               }
+                       } else {
+                               if (!is_eq(aeq->v_g[i], *aeq->g[i], 0.05)) {
+                                       aeq->v_g[i] += tau * (*aeq->g[i] - aeq->v_g[i]);
+                                       changed = true;
+                               }
+                       }
+
+                       if (i != 0 && i != 5) {
+                               if (!is_eq(aeq->v_bw[i], *aeq->bw[i], 0.001)) {
+                                       aeq->v_bw[i] += tau * (*aeq->bw[i] - aeq->v_bw[i]);
+                                       changed = true;
+                               }
+                       }
+
+                       if (changed) {
+                               set_params(aeq, i);
+                               any_changed = true;
                        }
                }
-               if (i != 0 && i != 5 && !is_eq(aeq->v_bw[i], *aeq->bw[i])) {
-                       aeq->v_bw[i] += tau * (*aeq->bw[i] - aeq->v_bw[i]);
-                       aeq->need_expose = true;
-               }
-               if (!is_eq(aeq->v_master, *aeq->master)) {
-                       aeq->v_master = *(aeq->master);
+
+               if (any_changed) {
                        aeq->need_expose = true;
+                       block = MIN (64, n_samples);
                }
-               if (aeq->need_expose == true) {
-                       set_params(aeq, i);
+
+               for (uint32_t i = 0; i < block; ++i) {
+                       float in0, out;
+                       in0 = input[i + offset];
+                       out = in0;
+                       for (uint32_t j = 0; j < BANDS; j++) {
+                               out = run_linear_svf(&aeq->v_filter[j], out);
+                       }
+                       output[i + offset] = out * from_dB(aeq->v_master);
                }
+               n_samples -= block;
+               offset += block;
+       }
+
+       for (uint32_t j = 0; j < BANDS; j++) {
+               linear_svf_protect(&aeq->v_filter[j]);
        }
 
 #ifdef LV2_EXTENDED