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.

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