-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgain.cpp
More file actions
147 lines (135 loc) · 3.41 KB
/
gain.cpp
File metadata and controls
147 lines (135 loc) · 3.41 KB
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
#include "biquad.h"
#include "descriptor.h"
#include "ladspa_ids.h"
#include <array>
#include <cmath>
namespace {
constexpr auto control = 1;
constexpr auto channels = 8;
struct filter {
LADSPA_Data gain;
std::array<std::array<LADSPA_Data *, 2>, channels> io = {};
};
LADSPA_Handle
instantiate(const LADSPA_Descriptor *d, unsigned long fs)
{
return new filter;
}
void
connect_port(LADSPA_Handle h, unsigned long port, LADSPA_Data *d)
{
filter *p = reinterpret_cast<filter *>(h);
switch (port) {
case 0:
/* db->magnitude */
p->gain = std::pow(10.0, *d / 20.0);
return;
}
port -= control;
if (port >= 2 * channels)
return;
p->io[port / 2][port % 2] = d;
}
void
run(LADSPA_Handle h, unsigned long samples)
{
filter *p = reinterpret_cast<filter *>(h);
for (auto i = 0; i < channels; ++i) {
auto in = p->io[i][0];
auto out = p->io[i][1];
/* stop on first unconnected port */
if (!in || !out)
return;
for (unsigned long j = 0; j < samples; ++j)
out[j] = in[j] * p->gain;
}
}
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_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 = {
"Gain (dB)",
"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_DEFAULT_0,
.LowerBound = -100,
.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 <patrick.oppenlander@gmail.com>",
.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 = nullptr,
.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::gain + i;
d.PortCount = control + (i + 1) * 2;
register_plugin({d,
"gain_" + std::to_string(i + 1) + "ch",
"Gain (" + std::to_string(i + 1) + " Channel)"});
}
}
} init;
} /* namespace */