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.

576 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. #include "carla-native-base.cpp"
  18. #include "JuceHeader.h"
  19. #include "lv2/atom.h"
  20. #include "lv2/buf-size.h"
  21. #include "lv2/instance-access.h"
  22. #include "lv2/midi.h"
  23. #include "lv2/options.h"
  24. #include "lv2/port-props.h"
  25. #include "lv2/state.h"
  26. #include "lv2/time.h"
  27. #include "lv2/ui.h"
  28. #include "lv2/units.h"
  29. #include "lv2/urid.h"
  30. #include "lv2/lv2_external_ui.h"
  31. #include "lv2/lv2_programs.h"
  32. #include <fstream>
  33. #if JUCE_WINDOWS
  34. # define PLUGIN_EXT ".dll"
  35. #elif JUCE_MAC
  36. # define PLUGIN_EXT ".dylib"
  37. #else
  38. # define PLUGIN_EXT ".so"
  39. #endif
  40. using juce::String;
  41. using juce::StringArray;
  42. using juce::juce_wchar;
  43. // -----------------------------------------------------------------------
  44. // Converts a parameter name to an LV2 compatible symbol
  45. static StringArray gUsedSymbols;
  46. const String nameToSymbol(const String& name, const uint32_t portIndex)
  47. {
  48. String symbol, trimmedName = name.trim().toLowerCase();
  49. if (trimmedName.isEmpty())
  50. {
  51. symbol += "lv2_port_";
  52. symbol += String(portIndex+1);
  53. }
  54. else
  55. {
  56. for (int i=0; i < trimmedName.length(); ++i)
  57. {
  58. const juce_wchar c = trimmedName[i];
  59. if (i == 0 && std::isdigit(c))
  60. symbol += "_";
  61. else if (std::isalpha(c) || std::isdigit(c))
  62. symbol += c;
  63. else
  64. symbol += "_";
  65. }
  66. }
  67. // Do not allow identical symbols
  68. if (gUsedSymbols.contains(symbol))
  69. {
  70. int offset = 2;
  71. String offsetStr = "_2";
  72. symbol += offsetStr;
  73. while (gUsedSymbols.contains(symbol))
  74. {
  75. offset += 1;
  76. String newOffsetStr = "_" + String(offset);
  77. symbol = symbol.replace(offsetStr, newOffsetStr);
  78. offsetStr = newOffsetStr;
  79. }
  80. }
  81. gUsedSymbols.add(symbol);
  82. return symbol;
  83. }
  84. // -----------------------------------------------------------------------
  85. void writeManifestFile()
  86. {
  87. String text;
  88. // -------------------------------------------------------------------
  89. // Header
  90. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
  91. text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  92. text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
  93. text += "\n";
  94. // -------------------------------------------------------------------
  95. // Plugins
  96. for (NonRtList<const PluginDescriptor*>::Itenerator it = sPluginDescsMgr.descs.begin(); it.valid(); it.next())
  97. {
  98. const PluginDescriptor*& pluginDesc(*it);
  99. const String label(pluginDesc->label);
  100. if (label == "carla")
  101. text += "<http://kxstudio.sf.net/carla>\n";
  102. else
  103. text += "<http://kxstudio.sf.net/carla/plugins/" + label + ">\n";
  104. switch (pluginDesc->category)
  105. {
  106. case PLUGIN_CATEGORY_SYNTH:
  107. text += " a lv2:InstrumentPlugin, lv2:Plugin ;\n";
  108. break;
  109. case PLUGIN_CATEGORY_DELAY:
  110. text += " a lv2:DelayPlugin, lv2:Plugin ;\n";
  111. break;
  112. case PLUGIN_CATEGORY_EQ:
  113. text += " a lv2:EQPlugin, lv2:Plugin ;\n";
  114. break;
  115. case PLUGIN_CATEGORY_FILTER:
  116. text += " a lv2:FilterPlugin, lv2:Plugin ;\n";
  117. break;
  118. case PLUGIN_CATEGORY_DYNAMICS:
  119. text += " a lv2:DynamicsPlugin, lv2:Plugin ;\n";
  120. break;
  121. case PLUGIN_CATEGORY_MODULATOR:
  122. text += " a lv2:ModulatorPlugin, lv2:Plugin ;\n";
  123. break;
  124. case PLUGIN_CATEGORY_UTILITY:
  125. text += " a lv2:UtilityPlugin, lv2:Plugin ;\n";
  126. break;
  127. default:
  128. text += " a lv2:Plugin ;\n";
  129. break;
  130. }
  131. text += " lv2:binary <carla-native" PLUGIN_EXT "> ;\n";
  132. text += " rdfs:seeAlso <" + label + ".ttl> .\n";
  133. text += "\n";
  134. }
  135. // -------------------------------------------------------------------
  136. // UI
  137. text += "<http://kxstudio.sf.net/carla#UI>\n";
  138. text += " a <" LV2_EXTERNAL_UI__Widget "> ;\n";
  139. text += " ui:binary <carla-native" PLUGIN_EXT "> ;\n";
  140. text += " lv2:extensionData <" LV2_PROGRAMS__UIInterface "> ;\n";
  141. text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> .\n";
  142. text += "\n";
  143. text += "<http://kxstudio.sf.net/carla#UIold>\n";
  144. text += " a <" LV2_EXTERNAL_UI_DEPRECATED_URI "> ;\n";
  145. text += " ui:binary <carla-native" PLUGIN_EXT "> ;\n";
  146. text += " lv2:extensionData <" LV2_PROGRAMS__UIInterface "> ;\n";
  147. text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> .\n";
  148. // -------------------------------------------------------------------
  149. // Write file now
  150. std::fstream manifest("carla-native.lv2/manifest.ttl", std::ios::out);
  151. manifest << text.toRawUTF8();
  152. manifest.close();
  153. }
  154. // -----------------------------------------------------------------------
  155. static uint32_t host_getBufferSize(HostHandle) { return 512; }
  156. static double host_getSampleRate(HostHandle) { return 44100.0; }
  157. static bool host_isOffline(HostHandle) { return true; }
  158. static intptr_t host_dispatcher(HostHandle, HostDispatcherOpcode, int32_t, intptr_t, void*, float) { return 0; }
  159. void writePluginFile(const PluginDescriptor* const pluginDesc)
  160. {
  161. const String pluginLabel(pluginDesc->label);
  162. const String pluginFile("carla-native.lv2/" + pluginLabel + ".ttl");
  163. uint32_t portIndex = 0;
  164. String text;
  165. gUsedSymbols.clear();
  166. carla_stdout("Generating data for %s...", pluginDesc->name);
  167. // -------------------------------------------------------------------
  168. // Init plugin
  169. HostDescriptor hostDesc;
  170. hostDesc.handle = nullptr;
  171. hostDesc.resourceDir = "";
  172. hostDesc.uiName = "";
  173. hostDesc.get_buffer_size = host_getBufferSize;
  174. hostDesc.get_sample_rate = host_getSampleRate;
  175. hostDesc.is_offline = host_isOffline;
  176. hostDesc.get_time_info = nullptr;
  177. hostDesc.write_midi_event = nullptr;
  178. hostDesc.ui_parameter_changed = nullptr;
  179. hostDesc.ui_midi_program_changed = nullptr;
  180. hostDesc.ui_custom_data_changed = nullptr;
  181. hostDesc.ui_closed = nullptr;
  182. hostDesc.ui_open_file = nullptr;
  183. hostDesc.ui_save_file = nullptr;
  184. hostDesc.dispatcher = host_dispatcher;
  185. PluginHandle pluginHandle = pluginDesc->instantiate(&hostDesc);
  186. CARLA_SAFE_ASSERT_RETURN(pluginHandle != nullptr,)
  187. // -------------------------------------------------------------------
  188. // Header
  189. text += "@prefix atom: <" LV2_ATOM_PREFIX "> .\n";
  190. text += "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  191. text += "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  192. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
  193. text += "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
  194. text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  195. text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
  196. text += "@prefix unit: <" LV2_UNITS_PREFIX "> .\n";
  197. text += "\n";
  198. // -------------------------------------------------------------------
  199. // Plugin URI
  200. if (pluginLabel == "carla")
  201. text += "<http://kxstudio.sf.net/carla>\n";
  202. else
  203. text += "<http://kxstudio.sf.net/carla/plugins/" + pluginLabel + ">\n";
  204. // -------------------------------------------------------------------
  205. // Features
  206. if (pluginDesc->hints & PLUGIN_IS_RTSAFE)
  207. text += " lv2:optionalFeature <" LV2_CORE__hardRTCapable "> ;\n\n";
  208. text += " lv2:requiredFeature <" LV2_BUF_SIZE__boundedBlockLength "> ,\n";
  209. if (pluginDesc->hints & PLUGIN_USES_STATIC_BUFFERS)
  210. text += " <" LV2_BUF_SIZE__fixedBlockLength "> ,\n";
  211. text += " <" LV2_OPTIONS__options "> ,\n";
  212. text += " <" LV2_URID__map "> ;\n";
  213. text += "\n";
  214. // -------------------------------------------------------------------
  215. // Extensions
  216. text += " lv2:extensionData <" LV2_OPTIONS__interface ">";
  217. if (pluginDesc->hints & PLUGIN_USES_STATE)
  218. {
  219. text += " ,\n";
  220. text += " <" LV2_STATE__interface ">";
  221. if ((pluginDesc->hints & PLUGIN_IS_SYNTH) == 0)
  222. {
  223. text += " ,\n";
  224. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  225. }
  226. else
  227. text += " ;\n";
  228. }
  229. else if ((pluginDesc->hints & PLUGIN_IS_SYNTH) == 0)
  230. {
  231. text += " ,\n";
  232. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  233. }
  234. else
  235. text += " ;\n";
  236. text += "\n";
  237. // -------------------------------------------------------------------
  238. // UIs
  239. if (pluginDesc->hints & PLUGIN_HAS_GUI)
  240. {
  241. text += " ui:ui <http://kxstudio.sf.net/carla#UI> ,\n";
  242. text += " <http://kxstudio.sf.net/carla#UIold> ;\n";
  243. text += "\n";
  244. }
  245. // -------------------------------------------------------------------
  246. // First MIDI/Time port
  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)
  251. {
  252. text += " atom:supports <" LV2_MIDI__MidiEvent "> ,\n";
  253. text += " <" LV2_TIME__Position "> ;\n";
  254. }
  255. else
  256. {
  257. text += " atom:supports <" LV2_TIME__Position "> ;\n";
  258. }
  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. // 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 += " units:unit [\n";
  433. text += " a units:Unit ;\n";
  434. text += " rdfs:label \"" + paramUnit + "\" ;\n";
  435. text += " units:symbol \"" + paramUnit + "\" ;\n";
  436. text += " units: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. // -----------------------------------------------------------------------