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.

carla-native-lv2-export.cpp 20KB

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