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