-
Notifications
You must be signed in to change notification settings - Fork 0
/
low_shelf.cpp
177 lines (164 loc) · 4.09 KB
/
low_shelf.cpp
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
#include "biquad.h"
#include "descriptor.h"
#include "ladspa_ids.h"
#include <array>
namespace {
constexpr auto control = 3;
constexpr auto channels = 8;
struct filter {
LADSPA_Data f0 = 0, gain = 0, Q = 0;
std::array<std::array<LADSPA_Data *, 2>, channels> io = {};
unsigned long fs = 0;
biquad_coefficients bqc;
std::array<biquad, channels> bq;
};
LADSPA_Handle
instantiate(const LADSPA_Descriptor *d, unsigned long fs)
{
auto p = new filter;
p->fs = fs;
return p;
}
void
connect_port(LADSPA_Handle h, unsigned long port, LADSPA_Data *d)
{
filter *p = reinterpret_cast<filter *>(h);
switch (port) {
case 0:
p->f0 = *d;
return;
case 1:
p->gain = *d;
return;
case 2:
p->Q = *d;
return;
}
port -= control;
if (port >= 2 * channels)
return;
p->io[port / 2][port % 2] = d;
}
void
activate(LADSPA_Handle h)
{
filter *p = reinterpret_cast<filter *>(h);
p->bqc.low_shelf(p->f0, p->gain, p->Q, p->fs);
}
void
run(LADSPA_Handle h, unsigned long samples)
{
filter *p = reinterpret_cast<filter *>(h);
for (auto i = 0; i < channels; ++i) {
/* stop on first unconnected port */
if (!p->io[i][0] || !p->io[i][1])
return;
p->bq[i].run(p->bqc, p->io[i][0], p->io[i][1], samples);
}
}
void
cleanup(LADSPA_Handle h)
{
filter *p = reinterpret_cast<filter *>(h);
delete p;
}
constexpr std::array<LADSPA_PortDescriptor, control + 2 * channels> ports = {
LADSPA_PORT_CONTROL | LADSPA_PORT_INPUT,
LADSPA_PORT_CONTROL | LADSPA_PORT_INPUT,
LADSPA_PORT_CONTROL | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_INPUT,
LADSPA_PORT_AUDIO | LADSPA_PORT_OUTPUT,
};
constexpr std::array<const char *, size(ports)> port_names = {
"Centre Frequency (Hz)",
"Gain (dB)",
"Bandwidth (Q)",
"Channel 1 Input",
"Channel 1 Output",
"Channel 2 Input",
"Channel 2 Output",
"Channel 3 Input",
"Channel 3 Output",
"Channel 4 Input",
"Channel 4 Output",
"Channel 5 Input",
"Channel 5 Output",
"Channel 6 Input",
"Channel 6 Output",
"Channel 7 Input",
"Channel 7 Output",
"Channel 8 Input",
"Channel 8 Output",
};
constexpr std::array<LADSPA_PortRangeHint, size(ports)> port_hints = { { {
.HintDescriptor = LADSPA_HINT_BOUNDED_BELOW |
LADSPA_HINT_BOUNDED_ABOVE |
LADSPA_HINT_SAMPLE_RATE |
LADSPA_HINT_DEFAULT_MIDDLE,
.LowerBound = 0,
.UpperBound = 0.45,
}, {
.HintDescriptor = LADSPA_HINT_BOUNDED_BELOW |
LADSPA_HINT_BOUNDED_ABOVE |
LADSPA_HINT_DEFAULT_0,
.LowerBound = -100,
.UpperBound = 100,
}, {
.HintDescriptor = LADSPA_HINT_BOUNDED_BELOW |
LADSPA_HINT_BOUNDED_ABOVE |
LADSPA_HINT_DEFAULT_1,
.LowerBound = 0,
.UpperBound = 100,
},
} };
/*
* register plugin instances
*/
struct init {
init()
{
LADSPA_Descriptor d = {
.UniqueID = 0,
.Label = nullptr,
.Properties = LADSPA_PROPERTY_HARD_RT_CAPABLE,
.Name = nullptr,
.Maker = "Patrick Oppenlander <[email protected]>",
.Copyright = "Patrick Oppenlander, 2021",
.PortCount = 0,
.PortDescriptors = data(ports),
.PortNames = data(port_names),
.PortRangeHints = data(port_hints),
.ImplementationData = nullptr,
.instantiate = instantiate,
.connect_port = connect_port,
.activate = activate,
.run = run,
.run_adding = nullptr,
.set_run_adding_gain = nullptr,
.deactivate = nullptr,
.cleanup = cleanup,
};
for (int i = 0; i < channels; ++i) {
d.UniqueID = ladspa_ids::low_shelf + i;
d.PortCount = control + (i + 1) * 2;
register_plugin({d,
"low_shelf_" + std::to_string(i + 1) + "ch",
"Low Shelf (" + std::to_string(i + 1) + " Channel)"});
}
}
} init;
} /* namespace */