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.

508 lines
17KB

  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. text += " a lv2:Plugin ;\n";
  105. text += " lv2:binary <carla-native" PLUGIN_EXT "> ;\n";
  106. text += " rdfs:seeAlso <" + label + ".ttl> .\n";
  107. text += "\n";
  108. }
  109. // -------------------------------------------------------------------
  110. // UI
  111. text += "<http://kxstudio.sf.net/carla#UI>\n";
  112. text += " a <" LV2_EXTERNAL_UI__Widget "> ;\n";
  113. text += " ui:binary <carla-native" PLUGIN_EXT "> ;\n";
  114. text += " lv2:extensionData <" LV2_PROGRAMS__UIInterface "> ;\n";
  115. text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> .\n";
  116. text += "\n";
  117. text += "<http://kxstudio.sf.net/carla#UIold>\n";
  118. text += " a <" LV2_EXTERNAL_UI_DEPRECATED_URI "> ;\n";
  119. text += " ui:binary <carla-native" PLUGIN_EXT "> ;\n";
  120. text += " lv2:extensionData <" LV2_PROGRAMS__UIInterface "> ;\n";
  121. text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> .\n";
  122. // -------------------------------------------------------------------
  123. // Write file now
  124. std::fstream manifest("carla-native.lv2/manifest.ttl", std::ios::out);
  125. manifest << text.toRawUTF8();
  126. manifest.close();
  127. }
  128. // -----------------------------------------------------------------------
  129. static uint32_t host_getBufferSize(HostHandle) { return 512; }
  130. static double host_getSampleRate(HostHandle) { return 44100.0; }
  131. static bool host_isOffline(HostHandle) { return true; }
  132. static intptr_t host_dispatcher(HostHandle, HostDispatcherOpcode, int32_t, intptr_t, void*, float) { return 0; }
  133. void writePluginFile(const PluginDescriptor* const pluginDesc)
  134. {
  135. const String pluginLabel(pluginDesc->label);
  136. const String pluginFile("carla-native.lv2/" + pluginLabel + ".ttl");
  137. uint32_t portIndex = 0;
  138. String text;
  139. gUsedSymbols.clear();
  140. carla_stdout("Generating data for %s...", pluginDesc->name);
  141. // -------------------------------------------------------------------
  142. // Init plugin
  143. HostDescriptor hostDesc;
  144. hostDesc.handle = nullptr;
  145. hostDesc.resourceDir = "";
  146. hostDesc.uiName = "";
  147. hostDesc.get_buffer_size = host_getBufferSize;
  148. hostDesc.get_sample_rate = host_getSampleRate;
  149. hostDesc.is_offline = host_isOffline;
  150. hostDesc.get_time_info = nullptr;
  151. hostDesc.write_midi_event = nullptr;
  152. hostDesc.ui_parameter_changed = nullptr;
  153. hostDesc.ui_midi_program_changed = nullptr;
  154. hostDesc.ui_custom_data_changed = nullptr;
  155. hostDesc.ui_closed = nullptr;
  156. hostDesc.ui_open_file = nullptr;
  157. hostDesc.ui_save_file = nullptr;
  158. hostDesc.dispatcher = host_dispatcher;
  159. PluginHandle pluginHandle = pluginDesc->instantiate(&hostDesc);
  160. CARLA_SAFE_ASSERT_RETURN(pluginHandle != nullptr,)
  161. // -------------------------------------------------------------------
  162. // Header
  163. text += "@prefix atom: <" LV2_ATOM_PREFIX "> .\n";
  164. text += "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  165. text += "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  166. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\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
  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. //text += " a " + getPluginType() + " ;\n";
  178. text += " lv2:requiredFeature <" LV2_BUF_SIZE__boundedBlockLength "> ,\n";
  179. text += " <" LV2_URID__map "> ;\n";
  180. text += " lv2:extensionData <" LV2_OPTIONS__interface "> ,\n";
  181. text += " <" LV2_STATE__interface "> ,\n";
  182. text += " <" LV2_PROGRAMS__Interface "> ;\n";
  183. text += "\n";
  184. // -------------------------------------------------------------------
  185. // UIs
  186. if (pluginDesc->hints & PLUGIN_HAS_GUI)
  187. {
  188. text += " ui:ui <http://kxstudio.sf.net/carla#UI> ,\n";
  189. text += " <http://kxstudio.sf.net/carla#UIold> ;\n";
  190. text += "\n";
  191. }
  192. // -------------------------------------------------------------------
  193. // First MIDI/Time port
  194. text += " lv2:port [\n";
  195. text += " a lv2:InputPort, atom:AtomPort ;\n";
  196. text += " atom:bufferType atom:Sequence ;\n";
  197. if (pluginDesc->midiIns > 0)
  198. {
  199. text += " atom:supports <" LV2_MIDI__MidiEvent "> ,\n";
  200. text += " <" LV2_TIME__Position "> ;\n";
  201. }
  202. else
  203. {
  204. text += " atom:supports <" LV2_TIME__Position "> ;\n";
  205. }
  206. text += " lv2:designation lv2:control ;\n";
  207. text += " lv2:index " + String(portIndex++) + " ;\n";
  208. if (pluginDesc->midiIns > 1)
  209. {
  210. text += " lv2:symbol \"lv2_events_in_1\" ;\n";
  211. text += " lv2:name \"Events Input #1\" ;\n";
  212. }
  213. else
  214. {
  215. text += " lv2:symbol \"lv2_events_in\" ;\n";
  216. text += " lv2:name \"Events Input\" ;\n";
  217. }
  218. text += " ] ;\n\n";
  219. // -------------------------------------------------------------------
  220. // MIDI inputs
  221. for (uint32_t i=1; i < pluginDesc->midiIns; ++i)
  222. {
  223. if (i == 1)
  224. text += " lv2:port [\n";
  225. else
  226. text += " [\n";
  227. text += " a lv2:InputPort, atom:AtomPort ;\n";
  228. text += " atom:bufferType atom:Sequence ;\n";
  229. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  230. text += " lv2:index " + String(portIndex++) + " ;\n";
  231. if (pluginDesc->midiIns > 1)
  232. {
  233. text += " lv2:symbol \"lv2_events_in_" + String(i+1) + "\" ;\n";
  234. text += " lv2:name \"Events Input #" + String(i+1) + "\" ;\n";
  235. }
  236. else
  237. {
  238. text += " lv2:symbol \"lv2_events_in\" ;\n";
  239. text += " lv2:name \"Events Input\" ;\n";
  240. }
  241. if (i+1 == pluginDesc->midiIns)
  242. text += " ] ;\n\n";
  243. else
  244. text += " ] ,\n";
  245. }
  246. // -------------------------------------------------------------------
  247. // MIDI outputs
  248. for (uint32_t i=0; i < pluginDesc->midiOuts; ++i)
  249. {
  250. if (i == 0)
  251. text += " lv2:port [\n";
  252. else
  253. text += " [\n";
  254. text += " a lv2:OutputPort, atom:AtomPort ;\n";
  255. text += " atom:bufferType atom:Sequence ;\n";
  256. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  257. text += " lv2:index " + String(portIndex++) + " ;\n";
  258. if (pluginDesc->midiOuts > 1)
  259. {
  260. text += " lv2:symbol \"lv2_midi_out_" + String(i+1) + "\" ;\n";
  261. text += " lv2:name \"MIDI Output #" + String(i+1) + "\" ;\n";
  262. }
  263. else
  264. {
  265. text += " lv2:symbol \"lv2_midi_out\" ;\n";
  266. text += " lv2:name \"MIDI Output\" ;\n";
  267. }
  268. if (i+1 == pluginDesc->midiOuts)
  269. text += " ] ;\n\n";
  270. else
  271. text += " ] ,\n";
  272. }
  273. // -------------------------------------------------------------------
  274. // Freewheel port
  275. text += " lv2:port [\n";
  276. text += " a lv2:InputPort, lv2:ControlPort ;\n";
  277. text += " lv2:index " + String(portIndex++) + " ;\n";
  278. text += " lv2:symbol \"lv2_freewheel\" ;\n";
  279. text += " lv2:name \"Freewheel\" ;\n";
  280. text += " lv2:default 0.0 ;\n";
  281. text += " lv2:minimum 0.0 ;\n";
  282. text += " lv2:maximum 1.0 ;\n";
  283. text += " lv2:designation <" LV2_CORE__freeWheeling "> ;\n";
  284. text += " lv2:portProperty lv2:toggled ;\n";
  285. text += " ] ;\n";
  286. text += "\n";
  287. // -------------------------------------------------------------------
  288. // Audio inputs
  289. for (uint32_t i=0; i < pluginDesc->audioIns; ++i)
  290. {
  291. if (i == 0)
  292. text += " lv2:port [\n";
  293. else
  294. text += " [\n";
  295. text += " a lv2:InputPort, lv2:AudioPort ;\n";
  296. text += " lv2:index " + String(portIndex++) + " ;\n";
  297. text += " lv2:symbol \"lv2_audio_in_" + String(i+1) + "\" ;\n";
  298. text += " lv2:name \"Audio Input " + String(i+1) + "\" ;\n";
  299. if (i+1 == pluginDesc->audioIns)
  300. text += " ] ;\n\n";
  301. else
  302. text += " ] ,\n";
  303. }
  304. // -------------------------------------------------------------------
  305. // Audio outputs
  306. for (uint32_t i=0; i < pluginDesc->audioOuts; ++i)
  307. {
  308. if (i == 0)
  309. text += " lv2:port [\n";
  310. else
  311. text += " [\n";
  312. text += " a lv2:OutputPort, lv2:AudioPort ;\n";
  313. text += " lv2:index " + String(portIndex++) + " ;\n";
  314. text += " lv2:symbol \"lv2_audio_out_" + String(i+1) + "\" ;\n";
  315. text += " lv2:name \"Audio Output " + String(i+1) + "\" ;\n";
  316. if (i+1 == pluginDesc->audioOuts)
  317. text += " ] ;\n\n";
  318. else
  319. text += " ] ,\n";
  320. }
  321. // -------------------------------------------------------------------
  322. // Parameters
  323. const uint32_t paramCount(pluginDesc->get_parameter_count != nullptr ? pluginDesc->get_parameter_count(pluginHandle) : 0);
  324. if (paramCount > 0)
  325. {
  326. CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_info != nullptr,)
  327. CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_value != nullptr,)
  328. }
  329. for (uint32_t i=0; i < paramCount; ++i)
  330. {
  331. const Parameter* paramInfo(pluginDesc->get_parameter_info(pluginHandle, i));
  332. const String paramName(paramInfo->name != nullptr ? paramInfo->name : "");
  333. const String paramUnit(paramInfo->unit != nullptr ? paramInfo->unit : "");
  334. CARLA_SAFE_ASSERT_RETURN(paramInfo != nullptr,)
  335. if (i == 0)
  336. text += " lv2:port [\n";
  337. else
  338. text += " [\n";
  339. if (paramInfo->hints & PARAMETER_IS_OUTPUT)
  340. text += " a lv2:OutputPort, lv2:ControlPort ;\n";
  341. else
  342. text += " a lv2:InputPort, lv2:ControlPort ;\n";
  343. text += " lv2:index " + String(portIndex++) + " ;\n";
  344. text += " lv2:symbol \"" + nameToSymbol(paramName, i) + "\" ;\n";
  345. if (paramName.isNotEmpty())
  346. text += " lv2:name \"" + paramName + "\" ;\n";
  347. else
  348. text += " lv2:name \"Port " + String(i+1) + "\" ;\n";
  349. text += " lv2:default " + String::formatted("%f", paramInfo->ranges.def) + " ;\n";
  350. text += " lv2:minimum " + String::formatted("%f", paramInfo->ranges.min) + " ;\n";
  351. text += " lv2:maximum " + String::formatted("%f", paramInfo->ranges.max) + " ;\n";
  352. if (paramInfo->hints & PARAMETER_IS_ENABLED)
  353. {
  354. if ((paramInfo->hints & PARAMETER_IS_AUTOMABLE) == 0)
  355. text += " lv2:portProperty <" LV2_PORT_PROPS__expensive "> ;\n";
  356. if (paramInfo->hints & PARAMETER_IS_BOOLEAN)
  357. text += " lv2:portProperty lv2:toggled ;\n";
  358. if (paramInfo->hints & PARAMETER_IS_INTEGER)
  359. text += " lv2:portProperty lv2:integer ;\n";
  360. if (paramInfo->hints & PARAMETER_IS_LOGARITHMIC)
  361. text += " lv2:portProperty <" LV2_PORT_PROPS__logarithmic "> ;\n";
  362. if (paramInfo->hints & PARAMETER_USES_SAMPLE_RATE)
  363. text += " lv2:portProperty lv2:toggled ;\n";
  364. if (paramInfo->hints & PARAMETER_USES_SCALEPOINTS)
  365. text += " lv2:portProperty lv2:enumeration ;\n";
  366. if (paramInfo->hints & PARAMETER_USES_CUSTOM_TEXT)
  367. pass(); // TODO: custom lv2 extension for this
  368. }
  369. else
  370. {
  371. text += " lv2:portProperty <" LV2_PORT_PROPS__notOnGUI "> ;\n";
  372. }
  373. // TODO - scalepoints
  374. if (paramUnit.isNotEmpty())
  375. {
  376. text += " units:unit [\n";
  377. text += " a units:Unit ;\n";
  378. text += " rdfs:label \"" + paramUnit + "\" ;\n";
  379. text += " units:symbol \"" + paramUnit + "\" ;\n";
  380. text += " units:render \"%f " + paramUnit + "\" ;\n";
  381. text += " ] ;\n";
  382. }
  383. if (i+1 == paramCount)
  384. text += " ] ;\n\n";
  385. else
  386. text += " ] ,\n";
  387. }
  388. text += " doap:name \"" + String(pluginDesc->name) + "\" ;\n";
  389. text += " doap:maintainer [ foaf:name \"" + String(pluginDesc->maker) + "\" ] .\n";
  390. // -------------------------------------------------------------------
  391. // Write file now
  392. std::fstream pluginStream(pluginFile.toRawUTF8(), std::ios::out);
  393. pluginStream << text.toRawUTF8();
  394. pluginStream.close();
  395. // -------------------------------------------------------------------
  396. // Cleanup plugin
  397. if (pluginDesc->cleanup != nullptr)
  398. pluginDesc->cleanup(pluginHandle);
  399. }
  400. // -----------------------------------------------------------------------
  401. int main()
  402. {
  403. writeManifestFile();
  404. for (NonRtList<const PluginDescriptor*>::Itenerator it = sPluginDescsMgr.descs.begin(); it.valid(); it.next())
  405. {
  406. const PluginDescriptor*& pluginDesc(*it);
  407. writePluginFile(pluginDesc);
  408. }
  409. carla_stdout("Done.");
  410. return 0;
  411. }
  412. // -----------------------------------------------------------------------