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
26 #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
29 #include <cairo/cairo.h>
30 #include "ardour/lv2_extensions.h"
33 #define AEQ_URI "urn:ardour:a-eq"
37 #define MIN(A,B) ((A) < (B)) ? (A) : (B)
72 return (20.f*log10(g));
77 return (exp(gdb/20.f*log(10.f)));
87 static void linear_svf_reset(struct linear_svf *self)
89 self->s[0] = self->s[1] = 0.0;
106 struct linear_svf v_filter[BANDS];
110 float v_filtog[BANDS];
118 LV2_Inline_Display_Image_Surface surf;
119 cairo_surface_t* display;
120 LV2_Inline_Display* queue_draw;
126 instantiate(const LV2_Descriptor* descriptor,
128 const char* bundle_path,
129 const LV2_Feature* const* features)
131 Aeq* aeq = (Aeq*)calloc(1, sizeof(Aeq));
135 for (int i=0; features[i]; ++i) {
136 if (!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw)) {
137 aeq->queue_draw = (LV2_Inline_Display*) features[i]->data;
142 for (int i = 0; i < BANDS; i++)
143 linear_svf_reset(&aeq->v_filter[i]);
145 // TODO initialize self->v_
147 aeq->need_expose = true;
152 return (LV2_Handle)aeq;
156 connect_port(LV2_Handle instance,
160 Aeq* aeq = (Aeq*)instance;
162 switch ((PortIndex)port) {
164 aeq->shelftogl = (float*)data;
167 aeq->f0[0] = (float*)data;
170 aeq->g[0] = (float*)data;
173 aeq->f0[1] = (float*)data;
176 aeq->g[1] = (float*)data;
179 aeq->bw[1] = (float*)data;
182 aeq->f0[2] = (float*)data;
185 aeq->g[2] = (float*)data;
188 aeq->bw[2] = (float*)data;
191 aeq->f0[3] = (float*)data;
194 aeq->g[3] = (float*)data;
197 aeq->bw[3] = (float*)data;
200 aeq->f0[4] = (float*)data;
203 aeq->g[4] = (float*)data;
206 aeq->bw[4] = (float*)data;
209 aeq->shelftogh = (float*)data;
212 aeq->f0[5] = (float*)data;
215 aeq->g[5] = (float*)data;
218 aeq->master = (float*)data;
221 aeq->filtog[0] = (float*)data;
224 aeq->filtog[1] = (float*)data;
227 aeq->filtog[2] = (float*)data;
230 aeq->filtog[3] = (float*)data;
233 aeq->filtog[4] = (float*)data;
236 aeq->filtog[5] = (float*)data;
239 aeq->input = (float*)data;
242 aeq->output = (float*)data;
248 activate(LV2_Handle instance)
251 Aeq* aeq = (Aeq*)instance;
253 for (i = 0; i < BANDS; i++)
254 linear_svf_reset(&aeq->v_filter[i]);
258 // http://www.cytomic.com/files/dsp/SvfLinearTrapOptimised2.pdf
260 static void linear_svf_set_hp(struct linear_svf *self, float sample_rate, float cutoff, float resonance)
262 double f0 = (double)cutoff;
263 double q = (double)resonance;
264 double sr = (double)sample_rate;
266 self->g = tan(M_PI * (f0 / sr));
269 self->a[0] = 1.0 / (1.0 + self->g * (self->g + self->k));
270 self->a[1] = self->g * self->a[0];
271 self->a[2] = self->g * self->a[1];
274 self->m[1] = -self->k;
278 static void linear_svf_set_lp(struct linear_svf *self, float sample_rate, float cutoff, float resonance)
280 double f0 = (double)cutoff;
281 double q = (double)resonance;
282 double sr = (double)sample_rate;
284 self->g = tan(M_PI * (f0 / sr));
287 self->a[0] = 1.0 / (1.0 + self->g * (self->g + self->k));
288 self->a[1] = self->g * self->a[0];
289 self->a[2] = self->g * self->a[1];
296 static void linear_svf_set_peq(struct linear_svf *self, float gdb, float sample_rate, float cutoff, float bandwidth)
298 double f0 = (double)cutoff;
299 double q = (double)pow(2.0, 1.0 / bandwidth) / (pow(2.0, bandwidth) - 1.0);
300 double sr = (double)sample_rate;
301 double A = pow(10.0, gdb/40.0);
303 self->g = tan(M_PI * (f0 / sr));
304 self->k = 1.0 / (q * A);
306 self->a[0] = 1.0 / (1.0 + self->g * (self->g + self->k));
307 self->a[1] = self->g * self->a[0];
308 self->a[2] = self->g * self->a[1];
311 self->m[1] = self->k * (A * A - 1.0);
315 static void linear_svf_set_highshelf(struct linear_svf *self, float gdb, float sample_rate, float cutoff, float resonance)
317 double f0 = (double)cutoff;
318 double q = (double)resonance;
319 double sr = (double)sample_rate;
320 double A = pow(10.0, gdb/40.0);
322 self->g = tan(M_PI * (f0 / sr));
325 self->a[0] = 1.0 / (1.0 + self->g * (self->g + self->k));
326 self->a[1] = self->g * self->a[0];
327 self->a[2] = self->g * self->a[1];
330 self->m[1] = self->k * (1.0 - A) * A;
331 self->m[2] = 1.0 - A * A;
334 static void linear_svf_set_lowshelf(struct linear_svf *self, float gdb, float sample_rate, float cutoff, float resonance)
336 double f0 = (double)cutoff;
337 double q = (double)resonance;
338 double sr = (double)sample_rate;
339 double A = pow(10.0, gdb/40.0);
341 self->g = tan(M_PI * (f0 / sr));
344 self->a[0] = 1.0 / (1.0 + self->g * (self->g + self->k));
345 self->a[1] = self->g * self->a[0];
346 self->a[2] = self->g * self->a[1];
349 self->m[1] = self->k * (A - 1.0);
350 self->m[2] = A * A - 1.0;
353 static float run_linear_svf(struct linear_svf *self, float in)
356 double din = (double)in;
359 v[2] = din - self->s[1];
360 v[0] = (self->a[0] * self->s[0]) + (self->a[1] * v[2]);
361 v[1] = self->s[1] + (self->a[1] * self->s[0]) + (self->a[2] * v[2]);
363 self->s[0] = (2.0 * v[0]) - self->s[0];
364 self->s[1] = (2.0 * v[1]) - self->s[1];
366 out = (self->m[0] * din)
367 + (self->m[1] * v[0])
368 + (self->m[2] * v[1]);
374 run(LV2_Handle instance, uint32_t n_samples)
376 Aeq* aeq = (Aeq*)instance;
378 const float* const input = aeq->input;
379 float* const output = aeq->output;
381 float srate = aeq->srate;
385 if (*(aeq->shelftogl) > 0.5) {
386 linear_svf_set_lowshelf(&aeq->v_filter[0], *(aeq->g[0]), srate, *(aeq->f0[0]), 0.7071068);
388 linear_svf_set_hp(&aeq->v_filter[0], srate, *(aeq->f0[0]), 0.7071068);
390 linear_svf_set_peq(&aeq->v_filter[1], *(aeq->g[1]), srate, *(aeq->f0[1]), *(aeq->bw[1]));
391 linear_svf_set_peq(&aeq->v_filter[2], *(aeq->g[2]), srate, *(aeq->f0[2]), *(aeq->bw[2]));
392 linear_svf_set_peq(&aeq->v_filter[3], *(aeq->g[3]), srate, *(aeq->f0[3]), *(aeq->bw[3]));
393 linear_svf_set_peq(&aeq->v_filter[4], *(aeq->g[4]), srate, *(aeq->f0[4]), *(aeq->bw[4]));
395 if (*(aeq->shelftogh) > 0.5) {
396 linear_svf_set_highshelf(&aeq->v_filter[5], *(aeq->g[5]), srate, *(aeq->f0[5]), 0.7071068);
398 linear_svf_set_lp(&aeq->v_filter[5], srate, *(aeq->f0[5]), 0.7071068);
401 for (i = 0; i < n_samples; i++) {
404 for (j = 0; j < BANDS; j++) {
405 if (*(aeq->filtog[j]) > 0.5)
406 out = run_linear_svf(&aeq->v_filter[j], out);
408 output[i] = out * from_dB(*(aeq->master));
411 for (i = 0; i < BANDS; i++) {
412 if (aeq->v_f0[i] != *(aeq->f0[i])) {
413 aeq->v_f0[i] = *(aeq->f0[i]);
414 aeq->need_expose = true;
416 if (aeq->v_g[i] != *(aeq->g[i])) {
417 aeq->v_g[i] = *(aeq->g[i]);
418 aeq->need_expose = true;
420 if (i != 0 && i != 5 && aeq->v_bw[i] != *(aeq->bw[i])) {
421 aeq->v_bw[i] = *(aeq->bw[i]);
422 aeq->need_expose = true;
424 if (aeq->v_filtog[i] != *(aeq->filtog[i])) {
425 aeq->v_filtog[i] = *(aeq->filtog[i]);
426 aeq->need_expose = true;
428 if (aeq->v_shelftogl != *(aeq->shelftogl)) {
429 aeq->v_shelftogl = *(aeq->shelftogl);
430 aeq->need_expose = true;
432 if (aeq->v_shelftogh != *(aeq->shelftogh)) {
433 aeq->v_shelftogh = *(aeq->shelftogh);
434 aeq->need_expose = true;
436 if (aeq->v_master != *(aeq->master)) {
437 aeq->v_master = *(aeq->master);
438 aeq->need_expose = true;
443 if (aeq->need_expose && aeq->queue_draw) {
444 aeq->need_expose = false;
445 aeq->queue_draw->queue_draw (aeq->queue_draw->handle);
451 calc_peq(Aeq* self, int i, double omega) {
452 double complex H = 0.0;
453 double complex z = cexp(I * omega);
454 double complex zz = cexp(2. * I * omega);
455 double complex zm = z - 1.0;
456 double complex zp = z + 1.0;
457 double complex zzm = zz - 1.0;
459 double A = pow(10.0, self->v_g[i]/40.0);
460 double g = self->v_filter[i].g;
461 double k = self->v_filter[i].k;
462 double m1 = k * (A * A - 1.0) / A;
464 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));
469 calc_lowpass(Aeq* self, double omega) {
470 double complex H = 0.0;
471 double complex z = cexp(I * omega);
472 double complex zz = cexp(2. * I * omega);
473 double complex zm = z - 1.0;
474 double complex zp = z + 1.0;
475 double complex zzm = zz - 1.0;
477 double g = self->v_filter[5].g;
478 double k = self->v_filter[5].k;
480 H = (g*g*zp*zp) / (zm*zm + g*g*zp*zp + g*k*zzm);
485 calc_highpass(Aeq* self, double omega) {
486 double complex H = 0.0;
487 double complex z = cexp(I * omega);
488 double complex zz = cexp(2. * I * omega);
489 double complex zm = z - 1.0;
490 double complex zp = z + 1.0;
491 double complex zzm = zz - 1.0;
493 double g = self->v_filter[0].g;
494 double k = self->v_filter[0].k;
496 H = zm*zm / (zm*zm + g*g*zp*zp + g*k*zzm);
501 calc_lowshelf(Aeq* self, double omega) {
502 double complex H = 0.0;
503 double complex z = cexp(I * omega);
504 double complex zz = cexp(2. * I * omega);
505 double complex zm = z - 1.0;
506 double complex zp = z + 1.0;
507 double complex zzm = zz - 1.0;
509 double A = pow(10.0, self->v_g[0]/40.0);
510 double g = self->v_filter[0].g;
511 double k = self->v_filter[0].k;
512 double m0 = self->v_filter[0].m[0];
513 double m1 = self->v_filter[0].m[1];
514 double m2 = self->v_filter[0].m[2];
516 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);
521 calc_highshelf(Aeq* self, double omega) {
522 double complex H = 0.0;
523 double complex z = cexp(I * omega);
524 double complex zz = cexp(2. * I * omega);
525 double complex zm = z - 1.0;
526 double complex zp = z + 1.0;
527 double complex zzm = zz - 1.0;
529 double A = pow(10.0, self->v_g[5]/40.0);
530 double g = self->v_filter[5].g;
531 double k = self->v_filter[5].k;
532 double m0 = self->v_filter[5].m[0];
533 double m1 = self->v_filter[5].m[1];
534 double m2 = self->v_filter[5].m[2];
536 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);
542 eq_curve (Aeq* self, float f) {
543 double complex response = 1.0;
544 double SR = (double)self->srate;
545 double omega = f * 2. * M_PI / SR;
548 if (self->v_filtog[0]) {
549 if (self->v_shelftogl) {
551 response *= calc_lowshelf(self, omega);
554 response *= calc_highpass(self, omega);
559 if (self->v_filtog[1]) {
560 response *= calc_peq(self, 1, omega);
564 if (self->v_filtog[2]) {
565 response *= calc_peq(self, 2, omega);
569 if (self->v_filtog[3]) {
570 response *= calc_peq(self, 3, omega);
574 if (self->v_filtog[4]) {
575 response *= calc_peq(self, 4, omega);
579 if (self->v_filtog[5]) {
580 if (self->v_shelftogh) {
582 response *= calc_highshelf(self, omega);
585 response *= calc_lowpass(self, omega);
592 static LV2_Inline_Display_Image_Surface *
593 render_inline (LV2_Handle instance, uint32_t w, uint32_t max_h)
595 Aeq* self = (Aeq*)instance;
596 uint32_t h = MIN (w * 9 / 16, max_h);
598 if (!self->display || self->w != w || self->h != h) {
599 if (self->display) cairo_surface_destroy(self->display);
600 self->display = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h);
605 cairo_t* cr = cairo_create (self->display);
608 cairo_rectangle (cr, 0, 0, w, h);
609 cairo_set_source_rgba (cr, .2, .2, .2, 1.0);
612 cairo_set_line_width(cr, 1.0);
614 // draw grid 5dB steps
615 const double dash2[] = {1, 3};
617 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
618 cairo_set_dash(cr, dash2, 2, 2);
619 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.5);
621 for (uint32_t d = 1; d < 8; ++d) {
622 const float y = -.5 + floorf (h * (d * 5.f / 40.f));
623 cairo_move_to (cr, 0, y);
624 cairo_line_to (cr, w, y);
631 cairo_set_source_rgba (cr, .8, .8, .8, 1.0);
632 cairo_move_to (cr, 0, h);
634 for (uint32_t x = 0; x < w; ++x) {
635 // plot 20..20kHz +-20dB
636 const float x_hz = 20.f * powf (1000.f, (float)x / (float)w);
637 const float y_db = to_dB(eq_curve(self, x_hz)) + self->v_master;
638 const float y = h * -y_db / 40.0 + h / 2;
639 cairo_line_to (cr, x, y);
640 //printf("(hz,H,db)=(%f, %f, %f)\n", x_hz, from_dB(y_db), y_db);
642 cairo_stroke_preserve (cr);
644 cairo_line_to (cr, w, h);
645 cairo_close_path (cr);
648 // create RGBA surface
650 cairo_surface_flush (self->display);
651 self->surf.width = cairo_image_surface_get_width (self->display);
652 self->surf.height = cairo_image_surface_get_height (self->display);
653 self->surf.stride = cairo_image_surface_get_stride (self->display);
654 self->surf.data = cairo_image_surface_get_data (self->display);
661 extension_data(const char* uri)
664 static const LV2_Inline_Display_Interface display = { render_inline };
665 if (!strcmp(uri, LV2_INLINEDISPLAY__interface)) {
673 cleanup(LV2_Handle instance)
676 Aeq* aeq = (Aeq*)instance;
678 cairo_surface_destroy (aeq->display);
684 static const LV2_Descriptor descriptor = {
696 const LV2_Descriptor*
697 lv2_descriptor(uint32_t index)