/
patchcord.c
190 lines (151 loc) · 5.78 KB
/
patchcord.c
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
/*
A super-simple LADSPA plugin that just copies audio from one port to another
has a run_adding function so it can be used to mix audio from multiple plugins
Should build on most linux systems with the LADSPA SDK installed by simply entering
gcc -shared patchcord.c -o patchcord.so
MIT License
Copyright (c) 2022 Bill Peterson (white2rnado@geekfunklabs.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "ladspa.h"
#define PATCHCORD_INPUT 0
#define PATCHCORD_OUTPUT 1
static LADSPA_Descriptor *patchcordDescriptor = NULL;
typedef struct {
LADSPA_Data *input;
LADSPA_Data *output;
LADSPA_Data run_adding_gain;
} Patchcord;
const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {
switch (index) {
case 0:
return patchcordDescriptor;
default:
return NULL;
}
}
static void cleanupPatchcord(LADSPA_Handle instance) {
free(instance);
}
static void connectPortPatchcord(
LADSPA_Handle instance,
unsigned long port,
LADSPA_Data *data) {
Patchcord *plugin;
plugin = (Patchcord *)instance;
switch (port) {
case PATCHCORD_INPUT:
plugin->input = data;
break;
case PATCHCORD_OUTPUT:
plugin->output = data;
break;
}
}
static LADSPA_Handle instantiatePatchcord(
const LADSPA_Descriptor *descriptor,
unsigned long s_rate) {
Patchcord *plugin_data = (Patchcord *)calloc(1, sizeof(Patchcord));
plugin_data->run_adding_gain = 1.0f;
return (LADSPA_Handle)plugin_data;
}
static void runPatchcord(LADSPA_Handle instance, unsigned long sample_count) {
Patchcord *plugin_data = (Patchcord *)instance;
/* Input (array of floats of length sample_count) */
const LADSPA_Data * const input = plugin_data->input;
/* Output (array of floats of length sample_count) */
LADSPA_Data * const output = plugin_data->output;
unsigned long pos;
for (pos = 0; pos < sample_count; pos++) {
output[pos] = input[pos];
}
}
static void setRunAddingGainPatchcord(LADSPA_Handle instance, LADSPA_Data gain) {
((Patchcord *)instance)->run_adding_gain = gain;
}
static void runAddingPatchcord(LADSPA_Handle instance, unsigned long sample_count) {
Patchcord *plugin_data = (Patchcord *)instance;
LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
/* Input (array of floats of length sample_count) */
const LADSPA_Data * const input = plugin_data->input;
/* Output (array of floats of length sample_count) */
LADSPA_Data * const output = plugin_data->output;
unsigned long pos;
for (pos = 0; pos < sample_count; pos++) {
output[pos] += input[pos] * run_adding_gain;
}
}
static void __attribute__((constructor)) patchcord_init() {
char **port_names;
LADSPA_PortDescriptor *port_descriptors;
LADSPA_PortRangeHint *port_range_hints;
patchcordDescriptor =
(LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));
if (patchcordDescriptor) {
patchcordDescriptor->UniqueID = 650879;
patchcordDescriptor->Label = "patchcord";
patchcordDescriptor->Properties =
LADSPA_PROPERTY_HARD_RT_CAPABLE;
patchcordDescriptor->Name = ("Patch cord");
patchcordDescriptor->Maker =
"Bill Peterson <white2rnado@geekfunklabs.com>";
patchcordDescriptor->Copyright =
"GPL";
patchcordDescriptor->PortCount = 2;
port_descriptors = (LADSPA_PortDescriptor *)calloc(2,
sizeof(LADSPA_PortDescriptor));
patchcordDescriptor->PortDescriptors =
(const LADSPA_PortDescriptor *)port_descriptors;
port_range_hints = (LADSPA_PortRangeHint *)calloc(2,
sizeof(LADSPA_PortRangeHint));
patchcordDescriptor->PortRangeHints =
(const LADSPA_PortRangeHint *)port_range_hints;
port_names = (char **)calloc(2, sizeof(char*));
patchcordDescriptor->PortNames =
(const char **)port_names;
/* Parameters for Input */
port_descriptors[PATCHCORD_INPUT] =
LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
port_names[PATCHCORD_INPUT] = ("Input");
port_range_hints[PATCHCORD_INPUT].HintDescriptor = 0;
/* Parameters for Output */
port_descriptors[PATCHCORD_OUTPUT] =
LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
port_names[PATCHCORD_OUTPUT] = ("Output");
port_range_hints[PATCHCORD_OUTPUT].HintDescriptor = 0;
patchcordDescriptor->activate = NULL;
patchcordDescriptor->cleanup = cleanupPatchcord;
patchcordDescriptor->connect_port = connectPortPatchcord;
patchcordDescriptor->deactivate = NULL;
patchcordDescriptor->instantiate = instantiatePatchcord;
patchcordDescriptor->run = runPatchcord;
patchcordDescriptor->run_adding = runAddingPatchcord;
patchcordDescriptor->set_run_adding_gain = setRunAddingGainPatchcord;
}
}
static void __attribute__((destructor)) patchcord_fini() {
if (patchcordDescriptor) {
free((LADSPA_PortDescriptor *)patchcordDescriptor->PortDescriptors);
free((char **)patchcordDescriptor->PortNames);
free((LADSPA_PortRangeHint *)patchcordDescriptor->PortRangeHints);
free(patchcordDescriptor);
}
patchcordDescriptor = NULL;
}