diff options
| author | Ludvig Gunne Lindström <ludviggunnelindstrom@gmail.com> | 2026-07-17 11:43:24 +0200 |
|---|---|---|
| committer | Ludvig Gunne Lindström <ludviggunnelindstrom@gmail.com> | 2026-11-08 14:15:04 +0100 |
| commit | 039847a29868d335c08fe0a587260dffc71ca5c4 (patch) | |
| tree | e9d775090fef45ca51db576779fb373040d50e6a /mod/biquad.c | |
| download | melisma-modules-039847a29868d335c08fe0a587260dffc71ca5c4.tar.gz | |
Diffstat (limited to 'mod/biquad.c')
| -rw-r--r-- | mod/biquad.c | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/mod/biquad.c b/mod/biquad.c new file mode 100644 index 0000000..6591d87 --- /dev/null +++ b/mod/biquad.c @@ -0,0 +1,202 @@ +/* + * melisma - extensible and scriptable audio engine + * Copyright (C) 2026 Ludvig Gunne Lindström + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <stdbool.h> +#include <math.h> +#include <lauxlib.h> +#include "melisma.h" +#include "mutil.h" + +#define LOWPASS_TYPENAME "biquad LPF" +#define LOWPASS_DEFAULT_CUTOFF 4000 +#define LOWPASS_DEFAULT_RESONANCE 0.5 + +struct biquad; + +typedef void (*recomp_fn)(struct biquad*); + +struct biquad +{ + ml_channel *out; + recomp_fn recomp; + double x_1,x_2,y_1,y_2; + double xc_0,xc_1,xc_2; + double yc_1,yc_2; + double Fs; + double f0; + double Q; +}; + +static inline void lowpass_recomp(struct biquad *bq) +{ + double w0,cosw0,sinw0,alpha,b0,b1,b2,a0,a1,a2; + w0 = 2*M_PI*bq->f0/bq->Fs; + cosw0 = cos(w0); + sinw0 = sin(w0); + alpha = sinw0/(2*bq->Q); + b0 = (1 - cosw0)/2; + b1 = 1 - cosw0; + b2 = (1 - cosw0)/2; + a0 = 1 + alpha; + a1 = -2*cosw0; + a2 = 1 - alpha; + bq->xc_0 = b0/a0; + bq->xc_1 = b1/a0; + bq->xc_2 = b2/a0; + bq->yc_1 = a1/a0; + bq->yc_2 = a2/a0; +} + +static void proc(const ml_proc_args *args) +{ + struct biquad bq = *(struct biquad*) args->userp; + + float *x = args->in[0]; + float *y = args->out[0]; + + for (size_t i = 0; i < args->nframes; i++) + { + y[i] = bq.xc_0*x[i] + + bq.xc_1*bq.x_1 + + bq.xc_2*bq.x_2 + - bq.yc_1*bq.y_1 + - bq.yc_2*bq.y_2; + + bq.x_2 = bq.x_1; + bq.x_1 = x[i]; + bq.y_2 = bq.y_1; + bq.y_1 = y[i]; + } + + *(struct biquad*) args->userp = bq; +} + +static int l_lowpass_init(lua_State *s) +{ + ml_channel *src = mutil_get_channel(s,1); + + struct biquad *bq = calloc(1,sizeof(*bq)); + bq->recomp = lowpass_recomp; + bq->Fs = ml_sample_rate(); + bq->f0 = LOWPASS_DEFAULT_CUTOFF; + bq->Q = LOWPASS_DEFAULT_RESONANCE; + bq->recomp(bq); + + ml_node *node = ml_create_node(proc,bq); + ml_label_node(node,"lowpass"); + + ml_channel *dst = ml_add_input(node); + ml_connect(src,dst); + + bq->out = ml_add_output(node); + + *(void**) lua_newuserdata(s,sizeof(void*)) = bq; + luaL_setmetatable(s,LOWPASS_TYPENAME); + + return 1; +} + +static int l_lowpass_resonance(lua_State *s) +{ + struct biquad *bq = *(void**) luaL_checkudata(s,1,LOWPASS_TYPENAME); + bq->Q = luaL_checknumber(s,2); + bq->recomp(bq); + *(void**) lua_newuserdata(s,sizeof(void*)) = bq; + luaL_setmetatable(s,LOWPASS_TYPENAME); + return 1; +} + +static int l_lowpass_cutoff(lua_State *s) +{ + struct biquad *bq = *(void**) luaL_checkudata(s,1,LOWPASS_TYPENAME); + bq->f0 = luaL_checknumber(s,2); + bq->recomp(bq); + *(void**) lua_newuserdata(s,sizeof(void*)) = bq; + luaL_setmetatable(s,LOWPASS_TYPENAME); + return 1; +} + +static int l_lowpass_out(lua_State *s) +{ + struct biquad *bq = *(void**) luaL_checkudata(s,1,LOWPASS_TYPENAME); + *(void**) lua_newuserdata(s,sizeof(void*)) = bq->out; + luaL_setmetatable(s,MELISMA_OUTPUT_CHANNEL_TYPENAME); + return 1; +} + +int luaopen_melisma_biquad(lua_State *s) +{ +/*** Digital biquad filters. +@module biquad +*/ + lua_newtable(s); + int tbl = lua_gettop(s); + +/*** Lowpass filter. +@type biquad.lowpass +@usage + + local mbiquad = require 'melisma.biquad' + local input = ... + local lowpass = mbiquad.lowpass.init(node, 2000, 0.6) + lowpass:resonance(1000) + lowpass:cutoff(0.25) +*/ + lua_newtable(s); + int lowpass_tbl = lua_gettop(s); + +/*** Create a lowpass filter. +@static +@function init +@tparam channel input Input signal. +@treturn biquad.lowpass The newly created lowpass filter. +*/ + lua_pushcfunction(s,l_lowpass_init); + lua_setfield(s,lowpass_tbl,"init"); + +/*** Set filter resonance. +@function lowpass:resonance +@tparam number res Resonance. +*/ + lua_pushcfunction(s,l_lowpass_resonance); + lua_setfield(s,lowpass_tbl,"resonance"); + + +/*** Set filter cutoff frequency. +@function lowpass:cutoff +@tparam number cut Cutoff frequency. +*/ + lua_pushcfunction(s,l_lowpass_cutoff); + lua_setfield(s,lowpass_tbl,"cutoff"); + + lua_pushcfunction(s,l_lowpass_out); + lua_setfield(s,lowpass_tbl,MUTIL_OUTPUT_CHANNEL_FUNCTION); + + luaL_newmetatable(s,LOWPASS_TYPENAME); + int lowpass_mt = lua_gettop(s); + + lua_pushvalue(s,lowpass_tbl); + lua_setfield(s,lowpass_mt,"__index"); + + lua_pushvalue(s,lowpass_tbl); + lua_setfield(s,tbl,"lowpass"); + + lua_settop(s,tbl); + return 1; +} |
