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.

580 lines
20KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 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-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 defined(CARLA_OS_WIN)
  35. # define PLUGIN_EXT ".dll"
  36. #elif defined(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. static 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. static void writeManifestFile(PluginListManager& plm)
  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 (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  98. {
  99. const NativePluginDescriptor* const& pluginDesc(it.getValue());
  100. const String label(pluginDesc->label);
  101. text += "<http://kxstudio.sf.net/carla/plugins/" + label + ">\n";
  102. text += " a lv2:Plugin ;\n";
  103. text += " lv2:binary <carla" PLUGIN_EXT "> ;\n";
  104. text += " rdfs:seeAlso <" + label + ".ttl> .\n";
  105. text += "\n";
  106. }
  107. // -------------------------------------------------------------------
  108. // UI
  109. text += "<http://kxstudio.sf.net/carla/ui>\n";
  110. text += " a <" LV2_EXTERNAL_UI__Widget "> ;\n";
  111. text += " ui:binary <carla" PLUGIN_EXT "> ;\n";
  112. text += " lv2:extensionData ui:idleInterface ,\n";
  113. text += " ui:showInterface ,\n";
  114. text += " <" LV2_PROGRAMS__UIInterface "> ;\n";
  115. text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> .\n";
  116. // -------------------------------------------------------------------
  117. // Write file now
  118. std::fstream manifest("carla.lv2/manifest.ttl", std::ios::out);
  119. manifest << text.toRawUTF8();
  120. manifest.close();
  121. }
  122. // -----------------------------------------------------------------------
  123. static uint32_t host_getBufferSize(NativeHostHandle) { return 512; }
  124. static double host_getSampleRate(NativeHostHandle) { return 44100.0; }
  125. static bool host_isOffline(NativeHostHandle) { return true; }
  126. static intptr_t host_dispatcher(NativeHostHandle, NativeHostDispatcherOpcode, int32_t, intptr_t, void*, float) { return 0; }
  127. static void writePluginFile(const NativePluginDescriptor* const pluginDesc)
  128. {
  129. const String pluginLabel(pluginDesc->label);
  130. const String pluginFile("carla.lv2/" + pluginLabel + ".ttl");
  131. uint32_t portIndex = 0;
  132. String text;
  133. gUsedSymbols.clear();
  134. carla_stdout("Generating data for %s...", pluginDesc->name);
  135. // -------------------------------------------------------------------
  136. // Init plugin
  137. NativeHostDescriptor hostDesc;
  138. hostDesc.handle = nullptr;
  139. hostDesc.resourceDir = "";
  140. hostDesc.uiName = "";
  141. hostDesc.get_buffer_size = host_getBufferSize;
  142. hostDesc.get_sample_rate = host_getSampleRate;
  143. hostDesc.is_offline = host_isOffline;
  144. hostDesc.get_time_info = nullptr;
  145. hostDesc.write_midi_event = nullptr;
  146. hostDesc.ui_parameter_changed = nullptr;
  147. hostDesc.ui_midi_program_changed = nullptr;
  148. hostDesc.ui_custom_data_changed = nullptr;
  149. hostDesc.ui_closed = nullptr;
  150. hostDesc.ui_open_file = nullptr;
  151. hostDesc.ui_save_file = nullptr;
  152. hostDesc.dispatcher = host_dispatcher;
  153. NativePluginHandle pluginHandle = nullptr;
  154. if (! pluginLabel.startsWithIgnoreCase("carla"))
  155. {
  156. pluginHandle = pluginDesc->instantiate(&hostDesc);
  157. CARLA_SAFE_ASSERT_RETURN(pluginHandle != nullptr,)
  158. }
  159. // -------------------------------------------------------------------
  160. // Header
  161. text += "@prefix atom: <" LV2_ATOM_PREFIX "> .\n";
  162. text += "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  163. text += "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  164. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
  165. text += "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
  166. text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  167. text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
  168. text += "@prefix unit: <" LV2_UNITS_PREFIX "> .\n";
  169. text += "\n";
  170. // -------------------------------------------------------------------
  171. // Plugin URI
  172. text += "<http://kxstudio.sf.net/carla/plugins/" + pluginLabel + ">\n";
  173. // -------------------------------------------------------------------
  174. // Category
  175. switch (pluginDesc->category)
  176. {
  177. case NATIVE_PLUGIN_CATEGORY_SYNTH:
  178. text += " a lv2:InstrumentPlugin, lv2:Plugin ;\n";
  179. break;
  180. case NATIVE_PLUGIN_CATEGORY_DELAY:
  181. text += " a lv2:DelayPlugin, lv2:Plugin ;\n";
  182. break;
  183. case NATIVE_PLUGIN_CATEGORY_EQ:
  184. text += " a lv2:EQPlugin, lv2:Plugin ;\n";
  185. break;
  186. case NATIVE_PLUGIN_CATEGORY_FILTER:
  187. text += " a lv2:FilterPlugin, lv2:Plugin ;\n";
  188. break;
  189. case NATIVE_PLUGIN_CATEGORY_DYNAMICS:
  190. text += " a lv2:DynamicsPlugin, lv2:Plugin ;\n";
  191. break;
  192. case NATIVE_PLUGIN_CATEGORY_MODULATOR:
  193. text += " a lv2:ModulatorPlugin, lv2:Plugin ;\n";
  194. break;
  195. case NATIVE_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 & NATIVE_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 & NATIVE_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 & NATIVE_PLUGIN_USES_STATE)
  217. {
  218. text += " ,\n";
  219. text += " <" LV2_STATE__interface ">";
  220. if (pluginDesc->category != NATIVE_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 != NATIVE_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 & NATIVE_PLUGIN_HAS_UI)
  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 & NATIVE_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 & NATIVE_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((pluginHandle != nullptr && 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 NativeParameter* 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 & NATIVE_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 & NATIVE_PARAMETER_IS_ENABLED)
  397. {
  398. if ((paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE) == 0)
  399. text += " lv2:portProperty <" LV2_PORT_PROPS__expensive "> ;\n";
  400. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  401. text += " lv2:portProperty lv2:toggled ;\n";
  402. if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  403. text += " lv2:portProperty lv2:integer ;\n";
  404. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  405. text += " lv2:portProperty <" LV2_PORT_PROPS__logarithmic "> ;\n";
  406. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  407. text += " lv2:portProperty lv2:toggled ;\n";
  408. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  409. text += " lv2:portProperty lv2:enumeration ;\n";
  410. if (paramInfo->hints & NATIVE_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 NativeParameterScalePoint* 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 (pluginHandle != nullptr && pluginDesc->cleanup != nullptr)
  455. pluginDesc->cleanup(pluginHandle);
  456. }
  457. // -----------------------------------------------------------------------
  458. int main()
  459. {
  460. PluginListManager& plm(PluginListManager::getInstance());
  461. writeManifestFile(plm);
  462. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  463. {
  464. const NativePluginDescriptor* const& pluginDesc(it.getValue());
  465. writePluginFile(pluginDesc);
  466. }
  467. carla_stdout("Done.");
  468. return 0;
  469. }
  470. // -----------------------------------------------------------------------