-
Notifications
You must be signed in to change notification settings - Fork 0
/
tap_utils.h
260 lines (203 loc) · 6.3 KB
/
tap_utils.h
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- linux-c -*-
Copyright (C) 2004 Tom Szilagyi
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _ISOC99_SOURCE
#define _ISOC99_SOURCE
#endif
#include <stdint.h>
/* push a sample into a ringbuffer and return the sample falling out */
static inline
LADSPA_Data
push_buffer(LADSPA_Data insample, LADSPA_Data * buffer,
unsigned long buflen, unsigned long * pos) {
LADSPA_Data outsample;
outsample = buffer[*pos];
buffer[(*pos)++] = insample;
if (*pos >= buflen)
*pos = 0;
return outsample;
}
/* read a value from a ringbuffer.
* n == 0 returns the oldest sample from the buffer.
* n == buflen-1 returns the sample written to the buffer
* at the last push_buffer call.
* n must not exceed buflen-1, or your computer will explode.
*/
static inline
LADSPA_Data
read_buffer(LADSPA_Data * buffer, unsigned long buflen,
unsigned long pos, unsigned long n) {
while (n + pos >= buflen)
n -= buflen;
return buffer[n + pos];
}
/* overwrites a value in a ringbuffer, but pos stays the same.
* n == 0 overwrites the oldest sample pushed in the buffer.
* n == buflen-1 overwrites the sample written to the buffer
* at the last push_buffer call.
* n must not exceed buflen-1, or your computer... you know.
*/
static inline
void
write_buffer(LADSPA_Data insample, LADSPA_Data * buffer, unsigned long buflen,
unsigned long pos, unsigned long n) {
while (n + pos >= buflen)
n -= buflen;
buffer[n + pos] = insample;
}
/* Please note that the majority of the definitions and helper
functions below have been derived from the source code of Steve
Harris's SWH plugins (particularly from the "biquad.h" file). While I
give him credit for his excellent work, I reserve myself to be blamed
for any bugs or malfunction. */
#define db2lin(x) ((x) > -90.0f ? powf(10.0f, (x) * 0.05f) : 0.0f)
#define ABS(x) (x)>0.0f?(x):-1.0f*(x)
#define LN_2_2 0.34657359f
#define LIMIT(v,l,u) ((v)<(l)?(l):((v)>(u)?(u):(v)))
#define BIQUAD_TYPE float
typedef BIQUAD_TYPE bq_t;
/* Biquad filter (adapted from lisp code by Eli Brandt,
http://www.cs.cmu.edu/~eli/) */
/* The prev. comment has been preserved from Steve Harris's biquad.h */
typedef struct {
bq_t a1;
bq_t a2;
bq_t b0;
bq_t b1;
bq_t b2;
bq_t x1;
bq_t x2;
bq_t y1;
bq_t y2;
} biquad;
static inline void biquad_init(biquad *f) {
f->x1 = 0.0f;
f->x2 = 0.0f;
f->y1 = 0.0f;
f->y2 = 0.0f;
}
static inline
void
eq_set_params(biquad *f, bq_t fc, bq_t gain, bq_t bw, bq_t fs) {
bq_t w = 2.0f * M_PI * LIMIT(fc, 1.0f, fs/2.0f) / fs;
bq_t cw = cosf(w);
bq_t sw = sinf(w);
bq_t J = pow(10.0f, gain * 0.025f);
bq_t g = sw * sinhf(LN_2_2 * LIMIT(bw, 0.0001f, 4.0f) * w / sw);
bq_t a0r = 1.0f / (1.0f + (g / J));
f->b0 = (1.0f + (g * J)) * a0r;
f->b1 = (-2.0f * cw) * a0r;
f->b2 = (1.0f - (g * J)) * a0r;
f->a1 = -(f->b1);
f->a2 = ((g / J) - 1.0f) * a0r;
}
static inline void lp_set_params(biquad *f, bq_t fc, bq_t bw, bq_t fs) {
bq_t omega = 2.0 * M_PI * fc/fs;
bq_t sn = sin(omega);
bq_t cs = cos(omega);
bq_t alpha = sn * sinh(M_LN2 / 2.0 * bw * omega / sn);
const float a0r = 1.0 / (1.0 + alpha);
#if 0
b0 = (1 - cs) /2;
b1 = 1 - cs;
b2 = (1 - cs) /2;
a0 = 1 + alpha;
a1 = -2 * cs;
a2 = 1 - alpha;
#endif
f->b0 = a0r * (1.0 - cs) * 0.5;
f->b1 = a0r * (1.0 - cs);
f->b2 = a0r * (1.0 - cs) * 0.5;
f->a1 = a0r * (2.0 * cs);
f->a2 = a0r * (alpha - 1.0);
}
static inline
void
hp_set_params(biquad *f, bq_t fc, bq_t bw, bq_t fs)
{
bq_t omega = 2.0 * M_PI * fc/fs;
bq_t sn = sin(omega);
bq_t cs = cos(omega);
bq_t alpha = sn * sinh(M_LN2 / 2.0 * bw * omega / sn);
const float a0r = 1.0 / (1.0 + alpha);
#if 0
b0 = (1 + cs) /2;
b1 = -(1 + cs);
b2 = (1 + cs) /2;
a0 = 1 + alpha;
a1 = -2 * cs;
a2 = 1 - alpha;
#endif
f->b0 = a0r * (1.0 + cs) * 0.5;
f->b1 = a0r * -(1.0 + cs);
f->b2 = a0r * (1.0 + cs) * 0.5;
f->a1 = a0r * (2.0 * cs);
f->a2 = a0r * (alpha - 1.0);
}
static inline
void
ls_set_params(biquad *f, bq_t fc, bq_t gain, bq_t slope, bq_t fs)
{
bq_t w = 2.0f * M_PI * LIMIT(fc, 1.0, fs/2.0) / fs;
bq_t cw = cosf(w);
bq_t sw = sinf(w);
bq_t A = powf(10.0f, gain * 0.025f);
bq_t b = sqrt(((1.0f + A * A) / LIMIT(slope, 0.0001f, 1.0f)) - ((A -
1.0f) * (A - 1.0)));
bq_t apc = cw * (A + 1.0f);
bq_t amc = cw * (A - 1.0f);
bq_t bs = b * sw;
bq_t a0r = 1.0f / (A + 1.0f + amc + bs);
f->b0 = a0r * A * (A + 1.0f - amc + bs);
f->b1 = a0r * 2.0f * A * (A - 1.0f - apc);
f->b2 = a0r * A * (A + 1.0f - amc - bs);
f->a1 = a0r * 2.0f * (A - 1.0f + apc);
f->a2 = a0r * (-A - 1.0f - amc + bs);
}
static inline
void
hs_set_params(biquad *f, bq_t fc, bq_t gain, bq_t slope, bq_t fs) {
bq_t w = 2.0f * M_PI * LIMIT(fc, 1.0, fs/2.0) / fs;
bq_t cw = cosf(w);
bq_t sw = sinf(w);
bq_t A = powf(10.0f, gain * 0.025f);
bq_t b = sqrt(((1.0f + A * A) / LIMIT(slope, 0.0001f, 1.0f)) - ((A -
1.0f) * (A - 1.0f)));
bq_t apc = cw * (A + 1.0f);
bq_t amc = cw * (A - 1.0f);
bq_t bs = b * sw;
bq_t a0r = 1.0f / (A + 1.0f - amc + bs);
f->b0 = a0r * A * (A + 1.0f + amc + bs);
f->b1 = a0r * -2.0f * A * (A - 1.0f + apc);
f->b2 = a0r * A * (A + 1.0f + amc - bs);
f->a1 = a0r * -2.0f * (A - 1.0f - apc);
f->a2 = a0r * (-A - 1.0f + amc + bs);
}
static inline
bq_t
biquad_run(biquad *f, bq_t x) {
union {
bq_t y;
uint32_t y_int;
} u;
u.y = f->b0 * x + f->b1 * f->x1 + f->b2 * f->x2
+ f->a1 * f->y1 + f->a2 * f->y2;
if ((u.y_int & 0x7f800000) == 0)
u.y = 0.0f;
f->x2 = f->x1;
f->x1 = x;
f->y2 = f->y1;
f->y1 = u.y;
return u.y;
}