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.

578 lines
19KB

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