-
Notifications
You must be signed in to change notification settings - Fork 0
/
svf.cc.in
95 lines (78 loc) · 2.61 KB
/
svf.cc.in
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
#include <cmath>
#include <lv2.h>
#include "state-variable-filter/svf-v2.h"
#include "common.cc"
struct svf {
float *ports[7];
svf_v2::lowbandhighpass filter;
float sample_rate;
float freq;
float q;
float lowgain;
float bandgain;
float highgain;
unsigned frame_index;
svf(float sample_rate) : sample_rate(sample_rate), freq(0.01f), q(0.5f), lowgain(0.0f), bandgain(0.0f), highgain(0.0f), frame_index(0) {
}
};
static LV2_Handle instantiate(const LV2_Descriptor *descriptor, double sample_rate, const char *bundle_path, const LV2_Feature *const *features)
{
return (LV2_Handle)(new svf(sample_rate));
}
static void cleanup(LV2_Handle instance)
{
delete ((svf*)instance);
}
static void connect_port(LV2_Handle instance, uint32_t port, void *data_location)
{
((svf*)instance)->ports[port] = (float*)data_location;
}
#define ALPHA 0.99f
#define PARAM_UPDATE 8
static void run(LV2_Handle instance, uint32_t sample_count)
{
svf *tinstance = (svf*)(instance);
const float p_freq = tinstance->ports[2][0] / tinstance->sample_rate;
const float p_q = tinstance->ports[3][0];
const float p_lowgain = db_to_gain(tinstance->ports[4][0]);
const float p_bandgain = db_to_gain(tinstance->ports[5][0]);
const float p_highgain = db_to_gain(tinstance->ports[6][0]);
for(uint32_t sample_index = 0; sample_index < sample_count; ++sample_index)
{
if (0 == tinstance->frame_index % PARAM_UPDATE)
{
tinstance->freq = ALPHA * tinstance->freq + (1.0f - ALPHA) * p_freq;
tinstance->q = ALPHA * tinstance->q + (1.0f - ALPHA) * p_q;
tinstance->lowgain = ALPHA * tinstance->lowgain + (1.0f - ALPHA) * p_lowgain;
tinstance->bandgain = ALPHA * tinstance->bandgain + (1.0f - ALPHA) * p_bandgain;
tinstance->highgain = ALPHA * tinstance->highgain + (1.0f - ALPHA) * p_highgain;
}
++(tinstance->frame_index);
tinstance->filter.process
(
tinstance->ports[0] + sample_index,
tinstance->ports[1] + sample_index,
1,
tinstance->highgain,
tinstance->bandgain,
tinstance->lowgain,
tinstance->freq,
tinstance->q
);
}
}
static const LV2_Descriptor descriptor = {
"http://fps.io/plugins/state-variable-filter-VERSION",
instantiate,
connect_port,
nullptr, // activate
run,
nullptr, // deactivate
cleanup,
nullptr // extension_data
};
LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor (uint32_t index)
{
if (0 == index) return &descriptor;
else return nullptr;
}