-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcore.cpp
296 lines (228 loc) · 7.69 KB
/
core.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/******************************************************************************
* Copyright (C) 2020 by Arkadiusz Hiler
* 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, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "obs-lv2.hpp"
using namespace std;
LV2Plugin::LV2Plugin(size_t channels)
{
feature_uri_map_data = { this, LV2Plugin::urid_map };
feature_uri_map = { LV2_URID_MAP_URI, &feature_uri_map_data };
feature_uri_unmap_data = { this, LV2Plugin::urid_unmap };
feature_uri_unmap = { LV2_URID_MAP_URI, &feature_uri_unmap_data };
/* data will be set to plugin instance each time we update */
feature_instance_access = { LV2_INSTANCE_ACCESS_URI, nullptr };
feature_data_access = { LV2_DATA_ACCESS_URI, &feature_data_access_data };
features[0] = &feature_uri_map;
/* XXX: don't expose it, crashes some plugins */
/* features[1] = &feature_uri_unmap; */
features[1] = &feature_instance_access;
features[2] = &feature_data_access;
features[3] = nullptr; /* NULL terminated */
this->channels = channels;
world = lilv_world_new();
lilv_world_load_all(world);
plugins = lilv_world_get_all_plugins(world);
plugin = nullptr;
ui_host = suil_host_new(LV2Plugin::suil_write_from_ui,
LV2Plugin::suil_port_index,
NULL, NULL);
populate_supported_plugins();
}
LV2Plugin::~LV2Plugin()
{
cleanup_ui();
cleanup_plugin_instance();
suil_host_free(ui_host);
lilv_world_free(world);
free(plugin_uri);
cleanup_ports();
}
bool LV2Plugin::is_feature_supported(const LilvNode* node)
{
bool is_supported = false;
if (!lilv_node_is_uri(node)) {
printf("tested feature passed is not an URI!\n");
abort();
}
auto node_uri = lilv_node_as_uri(node);
for (auto feature = this->features; *feature != nullptr; feature++) {
if (0 == strcmp((*feature)->URI, node_uri))
is_supported = true;
}
return is_supported;
}
void LV2Plugin::populate_supported_plugins(void)
{
LilvNode* input_port = lilv_new_uri(world, LV2_CORE__InputPort);
LilvNode* output_port = lilv_new_uri(world, LV2_CORE__OutputPort);
LilvNode* audio_port = lilv_new_uri(world, LV2_CORE__AudioPort);
LILV_FOREACH(plugins, i, this->plugins) {
auto plugin = lilv_plugins_get(this->plugins, i);
bool skip = false;
/* filter out plugins which require feature we don't support */
auto req_features = lilv_plugin_get_required_features(plugin);
LILV_FOREACH(nodes, j, req_features) {
const LilvNode* feature = lilv_nodes_get(req_features, j);
if (!this->is_feature_supported(feature)) {
skip = true;
printf("%s filtered out because we do not support %s\n",
lilv_node_as_string(lilv_plugin_get_name(plugin)),
lilv_node_as_string(feature));
break;
}
}
lilv_nodes_free(req_features);
if (skip)
continue;
/* filter out plugins without supported UI */
skip = true;
auto uis = lilv_plugin_get_uis(plugin);
auto qt5_uri = lilv_new_uri(this->world, LV2_UI__Qt5UI);
LILV_FOREACH(uis, i, uis) {
const LilvNode *ui_type;
auto ui = lilv_uis_get(uis, i);
if (lilv_ui_is_supported(ui, suil_ui_supported,
qt5_uri,
&ui_type)) {
skip = false;
}
}
lilv_node_free(qt5_uri);
if (skip) {
printf("%s filtered out - has no usable GUI\n",
lilv_node_as_string(lilv_plugin_get_name(plugin)));
continue;
}
auto in_aps = lilv_plugin_get_num_ports_of_class(plugin, audio_port, input_port, NULL);
auto out_aps = lilv_plugin_get_num_ports_of_class(plugin, audio_port, output_port, NULL);
if (in_aps < this->get_channels() || out_aps < this->get_channels()) {
printf("%s filtered out - supports only %u input and %u output channels, while OBS audio uses %lu\n",
lilv_node_as_string(lilv_plugin_get_name(plugin)),
in_aps, out_aps, this->get_channels());
continue;
}
this->supported_pluggins.push_back(pair<string,string>(
lilv_node_as_string(lilv_plugin_get_name(plugin)),
lilv_node_as_string(lilv_plugin_get_uri(plugin))));
}
lilv_node_free(audio_port);
lilv_node_free(output_port);
lilv_node_free(input_port);
}
void LV2Plugin::for_each_supported_plugin(function<void(const char *, const char *)> f)
{
for (auto const& p: this->supported_pluggins)
f(p.first.c_str(), p.second.c_str());
}
void LV2Plugin::set_uri(const char* uri)
{
bool replace = true;
if (this->plugin_uri != nullptr && uri != nullptr) {
if (!strcmp(this->plugin_uri, uri))
replace = false;
}
if (replace) {
if (this->plugin_uri != nullptr)
free(this->plugin_uri);
if (uri != nullptr)
this->plugin_uri = strdup(uri);
else
this->plugin_uri = nullptr;
instance_needs_update = true;
}
}
void LV2Plugin::cleanup_plugin_instance(void) {
if (this->plugin_instance == nullptr)
return;
lilv_instance_deactivate(this->plugin_instance);
lilv_instance_free(this->plugin_instance);
this->feature_instance_access.data = nullptr;
this->feature_data_access_data.data_access = nullptr;
this->plugin_instance = nullptr;
}
void LV2Plugin::update_plugin_instance(void)
{
if (!this->instance_needs_update)
return;
this->ready = false;
this->instance_needs_update = false;
cleanup_ui();
cleanup_plugin_instance();
this->plugin = nullptr;
this->ui = nullptr;
cleanup_ports();
LilvNode *uri = nullptr;
if (this->plugin_uri != nullptr) {
uri = lilv_new_uri(this->world, this->plugin_uri);
this->plugin = lilv_plugins_get_by_uri(plugins, uri);
lilv_node_free(uri);
}
if (this->plugin == nullptr) {
WARN("failed to get plugin by uri\n");
return;
}
auto qt5_uri = lilv_new_uri(this->world, LV2_UI__Qt5UI);
auto uis = lilv_plugin_get_uis(this->plugin);
LILV_FOREACH(uis, i, uis) {
const LilvNode *ui_type;
auto ui = lilv_uis_get(uis, i);
if (lilv_ui_is_supported(ui, suil_ui_supported,
qt5_uri,
&ui_type)) {
this->ui = ui;
this->ui_type = ui_type;
break;
}
}
lilv_node_free(qt5_uri);
this->plugin_instance = lilv_plugin_instantiate(this->plugin,
this->sample_rate,
this->features);
if (this->plugin_instance == nullptr) {
WARN("failed to instantiate plugin\n");
return;
}
this->feature_instance_access.data = lilv_instance_get_handle(this->plugin_instance);
/* XXX: digging in lilv's internals, there may be a better way to do this */
this->feature_data_access_data.data_access = this->plugin_instance->lv2_descriptor->extension_data;
this->prepare_ports();
lilv_instance_activate(this->plugin_instance);
this->ready = true;
}
void LV2Plugin::set_sample_rate(uint32_t sample_rate)
{
if (this->sample_rate != sample_rate) {
this->sample_rate = sample_rate;
this->instance_needs_update = true;
}
}
void LV2Plugin::set_channels(size_t channels)
{
if (this->channels != channels) {
this->channels = channels;
this->instance_needs_update = true;
}
}
size_t LV2Plugin::get_channels(void)
{
return this->channels;
}
uint32_t LV2Plugin::port_index(const char *symbol) {
LilvNode* lilv_sym = lilv_new_string(this->world, symbol);
const LilvPort* port = lilv_plugin_get_port_by_symbol(this->plugin, lilv_sym);
lilv_node_free(lilv_sym);
if (port == nullptr)
return LV2UI_INVALID_PORT_INDEX;
auto idx = lilv_port_get_index(this->plugin, port);
return idx;
}