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.

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