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.

583 lines
20KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 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 this first to ignore documentation warnings
  18. #include "CarlaLv2Utils.hpp"
  19. #define CARLA_NATIVE_PLUGIN_LV2
  20. #include "carla-native-base.cpp"
  21. #include "juce_core.h"
  22. #include "lv2/atom.h"
  23. #include "lv2/buf-size.h"
  24. #include "lv2/instance-access.h"
  25. #include "lv2/midi.h"
  26. #include "lv2/options.h"
  27. #include "lv2/port-props.h"
  28. #include "lv2/state.h"
  29. #include "lv2/time.h"
  30. #include "lv2/ui.h"
  31. #include "lv2/units.h"
  32. #include "lv2/urid.h"
  33. #include "lv2/lv2_external_ui.h"
  34. #include "lv2/lv2_programs.h"
  35. #include <fstream>
  36. #if defined(CARLA_OS_WIN)
  37. # define PLUGIN_EXT ".dll"
  38. #elif defined(JUCE_MAC)
  39. # define PLUGIN_EXT ".dylib"
  40. #else
  41. # define PLUGIN_EXT ".so"
  42. #endif
  43. using juce::String;
  44. using juce::StringArray;
  45. using juce::juce_wchar;
  46. // -----------------------------------------------------------------------
  47. // Converts a parameter name to an LV2 compatible symbol
  48. static StringArray gUsedSymbols;
  49. static const String nameToSymbol(const String& name, const uint32_t portIndex)
  50. {
  51. String symbol, trimmedName = name.trim().toLowerCase();
  52. if (trimmedName.isEmpty())
  53. {
  54. symbol += "lv2_port_";
  55. symbol += String(portIndex+1);
  56. }
  57. else
  58. {
  59. for (int i=0; i < trimmedName.length(); ++i)
  60. {
  61. const juce_wchar c = trimmedName[i];
  62. if (i == 0 && std::isdigit(c))
  63. symbol += "_";
  64. else if (std::isalpha(c) || std::isdigit(c))
  65. symbol += c;
  66. else
  67. symbol += "_";
  68. }
  69. }
  70. // Do not allow identical symbols
  71. if (gUsedSymbols.contains(symbol))
  72. {
  73. int offset = 2;
  74. String offsetStr = "_2";
  75. symbol += offsetStr;
  76. while (gUsedSymbols.contains(symbol))
  77. {
  78. offset += 1;
  79. String newOffsetStr = "_" + String(offset);
  80. symbol = symbol.replace(offsetStr, newOffsetStr);
  81. offsetStr = newOffsetStr;
  82. }
  83. }
  84. gUsedSymbols.add(symbol);
  85. return symbol;
  86. }
  87. // -----------------------------------------------------------------------
  88. static void writeManifestFile(PluginListManager& plm)
  89. {
  90. String text;
  91. // -------------------------------------------------------------------
  92. // Header
  93. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
  94. text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  95. text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
  96. text += "\n";
  97. // -------------------------------------------------------------------
  98. // Plugins
  99. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  100. {
  101. const NativePluginDescriptor* const& pluginDesc(it.getValue());
  102. const String label(pluginDesc->label);
  103. text += "<http://kxstudio.sf.net/carla/plugins/" + label + ">\n";
  104. text += " a lv2:Plugin ;\n";
  105. text += " lv2:binary <carla-native" PLUGIN_EXT "> ;\n";
  106. text += " rdfs:seeAlso <" + label + ".ttl> .\n";
  107. text += "\n";
  108. }
  109. // -------------------------------------------------------------------
  110. // UI
  111. text += "<http://kxstudio.sf.net/carla/ui>\n";
  112. text += " a <" LV2_EXTERNAL_UI__Widget "> ;\n";
  113. text += " ui:binary <carla-native" PLUGIN_EXT "> ;\n";
  114. text += " lv2:extensionData ui:idleInterface ,\n";
  115. text += " ui:showInterface ,\n";
  116. text += " <" LV2_PROGRAMS__UIInterface "> ;\n";
  117. text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> .\n";
  118. // -------------------------------------------------------------------
  119. // Write file now
  120. std::fstream manifest("carla-native.lv2/manifest.ttl", std::ios::out);
  121. manifest << text.toRawUTF8();
  122. manifest.close();
  123. }
  124. // -----------------------------------------------------------------------
  125. static uint32_t host_getBufferSize(NativeHostHandle) { return 512; }
  126. static double host_getSampleRate(NativeHostHandle) { return 44100.0; }
  127. static bool host_isOffline(NativeHostHandle) { return true; }
  128. static intptr_t host_dispatcher(NativeHostHandle, NativeHostDispatcherOpcode, int32_t, intptr_t, void*, float) { return 0; }
  129. static void writePluginFile(const NativePluginDescriptor* const pluginDesc)
  130. {
  131. const String pluginLabel(pluginDesc->label);
  132. const String pluginFile("carla-native.lv2/" + pluginLabel + ".ttl");
  133. uint32_t portIndex = 0;
  134. String text;
  135. gUsedSymbols.clear();
  136. carla_stdout("Generating data for %s...", pluginDesc->name);
  137. // -------------------------------------------------------------------
  138. // Init plugin
  139. NativeHostDescriptor hostDesc;
  140. hostDesc.handle = nullptr;
  141. hostDesc.resourceDir = "";
  142. hostDesc.uiName = "";
  143. hostDesc.get_buffer_size = host_getBufferSize;
  144. hostDesc.get_sample_rate = host_getSampleRate;
  145. hostDesc.is_offline = host_isOffline;
  146. hostDesc.get_time_info = nullptr;
  147. hostDesc.write_midi_event = nullptr;
  148. hostDesc.ui_parameter_changed = nullptr;
  149. hostDesc.ui_midi_program_changed = nullptr;
  150. hostDesc.ui_custom_data_changed = nullptr;
  151. hostDesc.ui_closed = nullptr;
  152. hostDesc.ui_open_file = nullptr;
  153. hostDesc.ui_save_file = nullptr;
  154. hostDesc.dispatcher = host_dispatcher;
  155. NativePluginHandle pluginHandle = nullptr;
  156. if (! pluginLabel.startsWithIgnoreCase("carla-"))
  157. {
  158. pluginHandle = pluginDesc->instantiate(&hostDesc);
  159. CARLA_SAFE_ASSERT_RETURN(pluginHandle != nullptr,)
  160. }
  161. // -------------------------------------------------------------------
  162. // Header
  163. text += "@prefix atom: <" LV2_ATOM_PREFIX "> .\n";
  164. text += "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  165. text += "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  166. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
  167. text += "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
  168. text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  169. text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
  170. text += "@prefix unit: <" LV2_UNITS_PREFIX "> .\n";
  171. text += "\n";
  172. // -------------------------------------------------------------------
  173. // Plugin URI
  174. text += "<http://kxstudio.sf.net/carla/plugins/" + pluginLabel + ">\n";
  175. // -------------------------------------------------------------------
  176. // Category
  177. switch (pluginDesc->category)
  178. {
  179. case PLUGIN_CATEGORY_SYNTH:
  180. text += " a lv2:InstrumentPlugin, lv2:Plugin ;\n";
  181. break;
  182. case PLUGIN_CATEGORY_DELAY:
  183. text += " a lv2:DelayPlugin, lv2:Plugin ;\n";
  184. break;
  185. case PLUGIN_CATEGORY_EQ:
  186. text += " a lv2:EQPlugin, lv2:Plugin ;\n";
  187. break;
  188. case PLUGIN_CATEGORY_FILTER:
  189. text += " a lv2:FilterPlugin, lv2:Plugin ;\n";
  190. break;
  191. case PLUGIN_CATEGORY_DYNAMICS:
  192. text += " a lv2:DynamicsPlugin, lv2:Plugin ;\n";
  193. break;
  194. case PLUGIN_CATEGORY_MODULATOR:
  195. text += " a lv2:ModulatorPlugin, lv2:Plugin ;\n";
  196. break;
  197. case PLUGIN_CATEGORY_UTILITY:
  198. text += " a lv2:UtilityPlugin, lv2:Plugin ;\n";
  199. break;
  200. default:
  201. text += " a lv2:Plugin ;\n";
  202. break;
  203. }
  204. text += "\n";
  205. // -------------------------------------------------------------------
  206. // Features
  207. if (pluginDesc->hints & PLUGIN_IS_RTSAFE)
  208. text += " lv2:optionalFeature <" LV2_CORE__hardRTCapable "> ;\n\n";
  209. text += " lv2:requiredFeature <" LV2_BUF_SIZE__boundedBlockLength "> ,\n";
  210. if (pluginDesc->hints & PLUGIN_NEEDS_FIXED_BUFFERS)
  211. text += " <" LV2_BUF_SIZE__fixedBlockLength "> ,\n";
  212. text += " <" LV2_OPTIONS__options "> ,\n";
  213. text += " <" LV2_URID__map "> ;\n";
  214. text += "\n";
  215. // -------------------------------------------------------------------
  216. // Extensions
  217. text += " lv2:extensionData <" LV2_OPTIONS__interface ">";
  218. if (pluginDesc->hints & PLUGIN_USES_STATE)
  219. {
  220. text += " ,\n";
  221. text += " <" LV2_STATE__interface ">";
  222. if (pluginDesc->category != PLUGIN_CATEGORY_SYNTH)
  223. {
  224. text += " ,\n";
  225. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  226. }
  227. else
  228. text += " ;\n";
  229. }
  230. else if (pluginDesc->category != PLUGIN_CATEGORY_SYNTH)
  231. {
  232. text += " ,\n";
  233. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  234. }
  235. else
  236. text += " ;\n";
  237. text += "\n";
  238. // -------------------------------------------------------------------
  239. // UIs
  240. if (pluginDesc->hints & PLUGIN_HAS_UI)
  241. {
  242. text += " ui:ui <http://kxstudio.sf.net/carla/ui> ;\n";
  243. text += "\n";
  244. }
  245. // -------------------------------------------------------------------
  246. // First MIDI/Time port
  247. if (pluginDesc->midiIns > 0 || (pluginDesc->hints & PLUGIN_USES_TIME) != 0)
  248. {
  249. text += " lv2:port [\n";
  250. text += " a lv2:InputPort, atom:AtomPort ;\n";
  251. text += " atom:bufferType atom:Sequence ;\n";
  252. if (pluginDesc->midiIns > 0 && (pluginDesc->hints & PLUGIN_USES_TIME) != 0)
  253. {
  254. text += " atom:supports <" LV2_MIDI__MidiEvent "> ,\n";
  255. text += " <" LV2_TIME__Position "> ;\n";
  256. }
  257. else if (pluginDesc->midiIns > 0)
  258. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  259. else
  260. text += " atom:supports <" LV2_TIME__Position "> ;\n";
  261. text += " lv2:designation lv2:control ;\n";
  262. text += " lv2:index " + String(portIndex++) + " ;\n";
  263. if (pluginDesc->midiIns > 1)
  264. {
  265. text += " lv2:symbol \"lv2_events_in_1\" ;\n";
  266. text += " lv2:name \"Events Input #1\" ;\n";
  267. }
  268. else
  269. {
  270. text += " lv2:symbol \"lv2_events_in\" ;\n";
  271. text += " lv2:name \"Events Input\" ;\n";
  272. }
  273. text += " ] ;\n\n";
  274. }
  275. // -------------------------------------------------------------------
  276. // MIDI inputs
  277. for (uint32_t i=1; i < pluginDesc->midiIns; ++i)
  278. {
  279. if (i == 1)
  280. text += " lv2:port [\n";
  281. text += " a lv2:InputPort, atom:AtomPort ;\n";
  282. text += " atom:bufferType atom:Sequence ;\n";
  283. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  284. text += " lv2:index " + String(portIndex++) + " ;\n";
  285. if (pluginDesc->midiIns > 1)
  286. {
  287. text += " lv2:symbol \"lv2_events_in_" + String(i+1) + "\" ;\n";
  288. text += " lv2:name \"Events Input #" + String(i+1) + "\" ;\n";
  289. }
  290. else
  291. {
  292. text += " lv2:symbol \"lv2_events_in\" ;\n";
  293. text += " lv2:name \"Events Input\" ;\n";
  294. }
  295. if (i+1 == pluginDesc->midiIns)
  296. text += " ] ;\n\n";
  297. else
  298. text += " ] , [\n";
  299. }
  300. // -------------------------------------------------------------------
  301. // MIDI outputs
  302. for (uint32_t i=0; i < pluginDesc->midiOuts; ++i)
  303. {
  304. if (i == 0)
  305. text += " lv2:port [\n";
  306. text += " a lv2:OutputPort, atom:AtomPort ;\n";
  307. text += " atom:bufferType atom:Sequence ;\n";
  308. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  309. text += " lv2:index " + String(portIndex++) + " ;\n";
  310. if (pluginDesc->midiOuts > 1)
  311. {
  312. text += " lv2:symbol \"lv2_midi_out_" + String(i+1) + "\" ;\n";
  313. text += " lv2:name \"MIDI Output #" + String(i+1) + "\" ;\n";
  314. }
  315. else
  316. {
  317. text += " lv2:symbol \"lv2_midi_out\" ;\n";
  318. text += " lv2:name \"MIDI Output\" ;\n";
  319. }
  320. if (i+1 == pluginDesc->midiOuts)
  321. text += " ] ;\n\n";
  322. else
  323. text += " ] , [\n";
  324. }
  325. // -------------------------------------------------------------------
  326. // Freewheel port
  327. text += " lv2:port [\n";
  328. text += " a lv2:InputPort, lv2:ControlPort ;\n";
  329. text += " lv2:index " + String(portIndex++) + " ;\n";
  330. text += " lv2:symbol \"lv2_freewheel\" ;\n";
  331. text += " lv2:name \"Freewheel\" ;\n";
  332. text += " lv2:default 0.0 ;\n";
  333. text += " lv2:minimum 0.0 ;\n";
  334. text += " lv2:maximum 1.0 ;\n";
  335. text += " lv2:designation <" LV2_CORE__freeWheeling "> ;\n";
  336. text += " lv2:portProperty lv2:toggled ;\n";
  337. text += " ] ;\n";
  338. text += "\n";
  339. // -------------------------------------------------------------------
  340. // Audio inputs
  341. for (uint32_t i=0; i < pluginDesc->audioIns; ++i)
  342. {
  343. if (i == 0)
  344. text += " lv2:port [\n";
  345. text += " a lv2:InputPort, lv2:AudioPort ;\n";
  346. text += " lv2:index " + String(portIndex++) + " ;\n";
  347. text += " lv2:symbol \"lv2_audio_in_" + String(i+1) + "\" ;\n";
  348. text += " lv2:name \"Audio Input " + String(i+1) + "\" ;\n";
  349. if (i+1 == pluginDesc->audioIns)
  350. text += " ] ;\n\n";
  351. else
  352. text += " ] , [\n";
  353. }
  354. // -------------------------------------------------------------------
  355. // Audio outputs
  356. for (uint32_t i=0; i < pluginDesc->audioOuts; ++i)
  357. {
  358. if (i == 0)
  359. text += " lv2:port [\n";
  360. text += " a lv2:OutputPort, lv2:AudioPort ;\n";
  361. text += " lv2:index " + String(portIndex++) + " ;\n";
  362. text += " lv2:symbol \"lv2_audio_out_" + String(i+1) + "\" ;\n";
  363. text += " lv2:name \"Audio Output " + String(i+1) + "\" ;\n";
  364. if (i+1 == pluginDesc->audioOuts)
  365. text += " ] ;\n\n";
  366. else
  367. text += " ] , [\n";
  368. }
  369. // -------------------------------------------------------------------
  370. // Parameters
  371. const uint32_t paramCount((pluginHandle != nullptr && pluginDesc->get_parameter_count != nullptr) ? pluginDesc->get_parameter_count(pluginHandle) : 0);
  372. if (paramCount > 0)
  373. {
  374. CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_info != nullptr,)
  375. CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_value != nullptr,)
  376. }
  377. for (uint32_t i=0; i < paramCount; ++i)
  378. {
  379. const NativeParameter* paramInfo(pluginDesc->get_parameter_info(pluginHandle, i));
  380. const String paramName(paramInfo->name != nullptr ? paramInfo->name : "");
  381. const String paramUnit(paramInfo->unit != nullptr ? paramInfo->unit : "");
  382. CARLA_SAFE_ASSERT_RETURN(paramInfo != nullptr,)
  383. if (i == 0)
  384. text += " lv2:port [\n";
  385. if (paramInfo->hints & PARAMETER_IS_OUTPUT)
  386. text += " a lv2:OutputPort, lv2:ControlPort ;\n";
  387. else
  388. text += " a lv2:InputPort, lv2:ControlPort ;\n";
  389. text += " lv2:index " + String(portIndex++) + " ;\n";
  390. text += " lv2:symbol \"" + nameToSymbol(paramName, i) + "\" ;\n";
  391. if (paramName.isNotEmpty())
  392. text += " lv2:name \"" + paramName + "\" ;\n";
  393. else
  394. text += " lv2:name \"Port " + String(i+1) + "\" ;\n";
  395. text += " lv2:default " + String::formatted("%f", paramInfo->ranges.def) + " ;\n";
  396. text += " lv2:minimum " + String::formatted("%f", paramInfo->ranges.min) + " ;\n";
  397. text += " lv2:maximum " + String::formatted("%f", paramInfo->ranges.max) + " ;\n";
  398. if (paramInfo->hints & PARAMETER_IS_ENABLED)
  399. {
  400. if ((paramInfo->hints & PARAMETER_IS_AUTOMABLE) == 0)
  401. text += " lv2:portProperty <" LV2_PORT_PROPS__expensive "> ;\n";
  402. if (paramInfo->hints & PARAMETER_IS_BOOLEAN)
  403. text += " lv2:portProperty lv2:toggled ;\n";
  404. if (paramInfo->hints & PARAMETER_IS_INTEGER)
  405. text += " lv2:portProperty lv2:integer ;\n";
  406. if (paramInfo->hints & PARAMETER_IS_LOGARITHMIC)
  407. text += " lv2:portProperty <" LV2_PORT_PROPS__logarithmic "> ;\n";
  408. if (paramInfo->hints & PARAMETER_USES_SAMPLE_RATE)
  409. text += " lv2:portProperty lv2:toggled ;\n";
  410. if (paramInfo->hints & PARAMETER_USES_SCALEPOINTS)
  411. text += " lv2:portProperty lv2:enumeration ;\n";
  412. if (paramInfo->hints & PARAMETER_USES_CUSTOM_TEXT)
  413. pass(); // TODO: custom lv2 extension for this
  414. }
  415. else
  416. {
  417. text += " lv2:portProperty <" LV2_PORT_PROPS__notOnGUI "> ;\n";
  418. }
  419. for (uint32_t j=0; j < paramInfo->scalePointCount; ++j)
  420. {
  421. const NativeParameterScalePoint* const scalePoint(&paramInfo->scalePoints[j]);
  422. if (j == 0)
  423. text += " lv2:scalePoint [ ";
  424. else
  425. text += " [ ";
  426. text += "rdfs:label \"" + String(scalePoint->label) + "\" ;\n";
  427. text += " rdf:value " + String::formatted("%f", scalePoint->value) + " ";
  428. if (j+1 == paramInfo->scalePointCount)
  429. text += "] ;\n";
  430. else
  431. text += "] ,\n";
  432. }
  433. if (paramUnit.isNotEmpty())
  434. {
  435. text += " unit:unit [\n";
  436. text += " a unit:Unit ;\n";
  437. text += " rdfs:label \"" + paramUnit + "\" ;\n";
  438. text += " unit:symbol \"" + paramUnit + "\" ;\n";
  439. text += " unit:render \"%f " + paramUnit + "\" ;\n";
  440. text += " ] ;\n";
  441. }
  442. if (i+1 == paramCount)
  443. text += " ] ;\n\n";
  444. else
  445. text += " ] , [\n";
  446. }
  447. text += " doap:name \"" + String(pluginDesc->name) + "\" ;\n";
  448. text += " doap:maintainer [ foaf:name \"" + String(pluginDesc->maker) + "\" ] .\n";
  449. // -------------------------------------------------------------------
  450. // Write file now
  451. std::fstream pluginStream(pluginFile.toRawUTF8(), std::ios::out);
  452. pluginStream << text.toRawUTF8();
  453. pluginStream.close();
  454. // -------------------------------------------------------------------
  455. // Cleanup plugin
  456. if (pluginHandle != nullptr && pluginDesc->cleanup != nullptr)
  457. pluginDesc->cleanup(pluginHandle);
  458. }
  459. // -----------------------------------------------------------------------
  460. int main()
  461. {
  462. PluginListManager& plm(PluginListManager::getInstance());
  463. writeManifestFile(plm);
  464. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  465. {
  466. const NativePluginDescriptor* const& pluginDesc(it.getValue());
  467. writePluginFile(pluginDesc);
  468. }
  469. carla_stdout("Done.");
  470. return 0;
  471. }
  472. // -----------------------------------------------------------------------