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.

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