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.

574 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/worker.h"
  32. #include "lv2/lv2_external_ui.h"
  33. #include "lv2/lv2_programs.h"
  34. #include <fstream>
  35. #if defined(CARLA_OS_WIN)
  36. # define PLUGIN_EXT ".dll"
  37. #elif defined(JUCE_MAC)
  38. # define PLUGIN_EXT ".dylib"
  39. #else
  40. # define PLUGIN_EXT ".so"
  41. #endif
  42. using juce::String;
  43. using juce::StringArray;
  44. using juce::juce_wchar;
  45. // -----------------------------------------------------------------------
  46. // Converts a parameter name to an LV2 compatible symbol
  47. static StringArray gUsedSymbols;
  48. static const String nameToSymbol(const String& name, const uint32_t portIndex)
  49. {
  50. String symbol, trimmedName = name.trim().toLowerCase();
  51. if (trimmedName.isEmpty())
  52. {
  53. symbol += "lv2_port_";
  54. symbol += String(portIndex+1);
  55. }
  56. else
  57. {
  58. for (int i=0; i < trimmedName.length(); ++i)
  59. {
  60. const juce_wchar c = trimmedName[i];
  61. if (i == 0 && std::isdigit(c))
  62. symbol += "_";
  63. else if (std::isalpha(c) || std::isdigit(c))
  64. symbol += c;
  65. else
  66. symbol += "_";
  67. }
  68. }
  69. // Do not allow identical symbols
  70. if (gUsedSymbols.contains(symbol))
  71. {
  72. int offset = 2;
  73. String offsetStr = "_2";
  74. symbol += offsetStr;
  75. while (gUsedSymbols.contains(symbol))
  76. {
  77. offset += 1;
  78. String newOffsetStr = "_" + String(offset);
  79. symbol = symbol.replace(offsetStr, newOffsetStr);
  80. offsetStr = newOffsetStr;
  81. }
  82. }
  83. gUsedSymbols.add(symbol);
  84. return symbol;
  85. }
  86. // -----------------------------------------------------------------------
  87. static void writeManifestFile(PluginListManager& plm)
  88. {
  89. String text;
  90. // -------------------------------------------------------------------
  91. // Header
  92. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
  93. text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
  94. text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
  95. text += "\n";
  96. // -------------------------------------------------------------------
  97. // Plugins
  98. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  99. {
  100. const NativePluginDescriptor* const& pluginDesc(it.getValue());
  101. const String label(pluginDesc->label);
  102. text += "<http://kxstudio.sf.net/carla/plugins/" + label + ">\n";
  103. text += " a lv2:Plugin ;\n";
  104. text += " lv2:binary <carla" PLUGIN_EXT "> ;\n";
  105. text += " rdfs:seeAlso <" + label + ".ttl> .\n";
  106. text += "\n";
  107. }
  108. // -------------------------------------------------------------------
  109. // UI
  110. text += "<http://kxstudio.sf.net/carla/ui>\n";
  111. text += " a <" LV2_EXTERNAL_UI__Widget "> ;\n";
  112. text += " ui:binary <carla" PLUGIN_EXT "> ;\n";
  113. text += " lv2:extensionData ui:idleInterface ,\n";
  114. text += " ui:showInterface ,\n";
  115. text += " <" LV2_PROGRAMS__UIInterface "> ;\n";
  116. text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> .\n";
  117. // -------------------------------------------------------------------
  118. // Write file now
  119. std::fstream manifest("carla.lv2/manifest.ttl", std::ios::out);
  120. manifest << text.toRawUTF8();
  121. manifest.close();
  122. }
  123. // -----------------------------------------------------------------------
  124. static uint32_t host_getBufferSize(NativeHostHandle) { return 512; }
  125. static double host_getSampleRate(NativeHostHandle) { return 44100.0; }
  126. static bool host_isOffline(NativeHostHandle) { return true; }
  127. static intptr_t host_dispatcher(NativeHostHandle, NativeHostDispatcherOpcode, int32_t, intptr_t, void*, float) { return 0; }
  128. static void writePluginFile(const NativePluginDescriptor* const pluginDesc)
  129. {
  130. const String pluginLabel(pluginDesc->label);
  131. const String pluginFile("carla.lv2/" + pluginLabel + ".ttl");
  132. uint32_t portIndex = 0;
  133. String text;
  134. gUsedSymbols.clear();
  135. carla_stdout("Generating data for %s...", pluginDesc->name);
  136. // -------------------------------------------------------------------
  137. // Init plugin
  138. NativeHostDescriptor hostDesc;
  139. hostDesc.handle = nullptr;
  140. hostDesc.resourceDir = "";
  141. hostDesc.uiName = "";
  142. hostDesc.get_buffer_size = host_getBufferSize;
  143. hostDesc.get_sample_rate = host_getSampleRate;
  144. hostDesc.is_offline = host_isOffline;
  145. hostDesc.get_time_info = nullptr;
  146. hostDesc.write_midi_event = nullptr;
  147. hostDesc.ui_parameter_changed = nullptr;
  148. hostDesc.ui_midi_program_changed = nullptr;
  149. hostDesc.ui_custom_data_changed = nullptr;
  150. hostDesc.ui_closed = nullptr;
  151. hostDesc.ui_open_file = nullptr;
  152. hostDesc.ui_save_file = nullptr;
  153. hostDesc.dispatcher = host_dispatcher;
  154. NativePluginHandle pluginHandle = nullptr;
  155. if (! pluginLabel.startsWithIgnoreCase("carla"))
  156. {
  157. pluginHandle = pluginDesc->instantiate(&hostDesc);
  158. CARLA_SAFE_ASSERT_RETURN(pluginHandle != nullptr,)
  159. }
  160. // -------------------------------------------------------------------
  161. // Header
  162. text += "@prefix atom: <" LV2_ATOM_PREFIX "> .\n";
  163. text += "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
  164. text += "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
  165. text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
  166. text += "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\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 URI
  173. text += "<http://kxstudio.sf.net/carla/plugins/" + pluginLabel + ">\n";
  174. // -------------------------------------------------------------------
  175. // Category
  176. switch (pluginDesc->category)
  177. {
  178. case NATIVE_PLUGIN_CATEGORY_SYNTH:
  179. text += " a lv2:InstrumentPlugin, lv2:Plugin ;\n";
  180. break;
  181. case NATIVE_PLUGIN_CATEGORY_DELAY:
  182. text += " a lv2:DelayPlugin, lv2:Plugin ;\n";
  183. break;
  184. case NATIVE_PLUGIN_CATEGORY_EQ:
  185. text += " a lv2:EQPlugin, lv2:Plugin ;\n";
  186. break;
  187. case NATIVE_PLUGIN_CATEGORY_FILTER:
  188. text += " a lv2:FilterPlugin, lv2:Plugin ;\n";
  189. break;
  190. case NATIVE_PLUGIN_CATEGORY_DYNAMICS:
  191. text += " a lv2:DynamicsPlugin, lv2:Plugin ;\n";
  192. break;
  193. case NATIVE_PLUGIN_CATEGORY_MODULATOR:
  194. text += " a lv2:ModulatorPlugin, lv2:Plugin ;\n";
  195. break;
  196. case NATIVE_PLUGIN_CATEGORY_UTILITY:
  197. text += " a lv2:UtilityPlugin, lv2:Plugin ;\n";
  198. break;
  199. default:
  200. text += " a lv2:Plugin ;\n";
  201. break;
  202. }
  203. text += "\n";
  204. // -------------------------------------------------------------------
  205. // Features
  206. // optional
  207. if (pluginDesc->hints & NATIVE_PLUGIN_IS_RTSAFE)
  208. text += " lv2:optionalFeature <" LV2_CORE__hardRTCapable "> ;\n\n";
  209. // required
  210. text += " lv2:requiredFeature <" LV2_BUF_SIZE__boundedBlockLength "> ,\n";
  211. if (pluginDesc->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  212. text += " <" LV2_BUF_SIZE__fixedBlockLength "> ,\n";
  213. if (pluginDesc->hints & NATIVE_PLUGIN_NEEDS_DSP_IDLE)
  214. text += " <" LV2_WORKER__schedule "> ,\n";
  215. text += " <" LV2_OPTIONS__options "> ,\n";
  216. text += " <" LV2_URID__map "> ;\n";
  217. text += "\n";
  218. // -------------------------------------------------------------------
  219. // Extensions
  220. text += " lv2:extensionData <" LV2_OPTIONS__interface "> ;";
  221. if (pluginDesc->hints & NATIVE_PLUGIN_USES_STATE)
  222. text += " lv2:extensionData <" LV2_STATE__interface "> ;";
  223. if (pluginDesc->hints & NATIVE_PLUGIN_NEEDS_DSP_IDLE)
  224. text += " lv2:extensionData <" LV2_WORKER__interface "> ;";
  225. if (pluginDesc->category != NATIVE_PLUGIN_CATEGORY_SYNTH)
  226. text += " lv2:extensionData <" LV2_PROGRAMS__Interface "> ;\n";
  227. text += "\n";
  228. // -------------------------------------------------------------------
  229. // UIs
  230. if (pluginDesc->hints & NATIVE_PLUGIN_HAS_UI)
  231. {
  232. text += " ui:ui <http://kxstudio.sf.net/carla/ui> ;\n";
  233. text += "\n";
  234. }
  235. // -------------------------------------------------------------------
  236. // First MIDI/Time port
  237. if (pluginDesc->midiIns > 0 || (pluginDesc->hints & NATIVE_PLUGIN_USES_TIME) != 0)
  238. {
  239. text += " lv2:port [\n";
  240. text += " a lv2:InputPort, atom:AtomPort ;\n";
  241. text += " atom:bufferType atom:Sequence ;\n";
  242. if (pluginDesc->midiIns > 0 && (pluginDesc->hints & NATIVE_PLUGIN_USES_TIME) != 0)
  243. {
  244. text += " atom:supports <" LV2_MIDI__MidiEvent "> ,\n";
  245. text += " <" LV2_TIME__Position "> ;\n";
  246. }
  247. else if (pluginDesc->midiIns > 0)
  248. text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
  249. else
  250. text += " atom:supports <" LV2_TIME__Position "> ;\n";
  251. text += " lv2:designation lv2:control ;\n";
  252. text += " lv2:index " + String(portIndex++) + " ;\n";
  253. if (pluginDesc->midiIns > 1)
  254. {
  255. text += " lv2:symbol \"lv2_events_in_1\" ;\n";
  256. text += " lv2:name \"Events Input #1\" ;\n";
  257. }
  258. else
  259. {
  260. text += " lv2:symbol \"lv2_events_in\" ;\n";
  261. text += " lv2:name \"Events Input\" ;\n";
  262. }
  263. text += " ] ;\n\n";
  264. }
  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((pluginHandle != nullptr && 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 NativeParameter* 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 & NATIVE_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 & NATIVE_PARAMETER_IS_ENABLED)
  389. {
  390. if ((paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE) == 0)
  391. text += " lv2:portProperty <" LV2_PORT_PROPS__expensive "> ;\n";
  392. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  393. text += " lv2:portProperty lv2:toggled ;\n";
  394. if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  395. text += " lv2:portProperty lv2:integer ;\n";
  396. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  397. text += " lv2:portProperty <" LV2_PORT_PROPS__logarithmic "> ;\n";
  398. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  399. text += " lv2:portProperty lv2:toggled ;\n";
  400. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  401. text += " lv2:portProperty lv2:enumeration ;\n";
  402. if (paramInfo->hints & NATIVE_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 NativeParameterScalePoint* 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 (pluginHandle != nullptr && pluginDesc->cleanup != nullptr)
  447. pluginDesc->cleanup(pluginHandle);
  448. }
  449. // -----------------------------------------------------------------------
  450. int main()
  451. {
  452. PluginListManager& plm(PluginListManager::getInstance());
  453. writeManifestFile(plm);
  454. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin(); it.valid(); it.next())
  455. {
  456. const NativePluginDescriptor* const& pluginDesc(it.getValue());
  457. writePluginFile(pluginDesc);
  458. }
  459. carla_stdout("Done.");
  460. return 0;
  461. }
  462. // -----------------------------------------------------------------------