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]);
398 if (*aeq->filtog[i] <= 0 || *aeq->enable <= 0) {
399 if (!is_eq(aeq->v_g[i], 0.f, 0.05)) {
400 aeq->v_g[i] += tau * (0.0 - aeq->v_g[i]);
404 if (!is_eq(aeq->v_g[i], *aeq->g[i], 0.05)) {
405 aeq->v_g[i] += tau * (*aeq->g[i] - aeq->v_g[i]);
410 if (i != 0 && i != 5) {
411 if (!is_eq(aeq->v_bw[i], *aeq->bw[i], 0.001)) {
412 aeq->v_bw[i] += tau * (*aeq->bw[i] - aeq->v_bw[i]);
424 aeq->need_expose = true;
425 block = MIN (64, n_samples);
428 for (uint32_t i = 0; i < block; ++i) {
430 in0 = input[i + offset];
432 for (uint32_t j = 0; j < BANDS; j++) {
433 out = run_linear_svf(&aeq->v_filter[j], out);
435 output[i + offset] = out * from_dB(aeq->v_master);
441 for (uint32_t j = 0; j < BANDS; j++) {
442 linear_svf_protect(&aeq->v_filter[j]);
446 if (aeq->need_expose && aeq->queue_draw) {
447 aeq->need_expose = false;
448 aeq->queue_draw->queue_draw (aeq->queue_draw->handle);
454 calc_peq(Aeq* self, int i, double omega) {
455 double complex H = 0.0;
456 double complex z = cexp(I * omega);
457 double complex zz = cexp(2. * I * omega);
458 double complex zm = z - 1.0;
459 double complex zp = z + 1.0;
460 double complex zzm = zz - 1.0;
462 double A = pow(10.0, self->v_g[i]/40.0);
463 double g = self->v_filter[i].g;
464 double k = self->v_filter[i].k * A;
465 double m1 = k * (A * A - 1.0) / A;
467 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));
472 calc_lowshelf(Aeq* self, double omega) {
473 double complex H = 0.0;
474 double complex z = cexp(I * omega);
475 double complex zz = cexp(2. * I * omega);
476 double complex zm = z - 1.0;
477 double complex zp = z + 1.0;
478 double complex zzm = zz - 1.0;
480 double A = pow(10.0, self->v_g[0]/40.0);
481 double g = self->v_filter[0].g;
482 double k = self->v_filter[0].k;
483 double m0 = self->v_filter[0].m[0];
484 double m1 = self->v_filter[0].m[1];
485 double m2 = self->v_filter[0].m[2];
487 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);
492 calc_highshelf(Aeq* self, double omega) {
493 double complex H = 0.0;
494 double complex z = cexp(I * omega);
495 double complex zz = cexp(2. * I * omega);
496 double complex zm = z - 1.0;
497 double complex zp = z + 1.0;
498 double complex zzm = zz - 1.0;
500 double A = pow(10.0, self->v_g[5]/40.0);
501 double g = self->v_filter[5].g;
502 double k = self->v_filter[5].k;
503 double m0 = self->v_filter[5].m[0];
504 double m1 = self->v_filter[5].m[1];
505 double m2 = self->v_filter[5].m[2];
507 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);
513 eq_curve (Aeq* self, float f) {
514 double response = 1.0;
515 double SR = (double)self->srate;
516 double omega = f * 2. * M_PI / SR;
519 response *= calc_lowshelf(self, omega);
522 response *= calc_peq(self, 1, omega);
523 response *= calc_peq(self, 2, omega);
524 response *= calc_peq(self, 3, omega);
525 response *= calc_peq(self, 4, omega);
528 response *= calc_highshelf(self, omega);
530 return (float)response;
533 static LV2_Inline_Display_Image_Surface *
534 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
536 Aeq* self = (Aeq*)instance;
537 uint32_t h = MIN (1 | (uint32_t)ceilf (w * 9.f / 16.f), max_h);
539 if (!self->display || self->w != w || self->h != h) {
540 if (self->display) cairo_surface_destroy(self->display);
541 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
546 cairo_t* cr = cairo_create (self->display);
549 cairo_rectangle (cr, 0, 0, w, h);
550 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
553 cairo_set_line_width(cr, 1.0);
555 // prepare grid drawing
557 const double dash2[] = {1, 3};
558 //cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
559 cairo_set_dash(cr, dash2, 2, 2);
560 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
562 // draw x-grid 6dB steps
563 for (int32_t d = -18; d <= 18; d+=6) {
564 float y = (float)h * (d / 40.0 + 0.5);
566 cairo_move_to (cr, 0, y);
567 cairo_line_to (cr, w, y);
570 // draw y-axis grid 100, 1k, 10K
571 for (int32_t f = 100; f <= 10000; f *= 10) {
572 float x = w * log10 (f / 20.0) / log10 (1000.0);
574 cairo_move_to (cr, x, 0);
575 cairo_line_to (cr, x, h);
583 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
584 cairo_move_to (cr, 0, h);
586 for (uint32_t x = 0; x < w; ++x) {
587 // plot 20..20kHz +-20dB
588 const float x_hz = 20.f * powf (1000.f, (float)x / (float)w);
589 const float y_db = to_dB(eq_curve(self, x_hz)) + self->v_master;
590 const float y = (float)h * (-y_db / 40.0 + 0.5);
591 cairo_line_to (cr, x, y);
593 cairo_stroke_preserve (cr);
595 cairo_line_to (cr, w, h);
596 cairo_close_path (cr);
599 // create RGBA surface
601 cairo_surface_flush (self->display);
602 self->surf.width = cairo_image_surface_get_width (self->display);
603 self->surf.height = cairo_image_surface_get_height (self->display);
604 self->surf.stride = cairo_image_surface_get_stride (self->display);
605 self->surf.data = cairo_image_surface_get_data (self->display);
612 extension_data(const char* uri)
615 static const LV2_Inline_Display_Interface display = { render_inline };
616 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
624 cleanup(LV2_Handle instance)
627 Aeq* aeq = (Aeq*)instance;
629 cairo_surface_destroy (aeq->display);
635 static const LV2_Descriptor descriptor = {
647 const LV2_Descriptor*
648 lv2_descriptor(uint32_t index)