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

  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. // -------------------------------------------------------------------
  203. // Features
  204. if (pluginDesc->hints & PLUGIN_IS_RTSAFE)
  205. text += " lv2:optionalFeature <" LV2_CORE__hardRTCapable "> ;\n\n";
  206. text += " lv2:requiredFeature <" LV2_BUF_SIZE__boundedBlockLength "> ,\n";
  207. if (pluginDesc->hints & PLUGIN_NEEDS_STATIC_BUFFERS)
  208. text += " <" LV2_BUF_SIZE__fixedBlockLength "> ,\n";
  209. text += " <" LV2_OPTIONS__options "> ,\n";
  210. text += " <" LV2_URID__map "> ;\n";
  211. text += "\n";
  212. // -------------------------------------------------------------------
  213. // Extensions
  214. text += " lv2:extensionData <" LV2_OPTIONS__interface ">";
  215. if (pluginDesc->hints & PLUGIN_USES_STATE)
  216. {
  217. text += " ,\n";
  218. text += " <" LV2_STATE__interface ">";
  219. if (pluginDesc->category != PLUGIN_CATEGORY_SYNTH)
  220. {
  221. text += " ,\n";
  222. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  223. }
  224. else
  225. text += " ;\n";
  226. }
  227. else if (pluginDesc->category != PLUGIN_CATEGORY_SYNTH)
  228. {
  229. text += " ,\n";
  230. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  231. }
  232. else
  233. text += " ;\n";
  234. text += "\n";
  235. // -------------------------------------------------------------------
  236. // UIs
  237. if (pluginDesc->hints & PLUGIN_HAS_GUI)
  238. {
  239. text += " ui:ui <http://kxstudio.sf.net/carla#UI> ;\n";
  240. text += "\n";
  241. }
  242. // -------------------------------------------------------------------
  243. // First MIDI/Time port
  244. if (pluginDesc->midiIns > 0 || (pluginDesc->hints & PLUGIN_USES_TIMEPOS) != 0)
  245. {
  246. text += " lv2:port [\n";
  247. text += " a lv2:InputPort, atom:AtomPort ;\n";
  248. text += " atom:bufferType atom:Sequence ;\n";
  249. if (pluginDesc->midiIns > 0 && (pluginDesc->hints & PLUGIN_USES_TIMEPOS) != 0)
  250. {
  251. text += " atom:supports <" LV2_MIDI__MidiEvent "> ,\n";
  252. text += " <" LV2_TIME__Position "> ;\n";
  253. }
  254. else if (pluginDesc->midiIns > 0)
  255. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  256. else
  257. text += " atom:supports <" LV2_TIME__Position "> ;\n";
  258. text += " lv2:designation lv2:control ;\n";
  259. text += " lv2:index " + String(portIndex++) + " ;\n";
  260. if (pluginDesc->midiIns > 1)
  261. {
  262. text += " lv2:symbol \"lv2_events_in_1\" ;\n";
  263. text += " lv2:name \"Events Input #1\" ;\n";
  264. }
  265. else
  266. {
  267. text += " lv2:symbol \"lv2_events_in\" ;\n";
  268. text += " lv2:name \"Events Input\" ;\n";
  269. }
  270. text += " ] ;\n\n";
  271. }
  272. // -------------------------------------------------------------------
  273. // MIDI inputs
  274. for (uint32_t i=1; i < pluginDesc->midiIns; ++i)
  275. {
  276. if (i == 1)
  277. text += " lv2:port [\n";
  278. text += " a lv2:InputPort, atom:AtomPort ;\n";
  279. text += " atom:bufferType atom:Sequence ;\n";
  280. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  281. text += " lv2:index " + String(portIndex++) + " ;\n";
  282. if (pluginDesc->midiIns > 1)
  283. {
  284. text += " lv2:symbol \"lv2_events_in_" + String(i+1) + "\" ;\n";
  285. text += " lv2:name \"Events Input #" + String(i+1) + "\" ;\n";
  286. }
  287. else
  288. {
  289. text += " lv2:symbol \"lv2_events_in\" ;\n";
  290. text += " lv2:name \"Events Input\" ;\n";
  291. }
  292. if (i+1 == pluginDesc->midiIns)
  293. text += " ] ;\n\n";
  294. else
  295. text += " ] , [\n";
  296. }
  297. // -------------------------------------------------------------------
  298. // MIDI outputs
  299. for (uint32_t i=0; i < pluginDesc->midiOuts; ++i)
  300. {
  301. if (i == 0)
  302. text += " lv2:port [\n";
  303. text += " a lv2:OutputPort, atom:AtomPort ;\n";
  304. text += " atom:bufferType atom:Sequence ;\n";
  305. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  306. text += " lv2:index " + String(portIndex++) + " ;\n";
  307. if (pluginDesc->midiOuts > 1)
  308. {
  309. text += " lv2:symbol \"lv2_midi_out_" + String(i+1) + "\" ;\n";
  310. text += " lv2:name \"MIDI Output #" + String(i+1) + "\" ;\n";
  311. }
  312. else
  313. {
  314. text += " lv2:symbol \"lv2_midi_out\" ;\n";
  315. text += " lv2:name \"MIDI Output\" ;\n";
  316. }
  317. if (i+1 == pluginDesc->midiOuts)
  318. text += " ] ;\n\n";
  319. else
  320. text += " ] , [\n";
  321. }
  322. // -------------------------------------------------------------------
  323. // Freewheel port
  324. text += " lv2:port [\n";
  325. text += " a lv2:InputPort, lv2:ControlPort ;\n";
  326. text += " lv2:index " + String(portIndex++) + " ;\n";
  327. text += " lv2:symbol \"lv2_freewheel\" ;\n";
  328. text += " lv2:name \"Freewheel\" ;\n";
  329. text += " lv2:default 0.0 ;\n";
  330. text += " lv2:minimum 0.0 ;\n";
  331. text += " lv2:maximum 1.0 ;\n";
  332. text += " lv2:designation <" LV2_CORE__freeWheeling "> ;\n";
  333. text += " lv2:portProperty lv2:toggled ;\n";
  334. text += " ] ;\n";
  335. text += "\n";
  336. // -------------------------------------------------------------------
  337. // Audio inputs
  338. for (uint32_t i=0; i < pluginDesc->audioIns; ++i)
  339. {
  340. if (i == 0)
  341. text += " lv2:port [\n";
  342. text += " a lv2:InputPort, lv2:AudioPort ;\n";
  343. text += " lv2:index " + String(portIndex++) + " ;\n";
  344. text += " lv2:symbol \"lv2_audio_in_" + String(i+1) + "\" ;\n";
  345. text += " lv2:name \"Audio Input " + String(i+1) + "\" ;\n";
  346. if (i+1 == pluginDesc->audioIns)
  347. text += " ] ;\n\n";
  348. else
  349. text += " ] , [\n";
  350. }
  351. // -------------------------------------------------------------------
  352. // Audio outputs
  353. for (uint32_t i=0; i < pluginDesc->audioOuts; ++i)
  354. {
  355. if (i == 0)
  356. text += " lv2:port [\n";
  357. text += " a lv2:OutputPort, lv2:AudioPort ;\n";
  358. text += " lv2:index " + String(portIndex++) + " ;\n";
  359. text += " lv2:symbol \"lv2_audio_out_" + String(i+1) + "\" ;\n";
  360. text += " lv2:name \"Audio Output " + String(i+1) + "\" ;\n";
  361. if (i+1 == pluginDesc->audioOuts)
  362. text += " ] ;\n\n";
  363. else
  364. text += " ] , [\n";
  365. }
  366. // -------------------------------------------------------------------
  367. // Parameters
  368. const uint32_t paramCount(pluginDesc->get_parameter_count != nullptr ? pluginDesc->get_parameter_count(pluginHandle) : 0);
  369. if (paramCount > 0)
  370. {
  371. CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_info != nullptr,)
  372. CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_value != nullptr,)
  373. }
  374. for (uint32_t i=0; i < paramCount; ++i)
  375. {
  376. const Parameter* paramInfo(pluginDesc->get_parameter_info(pluginHandle, i));
  377. const String paramName(paramInfo->name != nullptr ? paramInfo->name : "");
  378. const String paramUnit(paramInfo->unit != nullptr ? paramInfo->unit : "");
  379. CARLA_SAFE_ASSERT_RETURN(paramInfo != nullptr,)
  380. if (i == 0)
  381. text += " lv2:port [\n";
  382. if (paramInfo->hints & PARAMETER_IS_OUTPUT)
  383. text += " a lv2:OutputPort, lv2:ControlPort ;\n";
  384. else
  385. text += " a lv2:InputPort, lv2:ControlPort ;\n";
  386. text += " lv2:index " + String(portIndex++) + " ;\n";
  387. text += " lv2:symbol \"" + nameToSymbol(paramName, i) + "\" ;\n";
  388. if (paramName.isNotEmpty())
  389. text += " lv2:name \"" + paramName + "\" ;\n";
  390. else
  391. text += " lv2:name \"Port " + String(i+1) + "\" ;\n";
  392. text += " lv2:default " + String::formatted("%f", paramInfo->ranges.def) + " ;\n";
  393. text += " lv2:minimum " + String::formatted("%f", paramInfo->ranges.min) + " ;\n";
  394. text += " lv2:maximum " + String::formatted("%f", paramInfo->ranges.max) + " ;\n";
  395. if (paramInfo->hints & PARAMETER_IS_ENABLED)
  396. {
  397. if ((paramInfo->hints & PARAMETER_IS_AUTOMABLE) == 0)
  398. text += " lv2:portProperty <" LV2_PORT_PROPS__expensive "> ;\n";
  399. if (paramInfo->hints & PARAMETER_IS_BOOLEAN)
  400. text += " lv2:portProperty lv2:toggled ;\n";
  401. if (paramInfo->hints & PARAMETER_IS_INTEGER)
  402. text += " lv2:portProperty lv2:integer ;\n";
  403. if (paramInfo->hints & PARAMETER_IS_LOGARITHMIC)
  404. text += " lv2:portProperty <" LV2_PORT_PROPS__logarithmic "> ;\n";
  405. if (paramInfo->hints & PARAMETER_USES_SAMPLE_RATE)
  406. text += " lv2:portProperty lv2:toggled ;\n";
  407. if (paramInfo->hints & PARAMETER_USES_SCALEPOINTS)
  408. text += " lv2:portProperty lv2:enumeration ;\n";
  409. if (paramInfo->hints & PARAMETER_USES_CUSTOM_TEXT)
  410. pass(); // TODO: custom lv2 extension for this
  411. }
  412. else
  413. {
  414. text += " lv2:portProperty <" LV2_PORT_PROPS__notOnGUI "> ;\n";
  415. }
  416. for (uint32_t j=0; j < paramInfo->scalePointCount; ++j)
  417. {
  418. const ParameterScalePoint* const scalePoint(&paramInfo->scalePoints[j]);
  419. if (j == 0)
  420. text += " lv2:scalePoint [ ";
  421. else
  422. text += " [ ";
  423. text += "rdfs:label \"" + String(scalePoint->label) + "\" ;\n";
  424. text += " rdf:value " + String::formatted("%f", scalePoint->value) + " ";
  425. if (j+1 == paramInfo->scalePointCount)
  426. text += "] ;\n";
  427. else
  428. text += "] ,\n";
  429. }
  430. if (paramUnit.isNotEmpty())
  431. {
  432. text += " unit:unit [\n";
  433. text += " a unit:Unit ;\n";
  434. text += " rdfs:label \"" + paramUnit + "\" ;\n";
  435. text += " unit:symbol \"" + paramUnit + "\" ;\n";
  436. text += " unit:render \"%f " + paramUnit + "\" ;\n";
  437. text += " ] ;\n";
  438. }
  439. if (i+1 == paramCount)
  440. text += " ] ;\n\n";
  441. else
  442. text += " ] , [\n";
  443. }
  444. text += " doap:name \"" + String(pluginDesc->name) + "\" ;\n";
  445. text += " doap:maintainer [ foaf:name \"" + String(pluginDesc->maker) + "\" ] .\n";
  446. // -------------------------------------------------------------------
  447. // Write file now
  448. std::fstream pluginStream(pluginFile.toRawUTF8(), std::ios::out);
  449. pluginStream << text.toRawUTF8();
  450. pluginStream.close();
  451. // -------------------------------------------------------------------
  452. // Cleanup plugin
  453. if (pluginDesc->cleanup != nullptr)
  454. pluginDesc->cleanup(pluginHandle);
  455. }
  456. // -----------------------------------------------------------------------
  457. int main()
  458. {
  459. writeManifestFile();
  460. for (NonRtList<const PluginDescriptor*>::Itenerator it = sPluginDescsMgr.descs.begin(); it.valid(); it.next())
  461. {
  462. const PluginDescriptor*& pluginDesc(*it);
  463. writePluginFile(pluginDesc);
  464. }
  465. carla_stdout("Done.");
  466. return 0;
  467. }
  468. // -----------------------------------------------------------------------