2 * Copyright (C) 2016 Damien Zammit <damien@zamaudio.com>
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.
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.
16 #define _GNU_SOURCE // needed for M_PI
28 #define isfinite_local(val) (bool)_finite((double)val)
30 #define isfinite_local isfinite
33 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
36 #include <cairo/cairo.h>
37 #include "ardour/lv2_extensions.h"
40 #define AEQ_URI "urn:ardour:a-eq"
43 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
77 return (20.0*log10(g));
82 return (exp(gdb/20.0*log(10.0)));
86 is_eq(float a, float b, float small) {
87 return (fabsf(a - b) < small);
97 static void linear_svf_reset(struct linear_svf *self)
99 self->s[0] = self->s[1] = 0.0;
102 static void linear_svf_protect(struct linear_svf *self)
104 if (!isfinite_local (self->s[0]) || !isfinite_local (self->s[1])) {
105 linear_svf_reset (self);
113 float* filtog[BANDS];
123 struct linear_svf v_filter[BANDS];
132 LV2_Inline_Display_Image_Surface surf;
133 cairo_surface_t* display;
134 LV2_Inline_Display* queue_draw;
140 instantiate(const LV2_Descriptor* descriptor,
142 const char* bundle_path,
143 const LV2_Feature* const* features)
145 Aeq* aeq = (Aeq*)calloc(1, sizeof(Aeq));
147 aeq->tau = 1.0 - expf (-2.f * M_PI * 64.f * 25.f / aeq->srate); // 25Hz time constant @ 64fpp
150 for (int i=0; features[i]; ++i) {
151 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
152 aeq->queue_draw = (LV2_Inline_Display*) features[i]->data;
157 for (int i = 0; i < BANDS; i++)
158 linear_svf_reset(&aeq->v_filter[i]);
160 aeq->need_expose = true;
165 return (LV2_Handle)aeq;
169 connect_port(LV2_Handle instance,
173 Aeq* aeq = (Aeq*)instance;
175 switch ((PortIndex)port) {
177 aeq->enable = (float*)data;
180 aeq->f0[0] = (float*)data;
183 aeq->g[0] = (float*)data;
186 aeq->f0[1] = (float*)data;
189 aeq->g[1] = (float*)data;
192 aeq->bw[1] = (float*)data;
195 aeq->f0[2] = (float*)data;
198 aeq->g[2] = (float*)data;
201 aeq->bw[2] = (float*)data;
204 aeq->f0[3] = (float*)data;
207 aeq->g[3] = (float*)data;
210 aeq->bw[3] = (float*)data;
213 aeq->f0[4] = (float*)data;
216 aeq->g[4] = (float*)data;
219 aeq->bw[4] = (float*)data;
222 aeq->f0[5] = (float*)data;
225 aeq->g[5] = (float*)data;
228 aeq->master = (float*)data;
231 aeq->filtog[0] = (float*)data;
234 aeq->filtog[1] = (float*)data;
237 aeq->filtog[2] = (float*)data;
240 aeq->filtog[3] = (float*)data;
243 aeq->filtog[4] = (float*)data;
246 aeq->filtog[5] = (float*)data;
249 aeq->input = (float*)data;
252 aeq->output = (float*)data;
258 activate(LV2_Handle instance)
261 Aeq* aeq = (Aeq*)instance;
263 for (i = 0; i < BANDS; i++)
264 linear_svf_reset(&aeq->v_filter[i]);
268 // http://www.cytomic.com/files/dsp/SvfLinearTrapOptimised2.pdf
270 static void linear_svf_set_peq(struct linear_svf *self, float gdb, float sample_rate, float cutoff, float bandwidth)
272 double f0 = (double)cutoff;
273 double q = (double)pow(2.0, 1.0 / bandwidth) / (pow(2.0, bandwidth) - 1.0);
274 double sr = (double)sample_rate;
275 double A = pow(10.0, gdb/40.0);
277 self->g = tan(M_PI * (f0 / sr));
278 self->k = 1.0 / (q * A);
280 self->a[0] = 1.0 / (1.0 + self->g * (self->g + self->k));
281 self->a[1] = self->g * self->a[0];
282 self->a[2] = self->g * self->a[1];
285 self->m[1] = self->k * (A * A - 1.0);
289 static void linear_svf_set_highshelf(struct linear_svf *self, float gdb, float sample_rate, float cutoff, float resonance)
291 double f0 = (double)cutoff;
292 double q = (double)resonance;
293 double sr = (double)sample_rate;
294 double A = pow(10.0, gdb/40.0);
296 self->g = tan(M_PI * (f0 / sr));
299 self->a[0] = 1.0 / (1.0 + self->g * (self->g + self->k));
300 self->a[1] = self->g * self->a[0];
301 self->a[2] = self->g * self->a[1];
304 self->m[1] = self->k * (1.0 - A) * A;
305 self->m[2] = 1.0 - A * A;
308 static void linear_svf_set_lowshelf(struct linear_svf *self, float gdb, float sample_rate, float cutoff, float resonance)
310 double f0 = (double)cutoff;
311 double q = (double)resonance;
312 double sr = (double)sample_rate;
313 double A = pow(10.0, gdb/40.0);
315 self->g = tan(M_PI * (f0 / sr));
318 self->a[0] = 1.0 / (1.0 + self->g * (self->g + self->k));
319 self->a[1] = self->g * self->a[0];
320 self->a[2] = self->g * self->a[1];
323 self->m[1] = self->k * (A - 1.0);
324 self->m[2] = A * A - 1.0;
327 static float run_linear_svf(struct linear_svf *self, float in)
330 double din = (double)in;
333 v[2] = din - self->s[1];
334 v[0] = (self->a[0] * self->s[0]) + (self->a[1] * v[2]);
335 v[1] = self->s[1] + (self->a[1] * self->s[0]) + (self->a[2] * v[2]);
337 self->s[0] = (2.0 * v[0]) - self->s[0];
338 self->s[1] = (2.0 * v[1]) - self->s[1];
340 out = (self->m[0] * din)
341 + (self->m[1] * v[0])
342 + (self->m[2] * v[1]);
347 static void set_params(LV2_Handle instance, int band) {
348 Aeq* aeq = (Aeq*)instance;
352 linear_svf_set_lowshelf(&aeq->v_filter[0], aeq->v_g[0], aeq->srate, aeq->v_f0[0], 0.7071068);
358 linear_svf_set_peq(&aeq->v_filter[band], aeq->v_g[band], aeq->srate, aeq->v_f0[band], aeq->v_bw[band]);
361 linear_svf_set_highshelf(&aeq->v_filter[5], aeq->v_g[5], aeq->srate, aeq->v_f0[5], 0.7071068);
367 run(LV2_Handle instance, uint32_t n_samples)
369 Aeq* aeq = (Aeq*)instance;
371 const float* const input = aeq->input;
372 float* const output = aeq->output;
374 const float tau = aeq->tau;
377 const float target_gain = *aeq->enable <= 0 ? 0 : *aeq->master; // dB
379 while (n_samples > 0) {
380 uint32_t block = n_samples;
381 bool any_changed = false;
383 if (!is_eq(aeq->v_master, target_gain, 0.1)) {
384 aeq->v_master += tau * (target_gain - aeq->v_master);
387 aeq->v_master = target_gain;
390 for (int i = 0; i < BANDS; ++i) {
391 bool changed = false;
393 if (!is_eq(aeq->v_f0[i], *aeq->f0[i], 0.1)) {
394 aeq->v_f0[i] += tau * (*aeq->f0[i] - aeq->v_f0[i]);
397 aeq->v_f0[i] = *aeq->f0[i];
400 if (*aeq->filtog[i] <= 0 || *aeq->enable <= 0) {
401 if (!is_eq(aeq->v_g[i], 0.f, 0.05)) {
402 aeq->v_g[i] += tau * (0.0 - aeq->v_g[i]);
408 if (!is_eq(aeq->v_g[i], *aeq->g[i], 0.05)) {
409 aeq->v_g[i] += tau * (*aeq->g[i] - aeq->v_g[i]);
412 aeq->v_g[i] = *aeq->g[i];
416 if (i != 0 && i != 5) {
417 if (!is_eq(aeq->v_bw[i], *aeq->bw[i], 0.001)) {
418 aeq->v_bw[i] += tau * (*aeq->bw[i] - aeq->v_bw[i]);
421 aeq->v_bw[i] = *aeq->bw[i];
432 aeq->need_expose = true;
433 block = MIN (64, n_samples);
436 for (uint32_t i = 0; i < block; ++i) {
438 in0 = input[i + offset];
440 for (uint32_t j = 0; j < BANDS; j++) {
441 out = run_linear_svf(&aeq->v_filter[j], out);
443 output[i + offset] = out * from_dB(aeq->v_master);
449 for (uint32_t j = 0; j < BANDS; j++) {
450 linear_svf_protect(&aeq->v_filter[j]);
454 if (aeq->need_expose && aeq->queue_draw) {
455 aeq->need_expose = false;
456 aeq->queue_draw->queue_draw (aeq->queue_draw->handle);
462 calc_peq(Aeq* self, int i, double omega) {
463 double complex H = 0.0;
464 double complex z = cexp(I * omega);
465 double complex zz = cexp(2. * I * omega);
466 double complex zm = z - 1.0;
467 double complex zp = z + 1.0;
468 double complex zzm = zz - 1.0;
470 double A = pow(10.0, self->v_g[i]/40.0);
471 double g = self->v_filter[i].g;
472 double k = self->v_filter[i].k * A;
473 double m1 = k * (A * A - 1.0) / A;
475 H = (g*k*zzm + A*(g*zp*(m1*zm) + (zm*zm + g*g*zp*zp))) / (g*k*zzm + A*(zm*zm + g*g*zp*zp));
480 calc_lowshelf(Aeq* self, double omega) {
481 double complex H = 0.0;
482 double complex z = cexp(I * omega);
483 double complex zz = cexp(2. * I * omega);
484 double complex zm = z - 1.0;
485 double complex zp = z + 1.0;
486 double complex zzm = zz - 1.0;
488 double A = pow(10.0, self->v_g[0]/40.0);
489 double g = self->v_filter[0].g;
490 double k = self->v_filter[0].k;
491 double m0 = self->v_filter[0].m[0];
492 double m1 = self->v_filter[0].m[1];
493 double m2 = self->v_filter[0].m[2];
495 H = (A*m0*zm*zm + g*g*(m0+m2)*zp*zp + sqrt(A)*g*(k*m0+m1) * zzm) / (A*zm*zm + g*g*zp*zp + sqrt(A)*g*k*zzm);
500 calc_highshelf(Aeq* self, double omega) {
501 double complex H = 0.0;
502 double complex z = cexp(I * omega);
503 double complex zz = cexp(2. * I * omega);
504 double complex zm = z - 1.0;
505 double complex zp = z + 1.0;
506 double complex zzm = zz - 1.0;
508 double A = pow(10.0, self->v_g[5]/40.0);
509 double g = self->v_filter[5].g;
510 double k = self->v_filter[5].k;
511 double m0 = self->v_filter[5].m[0];
512 double m1 = self->v_filter[5].m[1];
513 double m2 = self->v_filter[5].m[2];
515 H = ( sqrt(A) * g * zp * (m1 * zm + sqrt(A)*g*m2*zp) + m0 * ( zm*zm + A*g*g*zp*zp + sqrt(A)*g*k*zzm)) / (zm*zm + A*g*g*zp*zp + sqrt(A)*g*k*zzm);
521 eq_curve (Aeq* self, float f) {
522 double response = 1.0;
523 double SR = (double)self->srate;
524 double omega = f * 2. * M_PI / SR;
527 response *= calc_lowshelf(self, omega);
530 response *= calc_peq(self, 1, omega);
531 response *= calc_peq(self, 2, omega);
532 response *= calc_peq(self, 3, omega);
533 response *= calc_peq(self, 4, omega);
536 response *= calc_highshelf(self, omega);
538 return (float)response;
541 static LV2_Inline_Display_Image_Surface *
542 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
544 Aeq* self = (Aeq*)instance;
545 uint32_t h = MIN (1 | (uint32_t)ceilf (w * 9.f / 16.f), max_h);
547 if (!self->display || self->w != w || self->h != h) {
548 if (self->display) cairo_surface_destroy(self->display);
549 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
554 cairo_t* cr = cairo_create (self->display);
557 cairo_rectangle (cr, 0, 0, w, h);
558 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
561 cairo_set_line_width(cr, 1.0);
563 // prepare grid drawing
565 const double dash2[] = {1, 3};
566 //cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
567 cairo_set_dash(cr, dash2, 2, 2);
568 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
570 // draw x-grid 6dB steps
571 for (int32_t d = -18; d <= 18; d+=6) {
572 float y = (float)h * (d / 40.0 + 0.5);
574 cairo_move_to (cr, 0, y);
575 cairo_line_to (cr, w, y);
578 // draw y-axis grid 100, 1k, 10K
579 for (int32_t f = 100; f <= 10000; f *= 10) {
580 float x = w * log10 (f / 20.0) / log10 (1000.0);
582 cairo_move_to (cr, x, 0);
583 cairo_line_to (cr, x, h);
591 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
592 cairo_move_to (cr, 0, h);
594 for (uint32_t x = 0; x < w; ++x) {
595 // plot 20..20kHz +-20dB
596 const float x_hz = 20.f * powf (1000.f, (float)x / (float)w);
597 const float y_db = to_dB(eq_curve(self, x_hz)) + self->v_master;
598 const float y = (float)h * (-y_db / 40.0 + 0.5);
599 cairo_line_to (cr, x, y);
601 cairo_stroke_preserve (cr);
603 cairo_line_to (cr, w, h);
604 cairo_close_path (cr);
607 // create RGBA surface
609 cairo_surface_flush (self->display);
610 self->surf.width = cairo_image_surface_get_width (self->display);
611 self->surf.height = cairo_image_surface_get_height (self->display);
612 self->surf.stride = cairo_image_surface_get_stride (self->display);
613 self->surf.data = cairo_image_surface_get_data (self->display);
620 extension_data(const char* uri)
623 static const LV2_Inline_Display_Interface display = { render_inline };
624 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
632 cleanup(LV2_Handle instance)
635 Aeq* aeq = (Aeq*)instance;
637 cairo_surface_destroy (aeq->display);
643 static const LV2_Descriptor descriptor = {
655 const LV2_Descriptor*
656 lv2_descriptor(uint32_t index)