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.

1672 lines
52KB

  1. /*
  2. * Carla Plugin discovery
  3. * Copyright (C) 2011-2017 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. #include "CarlaBackendUtils.hpp"
  18. #include "CarlaLibUtils.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "LinkedList.hpp"
  22. #ifdef BUILD_BRIDGE
  23. # undef HAVE_FLUIDSYNTH
  24. # undef HAVE_LINUXSAMPLER
  25. #endif
  26. #include "CarlaLadspaUtils.hpp"
  27. #include "CarlaDssiUtils.cpp"
  28. #include "CarlaLv2Utils.hpp"
  29. #include "CarlaVstUtils.hpp"
  30. #ifndef BUILD_BRIDGE
  31. // need to include this before linuxsampler
  32. # define CARLA_UTILS_CACHED_PLUGINS_ONLY
  33. # include "CarlaUtils.cpp"
  34. #endif
  35. #ifdef HAVE_FLUIDSYNTH
  36. # include <fluidsynth.h>
  37. #endif
  38. #ifdef HAVE_LINUXSAMPLER
  39. # include "linuxsampler/EngineFactory.h"
  40. #endif
  41. #include <iostream>
  42. #include "water/water.h"
  43. #define DISCOVERY_OUT(x, y) std::cout << "\ncarla-discovery::" << x << "::" << y << std::endl;
  44. using water::CharPointer_UTF8;
  45. using water::File;
  46. using water::StringArray;
  47. CARLA_BACKEND_USE_NAMESPACE
  48. // --------------------------------------------------------------------------
  49. // Dummy values to test plugins with
  50. static const uint32_t kBufferSize = 512;
  51. static const double kSampleRate = 44100.0;
  52. static const int32_t kSampleRatei = 44100;
  53. static const float kSampleRatef = 44100.0f;
  54. // --------------------------------------------------------------------------
  55. // Don't print ELF/EXE related errors since discovery can find multi-architecture binaries
  56. static void print_lib_error(const char* const filename)
  57. {
  58. const char* const error(lib_error(filename));
  59. if (error != nullptr && std::strstr(error, "wrong ELF class") == nullptr && std::strstr(error, "Bad EXE format") == nullptr)
  60. DISCOVERY_OUT("error", error);
  61. }
  62. // --------------------------------------------------------------------------
  63. // VST stuff
  64. // Check if plugin is currently processing
  65. static bool gVstIsProcessing = false;
  66. // Check if plugin needs idle
  67. static bool gVstNeedsIdle = false;
  68. // Check if plugin wants midi
  69. static bool gVstWantsMidi = false;
  70. // Check if plugin wants time
  71. static bool gVstWantsTime = false;
  72. // Current uniqueId for VST shell plugins
  73. static intptr_t gVstCurrentUniqueId = 0;
  74. // Supported Carla features
  75. static intptr_t vstHostCanDo(const char* const feature)
  76. {
  77. carla_debug("vstHostCanDo(\"%s\")", feature);
  78. if (std::strcmp(feature, "supplyIdle") == 0)
  79. return 1;
  80. if (std::strcmp(feature, "sendVstEvents") == 0)
  81. return 1;
  82. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  83. return 1;
  84. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  85. return 1;
  86. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  87. {
  88. gVstWantsTime = true;
  89. return 1;
  90. }
  91. if (std::strcmp(feature, "receiveVstEvents") == 0)
  92. return 1;
  93. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  94. return 1;
  95. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  96. return -1;
  97. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  98. return -1;
  99. if (std::strcmp(feature, "acceptIOChanges") == 0)
  100. return 1;
  101. if (std::strcmp(feature, "sizeWindow") == 0)
  102. return 1;
  103. if (std::strcmp(feature, "offline") == 0)
  104. return -1;
  105. if (std::strcmp(feature, "openFileSelector") == 0)
  106. return -1;
  107. if (std::strcmp(feature, "closeFileSelector") == 0)
  108. return -1;
  109. if (std::strcmp(feature, "startStopProcess") == 0)
  110. return 1;
  111. if (std::strcmp(feature, "supportShell") == 0)
  112. return 1;
  113. if (std::strcmp(feature, "shellCategory") == 0)
  114. return 1;
  115. // non-official features found in some plugins:
  116. // "asyncProcessing"
  117. // "editFile"
  118. // unimplemented
  119. carla_stderr("vstHostCanDo(\"%s\") - unknown feature", feature);
  120. return 0;
  121. }
  122. // Host-side callback
  123. static intptr_t VSTCALLBACK vstHostCallback(AEffect* const effect, const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  124. {
  125. carla_debug("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  126. static VstTimeInfo timeInfo;
  127. intptr_t ret = 0;
  128. switch (opcode)
  129. {
  130. case audioMasterAutomate:
  131. ret = 1;
  132. break;
  133. case audioMasterVersion:
  134. ret = kVstVersion;
  135. break;
  136. case audioMasterCurrentId:
  137. ret = gVstCurrentUniqueId;
  138. break;
  139. case DECLARE_VST_DEPRECATED(audioMasterWantMidi):
  140. if (gVstWantsMidi) { DISCOVERY_OUT("warning", "Plugin requested MIDI more than once"); }
  141. gVstWantsMidi = true;
  142. ret = 1;
  143. break;
  144. case audioMasterGetTime:
  145. if (! gVstIsProcessing) { DISCOVERY_OUT("warning", "Plugin requested timeInfo out of process"); }
  146. if (! gVstWantsTime) { DISCOVERY_OUT("warning", "Plugin requested timeInfo but didn't ask if host could do \"sendVstTimeInfo\""); }
  147. carla_zeroStruct(timeInfo);
  148. timeInfo.sampleRate = kSampleRate;
  149. // Tempo
  150. timeInfo.tempo = 120.0;
  151. timeInfo.flags |= kVstTempoValid;
  152. // Time Signature
  153. timeInfo.timeSigNumerator = 4;
  154. timeInfo.timeSigDenominator = 4;
  155. timeInfo.flags |= kVstTimeSigValid;
  156. ret = (intptr_t)&timeInfo;
  157. break;
  158. case DECLARE_VST_DEPRECATED(audioMasterTempoAt):
  159. ret = 120 * 10000;
  160. break;
  161. case DECLARE_VST_DEPRECATED(audioMasterGetNumAutomatableParameters):
  162. ret = carla_minPositive(effect->numParams, static_cast<int>(MAX_DEFAULT_PARAMETERS));
  163. break;
  164. case DECLARE_VST_DEPRECATED(audioMasterGetParameterQuantization):
  165. ret = 1; // full single float precision
  166. break;
  167. case DECLARE_VST_DEPRECATED(audioMasterNeedIdle):
  168. if (gVstNeedsIdle) { DISCOVERY_OUT("warning", "Plugin requested idle more than once"); }
  169. gVstNeedsIdle = true;
  170. ret = 1;
  171. break;
  172. case audioMasterGetSampleRate:
  173. ret = kSampleRatei;
  174. break;
  175. case audioMasterGetBlockSize:
  176. ret = kBufferSize;
  177. break;
  178. case DECLARE_VST_DEPRECATED(audioMasterWillReplaceOrAccumulate):
  179. ret = 1; // replace
  180. break;
  181. case audioMasterGetCurrentProcessLevel:
  182. ret = gVstIsProcessing ? kVstProcessLevelRealtime : kVstProcessLevelUser;
  183. break;
  184. case audioMasterGetAutomationState:
  185. ret = kVstAutomationOff;
  186. break;
  187. case audioMasterGetVendorString:
  188. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  189. std::strcpy((char*)ptr, "falkTX");
  190. ret = 1;
  191. break;
  192. case audioMasterGetProductString:
  193. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  194. std::strcpy((char*)ptr, "Carla-Discovery");
  195. ret = 1;
  196. break;
  197. case audioMasterGetVendorVersion:
  198. ret = CARLA_VERSION_HEX;
  199. break;
  200. case audioMasterCanDo:
  201. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  202. ret = vstHostCanDo((const char*)ptr);
  203. break;
  204. case audioMasterGetLanguage:
  205. ret = kVstLangEnglish;
  206. break;
  207. default:
  208. carla_stdout("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  209. break;
  210. }
  211. return ret;
  212. }
  213. #ifdef HAVE_LINUXSAMPLER
  214. // --------------------------------------------------------------------------
  215. // LinuxSampler stuff
  216. class LinuxSamplerScopedEngine
  217. {
  218. public:
  219. LinuxSamplerScopedEngine(const char* const filename, const char* const stype)
  220. : fEngine(nullptr)
  221. {
  222. using namespace LinuxSampler;
  223. try {
  224. fEngine = EngineFactory::Create(stype);
  225. }
  226. catch (const Exception& e)
  227. {
  228. DISCOVERY_OUT("error", e.what());
  229. return;
  230. }
  231. if (fEngine == nullptr)
  232. return;
  233. InstrumentManager* const insMan(fEngine->GetInstrumentManager());
  234. if (insMan == nullptr)
  235. {
  236. DISCOVERY_OUT("error", "Failed to get LinuxSampler instrument manager");
  237. return;
  238. }
  239. std::vector<InstrumentManager::instrument_id_t> ids;
  240. try {
  241. ids = insMan->GetInstrumentFileContent(filename);
  242. }
  243. catch (const InstrumentManagerException& e)
  244. {
  245. DISCOVERY_OUT("error", e.what());
  246. return;
  247. }
  248. if (ids.size() == 0)
  249. {
  250. DISCOVERY_OUT("error", "Failed to find any instruments");
  251. return;
  252. }
  253. InstrumentManager::instrument_info_t info;
  254. try {
  255. info = insMan->GetInstrumentInfo(ids[0]);
  256. }
  257. catch (const InstrumentManagerException& e)
  258. {
  259. DISCOVERY_OUT("error", e.what());
  260. return;
  261. }
  262. outputInfo(&info, nullptr, ids.size() > 1);
  263. }
  264. ~LinuxSamplerScopedEngine()
  265. {
  266. if (fEngine != nullptr)
  267. {
  268. LinuxSampler::EngineFactory::Destroy(fEngine);
  269. fEngine = nullptr;
  270. }
  271. }
  272. static void outputInfo(const LinuxSampler::InstrumentManager::instrument_info_t* const info, const char* const basename, const bool has16Outs)
  273. {
  274. CarlaString name;
  275. const char* label;
  276. if (info != nullptr)
  277. {
  278. name = info->InstrumentName.c_str();
  279. label = info->Product.c_str();
  280. }
  281. else
  282. {
  283. name = basename;
  284. label = basename;
  285. }
  286. // 2 channels
  287. DISCOVERY_OUT("init", "-----------");
  288. DISCOVERY_OUT("build", BINARY_NATIVE);
  289. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  290. DISCOVERY_OUT("name", name.buffer());
  291. DISCOVERY_OUT("label", label);
  292. if (info != nullptr)
  293. DISCOVERY_OUT("maker", info->Artists);
  294. DISCOVERY_OUT("audio.outs", 2);
  295. DISCOVERY_OUT("midi.ins", 1);
  296. DISCOVERY_OUT("end", "------------");
  297. // 16 channels
  298. if (name.isEmpty() || ! has16Outs)
  299. return;
  300. name += " (16 outputs)";
  301. DISCOVERY_OUT("init", "-----------");
  302. DISCOVERY_OUT("build", BINARY_NATIVE);
  303. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  304. DISCOVERY_OUT("name", name.buffer());
  305. DISCOVERY_OUT("label", label);
  306. if (info != nullptr)
  307. DISCOVERY_OUT("maker", info->Artists);
  308. DISCOVERY_OUT("audio.outs", 32);
  309. DISCOVERY_OUT("midi.ins", 1);
  310. DISCOVERY_OUT("end", "------------");
  311. }
  312. private:
  313. LinuxSampler::Engine* fEngine;
  314. CARLA_PREVENT_HEAP_ALLOCATION
  315. CARLA_DECLARE_NON_COPY_CLASS(LinuxSamplerScopedEngine)
  316. };
  317. #endif // HAVE_LINUXSAMPLER
  318. // ------------------------------ Plugin Checks -----------------------------
  319. #ifndef BUILD_BRIDGE
  320. static void do_cached_check(const PluginType type)
  321. {
  322. const char* const plugPath = (type == PLUGIN_LV2) ? std::getenv("LV2_PATH") : nullptr;
  323. const uint count = carla_get_cached_plugin_count(type, plugPath);
  324. for (uint i=0; i<count; ++i)
  325. {
  326. const CarlaCachedPluginInfo* pinfo(carla_get_cached_plugin_info(type, i));
  327. CARLA_SAFE_ASSERT_CONTINUE(pinfo != nullptr);
  328. DISCOVERY_OUT("init", "-----------");
  329. DISCOVERY_OUT("build", BINARY_NATIVE);
  330. DISCOVERY_OUT("hints", pinfo->hints);
  331. DISCOVERY_OUT("name", pinfo->name);
  332. DISCOVERY_OUT("maker", pinfo->maker);
  333. DISCOVERY_OUT("label", pinfo->label);
  334. DISCOVERY_OUT("audio.ins", pinfo->audioIns);
  335. DISCOVERY_OUT("audio.outs", pinfo->audioOuts);
  336. DISCOVERY_OUT("midi.ins", pinfo->midiIns);
  337. DISCOVERY_OUT("midi.outs", pinfo->midiOuts);
  338. DISCOVERY_OUT("parameters.ins", pinfo->parameterIns);
  339. DISCOVERY_OUT("parameters.outs", pinfo->parameterOuts);
  340. DISCOVERY_OUT("end", "------------");
  341. }
  342. }
  343. #endif
  344. static void do_ladspa_check(lib_t& libHandle, const char* const filename, const bool doInit)
  345. {
  346. LADSPA_Descriptor_Function descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  347. if (descFn == nullptr)
  348. {
  349. DISCOVERY_OUT("error", "Not a LADSPA plugin");
  350. return;
  351. }
  352. const LADSPA_Descriptor* descriptor;
  353. {
  354. descriptor = descFn(0);
  355. if (descriptor == nullptr)
  356. {
  357. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  358. return;
  359. }
  360. if (doInit && descriptor->instantiate != nullptr && descriptor->cleanup != nullptr)
  361. {
  362. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  363. if (handle == nullptr)
  364. {
  365. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  366. return;
  367. }
  368. descriptor->cleanup(handle);
  369. lib_close(libHandle);
  370. libHandle = lib_open(filename);
  371. if (libHandle == nullptr)
  372. {
  373. print_lib_error(filename);
  374. return;
  375. }
  376. descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  377. if (descFn == nullptr)
  378. {
  379. DISCOVERY_OUT("error", "Not a LADSPA plugin (#2)");
  380. return;
  381. }
  382. }
  383. }
  384. unsigned long i = 0;
  385. while ((descriptor = descFn(i++)) != nullptr)
  386. {
  387. if (descriptor->instantiate == nullptr)
  388. {
  389. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no instantiate()");
  390. continue;
  391. }
  392. if (descriptor->cleanup == nullptr)
  393. {
  394. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no cleanup()");
  395. continue;
  396. }
  397. if (descriptor->run == nullptr)
  398. {
  399. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no run()");
  400. continue;
  401. }
  402. if (! LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  403. {
  404. DISCOVERY_OUT("warning", "Plugin '" << descriptor->Name << "' is not hard real-time capable");
  405. }
  406. uint hints = 0x0;
  407. int audioIns = 0;
  408. int audioOuts = 0;
  409. int audioTotal = 0;
  410. int parametersIns = 0;
  411. int parametersOuts = 0;
  412. int parametersTotal = 0;
  413. if (LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  414. hints |= PLUGIN_IS_RTSAFE;
  415. for (unsigned long j=0; j < descriptor->PortCount; ++j)
  416. {
  417. CARLA_ASSERT(descriptor->PortNames[j] != nullptr);
  418. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  419. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  420. {
  421. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  422. audioIns += 1;
  423. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  424. audioOuts += 1;
  425. audioTotal += 1;
  426. }
  427. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  428. {
  429. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  430. parametersIns += 1;
  431. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(descriptor->PortNames[j], "latency") != 0 && std::strcmp(descriptor->PortNames[j], "_latency") != 0)
  432. parametersOuts += 1;
  433. parametersTotal += 1;
  434. }
  435. }
  436. if (doInit)
  437. {
  438. // -----------------------------------------------------------------------
  439. // start crash-free plugin test
  440. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  441. if (handle == nullptr)
  442. {
  443. DISCOVERY_OUT("error", "Failed to init LADSPA plugin");
  444. continue;
  445. }
  446. // Test quick init and cleanup
  447. descriptor->cleanup(handle);
  448. handle = descriptor->instantiate(descriptor, kSampleRatei);
  449. if (handle == nullptr)
  450. {
  451. DISCOVERY_OUT("error", "Failed to init LADSPA plugin (#2)");
  452. continue;
  453. }
  454. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  455. LADSPA_Data bufferParams[parametersTotal];
  456. LADSPA_Data min, max, def;
  457. for (unsigned long j=0, iA=0, iC=0; j < descriptor->PortCount; ++j)
  458. {
  459. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  460. const LADSPA_PortRangeHint portRangeHints = descriptor->PortRangeHints[j];
  461. const char* const portName = descriptor->PortNames[j];
  462. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  463. {
  464. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  465. descriptor->connect_port(handle, j, bufferAudio[iA++]);
  466. }
  467. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  468. {
  469. // min value
  470. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  471. min = portRangeHints.LowerBound;
  472. else
  473. min = 0.0f;
  474. // max value
  475. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  476. max = portRangeHints.UpperBound;
  477. else
  478. max = 1.0f;
  479. if (min > max)
  480. {
  481. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  482. max = min + 0.1f;
  483. }
  484. else if (carla_isEqual(min, max))
  485. {
  486. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  487. max = min + 0.1f;
  488. }
  489. // default value
  490. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  491. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  492. {
  493. min *= kSampleRatef;
  494. max *= kSampleRatef;
  495. def *= kSampleRatef;
  496. }
  497. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  498. {
  499. // latency parameter
  500. def = 0.0f;
  501. }
  502. else
  503. {
  504. if (def < min)
  505. def = min;
  506. else if (def > max)
  507. def = max;
  508. }
  509. bufferParams[iC] = def;
  510. descriptor->connect_port(handle, j, &bufferParams[iC++]);
  511. }
  512. }
  513. if (descriptor->activate != nullptr)
  514. descriptor->activate(handle);
  515. descriptor->run(handle, kBufferSize);
  516. if (descriptor->deactivate != nullptr)
  517. descriptor->deactivate(handle);
  518. descriptor->cleanup(handle);
  519. // end crash-free plugin test
  520. // -----------------------------------------------------------------------
  521. }
  522. DISCOVERY_OUT("init", "-----------");
  523. DISCOVERY_OUT("build", BINARY_NATIVE);
  524. DISCOVERY_OUT("hints", hints);
  525. DISCOVERY_OUT("name", descriptor->Name);
  526. DISCOVERY_OUT("label", descriptor->Label);
  527. DISCOVERY_OUT("maker", descriptor->Maker);
  528. DISCOVERY_OUT("uniqueId", descriptor->UniqueID);
  529. DISCOVERY_OUT("audio.ins", audioIns);
  530. DISCOVERY_OUT("audio.outs", audioOuts);
  531. DISCOVERY_OUT("parameters.ins", parametersIns);
  532. DISCOVERY_OUT("parameters.outs", parametersOuts);
  533. DISCOVERY_OUT("end", "------------");
  534. }
  535. }
  536. static void do_dssi_check(lib_t& libHandle, const char* const filename, const bool doInit)
  537. {
  538. DSSI_Descriptor_Function descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  539. if (descFn == nullptr)
  540. {
  541. DISCOVERY_OUT("error", "Not a DSSI plugin");
  542. return;
  543. }
  544. const DSSI_Descriptor* descriptor;
  545. {
  546. descriptor = descFn(0);
  547. if (descriptor == nullptr)
  548. {
  549. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  550. return;
  551. }
  552. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  553. if (ldescriptor == nullptr)
  554. {
  555. DISCOVERY_OUT("error", "DSSI plugin doesn't provide the LADSPA interface");
  556. return;
  557. }
  558. if (doInit && ldescriptor->instantiate != nullptr && ldescriptor->cleanup != nullptr)
  559. {
  560. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  561. if (handle == nullptr)
  562. {
  563. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  564. return;
  565. }
  566. ldescriptor->cleanup(handle);
  567. lib_close(libHandle);
  568. libHandle = lib_open(filename);
  569. if (libHandle == nullptr)
  570. {
  571. print_lib_error(filename);
  572. return;
  573. }
  574. descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  575. if (descFn == nullptr)
  576. {
  577. DISCOVERY_OUT("error", "Not a DSSI plugin (#2)");
  578. return;
  579. }
  580. }
  581. }
  582. unsigned long i = 0;
  583. while ((descriptor = descFn(i++)) != nullptr)
  584. {
  585. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  586. if (ldescriptor == nullptr)
  587. {
  588. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no LADSPA interface");
  589. continue;
  590. }
  591. if (descriptor->DSSI_API_Version != DSSI_VERSION_MAJOR)
  592. {
  593. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' uses an unsupported DSSI spec version " << descriptor->DSSI_API_Version);
  594. continue;
  595. }
  596. if (ldescriptor->instantiate == nullptr)
  597. {
  598. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no instantiate()");
  599. continue;
  600. }
  601. if (ldescriptor->cleanup == nullptr)
  602. {
  603. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no cleanup()");
  604. continue;
  605. }
  606. if (ldescriptor->run == nullptr && descriptor->run_synth == nullptr)
  607. {
  608. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no run() or run_synth()");
  609. continue;
  610. }
  611. if (descriptor->run_synth == nullptr && descriptor->run_multiple_synths != nullptr)
  612. {
  613. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' requires run_multiple_synths which is not supported");
  614. continue;
  615. }
  616. if (! LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  617. {
  618. DISCOVERY_OUT("warning", "Plugin '" << ldescriptor->Name << "' is not hard real-time capable");
  619. }
  620. uint hints = 0x0;
  621. int audioIns = 0;
  622. int audioOuts = 0;
  623. int audioTotal = 0;
  624. int midiIns = 0;
  625. int parametersIns = 0;
  626. int parametersOuts = 0;
  627. int parametersTotal = 0;
  628. if (LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  629. hints |= PLUGIN_IS_RTSAFE;
  630. for (unsigned long j=0; j < ldescriptor->PortCount; ++j)
  631. {
  632. CARLA_ASSERT(ldescriptor->PortNames[j] != nullptr);
  633. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  634. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  635. {
  636. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  637. audioIns += 1;
  638. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  639. audioOuts += 1;
  640. audioTotal += 1;
  641. }
  642. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  643. {
  644. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  645. parametersIns += 1;
  646. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(ldescriptor->PortNames[j], "latency") != 0 && std::strcmp(ldescriptor->PortNames[j], "_latency") != 0)
  647. parametersOuts += 1;
  648. parametersTotal += 1;
  649. }
  650. }
  651. if (descriptor->run_synth != nullptr)
  652. midiIns = 1;
  653. if (midiIns > 0 && audioIns == 0 && audioOuts > 0)
  654. hints |= PLUGIN_IS_SYNTH;
  655. if (const char* const ui = find_dssi_ui(filename, ldescriptor->Label))
  656. {
  657. hints |= PLUGIN_HAS_CUSTOM_UI;
  658. delete[] ui;
  659. }
  660. if (doInit)
  661. {
  662. // -----------------------------------------------------------------------
  663. // start crash-free plugin test
  664. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  665. if (handle == nullptr)
  666. {
  667. DISCOVERY_OUT("error", "Failed to init DSSI plugin");
  668. continue;
  669. }
  670. // Test quick init and cleanup
  671. ldescriptor->cleanup(handle);
  672. handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  673. if (handle == nullptr)
  674. {
  675. DISCOVERY_OUT("error", "Failed to init DSSI plugin (#2)");
  676. continue;
  677. }
  678. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  679. LADSPA_Data bufferParams[parametersTotal];
  680. LADSPA_Data min, max, def;
  681. for (unsigned long j=0, iA=0, iC=0; j < ldescriptor->PortCount; ++j)
  682. {
  683. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  684. const LADSPA_PortRangeHint portRangeHints = ldescriptor->PortRangeHints[j];
  685. const char* const portName = ldescriptor->PortNames[j];
  686. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  687. {
  688. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  689. ldescriptor->connect_port(handle, j, bufferAudio[iA++]);
  690. }
  691. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  692. {
  693. // min value
  694. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  695. min = portRangeHints.LowerBound;
  696. else
  697. min = 0.0f;
  698. // max value
  699. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  700. max = portRangeHints.UpperBound;
  701. else
  702. max = 1.0f;
  703. if (min > max)
  704. {
  705. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  706. max = min + 0.1f;
  707. }
  708. else if (carla_isEqual(min, max))
  709. {
  710. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  711. max = min + 0.1f;
  712. }
  713. // default value
  714. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  715. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  716. {
  717. min *= kSampleRatef;
  718. max *= kSampleRatef;
  719. def *= kSampleRatef;
  720. }
  721. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  722. {
  723. // latency parameter
  724. def = 0.0f;
  725. }
  726. else
  727. {
  728. if (def < min)
  729. def = min;
  730. else if (def > max)
  731. def = max;
  732. }
  733. bufferParams[iC] = def;
  734. ldescriptor->connect_port(handle, j, &bufferParams[iC++]);
  735. }
  736. }
  737. // select first midi-program if available
  738. if (descriptor->get_program != nullptr && descriptor->select_program != nullptr)
  739. {
  740. if (const DSSI_Program_Descriptor* const pDesc = descriptor->get_program(handle, 0))
  741. descriptor->select_program(handle, pDesc->Bank, pDesc->Program);
  742. }
  743. if (ldescriptor->activate != nullptr)
  744. ldescriptor->activate(handle);
  745. if (descriptor->run_synth != nullptr)
  746. {
  747. snd_seq_event_t midiEvents[2];
  748. carla_zeroStructs(midiEvents, 2);
  749. const unsigned long midiEventCount = 2;
  750. midiEvents[0].type = SND_SEQ_EVENT_NOTEON;
  751. midiEvents[0].data.note.note = 64;
  752. midiEvents[0].data.note.velocity = 100;
  753. midiEvents[1].type = SND_SEQ_EVENT_NOTEOFF;
  754. midiEvents[1].data.note.note = 64;
  755. midiEvents[1].data.note.velocity = 0;
  756. midiEvents[1].time.tick = kBufferSize/2;
  757. descriptor->run_synth(handle, kBufferSize, midiEvents, midiEventCount);
  758. }
  759. else
  760. ldescriptor->run(handle, kBufferSize);
  761. if (ldescriptor->deactivate != nullptr)
  762. ldescriptor->deactivate(handle);
  763. ldescriptor->cleanup(handle);
  764. // end crash-free plugin test
  765. // -----------------------------------------------------------------------
  766. }
  767. DISCOVERY_OUT("init", "-----------");
  768. DISCOVERY_OUT("build", BINARY_NATIVE);
  769. DISCOVERY_OUT("hints", hints);
  770. DISCOVERY_OUT("name", ldescriptor->Name);
  771. DISCOVERY_OUT("label", ldescriptor->Label);
  772. DISCOVERY_OUT("maker", ldescriptor->Maker);
  773. DISCOVERY_OUT("uniqueId", ldescriptor->UniqueID);
  774. DISCOVERY_OUT("audio.ins", audioIns);
  775. DISCOVERY_OUT("audio.outs", audioOuts);
  776. DISCOVERY_OUT("midi.ins", midiIns);
  777. DISCOVERY_OUT("parameters.ins", parametersIns);
  778. DISCOVERY_OUT("parameters.outs", parametersOuts);
  779. DISCOVERY_OUT("end", "------------");
  780. }
  781. }
  782. static void do_lv2_check(const char* const bundle, const bool doInit)
  783. {
  784. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  785. Lilv::Node bundleNode(lv2World.new_file_uri(nullptr, bundle));
  786. CARLA_SAFE_ASSERT_RETURN(bundleNode.is_uri(),);
  787. CarlaString sBundle(bundleNode.as_uri());
  788. if (! sBundle.endsWith("/"))
  789. sBundle += "/";
  790. // Load bundle
  791. lv2World.load_bundle(sBundle);
  792. // Load plugins in this bundle
  793. const Lilv::Plugins lilvPlugins(lv2World.get_all_plugins());
  794. // Get all plugin URIs in this bundle
  795. StringArray URIs;
  796. LILV_FOREACH(plugins, it, lilvPlugins)
  797. {
  798. Lilv::Plugin lilvPlugin(lilv_plugins_get(lilvPlugins, it));
  799. if (const char* const uri = lilvPlugin.get_uri().as_string())
  800. URIs.addIfNotAlreadyThere(water::String(uri));
  801. }
  802. if (URIs.size() == 0)
  803. {
  804. DISCOVERY_OUT("warning", "LV2 Bundle doesn't provide any plugins");
  805. return;
  806. }
  807. // Get & check every plugin-instance
  808. for (int i=0, count=URIs.size(); i < count; ++i)
  809. {
  810. const LV2_RDF_Descriptor* const rdfDescriptor(lv2_rdf_new(URIs[i].toRawUTF8(), false));
  811. if (rdfDescriptor == nullptr || rdfDescriptor->URI == nullptr)
  812. {
  813. DISCOVERY_OUT("error", "Failed to find LV2 plugin '" << URIs[i].toRawUTF8() << "'");
  814. continue;
  815. }
  816. if (doInit)
  817. {
  818. // test if DLL is loadable, twice
  819. const lib_t libHandle1 = lib_open(rdfDescriptor->Binary);
  820. if (libHandle1 == nullptr)
  821. {
  822. print_lib_error(rdfDescriptor->Binary);
  823. delete rdfDescriptor;
  824. continue;
  825. }
  826. lib_close(libHandle1);
  827. const lib_t libHandle2 = lib_open(rdfDescriptor->Binary);
  828. if (libHandle2 == nullptr)
  829. {
  830. print_lib_error(rdfDescriptor->Binary);
  831. delete rdfDescriptor;
  832. continue;
  833. }
  834. lib_close(libHandle2);
  835. }
  836. // test if we support all required ports and features
  837. {
  838. bool supported = true;
  839. for (uint32_t j=0; j < rdfDescriptor->PortCount && supported; ++j)
  840. {
  841. const LV2_RDF_Port* const rdfPort(&rdfDescriptor->Ports[j]);
  842. if (is_lv2_port_supported(rdfPort->Types))
  843. {
  844. pass();
  845. }
  846. else if (! LV2_IS_PORT_OPTIONAL(rdfPort->Properties))
  847. {
  848. DISCOVERY_OUT("error", "Plugin '" << rdfDescriptor->URI << "' requires a non-supported port type (portName: '" << rdfPort->Name << "')");
  849. supported = false;
  850. break;
  851. }
  852. }
  853. for (uint32_t j=0; j < rdfDescriptor->FeatureCount && supported; ++j)
  854. {
  855. const LV2_RDF_Feature& feature(rdfDescriptor->Features[j]);
  856. if (std::strcmp(feature.URI, LV2_DATA_ACCESS_URI) == 0 || std::strcmp(feature.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  857. {
  858. DISCOVERY_OUT("warning", "Plugin '" << rdfDescriptor->URI << "' DSP wants UI feature '" << feature.URI << "', ignoring this");
  859. }
  860. else if (feature.Required && ! is_lv2_feature_supported(feature.URI))
  861. {
  862. DISCOVERY_OUT("error", "Plugin '" << rdfDescriptor->URI << "' requires a non-supported feature '" << feature.URI << "'");
  863. supported = false;
  864. break;
  865. }
  866. }
  867. if (! supported)
  868. {
  869. delete rdfDescriptor;
  870. continue;
  871. }
  872. }
  873. uint hints = 0x0;
  874. int audioIns = 0;
  875. int audioOuts = 0;
  876. int midiIns = 0;
  877. int midiOuts = 0;
  878. int parametersIns = 0;
  879. int parametersOuts = 0;
  880. for (uint32_t j=0; j < rdfDescriptor->FeatureCount; ++j)
  881. {
  882. const LV2_RDF_Feature* const rdfFeature(&rdfDescriptor->Features[j]);
  883. if (std::strcmp(rdfFeature->URI, LV2_CORE__hardRTCapable) == 0)
  884. hints |= PLUGIN_IS_RTSAFE;
  885. }
  886. for (uint32_t j=0; j < rdfDescriptor->PortCount; ++j)
  887. {
  888. const LV2_RDF_Port* const rdfPort(&rdfDescriptor->Ports[j]);
  889. if (LV2_IS_PORT_AUDIO(rdfPort->Types))
  890. {
  891. if (LV2_IS_PORT_INPUT(rdfPort->Types))
  892. audioIns += 1;
  893. else if (LV2_IS_PORT_OUTPUT(rdfPort->Types))
  894. audioOuts += 1;
  895. }
  896. else if (LV2_IS_PORT_CONTROL(rdfPort->Types))
  897. {
  898. if (LV2_IS_PORT_DESIGNATION_LATENCY(rdfPort->Designation))
  899. {
  900. pass();
  901. }
  902. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(rdfPort->Designation))
  903. {
  904. pass();
  905. }
  906. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(rdfPort->Designation))
  907. {
  908. pass();
  909. }
  910. else if (LV2_IS_PORT_DESIGNATION_TIME(rdfPort->Designation))
  911. {
  912. pass();
  913. }
  914. else
  915. {
  916. if (LV2_IS_PORT_INPUT(rdfPort->Types))
  917. parametersIns += 1;
  918. else if (LV2_IS_PORT_OUTPUT(rdfPort->Types))
  919. parametersOuts += 1;
  920. }
  921. }
  922. else if (LV2_PORT_SUPPORTS_MIDI_EVENT(rdfPort->Types))
  923. {
  924. if (LV2_IS_PORT_INPUT(rdfPort->Types))
  925. midiIns += 1;
  926. else if (LV2_IS_PORT_OUTPUT(rdfPort->Types))
  927. midiOuts += 1;
  928. }
  929. }
  930. if (LV2_IS_INSTRUMENT(rdfDescriptor->Type[0], rdfDescriptor->Type[1]))
  931. hints |= PLUGIN_IS_SYNTH;
  932. if (rdfDescriptor->UICount > 0)
  933. hints |= PLUGIN_HAS_CUSTOM_UI;
  934. DISCOVERY_OUT("init", "-----------");
  935. DISCOVERY_OUT("build", BINARY_NATIVE);
  936. DISCOVERY_OUT("hints", hints);
  937. if (rdfDescriptor->Name != nullptr)
  938. DISCOVERY_OUT("name", rdfDescriptor->Name);
  939. if (rdfDescriptor->Author != nullptr)
  940. DISCOVERY_OUT("maker", rdfDescriptor->Author);
  941. DISCOVERY_OUT("uri", rdfDescriptor->URI);
  942. DISCOVERY_OUT("uniqueId", rdfDescriptor->UniqueID);
  943. DISCOVERY_OUT("audio.ins", audioIns);
  944. DISCOVERY_OUT("audio.outs", audioOuts);
  945. DISCOVERY_OUT("midi.ins", midiIns);
  946. DISCOVERY_OUT("midi.outs", midiOuts);
  947. DISCOVERY_OUT("parameters.ins", parametersIns);
  948. DISCOVERY_OUT("parameters.outs", parametersOuts);
  949. DISCOVERY_OUT("end", "------------");
  950. delete rdfDescriptor;
  951. }
  952. }
  953. static void do_vst_check(lib_t& libHandle, const bool doInit)
  954. {
  955. VST_Function vstFn = lib_symbol<VST_Function>(libHandle, "VSTPluginMain");
  956. if (vstFn == nullptr)
  957. {
  958. vstFn = lib_symbol<VST_Function>(libHandle, "main");
  959. if (vstFn == nullptr)
  960. {
  961. DISCOVERY_OUT("error", "Not a VST plugin");
  962. return;
  963. }
  964. }
  965. AEffect* effect = vstFn(vstHostCallback);
  966. if (effect == nullptr || effect->magic != kEffectMagic)
  967. {
  968. DISCOVERY_OUT("error", "Failed to init VST plugin, or VST magic failed");
  969. return;
  970. }
  971. if (effect->uniqueID == 0)
  972. {
  973. DISCOVERY_OUT("error", "Plugin doesn't have an Unique ID");
  974. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  975. return;
  976. }
  977. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdentify), 0, 0, nullptr, 0.0f);
  978. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effSetBlockSizeAndSampleRate), 0, kBufferSize, nullptr, kSampleRatef);
  979. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, kSampleRatef);
  980. effect->dispatcher(effect, effSetBlockSize, 0, kBufferSize, nullptr, 0.0f);
  981. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  982. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  983. if (effect->numPrograms > 0)
  984. effect->dispatcher(effect, effSetProgram, 0, 0, nullptr, 0.0f);
  985. const bool isShell = (effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f) == kPlugCategShell);
  986. gVstCurrentUniqueId = effect->uniqueID;
  987. char strBuf[STR_MAX+1];
  988. CarlaString cName;
  989. CarlaString cProduct;
  990. CarlaString cVendor;
  991. LinkedList<intptr_t> uniqueIds;
  992. if (isShell)
  993. {
  994. for (;;)
  995. {
  996. carla_zeroChars(strBuf, STR_MAX+1);
  997. gVstCurrentUniqueId = effect->dispatcher(effect, effShellGetNextPlugin, 0, 0, strBuf, 0.0f);
  998. if (gVstCurrentUniqueId == 0)
  999. break;
  1000. uniqueIds.append(gVstCurrentUniqueId);
  1001. }
  1002. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1003. effect = nullptr;
  1004. }
  1005. else
  1006. {
  1007. uniqueIds.append(gVstCurrentUniqueId);
  1008. }
  1009. for (LinkedList<intptr_t>::Itenerator it = uniqueIds.begin2(); it.valid(); it.next())
  1010. {
  1011. gVstCurrentUniqueId = it.getValue(0);
  1012. if (effect == nullptr)
  1013. {
  1014. effect = vstFn(vstHostCallback);
  1015. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdentify), 0, 0, nullptr, 0.0f);
  1016. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effSetBlockSizeAndSampleRate), 0, kBufferSize, nullptr, kSampleRatef);
  1017. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, kSampleRatef);
  1018. effect->dispatcher(effect, effSetBlockSize, 0, kBufferSize, nullptr, 0.0f);
  1019. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  1020. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  1021. if (effect->numPrograms > 0)
  1022. effect->dispatcher(effect, effSetProgram, 0, 0, nullptr, 0.0f);
  1023. }
  1024. // get name
  1025. carla_zeroChars(strBuf, STR_MAX+1);
  1026. if (effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f) == 1)
  1027. cName = strBuf;
  1028. else
  1029. cName.clear();
  1030. // get product
  1031. carla_zeroChars(strBuf, STR_MAX+1);
  1032. if (effect->dispatcher(effect, effGetProductString, 0, 0, strBuf, 0.0f) == 1)
  1033. cProduct = strBuf;
  1034. else
  1035. cProduct.clear();
  1036. // get vendor
  1037. carla_zeroChars(strBuf, STR_MAX+1);
  1038. if (effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f) == 1)
  1039. cVendor = strBuf;
  1040. else
  1041. cVendor.clear();
  1042. // get everything else
  1043. uint hints = 0x0;
  1044. int audioIns = effect->numInputs;
  1045. int audioOuts = effect->numOutputs;
  1046. int midiIns = 0;
  1047. int midiOuts = 0;
  1048. int parameters = effect->numParams;
  1049. if (effect->flags & effFlagsHasEditor)
  1050. hints |= PLUGIN_HAS_CUSTOM_UI;
  1051. if (effect->flags & effFlagsIsSynth)
  1052. hints |= PLUGIN_IS_SYNTH;
  1053. if (vstPluginCanDo(effect, "receiveVstEvents") || vstPluginCanDo(effect, "receiveVstMidiEvent") || (effect->flags & effFlagsIsSynth) != 0)
  1054. midiIns = 1;
  1055. if (vstPluginCanDo(effect, "sendVstEvents") || vstPluginCanDo(effect, "sendVstMidiEvent"))
  1056. midiOuts = 1;
  1057. // -----------------------------------------------------------------------
  1058. // start crash-free plugin test
  1059. if (doInit)
  1060. {
  1061. if (gVstNeedsIdle)
  1062. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1063. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1064. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1065. if (gVstNeedsIdle)
  1066. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1067. // Plugin might call wantMidi() during resume
  1068. if (midiIns == 0 && gVstWantsMidi)
  1069. {
  1070. midiIns = 1;
  1071. }
  1072. float* bufferAudioIn[audioIns];
  1073. for (int j=0; j < audioIns; ++j)
  1074. {
  1075. bufferAudioIn[j] = new float[kBufferSize];
  1076. carla_zeroFloats(bufferAudioIn[j], kBufferSize);
  1077. }
  1078. float* bufferAudioOut[audioOuts];
  1079. for (int j=0; j < audioOuts; ++j)
  1080. {
  1081. bufferAudioOut[j] = new float[kBufferSize];
  1082. carla_zeroFloats(bufferAudioOut[j], kBufferSize);
  1083. }
  1084. struct VstEventsFixed {
  1085. int32_t numEvents;
  1086. intptr_t reserved;
  1087. VstEvent* data[2];
  1088. VstEventsFixed()
  1089. : numEvents(0),
  1090. reserved(0)
  1091. {
  1092. data[0] = data[1] = nullptr;
  1093. }
  1094. } events;
  1095. VstMidiEvent midiEvents[2];
  1096. carla_zeroStructs(midiEvents, 2);
  1097. midiEvents[0].type = kVstMidiType;
  1098. midiEvents[0].byteSize = sizeof(VstMidiEvent);
  1099. midiEvents[0].midiData[0] = char(MIDI_STATUS_NOTE_ON);
  1100. midiEvents[0].midiData[1] = 64;
  1101. midiEvents[0].midiData[2] = 100;
  1102. midiEvents[1].type = kVstMidiType;
  1103. midiEvents[1].byteSize = sizeof(VstMidiEvent);
  1104. midiEvents[1].midiData[0] = char(MIDI_STATUS_NOTE_OFF);
  1105. midiEvents[1].midiData[1] = 64;
  1106. midiEvents[1].deltaFrames = kBufferSize/2;
  1107. events.numEvents = 2;
  1108. events.data[0] = (VstEvent*)&midiEvents[0];
  1109. events.data[1] = (VstEvent*)&midiEvents[1];
  1110. // processing
  1111. gVstIsProcessing = true;
  1112. if (midiIns > 0)
  1113. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  1114. if ((effect->flags & effFlagsCanReplacing) > 0 && effect->processReplacing != nullptr && effect->processReplacing != effect->DECLARE_VST_DEPRECATED(process))
  1115. effect->processReplacing(effect, bufferAudioIn, bufferAudioOut, kBufferSize);
  1116. else if (effect->DECLARE_VST_DEPRECATED(process) != nullptr)
  1117. effect->DECLARE_VST_DEPRECATED(process)(effect, bufferAudioIn, bufferAudioOut, kBufferSize);
  1118. else
  1119. DISCOVERY_OUT("error", "Plugin doesn't have a process function");
  1120. gVstIsProcessing = false;
  1121. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1122. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1123. if (gVstNeedsIdle)
  1124. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1125. for (int j=0; j < audioIns; ++j)
  1126. delete[] bufferAudioIn[j];
  1127. for (int j=0; j < audioOuts; ++j)
  1128. delete[] bufferAudioOut[j];
  1129. }
  1130. // end crash-free plugin test
  1131. // -----------------------------------------------------------------------
  1132. DISCOVERY_OUT("init", "-----------");
  1133. DISCOVERY_OUT("build", BINARY_NATIVE);
  1134. DISCOVERY_OUT("hints", hints);
  1135. DISCOVERY_OUT("name", cName.buffer());
  1136. DISCOVERY_OUT("label", cProduct.buffer());
  1137. DISCOVERY_OUT("maker", cVendor.buffer());
  1138. DISCOVERY_OUT("uniqueId", gVstCurrentUniqueId);
  1139. DISCOVERY_OUT("audio.ins", audioIns);
  1140. DISCOVERY_OUT("audio.outs", audioOuts);
  1141. DISCOVERY_OUT("midi.ins", midiIns);
  1142. DISCOVERY_OUT("midi.outs", midiOuts);
  1143. DISCOVERY_OUT("parameters.ins", parameters);
  1144. DISCOVERY_OUT("end", "------------");
  1145. gVstWantsMidi = false;
  1146. gVstWantsTime = false;
  1147. if (! isShell)
  1148. break;
  1149. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1150. effect = nullptr;
  1151. }
  1152. uniqueIds.clear();
  1153. if (effect == nullptr)
  1154. return;
  1155. if (gVstNeedsIdle)
  1156. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1157. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1158. }
  1159. static void do_fluidsynth_check(const char* const filename, const bool doInit)
  1160. {
  1161. #ifdef HAVE_FLUIDSYNTH
  1162. const water::String jfilename = water::String(CharPointer_UTF8(filename));
  1163. const File file(jfilename);
  1164. if (! file.existsAsFile())
  1165. {
  1166. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1167. return;
  1168. }
  1169. if (! fluid_is_soundfont(filename))
  1170. {
  1171. DISCOVERY_OUT("error", "Not a SF2 file");
  1172. return;
  1173. }
  1174. int programs = 0;
  1175. if (doInit)
  1176. {
  1177. fluid_settings_t* const f_settings = new_fluid_settings();
  1178. CARLA_SAFE_ASSERT_RETURN(f_settings != nullptr,);
  1179. fluid_synth_t* const f_synth = new_fluid_synth(f_settings);
  1180. CARLA_SAFE_ASSERT_RETURN(f_synth != nullptr,);
  1181. const int f_id = fluid_synth_sfload(f_synth, filename, 0);
  1182. if (f_id < 0)
  1183. {
  1184. DISCOVERY_OUT("error", "Failed to load SF2 file");
  1185. return;
  1186. }
  1187. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(f_synth, static_cast<uint>(f_id)))
  1188. {
  1189. fluid_preset_t f_preset;
  1190. f_sfont->iteration_start(f_sfont);
  1191. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  1192. ++programs;
  1193. }
  1194. delete_fluid_synth(f_synth);
  1195. delete_fluid_settings(f_settings);
  1196. }
  1197. CarlaString name(file.getFileNameWithoutExtension().toRawUTF8());
  1198. CarlaString label(name);
  1199. // 2 channels
  1200. DISCOVERY_OUT("init", "-----------");
  1201. DISCOVERY_OUT("build", BINARY_NATIVE);
  1202. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1203. DISCOVERY_OUT("name", name.buffer());
  1204. DISCOVERY_OUT("label", label.buffer());
  1205. DISCOVERY_OUT("audio.outs", 2);
  1206. DISCOVERY_OUT("midi.ins", 1);
  1207. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1208. DISCOVERY_OUT("parameters.outs", 1);
  1209. DISCOVERY_OUT("end", "------------");
  1210. // 16 channels
  1211. if (doInit && (name.isEmpty() || programs <= 1))
  1212. return;
  1213. name += " (16 outputs)";
  1214. DISCOVERY_OUT("init", "-----------");
  1215. DISCOVERY_OUT("build", BINARY_NATIVE);
  1216. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1217. DISCOVERY_OUT("name", name.buffer());
  1218. DISCOVERY_OUT("label", label.buffer());
  1219. DISCOVERY_OUT("audio.outs", 32);
  1220. DISCOVERY_OUT("midi.ins", 1);
  1221. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1222. DISCOVERY_OUT("parameters.outs", 1);
  1223. DISCOVERY_OUT("end", "------------");
  1224. #else // HAVE_FLUIDSYNTH
  1225. DISCOVERY_OUT("error", "SF2 support not available");
  1226. return;
  1227. // unused
  1228. (void)filename;
  1229. (void)doInit;
  1230. #endif
  1231. }
  1232. static void do_linuxsampler_check(const char* const filename, const char* const stype, const bool doInit)
  1233. {
  1234. #ifdef HAVE_LINUXSAMPLER
  1235. const water::String jfilename = water::String(CharPointer_UTF8(filename));
  1236. const File file(jfilename);
  1237. if (! file.existsAsFile())
  1238. {
  1239. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1240. return;
  1241. }
  1242. if (doInit)
  1243. const LinuxSamplerScopedEngine engine(filename, stype);
  1244. else
  1245. LinuxSamplerScopedEngine::outputInfo(nullptr, file.getFileNameWithoutExtension().toRawUTF8(), std::strcmp(stype, "gig") == 0);
  1246. #else // HAVE_LINUXSAMPLER
  1247. DISCOVERY_OUT("error", stype << " support not available");
  1248. return;
  1249. // unused
  1250. (void)filename;
  1251. (void)doInit;
  1252. #endif
  1253. }
  1254. // ------------------------------ main entry point ------------------------------
  1255. int main(int argc, char* argv[])
  1256. {
  1257. if (argc != 3)
  1258. {
  1259. carla_stdout("usage: %s <type> </path/to/plugin>", argv[0]);
  1260. return 1;
  1261. }
  1262. const char* const stype = argv[1];
  1263. const char* const filename = argv[2];
  1264. const PluginType type = getPluginTypeFromString(stype);
  1265. CarlaString filenameCheck(filename);
  1266. filenameCheck.toLower();
  1267. if (type != PLUGIN_GIG && type != PLUGIN_SF2 && type != PLUGIN_SFZ)
  1268. {
  1269. if (filenameCheck.contains("fluidsynth", true))
  1270. {
  1271. DISCOVERY_OUT("info", "skipping fluidsynth based plugin");
  1272. return 0;
  1273. }
  1274. if (filenameCheck.contains("linuxsampler", true) || filenameCheck.endsWith("ls16.so"))
  1275. {
  1276. DISCOVERY_OUT("info", "skipping linuxsampler based plugin");
  1277. return 0;
  1278. }
  1279. }
  1280. bool openLib = false;
  1281. lib_t handle = nullptr;
  1282. switch (type)
  1283. {
  1284. case PLUGIN_LADSPA:
  1285. case PLUGIN_DSSI:
  1286. case PLUGIN_VST2:
  1287. openLib = true;
  1288. default:
  1289. break;
  1290. }
  1291. if (openLib)
  1292. {
  1293. handle = lib_open(filename);
  1294. if (handle == nullptr)
  1295. {
  1296. print_lib_error(filename);
  1297. return 1;
  1298. }
  1299. }
  1300. // never do init for dssi-vst, takes too long and it's crashy
  1301. bool doInit = ! filenameCheck.contains("dssi-vst", true);
  1302. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS") != nullptr)
  1303. doInit = false;
  1304. if (doInit && handle != nullptr)
  1305. {
  1306. // test fast loading & unloading DLL without initializing the plugin(s)
  1307. if (! lib_close(handle))
  1308. {
  1309. print_lib_error(filename);
  1310. return 1;
  1311. }
  1312. handle = lib_open(filename);
  1313. if (handle == nullptr)
  1314. {
  1315. print_lib_error(filename);
  1316. return 1;
  1317. }
  1318. }
  1319. #ifndef BUILD_BRIDGE
  1320. if (std::strcmp(filename, ":all") == 0)
  1321. {
  1322. do_cached_check(type);
  1323. return 0;
  1324. }
  1325. #endif
  1326. switch (type)
  1327. {
  1328. case PLUGIN_LADSPA:
  1329. do_ladspa_check(handle, filename, doInit);
  1330. break;
  1331. case PLUGIN_DSSI:
  1332. do_dssi_check(handle, filename, doInit);
  1333. break;
  1334. case PLUGIN_LV2:
  1335. do_lv2_check(filename, doInit);
  1336. break;
  1337. case PLUGIN_VST2:
  1338. do_vst_check(handle, doInit);
  1339. break;
  1340. case PLUGIN_GIG:
  1341. do_linuxsampler_check(filename, "gig", doInit);
  1342. break;
  1343. case PLUGIN_SF2:
  1344. do_fluidsynth_check(filename, doInit);
  1345. break;
  1346. case PLUGIN_SFZ:
  1347. do_linuxsampler_check(filename, "sfz", doInit);
  1348. break;
  1349. default:
  1350. break;
  1351. }
  1352. if (openLib && handle != nullptr)
  1353. lib_close(handle);
  1354. return 0;
  1355. }
  1356. // --------------------------------------------------------------------------