1 /*
2 * Copyright (C) 2018-2021 Alexandros Theodotou <alex at zrythm dot org>
3 *
4 * This file is part of Zrythm
5 *
6 * Zrythm is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Zrythm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Zrythm. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <stdlib.h>
21
22 #include "audio/track.h"
23 #include "plugins/carla_native_plugin.h"
24 #include "plugins/lv2_plugin.h"
25 #include "plugins/plugin_descriptor.h"
26 #include "plugins/plugin_manager.h"
27 #include "plugins/plugin.h"
28 #include "utils/objects.h"
29 #include "utils/string.h"
30 #include "zrythm.h"
31
32 #include <gtk/gtk.h>
33
34 #include <lv2/instance-access/instance-access.h>
35
36 PluginDescriptor *
plugin_descriptor_new(void)37 plugin_descriptor_new (void)
38 {
39 PluginDescriptor * self =
40 object_new (PluginDescriptor);
41 self->schema_version =
42 PLUGIN_DESCRIPTOR_SCHEMA_VERSION;
43
44 return self;
45 }
46
47 const char *
plugin_protocol_to_str(PluginProtocol prot)48 plugin_protocol_to_str (
49 PluginProtocol prot)
50 {
51 for (size_t i = 0;
52 i < G_N_ELEMENTS (plugin_protocol_strings);
53 i++)
54 {
55 if (plugin_protocol_strings[i].val ==
56 (int64_t) prot)
57 {
58 return
59 plugin_protocol_strings[i].str;
60 }
61 }
62 g_return_val_if_reached (NULL);
63 }
64
65 /**
66 * Clones the plugin descriptor.
67 */
68 void
plugin_descriptor_copy(PluginDescriptor * dest,const PluginDescriptor * src)69 plugin_descriptor_copy (
70 PluginDescriptor * dest,
71 const PluginDescriptor * src)
72 {
73 g_return_if_fail (src->schema_version > 0);
74 dest->schema_version = src->schema_version;
75 dest->author = g_strdup (src->author);
76 dest->name = g_strdup (src->name);
77 dest->website = g_strdup (src->website);
78 dest->category_str = g_strdup (src->category_str);
79 dest->category = src->category;
80 dest->num_audio_ins = src->num_audio_ins;
81 dest->num_midi_ins = src->num_midi_ins;
82 dest->num_audio_outs = src->num_audio_outs;
83 dest->num_midi_outs = src->num_midi_outs;
84 dest->num_ctrl_ins = src->num_ctrl_ins;
85 dest->num_ctrl_outs = src->num_ctrl_outs;
86 dest->num_cv_ins = src->num_cv_ins;
87 dest->num_cv_outs = src->num_cv_outs;
88 dest->arch = src->arch;
89 dest->protocol = src->protocol;
90 dest->path = g_strdup (src->path);
91 dest->uri = g_strdup (src->uri);
92 dest->min_bridge_mode = src->min_bridge_mode;
93 dest->has_custom_ui = src->has_custom_ui;
94 dest->ghash = src->ghash;
95 }
96
97 /**
98 * Clones the plugin descriptor.
99 */
100 PluginDescriptor *
plugin_descriptor_clone(const PluginDescriptor * src)101 plugin_descriptor_clone (
102 const PluginDescriptor * src)
103 {
104 PluginDescriptor * self =
105 object_new (PluginDescriptor);
106
107 plugin_descriptor_copy (self, src);
108
109 return self;
110 }
111
112 #define IS_CAT(x) \
113 (descr->category == PC_##x)
114
115 /**
116 * Returns if the Plugin is an instrument or not.
117 */
118 bool
plugin_descriptor_is_instrument(const PluginDescriptor * descr)119 plugin_descriptor_is_instrument (
120 const PluginDescriptor * descr)
121 {
122 if (descr->num_midi_ins == 0 ||
123 descr->num_audio_outs == 0)
124 {
125 return false;
126 }
127
128 if (descr->category == PC_INSTRUMENT)
129 {
130 return true;
131 }
132 else
133 {
134 return
135 /* if VSTs are instruments their category
136 * must be INSTRUMENT, otherwise they are
137 * not */
138 descr->protocol != PROT_VST &&
139 descr->category == ZPLUGIN_CATEGORY_NONE &&
140 descr->num_midi_ins > 0 &&
141 descr->num_audio_outs > 0;
142 }
143 }
144
145 /**
146 * Returns if the Plugin is an effect or not.
147 */
148 bool
plugin_descriptor_is_effect(PluginDescriptor * descr)149 plugin_descriptor_is_effect (
150 PluginDescriptor * descr)
151 {
152
153 return
154 (descr->category > ZPLUGIN_CATEGORY_NONE &&
155 (IS_CAT (DELAY) ||
156 IS_CAT (REVERB) ||
157 IS_CAT (DISTORTION) ||
158 IS_CAT (WAVESHAPER) ||
159 IS_CAT (DYNAMICS) ||
160 IS_CAT (AMPLIFIER) ||
161 IS_CAT (COMPRESSOR) ||
162 IS_CAT (ENVELOPE) ||
163 IS_CAT (EXPANDER) ||
164 IS_CAT (GATE) ||
165 IS_CAT (LIMITER) ||
166 IS_CAT (FILTER) ||
167 IS_CAT (ALLPASS_FILTER) ||
168 IS_CAT (BANDPASS_FILTER) ||
169 IS_CAT (COMB_FILTER) ||
170 IS_CAT (EQ) ||
171 IS_CAT (MULTI_EQ) ||
172 IS_CAT (PARA_EQ) ||
173 IS_CAT (HIGHPASS_FILTER) ||
174 IS_CAT (LOWPASS_FILTER) ||
175 IS_CAT (GENERATOR) ||
176 IS_CAT (CONSTANT) ||
177 IS_CAT (OSCILLATOR) ||
178 IS_CAT (MODULATOR) ||
179 IS_CAT (CHORUS) ||
180 IS_CAT (FLANGER) ||
181 IS_CAT (PHASER) ||
182 IS_CAT (SIMULATOR) ||
183 IS_CAT (SIMULATOR_REVERB) ||
184 IS_CAT (SPATIAL) ||
185 IS_CAT (SPECTRAL) ||
186 IS_CAT (PITCH) ||
187 IS_CAT (UTILITY) ||
188 IS_CAT (ANALYZER) ||
189 IS_CAT (CONVERTER) ||
190 IS_CAT (FUNCTION) ||
191 IS_CAT (MIXER))) ||
192 (descr->category == ZPLUGIN_CATEGORY_NONE &&
193 descr->num_audio_ins > 0 &&
194 descr->num_audio_outs > 0);
195 }
196
197 /**
198 * Returns if the Plugin is a modulator or not.
199 */
200 int
plugin_descriptor_is_modulator(PluginDescriptor * descr)201 plugin_descriptor_is_modulator (
202 PluginDescriptor * descr)
203 {
204 return
205 (descr->category == ZPLUGIN_CATEGORY_NONE ||
206 (descr->category > ZPLUGIN_CATEGORY_NONE &&
207 (IS_CAT (ENVELOPE) ||
208 IS_CAT (GENERATOR) ||
209 IS_CAT (CONSTANT) ||
210 IS_CAT (OSCILLATOR) ||
211 IS_CAT (MODULATOR) ||
212 IS_CAT (UTILITY) ||
213 IS_CAT (CONVERTER) ||
214 IS_CAT (FUNCTION)))) &&
215 descr->num_cv_outs > 0;
216 }
217
218 /**
219 * Returns if the Plugin is a midi modifier or not.
220 */
221 int
plugin_descriptor_is_midi_modifier(PluginDescriptor * descr)222 plugin_descriptor_is_midi_modifier (
223 PluginDescriptor * descr)
224 {
225 return
226 (descr->category > ZPLUGIN_CATEGORY_NONE &&
227 descr->category == PC_MIDI) ||
228 (descr->category == ZPLUGIN_CATEGORY_NONE &&
229 descr->num_midi_ins > 0 &&
230 descr->num_midi_outs > 0 &&
231 descr->protocol != PROT_VST);
232 }
233
234 #undef IS_CAT
235
236 /**
237 * Returns the ZPluginCategory matching the given
238 * string.
239 */
240 ZPluginCategory
plugin_descriptor_string_to_category(const char * str)241 plugin_descriptor_string_to_category (
242 const char * str)
243 {
244 ZPluginCategory category = ZPLUGIN_CATEGORY_NONE;
245
246 #define CHECK_CAT(term,cat) \
247 if (g_strrstr (str, term)) \
248 category = PC_##cat
249
250 /* add category */
251 CHECK_CAT ("Delay", DELAY);
252 CHECK_CAT ("Reverb", REVERB);
253 CHECK_CAT ("Distortion", DISTORTION);
254 CHECK_CAT ("Waveshaper", WAVESHAPER);
255 CHECK_CAT ("Dynamics", DYNAMICS);
256 CHECK_CAT ("Amplifier", AMPLIFIER);
257 CHECK_CAT ("Compressor", COMPRESSOR);
258 CHECK_CAT ("Envelope", ENVELOPE);
259 CHECK_CAT ("Expander", EXPANDER);
260 CHECK_CAT ("Gate", GATE);
261 CHECK_CAT ("Limiter", LIMITER);
262 CHECK_CAT ("Filter", FILTER);
263 CHECK_CAT ("Allpass", ALLPASS_FILTER);
264 CHECK_CAT ("Bandpass", BANDPASS_FILTER);
265 CHECK_CAT ("Comb", COMB_FILTER);
266 CHECK_CAT ("Equaliser", EQ);
267 CHECK_CAT ("Equalizer", EQ);
268 CHECK_CAT ("Multiband", MULTI_EQ);
269 CHECK_CAT ("Para", PARA_EQ);
270 CHECK_CAT ("Highpass", HIGHPASS_FILTER);
271 CHECK_CAT ("Lowpass", LOWPASS_FILTER);
272 CHECK_CAT ("Generator", GENERATOR);
273 CHECK_CAT ("Constant", CONSTANT);
274 CHECK_CAT ("Instrument", INSTRUMENT);
275 CHECK_CAT ("Oscillator", OSCILLATOR);
276 CHECK_CAT ("MIDI", MIDI);
277 CHECK_CAT ("Modulator", MODULATOR);
278 CHECK_CAT ("Chorus", CHORUS);
279 CHECK_CAT ("Flanger", FLANGER);
280 CHECK_CAT ("Phaser", PHASER);
281 CHECK_CAT ("Simulator", SIMULATOR);
282 CHECK_CAT ("SimulatorReverb", SIMULATOR_REVERB);
283 CHECK_CAT ("Spatial", SPATIAL);
284 CHECK_CAT ("Spectral", SPECTRAL);
285 CHECK_CAT ("Pitch", PITCH);
286 CHECK_CAT ("Utility", UTILITY);
287 CHECK_CAT ("Analyser", ANALYZER);
288 CHECK_CAT ("Analyzer", ANALYZER);
289 CHECK_CAT ("Converter", CONVERTER);
290 CHECK_CAT ("Function", FUNCTION);
291 CHECK_CAT ("Mixer", MIXER);
292
293 #undef CHECK_CAT
294
295 return category;
296 }
297
298 char *
plugin_descriptor_category_to_string(ZPluginCategory category)299 plugin_descriptor_category_to_string (
300 ZPluginCategory category)
301 {
302
303 #define RET_STRING(term,cat) \
304 if (category == PC_##cat) \
305 return g_strdup (term)
306
307 /* add category */
308 RET_STRING ("Delay", DELAY);
309 RET_STRING ("Reverb", REVERB);
310 RET_STRING ("Distortion", DISTORTION);
311 RET_STRING ("Waveshaper", WAVESHAPER);
312 RET_STRING ("Dynamics", DYNAMICS);
313 RET_STRING ("Amplifier", AMPLIFIER);
314 RET_STRING ("Compressor", COMPRESSOR);
315 RET_STRING ("Envelope", ENVELOPE);
316 RET_STRING ("Expander", EXPANDER);
317 RET_STRING ("Gate", GATE);
318 RET_STRING ("Limiter", LIMITER);
319 RET_STRING ("Filter", FILTER);
320 RET_STRING ("Allpass", ALLPASS_FILTER);
321 RET_STRING ("Bandpass", BANDPASS_FILTER);
322 RET_STRING ("Comb", COMB_FILTER);
323 RET_STRING ("Equaliser", EQ);
324 RET_STRING ("Equalizer", EQ);
325 RET_STRING ("Multiband", MULTI_EQ);
326 RET_STRING ("Para", PARA_EQ);
327 RET_STRING ("Highpass", HIGHPASS_FILTER);
328 RET_STRING ("Lowpass", LOWPASS_FILTER);
329 RET_STRING ("Generator", GENERATOR);
330 RET_STRING ("Constant", CONSTANT);
331 RET_STRING ("Instrument", INSTRUMENT);
332 RET_STRING ("Oscillator", OSCILLATOR);
333 RET_STRING ("MIDI", MIDI);
334 RET_STRING ("Modulator", MODULATOR);
335 RET_STRING ("Chorus", CHORUS);
336 RET_STRING ("Flanger", FLANGER);
337 RET_STRING ("Phaser", PHASER);
338 RET_STRING ("Simulator", SIMULATOR);
339 RET_STRING ("SimulatorReverb", SIMULATOR_REVERB);
340 RET_STRING ("Spatial", SPATIAL);
341 RET_STRING ("Spectral", SPECTRAL);
342 RET_STRING ("Pitch", PITCH);
343 RET_STRING ("Utility", UTILITY);
344 RET_STRING ("Analyser", ANALYZER);
345 RET_STRING ("Analyzer", ANALYZER);
346 RET_STRING ("Converter", CONVERTER);
347 RET_STRING ("Function", FUNCTION);
348 RET_STRING ("Mixer", MIXER);
349
350 #undef RET_STRING
351
352 return g_strdup ("Other");
353 }
354
355 /**
356 * Returns if the given plugin identifier can be
357 * dropped in a slot of the given type.
358 */
359 bool
plugin_descriptor_is_valid_for_slot_type(PluginDescriptor * self,int slot_type,int track_type)360 plugin_descriptor_is_valid_for_slot_type (
361 PluginDescriptor * self,
362 int slot_type,
363 int track_type)
364 {
365 switch (slot_type)
366 {
367 case PLUGIN_SLOT_INSERT:
368 if (track_type == TRACK_TYPE_MIDI)
369 {
370 return self->num_midi_outs > 0;
371 }
372 else
373 {
374 return self->num_audio_outs > 0;
375 }
376 case PLUGIN_SLOT_MIDI_FX:
377 return self->num_midi_outs > 0;
378 break;
379 case PLUGIN_SLOT_INSTRUMENT:
380 return
381 track_type == TRACK_TYPE_INSTRUMENT &&
382 plugin_descriptor_is_instrument (self);
383 default:
384 break;
385 }
386
387 g_return_val_if_reached (false);
388 }
389
390 /**
391 * Returns whether the two descriptors describe
392 * the same plugin, ignoring irrelevant fields.
393 */
394 bool
plugin_descriptor_is_same_plugin(const PluginDescriptor * a,const PluginDescriptor * b)395 plugin_descriptor_is_same_plugin (
396 const PluginDescriptor * a,
397 const PluginDescriptor * b)
398 {
399 return a->arch == b->arch &&
400 a->protocol == b->protocol &&
401 string_is_equal (a->path, b->path) &&
402 string_is_equal (a->uri, b->uri) &&
403 a->unique_id == b->unique_id &&
404 a->ghash == b->ghash;
405 }
406
407 /**
408 * Returns if the Plugin has a supported custom
409 * UI.
410 */
411 bool
plugin_descriptor_has_custom_ui(const PluginDescriptor * self)412 plugin_descriptor_has_custom_ui (
413 const PluginDescriptor * self)
414 {
415 switch (self->protocol)
416 {
417 case PROT_LV2:
418 {
419 return
420 lv2_plugin_pick_most_preferable_ui (
421 self->uri, NULL, NULL, true);
422 }
423 break;
424 case PROT_VST:
425 case PROT_VST3:
426 case PROT_AU:
427 #ifdef HAVE_CARLA
428 return
429 carla_native_plugin_has_custom_ui (self);
430 #else
431 return false;
432 #endif
433 break;
434 default:
435 return false;
436 break;
437 }
438
439 g_return_val_if_reached (false);
440 }
441
442 /**
443 * Returns the minimum bridge mode required for this
444 * plugin.
445 */
446 CarlaBridgeMode
plugin_descriptor_get_min_bridge_mode(const PluginDescriptor * self)447 plugin_descriptor_get_min_bridge_mode (
448 const PluginDescriptor * self)
449 {
450 if (self->protocol == PROT_LV2)
451 {
452 /* TODO if the UI and DSP binary is the same
453 * file, bridge the whole plugin */
454 LilvNode * lv2_uri =
455 lilv_new_uri (LILV_WORLD, self->uri);
456 const LilvPlugin * lilv_plugin =
457 lilv_plugins_get_by_uri (
458 LILV_PLUGINS, lv2_uri);
459 lilv_node_free (lv2_uri);
460 LilvUIs * uis =
461 lilv_plugin_get_uis (lilv_plugin);
462 const LilvUI * picked_ui;
463 const LilvNode * picked_ui_type;
464 bool needs_bridging =
465 lv2_plugin_pick_ui (
466 uis, LV2_PLUGIN_UI_FOR_BRIDGING,
467 &picked_ui, &picked_ui_type);
468 if (needs_bridging)
469 {
470 const LilvNode * ui_uri =
471 lilv_ui_get_uri (picked_ui);
472 LilvNodes * ui_required_features =
473 lilv_world_find_nodes (
474 LILV_WORLD, ui_uri,
475 PM_GET_NODE (
476 LV2_CORE__requiredFeature),
477 NULL);
478 if (lilv_nodes_contains (
479 ui_required_features,
480 PM_GET_NODE (LV2_DATA_ACCESS_URI)) ||
481 lilv_nodes_contains (
482 ui_required_features,
483 PM_GET_NODE (
484 LV2_INSTANCE_ACCESS_URI)) ||
485 lilv_node_equals (
486 picked_ui_type,
487 PM_GET_NODE (LV2_UI__Qt4UI)) ||
488 lilv_node_equals (
489 picked_ui_type,
490 PM_GET_NODE (LV2_UI__Qt5UI)) ||
491 lilv_node_equals (
492 picked_ui_type,
493 PM_GET_NODE (LV2_UI__GtkUI)) ||
494 lilv_node_equals (
495 picked_ui_type,
496 PM_GET_NODE (LV2_UI__Gtk3UI)))
497 {
498 return CARLA_BRIDGE_FULL;
499 }
500 else
501 {
502 return CARLA_BRIDGE_UI;
503 }
504 lilv_nodes_free (ui_required_features);
505 }
506 else /* does not need bridging */
507 {
508 return CARLA_BRIDGE_NONE;
509 }
510 lilv_uis_free (uis);
511 }
512 else if (self->arch == ARCH_32)
513 {
514 return CARLA_BRIDGE_FULL;
515 }
516 else
517 {
518 return CARLA_BRIDGE_NONE;
519 }
520
521 g_return_val_if_reached (CARLA_BRIDGE_NONE);
522 }
523
524 void
plugin_descriptor_free(PluginDescriptor * self)525 plugin_descriptor_free (
526 PluginDescriptor * self)
527 {
528 g_free_and_null (self->author);
529 g_free_and_null (self->name);
530 g_free_and_null (self->website);
531 g_free_and_null (self->category_str);
532 g_free_and_null (self->path);
533 g_free_and_null (self->uri);
534
535 object_zero_and_free (self);
536 }
537