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 19KB

11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013 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 JUCE_WINDOWS
  35. # define PLUGIN_EXT ".dll"
  36. #elif 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. 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. void writeManifestFile()
  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 (NonRtList<const PluginDescriptor*>::Itenerator it = sPluginDescsMgr.descs.begin(); it.valid(); it.next())
  98. {
  99. const PluginDescriptor*& pluginDesc(*it);
  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(HostHandle) { return 512; }
  125. static double host_getSampleRate(HostHandle) { return 44100.0; }
  126. static bool host_isOffline(HostHandle) { return true; }
  127. static intptr_t host_dispatcher(HostHandle, HostDispatcherOpcode, int32_t, intptr_t, void*, float) { return 0; }
  128. void writePluginFile(const PluginDescriptor* 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. HostDescriptor 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. PluginHandle pluginHandle = pluginDesc->instantiate(&hostDesc);
  155. CARLA_SAFE_ASSERT_RETURN(pluginHandle != nullptr,)
  156. // -------------------------------------------------------------------
  157. // Header
  158. text += "@prefix atom: <" LV2_ATOM_PREFIX "> .\n";
  159. text += "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  160. text += "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  161. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
  162. text += "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
  163. text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  164. text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
  165. text += "@prefix unit: <" LV2_UNITS_PREFIX "> .\n";
  166. text += "\n";
  167. // -------------------------------------------------------------------
  168. // Plugin URI
  169. if (pluginLabel == "carla")
  170. text += "<http://kxstudio.sf.net/carla>\n";
  171. else
  172. text += "<http://kxstudio.sf.net/carla/plugins/" + pluginLabel + ">\n";
  173. // -------------------------------------------------------------------
  174. // Category
  175. switch (pluginDesc->category)
  176. {
  177. case PLUGIN_CATEGORY_SYNTH:
  178. text += " a lv2:InstrumentPlugin, lv2:Plugin ;\n";
  179. break;
  180. case PLUGIN_CATEGORY_DELAY:
  181. text += " a lv2:DelayPlugin, lv2:Plugin ;\n";
  182. break;
  183. case PLUGIN_CATEGORY_EQ:
  184. text += " a lv2:EQPlugin, lv2:Plugin ;\n";
  185. break;
  186. case PLUGIN_CATEGORY_FILTER:
  187. text += " a lv2:FilterPlugin, lv2:Plugin ;\n";
  188. break;
  189. case PLUGIN_CATEGORY_DYNAMICS:
  190. text += " a lv2:DynamicsPlugin, lv2:Plugin ;\n";
  191. break;
  192. case PLUGIN_CATEGORY_MODULATOR:
  193. text += " a lv2:ModulatorPlugin, lv2:Plugin ;\n";
  194. break;
  195. case PLUGIN_CATEGORY_UTILITY:
  196. text += " a lv2:UtilityPlugin, lv2:Plugin ;\n";
  197. break;
  198. default:
  199. text += " a lv2:Plugin ;\n";
  200. break;
  201. }
  202. text += "\n";
  203. // -------------------------------------------------------------------
  204. // Features
  205. if (pluginDesc->hints & PLUGIN_IS_RTSAFE)
  206. text += " lv2:optionalFeature <" LV2_CORE__hardRTCapable "> ;\n\n";
  207. text += " lv2:requiredFeature <" LV2_BUF_SIZE__boundedBlockLength "> ,\n";
  208. if (pluginDesc->hints & PLUGIN_NEEDS_FIXED_BUFFERS)
  209. text += " <" LV2_BUF_SIZE__fixedBlockLength "> ,\n";
  210. text += " <" LV2_OPTIONS__options "> ,\n";
  211. text += " <" LV2_URID__map "> ;\n";
  212. text += "\n";
  213. // -------------------------------------------------------------------
  214. // Extensions
  215. text += " lv2:extensionData <" LV2_OPTIONS__interface ">";
  216. if (pluginDesc->hints & PLUGIN_USES_STATE)
  217. {
  218. text += " ,\n";
  219. text += " <" LV2_STATE__interface ">";
  220. if (pluginDesc->category != PLUGIN_CATEGORY_SYNTH)
  221. {
  222. text += " ,\n";
  223. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  224. }
  225. else
  226. text += " ;\n";
  227. }
  228. else if (pluginDesc->category != PLUGIN_CATEGORY_SYNTH)
  229. {
  230. text += " ,\n";
  231. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  232. }
  233. else
  234. text += " ;\n";
  235. text += "\n";
  236. // -------------------------------------------------------------------
  237. // UIs
  238. if (pluginDesc->hints & PLUGIN_HAS_GUI)
  239. {
  240. text += " ui:ui <http://kxstudio.sf.net/carla/ui> ;\n";
  241. text += "\n";
  242. }
  243. // -------------------------------------------------------------------
  244. // First MIDI/Time port
  245. if (pluginDesc->midiIns > 0 || (pluginDesc->hints & PLUGIN_USES_TIME) != 0)
  246. {
  247. text += " lv2:port [\n";
  248. text += " a lv2:InputPort, atom:AtomPort ;\n";
  249. text += " atom:bufferType atom:Sequence ;\n";
  250. if (pluginDesc->midiIns > 0 && (pluginDesc->hints & PLUGIN_USES_TIME) != 0)
  251. {
  252. text += " atom:supports <" LV2_MIDI__MidiEvent "> ,\n";
  253. text += " <" LV2_TIME__Position "> ;\n";
  254. }
  255. else if (pluginDesc->midiIns > 0)
  256. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  257. else
  258. text += " atom:supports <" LV2_TIME__Position "> ;\n";
  259. text += " lv2:designation lv2:control ;\n";
  260. text += " lv2:index " + String(portIndex++) + " ;\n";
  261. if (pluginDesc->midiIns > 1)
  262. {
  263. text += " lv2:symbol \"lv2_events_in_1\" ;\n";
  264. text += " lv2:name \"Events Input #1\" ;\n";
  265. }
  266. else
  267. {
  268. text += " lv2:symbol \"lv2_events_in\" ;\n";
  269. text += " lv2:name \"Events Input\" ;\n";
  270. }
  271. text += " ] ;\n\n";
  272. }
  273. // -------------------------------------------------------------------
  274. // MIDI inputs
  275. for (uint32_t i=1; i < pluginDesc->midiIns; ++i)
  276. {
  277. if (i == 1)
  278. text += " lv2:port [\n";
  279. text += " a lv2:InputPort, atom:AtomPort ;\n";
  280. text += " atom:bufferType atom:Sequence ;\n";
  281. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  282. text += " lv2:index " + String(portIndex++) + " ;\n";
  283. if (pluginDesc->midiIns > 1)
  284. {
  285. text += " lv2:symbol \"lv2_events_in_" + String(i+1) + "\" ;\n";
  286. text += " lv2:name \"Events Input #" + String(i+1) + "\" ;\n";
  287. }
  288. else
  289. {
  290. text += " lv2:symbol \"lv2_events_in\" ;\n";
  291. text += " lv2:name \"Events Input\" ;\n";
  292. }
  293. if (i+1 == pluginDesc->midiIns)
  294. text += " ] ;\n\n";
  295. else
  296. text += " ] , [\n";
  297. }
  298. // -------------------------------------------------------------------
  299. // MIDI outputs
  300. for (uint32_t i=0; i < pluginDesc->midiOuts; ++i)
  301. {
  302. if (i == 0)
  303. text += " lv2:port [\n";
  304. text += " a lv2:OutputPort, atom:AtomPort ;\n";
  305. text += " atom:bufferType atom:Sequence ;\n";
  306. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  307. text += " lv2:index " + String(portIndex++) + " ;\n";
  308. if (pluginDesc->midiOuts > 1)
  309. {
  310. text += " lv2:symbol \"lv2_midi_out_" + String(i+1) + "\" ;\n";
  311. text += " lv2:name \"MIDI Output #" + String(i+1) + "\" ;\n";
  312. }
  313. else
  314. {
  315. text += " lv2:symbol \"lv2_midi_out\" ;\n";
  316. text += " lv2:name \"MIDI Output\" ;\n";
  317. }
  318. if (i+1 == pluginDesc->midiOuts)
  319. text += " ] ;\n\n";
  320. else
  321. text += " ] , [\n";
  322. }
  323. // -------------------------------------------------------------------
  324. // Freewheel port
  325. text += " lv2:port [\n";
  326. text += " a lv2:InputPort, lv2:ControlPort ;\n";
  327. text += " lv2:index " + String(portIndex++) + " ;\n";
  328. text += " lv2:symbol \"lv2_freewheel\" ;\n";
  329. text += " lv2:name \"Freewheel\" ;\n";
  330. text += " lv2:default 0.0 ;\n";
  331. text += " lv2:minimum 0.0 ;\n";
  332. text += " lv2:maximum 1.0 ;\n";
  333. text += " lv2:designation <" LV2_CORE__freeWheeling "> ;\n";
  334. text += " lv2:portProperty lv2:toggled ;\n";
  335. text += " ] ;\n";
  336. text += "\n";
  337. // -------------------------------------------------------------------
  338. // Audio inputs
  339. for (uint32_t i=0; i < pluginDesc->audioIns; ++i)
  340. {
  341. if (i == 0)
  342. text += " lv2:port [\n";
  343. text += " a lv2:InputPort, lv2:AudioPort ;\n";
  344. text += " lv2:index " + String(portIndex++) + " ;\n";
  345. text += " lv2:symbol \"lv2_audio_in_" + String(i+1) + "\" ;\n";
  346. text += " lv2:name \"Audio Input " + String(i+1) + "\" ;\n";
  347. if (i+1 == pluginDesc->audioIns)
  348. text += " ] ;\n\n";
  349. else
  350. text += " ] , [\n";
  351. }
  352. // -------------------------------------------------------------------
  353. // Audio outputs
  354. for (uint32_t i=0; i < pluginDesc->audioOuts; ++i)
  355. {
  356. if (i == 0)
  357. text += " lv2:port [\n";
  358. text += " a lv2:OutputPort, lv2:AudioPort ;\n";
  359. text += " lv2:index " + String(portIndex++) + " ;\n";
  360. text += " lv2:symbol \"lv2_audio_out_" + String(i+1) + "\" ;\n";
  361. text += " lv2:name \"Audio Output " + String(i+1) + "\" ;\n";
  362. if (i+1 == pluginDesc->audioOuts)
  363. text += " ] ;\n\n";
  364. else
  365. text += " ] , [\n";
  366. }
  367. // -------------------------------------------------------------------
  368. // Parameters
  369. const uint32_t paramCount(pluginDesc->get_parameter_count != nullptr ? pluginDesc->get_parameter_count(pluginHandle) : 0);
  370. if (paramCount > 0)
  371. {
  372. CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_info != nullptr,)
  373. CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_value != nullptr,)
  374. }
  375. for (uint32_t i=0; i < paramCount; ++i)
  376. {
  377. const Parameter* paramInfo(pluginDesc->get_parameter_info(pluginHandle, i));
  378. const String paramName(paramInfo->name != nullptr ? paramInfo->name : "");
  379. const String paramUnit(paramInfo->unit != nullptr ? paramInfo->unit : "");
  380. CARLA_SAFE_ASSERT_RETURN(paramInfo != nullptr,)
  381. if (i == 0)
  382. text += " lv2:port [\n";
  383. if (paramInfo->hints & PARAMETER_IS_OUTPUT)
  384. text += " a lv2:OutputPort, lv2:ControlPort ;\n";
  385. else
  386. text += " a lv2:InputPort, lv2:ControlPort ;\n";
  387. text += " lv2:index " + String(portIndex++) + " ;\n";
  388. text += " lv2:symbol \"" + nameToSymbol(paramName, i) + "\" ;\n";
  389. if (paramName.isNotEmpty())
  390. text += " lv2:name \"" + paramName + "\" ;\n";
  391. else
  392. text += " lv2:name \"Port " + String(i+1) + "\" ;\n";
  393. text += " lv2:default " + String::formatted("%f", paramInfo->ranges.def) + " ;\n";
  394. text += " lv2:minimum " + String::formatted("%f", paramInfo->ranges.min) + " ;\n";
  395. text += " lv2:maximum " + String::formatted("%f", paramInfo->ranges.max) + " ;\n";
  396. if (paramInfo->hints & PARAMETER_IS_ENABLED)
  397. {
  398. if ((paramInfo->hints & PARAMETER_IS_AUTOMABLE) == 0)
  399. text += " lv2:portProperty <" LV2_PORT_PROPS__expensive "> ;\n";
  400. if (paramInfo->hints & PARAMETER_IS_BOOLEAN)
  401. text += " lv2:portProperty lv2:toggled ;\n";
  402. if (paramInfo->hints & PARAMETER_IS_INTEGER)
  403. text += " lv2:portProperty lv2:integer ;\n";
  404. if (paramInfo->hints & PARAMETER_IS_LOGARITHMIC)
  405. text += " lv2:portProperty <" LV2_PORT_PROPS__logarithmic "> ;\n";
  406. if (paramInfo->hints & PARAMETER_USES_SAMPLE_RATE)
  407. text += " lv2:portProperty lv2:toggled ;\n";
  408. if (paramInfo->hints & PARAMETER_USES_SCALEPOINTS)
  409. text += " lv2:portProperty lv2:enumeration ;\n";
  410. if (paramInfo->hints & PARAMETER_USES_CUSTOM_TEXT)
  411. pass(); // TODO: custom lv2 extension for this
  412. }
  413. else
  414. {
  415. text += " lv2:portProperty <" LV2_PORT_PROPS__notOnGUI "> ;\n";
  416. }
  417. for (uint32_t j=0; j < paramInfo->scalePointCount; ++j)
  418. {
  419. const ParameterScalePoint* const scalePoint(&paramInfo->scalePoints[j]);
  420. if (j == 0)
  421. text += " lv2:scalePoint [ ";
  422. else
  423. text += " [ ";
  424. text += "rdfs:label \"" + String(scalePoint->label) + "\" ;\n";
  425. text += " rdf:value " + String::formatted("%f", scalePoint->value) + " ";
  426. if (j+1 == paramInfo->scalePointCount)
  427. text += "] ;\n";
  428. else
  429. text += "] ,\n";
  430. }
  431. if (paramUnit.isNotEmpty())
  432. {
  433. text += " unit:unit [\n";
  434. text += " a unit:Unit ;\n";
  435. text += " rdfs:label \"" + paramUnit + "\" ;\n";
  436. text += " unit:symbol \"" + paramUnit + "\" ;\n";
  437. text += " unit:render \"%f " + paramUnit + "\" ;\n";
  438. text += " ] ;\n";
  439. }
  440. if (i+1 == paramCount)
  441. text += " ] ;\n\n";
  442. else
  443. text += " ] , [\n";
  444. }
  445. text += " doap:name \"" + String(pluginDesc->name) + "\" ;\n";
  446. text += " doap:maintainer [ foaf:name \"" + String(pluginDesc->maker) + "\" ] .\n";
  447. // -------------------------------------------------------------------
  448. // Write file now
  449. std::fstream pluginStream(pluginFile.toRawUTF8(), std::ios::out);
  450. pluginStream << text.toRawUTF8();
  451. pluginStream.close();
  452. // -------------------------------------------------------------------
  453. // Cleanup plugin
  454. if (pluginDesc->cleanup != nullptr)
  455. pluginDesc->cleanup(pluginHandle);
  456. }
  457. // -----------------------------------------------------------------------
  458. int main()
  459. {
  460. writeManifestFile();
  461. for (NonRtList<const PluginDescriptor*>::Itenerator it = sPluginDescsMgr.descs.begin(); it.valid(); it.next())
  462. {
  463. const PluginDescriptor*& pluginDesc(*it);
  464. writePluginFile(pluginDesc);
  465. }
  466. carla_stdout("Done.");
  467. return 0;
  468. }
  469. // -----------------------------------------------------------------------