Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

577 lines
20KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaUtils.h"
  18. #include "CarlaNative.h"
  19. #include "CarlaString.hpp"
  20. #include "CarlaLv2Utils.hpp"
  21. #ifdef DEBUG
  22. # include "CarlaBackendUtils.hpp"
  23. #endif
  24. #include "water/containers/Array.h"
  25. #include "water/files/File.h"
  26. namespace CB = CarlaBackend;
  27. using water::Array;
  28. using water::File;
  29. using water::String;
  30. using water::StringArray;
  31. static const char* const gNullCharPtr = "";
  32. static bool isCachedPluginType(const CB::PluginType ptype)
  33. {
  34. switch (ptype)
  35. {
  36. case CB::PLUGIN_INTERNAL:
  37. case CB::PLUGIN_LV2:
  38. case CB::PLUGIN_SFZ:
  39. return true;
  40. default:
  41. return false;
  42. }
  43. }
  44. // -------------------------------------------------------------------------------------------------------------------
  45. _CarlaCachedPluginInfo::_CarlaCachedPluginInfo() noexcept
  46. : valid(false),
  47. category(CB::PLUGIN_CATEGORY_NONE),
  48. hints(0x0),
  49. audioIns(0),
  50. audioOuts(0),
  51. midiIns(0),
  52. midiOuts(0),
  53. parameterIns(0),
  54. parameterOuts(0),
  55. name(gNullCharPtr),
  56. label(gNullCharPtr),
  57. maker(gNullCharPtr),
  58. copyright(gNullCharPtr) {}
  59. // -------------------------------------------------------------------------------------------------------------------
  60. static Array<File> gSFZs;
  61. void findSFZsIfNeeded(const char* const sfzPaths)
  62. {
  63. CARLA_SAFE_ASSERT_RETURN(sfzPaths != nullptr && sfzPaths[0] != '\0',);
  64. static bool needsInit = true;
  65. if (! needsInit)
  66. return;
  67. needsInit = false;
  68. const StringArray splitPaths(StringArray::fromTokens(sfzPaths, CARLA_OS_SPLIT_STR, ""));
  69. for (String *it = splitPaths.begin(), *end = splitPaths.end(); it != end; ++it)
  70. {
  71. Array<File> results;
  72. if (File(*it).findChildFiles(results, File::findFiles|File::ignoreHiddenFiles, true, "*.sfz") > 0)
  73. gSFZs.addArray(results);
  74. }
  75. }
  76. // -------------------------------------------------------------------------------------------------------------------
  77. const CarlaCachedPluginInfo* get_cached_plugin_internal(const NativePluginDescriptor& desc)
  78. {
  79. static CarlaCachedPluginInfo info;
  80. info.category = static_cast<CB::PluginCategory>(desc.category);
  81. info.hints = 0x0;
  82. if (desc.hints & NATIVE_PLUGIN_IS_RTSAFE)
  83. info.hints |= CB::PLUGIN_IS_RTSAFE;
  84. if (desc.hints & NATIVE_PLUGIN_IS_SYNTH)
  85. info.hints |= CB::PLUGIN_IS_SYNTH;
  86. if (desc.hints & NATIVE_PLUGIN_HAS_UI)
  87. info.hints |= CB::PLUGIN_HAS_CUSTOM_UI;
  88. if (desc.hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  89. info.hints |= CB::PLUGIN_NEEDS_FIXED_BUFFERS;
  90. if (desc.hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  91. info.hints |= CB::PLUGIN_NEEDS_UI_MAIN_THREAD;
  92. if (desc.hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  93. info.hints |= CB::PLUGIN_USES_MULTI_PROGS;
  94. info.valid = true;
  95. info.audioIns = desc.audioIns;
  96. info.audioOuts = desc.audioOuts;
  97. info.midiIns = desc.midiIns;
  98. info.midiOuts = desc.midiOuts;
  99. info.parameterIns = desc.paramIns;
  100. info.parameterOuts = desc.paramOuts;
  101. info.name = desc.name;
  102. info.label = desc.label;
  103. info.maker = desc.maker;
  104. info.copyright = desc.copyright;
  105. return &info;
  106. }
  107. const CarlaCachedPluginInfo* get_cached_plugin_lv2(Lv2WorldClass& lv2World, Lilv::Plugin& lilvPlugin)
  108. {
  109. static CarlaCachedPluginInfo info;
  110. info.valid = false;
  111. bool supported = true;
  112. // ----------------------------------------------------------------------------------------------------------------
  113. // text data
  114. {
  115. static CarlaString suri, sname, smaker, slicense;
  116. suri.clear(); sname.clear(); smaker.clear(); slicense.clear();
  117. suri = lilvPlugin.get_uri().as_uri();
  118. if (LilvNode* const nameNode = lilv_plugin_get_name(lilvPlugin.me))
  119. {
  120. if (const char* const name = lilv_node_as_string(nameNode))
  121. sname = name;
  122. lilv_node_free(nameNode);
  123. }
  124. if (const char* const author = lilvPlugin.get_author_name().as_string())
  125. smaker = author;
  126. Lilv::Nodes licenseNodes(lilvPlugin.get_value(lv2World.doap_license));
  127. if (licenseNodes.size() > 0)
  128. {
  129. if (const char* const license = licenseNodes.get_first().as_string())
  130. slicense = license;
  131. }
  132. lilv_nodes_free(const_cast<LilvNodes*>(licenseNodes.me));
  133. info.name = sname.buffer();
  134. info.label = suri.buffer();
  135. info.maker = smaker.buffer();
  136. info.copyright = slicense.buffer();
  137. }
  138. // ----------------------------------------------------------------------------------------------------------------
  139. // features
  140. info.hints = 0x0;
  141. if (lilvPlugin.get_uis().size() > 0)
  142. info.hints |= CB::PLUGIN_HAS_CUSTOM_UI;
  143. {
  144. Lilv::Nodes lilvRequiredFeatureNodes(lilvPlugin.get_required_features());
  145. LILV_FOREACH(nodes, it, lilvRequiredFeatureNodes)
  146. {
  147. Lilv::Node lilvFeatureNode(lilvRequiredFeatureNodes.get(it));
  148. const char* const featureURI(lilvFeatureNode.as_uri());
  149. CARLA_SAFE_ASSERT_CONTINUE(featureURI != nullptr);
  150. if (! is_lv2_feature_supported(featureURI))
  151. {
  152. if (std::strcmp(featureURI, LV2_DATA_ACCESS_URI) == 0
  153. || std::strcmp(featureURI, LV2_INSTANCE_ACCESS_URI) == 0)
  154. {
  155. // we give a warning about this below
  156. continue;
  157. }
  158. supported = false;
  159. carla_stderr("LV2 plugin '%s' requires unsupported feature '%s'", info.label, featureURI);
  160. }
  161. }
  162. lilv_nodes_free(const_cast<LilvNodes*>(lilvRequiredFeatureNodes.me));
  163. }
  164. {
  165. Lilv::Nodes lilvSupportedFeatureNodes(lilvPlugin.get_supported_features());
  166. LILV_FOREACH(nodes, it, lilvSupportedFeatureNodes)
  167. {
  168. Lilv::Node lilvFeatureNode(lilvSupportedFeatureNodes.get(it));
  169. const char* const featureURI(lilvFeatureNode.as_uri());
  170. CARLA_SAFE_ASSERT_CONTINUE(featureURI != nullptr);
  171. if (std::strcmp(featureURI, LV2_CORE__hardRTCapable) == 0)
  172. {
  173. info.hints |= CB::PLUGIN_IS_RTSAFE;
  174. }
  175. else if (std::strcmp(featureURI, LV2_DATA_ACCESS_URI) == 0
  176. || std::strcmp(featureURI, LV2_INSTANCE_ACCESS_URI) == 0)
  177. {
  178. carla_stderr("LV2 plugin '%s' DSP wants UI feature '%s', ignoring this", info.label, featureURI);
  179. }
  180. }
  181. lilv_nodes_free(const_cast<LilvNodes*>(lilvSupportedFeatureNodes.me));
  182. }
  183. // ----------------------------------------------------------------------------------------------------------------
  184. // category
  185. info.category = CB::PLUGIN_CATEGORY_NONE;
  186. {
  187. Lilv::Nodes typeNodes(lilvPlugin.get_value(lv2World.rdf_type));
  188. if (typeNodes.size() > 0)
  189. {
  190. if (typeNodes.contains(lv2World.class_allpass))
  191. info.category = CB::PLUGIN_CATEGORY_FILTER;
  192. if (typeNodes.contains(lv2World.class_amplifier))
  193. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  194. if (typeNodes.contains(lv2World.class_analyzer))
  195. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  196. if (typeNodes.contains(lv2World.class_bandpass))
  197. info.category = CB::PLUGIN_CATEGORY_FILTER;
  198. if (typeNodes.contains(lv2World.class_chorus))
  199. info.category = CB::PLUGIN_CATEGORY_MODULATOR;
  200. if (typeNodes.contains(lv2World.class_comb))
  201. info.category = CB::PLUGIN_CATEGORY_FILTER;
  202. if (typeNodes.contains(lv2World.class_compressor))
  203. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  204. if (typeNodes.contains(lv2World.class_constant))
  205. info.category = CB::PLUGIN_CATEGORY_OTHER;
  206. if (typeNodes.contains(lv2World.class_converter))
  207. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  208. if (typeNodes.contains(lv2World.class_delay))
  209. info.category = CB::PLUGIN_CATEGORY_DELAY;
  210. if (typeNodes.contains(lv2World.class_distortion))
  211. info.category = CB::PLUGIN_CATEGORY_DISTORTION;
  212. if (typeNodes.contains(lv2World.class_dynamics))
  213. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  214. if (typeNodes.contains(lv2World.class_eq))
  215. info.category = CB::PLUGIN_CATEGORY_EQ;
  216. if (typeNodes.contains(lv2World.class_envelope))
  217. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  218. if (typeNodes.contains(lv2World.class_expander))
  219. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  220. if (typeNodes.contains(lv2World.class_filter))
  221. info.category = CB::PLUGIN_CATEGORY_FILTER;
  222. if (typeNodes.contains(lv2World.class_flanger))
  223. info.category = CB::PLUGIN_CATEGORY_MODULATOR;
  224. if (typeNodes.contains(lv2World.class_function))
  225. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  226. if (typeNodes.contains(lv2World.class_gate))
  227. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  228. if (typeNodes.contains(lv2World.class_generator))
  229. info.category = CB::PLUGIN_CATEGORY_OTHER;
  230. if (typeNodes.contains(lv2World.class_highpass))
  231. info.category = CB::PLUGIN_CATEGORY_FILTER;
  232. if (typeNodes.contains(lv2World.class_limiter))
  233. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  234. if (typeNodes.contains(lv2World.class_lowpass))
  235. info.category = CB::PLUGIN_CATEGORY_FILTER;
  236. if (typeNodes.contains(lv2World.class_mixer))
  237. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  238. if (typeNodes.contains(lv2World.class_modulator))
  239. info.category = CB::PLUGIN_CATEGORY_MODULATOR;
  240. if (typeNodes.contains(lv2World.class_multiEQ))
  241. info.category = CB::PLUGIN_CATEGORY_EQ;
  242. if (typeNodes.contains(lv2World.class_oscillator))
  243. info.category = CB::PLUGIN_CATEGORY_OTHER;
  244. if (typeNodes.contains(lv2World.class_paraEQ))
  245. info.category = CB::PLUGIN_CATEGORY_EQ;
  246. if (typeNodes.contains(lv2World.class_phaser))
  247. info.category = CB::PLUGIN_CATEGORY_MODULATOR;
  248. if (typeNodes.contains(lv2World.class_pitch))
  249. info.category = CB::PLUGIN_CATEGORY_OTHER;
  250. if (typeNodes.contains(lv2World.class_reverb))
  251. info.category = CB::PLUGIN_CATEGORY_DELAY;
  252. if (typeNodes.contains(lv2World.class_simulator))
  253. info.category = CB::PLUGIN_CATEGORY_OTHER;
  254. if (typeNodes.contains(lv2World.class_spatial))
  255. info.category = CB::PLUGIN_CATEGORY_OTHER;
  256. if (typeNodes.contains(lv2World.class_spectral))
  257. info.category = CB::PLUGIN_CATEGORY_OTHER;
  258. if (typeNodes.contains(lv2World.class_utility))
  259. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  260. if (typeNodes.contains(lv2World.class_waveshaper))
  261. info.category = CB::PLUGIN_CATEGORY_DISTORTION;
  262. if (typeNodes.contains(lv2World.class_instrument))
  263. {
  264. info.category = CB::PLUGIN_CATEGORY_SYNTH;
  265. info.hints |= CB::PLUGIN_IS_SYNTH;
  266. }
  267. }
  268. lilv_nodes_free(const_cast<LilvNodes*>(typeNodes.me));
  269. }
  270. // ----------------------------------------------------------------------------------------------------------------
  271. // number data
  272. for (uint i=0, count=lilvPlugin.get_num_ports(); i<count; ++i)
  273. {
  274. Lilv::Port lilvPort(lilvPlugin.get_port_by_index(i));
  275. bool isInput;
  276. /**/ if (lilvPort.is_a(lv2World.port_input))
  277. {
  278. isInput = true;
  279. }
  280. else if (lilvPort.is_a(lv2World.port_output))
  281. {
  282. isInput = false;
  283. }
  284. else
  285. {
  286. const LilvNode* const symbolNode = lilvPort.get_symbol();
  287. CARLA_SAFE_ASSERT_CONTINUE(symbolNode != nullptr && lilv_node_is_string(symbolNode));
  288. const char* const symbol = lilv_node_as_string(symbolNode);
  289. CARLA_SAFE_ASSERT_CONTINUE(symbol != nullptr);
  290. carla_stderr("LV2 plugin '%s' port '%s' is neither input or output", info.label, symbol);
  291. continue;
  292. }
  293. /**/ if (lilvPort.is_a(lv2World.port_control))
  294. {
  295. // skip some control ports
  296. if (lilvPort.has_property(lv2World.reportsLatency))
  297. continue;
  298. if (LilvNode* const designationNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.designation.me))
  299. {
  300. bool skip = false;
  301. if (const char* const designation = lilv_node_as_string(designationNode))
  302. {
  303. /**/ if (std::strcmp(designation, LV2_CORE__control) == 0)
  304. skip = true;
  305. else if (std::strcmp(designation, LV2_CORE__freeWheeling) == 0)
  306. skip = true;
  307. else if (std::strcmp(designation, LV2_CORE__latency) == 0)
  308. skip = true;
  309. else if (std::strcmp(designation, LV2_PARAMETERS__sampleRate) == 0)
  310. skip = true;
  311. else if (std::strcmp(designation, LV2_TIME__bar) == 0)
  312. skip = true;
  313. else if (std::strcmp(designation, LV2_TIME__barBeat) == 0)
  314. skip = true;
  315. else if (std::strcmp(designation, LV2_TIME__beat) == 0)
  316. skip = true;
  317. else if (std::strcmp(designation, LV2_TIME__beatUnit) == 0)
  318. skip = true;
  319. else if (std::strcmp(designation, LV2_TIME__beatsPerBar) == 0)
  320. skip = true;
  321. else if (std::strcmp(designation, LV2_TIME__beatsPerMinute) == 0)
  322. skip = true;
  323. else if (std::strcmp(designation, LV2_TIME__frame) == 0)
  324. skip = true;
  325. else if (std::strcmp(designation, LV2_TIME__framesPerSecond) == 0)
  326. skip = true;
  327. else if (std::strcmp(designation, LV2_TIME__speed) == 0)
  328. skip = true;
  329. else if (std::strcmp(designation, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  330. skip = true;
  331. }
  332. lilv_node_free(designationNode);
  333. if (skip)
  334. continue;
  335. }
  336. if (isInput)
  337. ++(info.parameterIns);
  338. else
  339. ++(info.parameterOuts);
  340. }
  341. else if (lilvPort.is_a(lv2World.port_audio))
  342. {
  343. if (isInput)
  344. ++(info.audioIns);
  345. else
  346. ++(info.audioOuts);
  347. }
  348. else if (lilvPort.is_a(lv2World.port_cv))
  349. {
  350. }
  351. else if (lilvPort.is_a(lv2World.port_atom))
  352. {
  353. Lilv::Nodes supportNodes(lilvPort.get_value(lv2World.atom_supports));
  354. for (LilvIter *it = lilv_nodes_begin(supportNodes.me); ! lilv_nodes_is_end(supportNodes.me, it); it = lilv_nodes_next(supportNodes.me, it))
  355. {
  356. const Lilv::Node node(lilv_nodes_get(supportNodes.me, it));
  357. CARLA_SAFE_ASSERT_CONTINUE(node.is_uri());
  358. if (node.equals(lv2World.midi_event))
  359. {
  360. if (isInput)
  361. ++(info.midiIns);
  362. else
  363. ++(info.midiOuts);
  364. }
  365. }
  366. lilv_nodes_free(const_cast<LilvNodes*>(supportNodes.me));
  367. }
  368. else if (lilvPort.is_a(lv2World.port_event))
  369. {
  370. if (lilvPort.supports_event(lv2World.midi_event))
  371. {
  372. if (isInput)
  373. ++(info.midiIns);
  374. else
  375. ++(info.midiOuts);
  376. }
  377. }
  378. else if (lilvPort.is_a(lv2World.port_midi))
  379. {
  380. if (isInput)
  381. ++(info.midiIns);
  382. else
  383. ++(info.midiOuts);
  384. }
  385. else
  386. {
  387. const LilvNode* const symbolNode = lilvPort.get_symbol();
  388. CARLA_SAFE_ASSERT_CONTINUE(symbolNode != nullptr && lilv_node_is_string(symbolNode));
  389. const char* const symbol = lilv_node_as_string(symbolNode);
  390. CARLA_SAFE_ASSERT_CONTINUE(symbol != nullptr);
  391. supported = false;
  392. carla_stderr("LV2 plugin '%s' port '%s' is required but has unsupported type", info.label, symbol);
  393. }
  394. }
  395. if (supported)
  396. info.valid = true;
  397. return &info;
  398. }
  399. const CarlaCachedPluginInfo* get_cached_plugin_sfz(const File file)
  400. {
  401. static CarlaCachedPluginInfo info;
  402. static CarlaString name, filename;
  403. name = file.getFileNameWithoutExtension().toRawUTF8();
  404. name.replace('_',' ');
  405. filename = file.getFullPathName().toRawUTF8();
  406. info.category = CB::PLUGIN_CATEGORY_SYNTH;
  407. info.hints = CB::PLUGIN_IS_SYNTH;
  408. // CB::PLUGIN_IS_RTSAFE
  409. info.valid = true;
  410. info.audioIns = 0;
  411. info.audioOuts = 2;
  412. info.midiIns = 1;
  413. info.midiOuts = 0;
  414. info.parameterIns = 0;
  415. info.parameterOuts = 1;
  416. info.name = name.buffer();
  417. info.label = filename.buffer();
  418. info.maker = gNullCharPtr;
  419. info.copyright = gNullCharPtr;
  420. return &info;
  421. }
  422. // -------------------------------------------------------------------------------------------------------------------
  423. uint carla_get_cached_plugin_count(CB::PluginType ptype, const char* pluginPath)
  424. {
  425. CARLA_SAFE_ASSERT_RETURN(isCachedPluginType(ptype), 0);
  426. carla_debug("carla_get_cached_plugin_count(%i:%s, %s)", ptype, CB::PluginType2Str(ptype), pluginPath);
  427. switch (ptype)
  428. {
  429. case CB::PLUGIN_INTERNAL: {
  430. uint32_t count = 0;
  431. carla_get_native_plugins_data(&count);
  432. return count;
  433. }
  434. case CB::PLUGIN_LV2: {
  435. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  436. lv2World.initIfNeeded(pluginPath);
  437. return lv2World.getPluginCount();
  438. }
  439. case CB::PLUGIN_SFZ: {
  440. findSFZsIfNeeded(pluginPath);
  441. return gSFZs.size();
  442. }
  443. default:
  444. return 0;
  445. }
  446. }
  447. const CarlaCachedPluginInfo* carla_get_cached_plugin_info(CB::PluginType ptype, uint index)
  448. {
  449. carla_debug("carla_get_cached_plugin_info(%i:%s, %i)", ptype, CB::PluginType2Str(ptype), index);
  450. switch (ptype)
  451. {
  452. case CB::PLUGIN_INTERNAL: {
  453. uint32_t count = 0;
  454. const NativePluginDescriptor* const descs(carla_get_native_plugins_data(&count));
  455. CARLA_SAFE_ASSERT_BREAK(index < count);
  456. CARLA_SAFE_ASSERT_BREAK(descs != nullptr);
  457. const NativePluginDescriptor& desc(descs[index]);
  458. return get_cached_plugin_internal(desc);
  459. }
  460. case CB::PLUGIN_LV2: {
  461. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  462. const LilvPlugin* const cPlugin(lv2World.getPluginFromIndex(index));
  463. CARLA_SAFE_ASSERT_BREAK(cPlugin != nullptr);
  464. Lilv::Plugin lilvPlugin(cPlugin);
  465. CARLA_SAFE_ASSERT_BREAK(lilvPlugin.get_uri().is_uri());
  466. return get_cached_plugin_lv2(lv2World, lilvPlugin);
  467. }
  468. case CB::PLUGIN_SFZ: {
  469. CARLA_SAFE_ASSERT_BREAK(index < static_cast<uint>(gSFZs.size()));
  470. return get_cached_plugin_sfz(gSFZs.getUnchecked(index));
  471. }
  472. default:
  473. break;
  474. }
  475. static CarlaCachedPluginInfo info;
  476. return &info;
  477. }
  478. // -------------------------------------------------------------------------------------------------------------------
  479. #include "../native-plugins/_data.all.cpp"
  480. // -------------------------------------------------------------------------------------------------------------------