1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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;
}
|