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.

1673 lines
53KB

  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. // need to include this before linuxsampler
  31. #define CARLA_UTILS_CACHED_PLUGINS_ONLY
  32. #include "CarlaUtils.cpp"
  33. #ifdef HAVE_FLUIDSYNTH
  34. # include <fluidsynth.h>
  35. #endif
  36. #ifdef HAVE_LINUXSAMPLER
  37. # include "linuxsampler/EngineFactory.h"
  38. #endif
  39. #include <iostream>
  40. #include "AppConfig.h"
  41. #include "juce_core/juce_core.h"
  42. #define DISCOVERY_OUT(x, y) std::cout << "\ncarla-discovery::" << x << "::" << y << std::endl;
  43. using juce::CharPointer_UTF8;
  44. using juce::File;
  45. using juce::StringArray;
  46. CARLA_BACKEND_USE_NAMESPACE
  47. // --------------------------------------------------------------------------
  48. // Dummy values to test plugins with
  49. static const uint32_t kBufferSize = 512;
  50. static const double kSampleRate = 44100.0;
  51. static const int32_t kSampleRatei = 44100;
  52. static const float kSampleRatef = 44100.0f;
  53. // --------------------------------------------------------------------------
  54. // Don't print ELF/EXE related errors since discovery can find multi-architecture binaries
  55. static void print_lib_error(const char* const filename)
  56. {
  57. const char* const error(lib_error(filename));
  58. if (error != nullptr && std::strstr(error, "wrong ELF class") == nullptr && std::strstr(error, "Bad EXE format") == nullptr)
  59. DISCOVERY_OUT("error", error);
  60. }
  61. #ifndef CARLA_OS_MAC
  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. #endif // ! CARLA_OS_MAC
  214. #ifdef HAVE_LINUXSAMPLER
  215. // --------------------------------------------------------------------------
  216. // LinuxSampler stuff
  217. class LinuxSamplerScopedEngine
  218. {
  219. public:
  220. LinuxSamplerScopedEngine(const char* const filename, const char* const stype)
  221. : fEngine(nullptr)
  222. {
  223. using namespace LinuxSampler;
  224. try {
  225. fEngine = EngineFactory::Create(stype);
  226. }
  227. catch (const Exception& e)
  228. {
  229. DISCOVERY_OUT("error", e.what());
  230. return;
  231. }
  232. if (fEngine == nullptr)
  233. return;
  234. InstrumentManager* const insMan(fEngine->GetInstrumentManager());
  235. if (insMan == nullptr)
  236. {
  237. DISCOVERY_OUT("error", "Failed to get LinuxSampler instrument manager");
  238. return;
  239. }
  240. std::vector<InstrumentManager::instrument_id_t> ids;
  241. try {
  242. ids = insMan->GetInstrumentFileContent(filename);
  243. }
  244. catch (const InstrumentManagerException& e)
  245. {
  246. DISCOVERY_OUT("error", e.what());
  247. return;
  248. }
  249. if (ids.size() == 0)
  250. {
  251. DISCOVERY_OUT("error", "Failed to find any instruments");
  252. return;
  253. }
  254. InstrumentManager::instrument_info_t info;
  255. try {
  256. info = insMan->GetInstrumentInfo(ids[0]);
  257. }
  258. catch (const InstrumentManagerException& e)
  259. {
  260. DISCOVERY_OUT("error", e.what());
  261. return;
  262. }
  263. outputInfo(&info, nullptr, ids.size() > 1);
  264. }
  265. ~LinuxSamplerScopedEngine()
  266. {
  267. if (fEngine != nullptr)
  268. {
  269. LinuxSampler::EngineFactory::Destroy(fEngine);
  270. fEngine = nullptr;
  271. }
  272. }
  273. static void outputInfo(const LinuxSampler::InstrumentManager::instrument_info_t* const info, const char* const basename, const bool has16Outs)
  274. {
  275. CarlaString name;
  276. const char* label;
  277. if (info != nullptr)
  278. {
  279. name = info->InstrumentName.c_str();
  280. label = info->Product.c_str();
  281. }
  282. else
  283. {
  284. name = basename;
  285. label = basename;
  286. }
  287. // 2 channels
  288. DISCOVERY_OUT("init", "-----------");
  289. DISCOVERY_OUT("build", BINARY_NATIVE);
  290. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  291. DISCOVERY_OUT("name", name.buffer());
  292. DISCOVERY_OUT("label", label);
  293. if (info != nullptr)
  294. DISCOVERY_OUT("maker", info->Artists);
  295. DISCOVERY_OUT("audio.outs", 2);
  296. DISCOVERY_OUT("midi.ins", 1);
  297. DISCOVERY_OUT("end", "------------");
  298. // 16 channels
  299. if (name.isEmpty() || ! has16Outs)
  300. return;
  301. name += " (16 outputs)";
  302. DISCOVERY_OUT("init", "-----------");
  303. DISCOVERY_OUT("build", BINARY_NATIVE);
  304. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  305. DISCOVERY_OUT("name", name.buffer());
  306. DISCOVERY_OUT("label", label);
  307. if (info != nullptr)
  308. DISCOVERY_OUT("maker", info->Artists);
  309. DISCOVERY_OUT("audio.outs", 32);
  310. DISCOVERY_OUT("midi.ins", 1);
  311. DISCOVERY_OUT("end", "------------");
  312. }
  313. private:
  314. LinuxSampler::Engine* fEngine;
  315. CARLA_PREVENT_HEAP_ALLOCATION
  316. CARLA_DECLARE_NON_COPY_CLASS(LinuxSamplerScopedEngine)
  317. };
  318. #endif // HAVE_LINUXSAMPLER
  319. // ------------------------------ Plugin Checks -----------------------------
  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. static void do_ladspa_check(lib_t& libHandle, const char* const filename, const bool doInit)
  344. {
  345. LADSPA_Descriptor_Function descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  346. if (descFn == nullptr)
  347. {
  348. DISCOVERY_OUT("error", "Not a LADSPA plugin");
  349. return;
  350. }
  351. const LADSPA_Descriptor* descriptor;
  352. {
  353. descriptor = descFn(0);
  354. if (descriptor == nullptr)
  355. {
  356. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  357. return;
  358. }
  359. if (doInit && descriptor->instantiate != nullptr && descriptor->cleanup != nullptr)
  360. {
  361. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  362. if (handle == nullptr)
  363. {
  364. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  365. return;
  366. }
  367. descriptor->cleanup(handle);
  368. lib_close(libHandle);
  369. libHandle = lib_open(filename);
  370. if (libHandle == nullptr)
  371. {
  372. print_lib_error(filename);
  373. return;
  374. }
  375. descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  376. if (descFn == nullptr)
  377. {
  378. DISCOVERY_OUT("error", "Not a LADSPA plugin (#2)");
  379. return;
  380. }
  381. }
  382. }
  383. unsigned long i = 0;
  384. while ((descriptor = descFn(i++)) != nullptr)
  385. {
  386. if (descriptor->instantiate == nullptr)
  387. {
  388. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no instantiate()");
  389. continue;
  390. }
  391. if (descriptor->cleanup == nullptr)
  392. {
  393. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no cleanup()");
  394. continue;
  395. }
  396. if (descriptor->run == nullptr)
  397. {
  398. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no run()");
  399. continue;
  400. }
  401. if (! LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  402. {
  403. DISCOVERY_OUT("warning", "Plugin '" << descriptor->Name << "' is not hard real-time capable");
  404. }
  405. uint hints = 0x0;
  406. int audioIns = 0;
  407. int audioOuts = 0;
  408. int audioTotal = 0;
  409. int parametersIns = 0;
  410. int parametersOuts = 0;
  411. int parametersTotal = 0;
  412. if (LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  413. hints |= PLUGIN_IS_RTSAFE;
  414. for (unsigned long j=0; j < descriptor->PortCount; ++j)
  415. {
  416. CARLA_ASSERT(descriptor->PortNames[j] != nullptr);
  417. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  418. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  419. {
  420. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  421. audioIns += 1;
  422. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  423. audioOuts += 1;
  424. audioTotal += 1;
  425. }
  426. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  427. {
  428. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  429. parametersIns += 1;
  430. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(descriptor->PortNames[j], "latency") != 0 && std::strcmp(descriptor->PortNames[j], "_latency") != 0)
  431. parametersOuts += 1;
  432. parametersTotal += 1;
  433. }
  434. }
  435. if (doInit)
  436. {
  437. // -----------------------------------------------------------------------
  438. // start crash-free plugin test
  439. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  440. if (handle == nullptr)
  441. {
  442. DISCOVERY_OUT("error", "Failed to init LADSPA plugin");
  443. continue;
  444. }
  445. // Test quick init and cleanup
  446. descriptor->cleanup(handle);
  447. handle = descriptor->instantiate(descriptor, kSampleRatei);
  448. if (handle == nullptr)
  449. {
  450. DISCOVERY_OUT("error", "Failed to init LADSPA plugin (#2)");
  451. continue;
  452. }
  453. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  454. LADSPA_Data bufferParams[parametersTotal];
  455. LADSPA_Data min, max, def;
  456. for (unsigned long j=0, iA=0, iC=0; j < descriptor->PortCount; ++j)
  457. {
  458. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  459. const LADSPA_PortRangeHint portRangeHints = descriptor->PortRangeHints[j];
  460. const char* const portName = descriptor->PortNames[j];
  461. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  462. {
  463. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  464. descriptor->connect_port(handle, j, bufferAudio[iA++]);
  465. }
  466. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  467. {
  468. // min value
  469. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  470. min = portRangeHints.LowerBound;
  471. else
  472. min = 0.0f;
  473. // max value
  474. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  475. max = portRangeHints.UpperBound;
  476. else
  477. max = 1.0f;
  478. if (min > max)
  479. {
  480. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  481. max = min + 0.1f;
  482. }
  483. else if (carla_isEqual(min, max))
  484. {
  485. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  486. max = min + 0.1f;
  487. }
  488. // default value
  489. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  490. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  491. {
  492. min *= kSampleRatef;
  493. max *= kSampleRatef;
  494. def *= kSampleRatef;
  495. }
  496. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  497. {
  498. // latency parameter
  499. def = 0.0f;
  500. }
  501. else
  502. {
  503. if (def < min)
  504. def = min;
  505. else if (def > max)
  506. def = max;
  507. }
  508. bufferParams[iC] = def;
  509. descriptor->connect_port(handle, j, &bufferParams[iC++]);
  510. }
  511. }
  512. if (descriptor->activate != nullptr)
  513. descriptor->activate(handle);
  514. descriptor->run(handle, kBufferSize);
  515. if (descriptor->deactivate != nullptr)
  516. descriptor->deactivate(handle);
  517. descriptor->cleanup(handle);
  518. // end crash-free plugin test
  519. // -----------------------------------------------------------------------
  520. }
  521. DISCOVERY_OUT("init", "-----------");
  522. DISCOVERY_OUT("build", BINARY_NATIVE);
  523. DISCOVERY_OUT("hints", hints);
  524. DISCOVERY_OUT("name", descriptor->Name);
  525. DISCOVERY_OUT("label", descriptor->Label);
  526. DISCOVERY_OUT("maker", descriptor->Maker);
  527. DISCOVERY_OUT("uniqueId", descriptor->UniqueID);
  528. DISCOVERY_OUT("audio.ins", audioIns);
  529. DISCOVERY_OUT("audio.outs", audioOuts);
  530. DISCOVERY_OUT("parameters.ins", parametersIns);
  531. DISCOVERY_OUT("parameters.outs", parametersOuts);
  532. DISCOVERY_OUT("end", "------------");
  533. }
  534. }
  535. static void do_dssi_check(lib_t& libHandle, const char* const filename, const bool doInit)
  536. {
  537. DSSI_Descriptor_Function descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  538. if (descFn == nullptr)
  539. {
  540. DISCOVERY_OUT("error", "Not a DSSI plugin");
  541. return;
  542. }
  543. const DSSI_Descriptor* descriptor;
  544. {
  545. descriptor = descFn(0);
  546. if (descriptor == nullptr)
  547. {
  548. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  549. return;
  550. }
  551. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  552. if (ldescriptor == nullptr)
  553. {
  554. DISCOVERY_OUT("error", "DSSI plugin doesn't provide the LADSPA interface");
  555. return;
  556. }
  557. if (doInit && ldescriptor->instantiate != nullptr && ldescriptor->cleanup != nullptr)
  558. {
  559. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  560. if (handle == nullptr)
  561. {
  562. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  563. return;
  564. }
  565. ldescriptor->cleanup(handle);
  566. lib_close(libHandle);
  567. libHandle = lib_open(filename);
  568. if (libHandle == nullptr)
  569. {
  570. print_lib_error(filename);
  571. return;
  572. }
  573. descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  574. if (descFn == nullptr)
  575. {
  576. DISCOVERY_OUT("error", "Not a DSSI plugin (#2)");
  577. return;
  578. }
  579. }
  580. }
  581. unsigned long i = 0;
  582. while ((descriptor = descFn(i++)) != nullptr)
  583. {
  584. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  585. if (ldescriptor == nullptr)
  586. {
  587. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no LADSPA interface");
  588. continue;
  589. }
  590. if (descriptor->DSSI_API_Version != DSSI_VERSION_MAJOR)
  591. {
  592. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' uses an unsupported DSSI spec version " << descriptor->DSSI_API_Version);
  593. continue;
  594. }
  595. if (ldescriptor->instantiate == nullptr)
  596. {
  597. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no instantiate()");
  598. continue;
  599. }
  600. if (ldescriptor->cleanup == nullptr)
  601. {
  602. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no cleanup()");
  603. continue;
  604. }
  605. if (ldescriptor->run == nullptr && descriptor->run_synth == nullptr)
  606. {
  607. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no run() or run_synth()");
  608. continue;
  609. }
  610. if (descriptor->run_synth == nullptr && descriptor->run_multiple_synths != nullptr)
  611. {
  612. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' requires run_multiple_synths which is not supported");
  613. continue;
  614. }
  615. if (! LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  616. {
  617. DISCOVERY_OUT("warning", "Plugin '" << ldescriptor->Name << "' is not hard real-time capable");
  618. }
  619. uint hints = 0x0;
  620. int audioIns = 0;
  621. int audioOuts = 0;
  622. int audioTotal = 0;
  623. int midiIns = 0;
  624. int parametersIns = 0;
  625. int parametersOuts = 0;
  626. int parametersTotal = 0;
  627. if (LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  628. hints |= PLUGIN_IS_RTSAFE;
  629. for (unsigned long j=0; j < ldescriptor->PortCount; ++j)
  630. {
  631. CARLA_ASSERT(ldescriptor->PortNames[j] != nullptr);
  632. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  633. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  634. {
  635. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  636. audioIns += 1;
  637. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  638. audioOuts += 1;
  639. audioTotal += 1;
  640. }
  641. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  642. {
  643. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  644. parametersIns += 1;
  645. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(ldescriptor->PortNames[j], "latency") != 0 && std::strcmp(ldescriptor->PortNames[j], "_latency") != 0)
  646. parametersOuts += 1;
  647. parametersTotal += 1;
  648. }
  649. }
  650. if (descriptor->run_synth != nullptr)
  651. midiIns = 1;
  652. if (midiIns > 0 && audioIns == 0 && audioOuts > 0)
  653. hints |= PLUGIN_IS_SYNTH;
  654. if (const char* const ui = find_dssi_ui(filename, ldescriptor->Label))
  655. {
  656. hints |= PLUGIN_HAS_CUSTOM_UI;
  657. delete[] ui;
  658. }
  659. if (doInit)
  660. {
  661. // -----------------------------------------------------------------------
  662. // start crash-free plugin test
  663. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  664. if (handle == nullptr)
  665. {
  666. DISCOVERY_OUT("error", "Failed to init DSSI plugin");
  667. continue;
  668. }
  669. // Test quick init and cleanup
  670. ldescriptor->cleanup(handle);
  671. handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  672. if (handle == nullptr)
  673. {
  674. DISCOVERY_OUT("error", "Failed to init DSSI plugin (#2)");
  675. continue;
  676. }
  677. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  678. LADSPA_Data bufferParams[parametersTotal];
  679. LADSPA_Data min, max, def;
  680. for (unsigned long j=0, iA=0, iC=0; j < ldescriptor->PortCount; ++j)
  681. {
  682. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  683. const LADSPA_PortRangeHint portRangeHints = ldescriptor->PortRangeHints[j];
  684. const char* const portName = ldescriptor->PortNames[j];
  685. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  686. {
  687. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  688. ldescriptor->connect_port(handle, j, bufferAudio[iA++]);
  689. }
  690. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  691. {
  692. // min value
  693. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  694. min = portRangeHints.LowerBound;
  695. else
  696. min = 0.0f;
  697. // max value
  698. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  699. max = portRangeHints.UpperBound;
  700. else
  701. max = 1.0f;
  702. if (min > max)
  703. {
  704. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  705. max = min + 0.1f;
  706. }
  707. else if (carla_isEqual(min, max))
  708. {
  709. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  710. max = min + 0.1f;
  711. }
  712. // default value
  713. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  714. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  715. {
  716. min *= kSampleRatef;
  717. max *= kSampleRatef;
  718. def *= kSampleRatef;
  719. }
  720. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  721. {
  722. // latency parameter
  723. def = 0.0f;
  724. }
  725. else
  726. {
  727. if (def < min)
  728. def = min;
  729. else if (def > max)
  730. def = max;
  731. }
  732. bufferParams[iC] = def;
  733. ldescriptor->connect_port(handle, j, &bufferParams[iC++]);
  734. }
  735. }
  736. // select first midi-program if available
  737. if (descriptor->get_program != nullptr && descriptor->select_program != nullptr)
  738. {
  739. if (const DSSI_Program_Descriptor* const pDesc = descriptor->get_program(handle, 0))
  740. descriptor->select_program(handle, pDesc->Bank, pDesc->Program);
  741. }
  742. if (ldescriptor->activate != nullptr)
  743. ldescriptor->activate(handle);
  744. if (descriptor->run_synth != nullptr)
  745. {
  746. snd_seq_event_t midiEvents[2];
  747. carla_zeroStructs(midiEvents, 2);
  748. const unsigned long midiEventCount = 2;
  749. midiEvents[0].type = SND_SEQ_EVENT_NOTEON;
  750. midiEvents[0].data.note.note = 64;
  751. midiEvents[0].data.note.velocity = 100;
  752. midiEvents[1].type = SND_SEQ_EVENT_NOTEOFF;
  753. midiEvents[1].data.note.note = 64;
  754. midiEvents[1].data.note.velocity = 0;
  755. midiEvents[1].time.tick = kBufferSize/2;
  756. descriptor->run_synth(handle, kBufferSize, midiEvents, midiEventCount);
  757. }
  758. else
  759. ldescriptor->run(handle, kBufferSize);
  760. if (ldescriptor->deactivate != nullptr)
  761. ldescriptor->deactivate(handle);
  762. ldescriptor->cleanup(handle);
  763. // end crash-free plugin test
  764. // -----------------------------------------------------------------------
  765. }
  766. DISCOVERY_OUT("init", "-----------");
  767. DISCOVERY_OUT("build", BINARY_NATIVE);
  768. DISCOVERY_OUT("hints", hints);
  769. DISCOVERY_OUT("name", ldescriptor->Name);
  770. DISCOVERY_OUT("label", ldescriptor->Label);
  771. DISCOVERY_OUT("maker", ldescriptor->Maker);
  772. DISCOVERY_OUT("uniqueId", ldescriptor->UniqueID);
  773. DISCOVERY_OUT("audio.ins", audioIns);
  774. DISCOVERY_OUT("audio.outs", audioOuts);
  775. DISCOVERY_OUT("midi.ins", midiIns);
  776. DISCOVERY_OUT("parameters.ins", parametersIns);
  777. DISCOVERY_OUT("parameters.outs", parametersOuts);
  778. DISCOVERY_OUT("end", "------------");
  779. }
  780. }
  781. static void do_lv2_check(const char* const bundle, const bool doInit)
  782. {
  783. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  784. Lilv::Node bundleNode(lv2World.new_file_uri(nullptr, bundle));
  785. CARLA_SAFE_ASSERT_RETURN(bundleNode.is_uri(),);
  786. CarlaString sBundle(bundleNode.as_uri());
  787. if (! sBundle.endsWith("/"))
  788. sBundle += "/";
  789. // Load bundle
  790. lv2World.load_bundle(sBundle);
  791. // Load plugins in this bundle
  792. const Lilv::Plugins lilvPlugins(lv2World.get_all_plugins());
  793. // Get all plugin URIs in this bundle
  794. StringArray URIs;
  795. LILV_FOREACH(plugins, it, lilvPlugins)
  796. {
  797. Lilv::Plugin lilvPlugin(lilv_plugins_get(lilvPlugins, it));
  798. if (const char* const uri = lilvPlugin.get_uri().as_string())
  799. URIs.addIfNotAlreadyThere(juce::String(uri));
  800. }
  801. if (URIs.size() == 0)
  802. {
  803. DISCOVERY_OUT("warning", "LV2 Bundle doesn't provide any plugins");
  804. return;
  805. }
  806. // Get & check every plugin-instance
  807. for (int i=0, count=URIs.size(); i < count; ++i)
  808. {
  809. const LV2_RDF_Descriptor* const rdfDescriptor(lv2_rdf_new(URIs[i].toRawUTF8(), false));
  810. if (rdfDescriptor == nullptr || rdfDescriptor->URI == nullptr)
  811. {
  812. DISCOVERY_OUT("error", "Failed to find LV2 plugin '" << URIs[i].toRawUTF8() << "'");
  813. continue;
  814. }
  815. if (doInit)
  816. {
  817. // test if DLL is loadable, twice
  818. const lib_t libHandle1 = lib_open(rdfDescriptor->Binary);
  819. if (libHandle1 == nullptr)
  820. {
  821. print_lib_error(rdfDescriptor->Binary);
  822. delete rdfDescriptor;
  823. continue;
  824. }
  825. lib_close(libHandle1);
  826. const lib_t libHandle2 = lib_open(rdfDescriptor->Binary);
  827. if (libHandle2 == nullptr)
  828. {
  829. print_lib_error(rdfDescriptor->Binary);
  830. delete rdfDescriptor;
  831. continue;
  832. }
  833. lib_close(libHandle2);
  834. }
  835. // test if we support all required ports and features
  836. {
  837. bool supported = true;
  838. for (uint32_t j=0; j < rdfDescriptor->PortCount && supported; ++j)
  839. {
  840. const LV2_RDF_Port* const rdfPort(&rdfDescriptor->Ports[j]);
  841. if (is_lv2_port_supported(rdfPort->Types))
  842. {
  843. pass();
  844. }
  845. else if (! LV2_IS_PORT_OPTIONAL(rdfPort->Properties))
  846. {
  847. DISCOVERY_OUT("error", "Plugin '" << rdfDescriptor->URI << "' requires a non-supported port type (portName: '" << rdfPort->Name << "')");
  848. supported = false;
  849. break;
  850. }
  851. }
  852. for (uint32_t j=0; j < rdfDescriptor->FeatureCount && supported; ++j)
  853. {
  854. const LV2_RDF_Feature& feature(rdfDescriptor->Features[j]);
  855. if (std::strcmp(feature.URI, LV2_DATA_ACCESS_URI) == 0 || std::strcmp(feature.URI, LV2_INSTANCE_ACCESS_URI) == 0)
  856. {
  857. DISCOVERY_OUT("warning", "Plugin '" << rdfDescriptor->URI << "' DSP wants UI feature '" << feature.URI << "', ignoring this");
  858. }
  859. else if (feature.Required && ! is_lv2_feature_supported(feature.URI))
  860. {
  861. DISCOVERY_OUT("error", "Plugin '" << rdfDescriptor->URI << "' requires a non-supported feature '" << feature.URI << "'");
  862. supported = false;
  863. break;
  864. }
  865. }
  866. if (! supported)
  867. {
  868. delete rdfDescriptor;
  869. continue;
  870. }
  871. }
  872. uint hints = 0x0;
  873. int audioIns = 0;
  874. int audioOuts = 0;
  875. int midiIns = 0;
  876. int midiOuts = 0;
  877. int parametersIns = 0;
  878. int parametersOuts = 0;
  879. for (uint32_t j=0; j < rdfDescriptor->FeatureCount; ++j)
  880. {
  881. const LV2_RDF_Feature* const rdfFeature(&rdfDescriptor->Features[j]);
  882. if (std::strcmp(rdfFeature->URI, LV2_CORE__hardRTCapable) == 0)
  883. hints |= PLUGIN_IS_RTSAFE;
  884. }
  885. for (uint32_t j=0; j < rdfDescriptor->PortCount; ++j)
  886. {
  887. const LV2_RDF_Port* const rdfPort(&rdfDescriptor->Ports[j]);
  888. if (LV2_IS_PORT_AUDIO(rdfPort->Types))
  889. {
  890. if (LV2_IS_PORT_INPUT(rdfPort->Types))
  891. audioIns += 1;
  892. else if (LV2_IS_PORT_OUTPUT(rdfPort->Types))
  893. audioOuts += 1;
  894. }
  895. else if (LV2_IS_PORT_CONTROL(rdfPort->Types))
  896. {
  897. if (LV2_IS_PORT_DESIGNATION_LATENCY(rdfPort->Designation))
  898. {
  899. pass();
  900. }
  901. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(rdfPort->Designation))
  902. {
  903. pass();
  904. }
  905. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(rdfPort->Designation))
  906. {
  907. pass();
  908. }
  909. else if (LV2_IS_PORT_DESIGNATION_TIME(rdfPort->Designation))
  910. {
  911. pass();
  912. }
  913. else
  914. {
  915. if (LV2_IS_PORT_INPUT(rdfPort->Types))
  916. parametersIns += 1;
  917. else if (LV2_IS_PORT_OUTPUT(rdfPort->Types))
  918. parametersOuts += 1;
  919. }
  920. }
  921. else if (LV2_PORT_SUPPORTS_MIDI_EVENT(rdfPort->Types))
  922. {
  923. if (LV2_IS_PORT_INPUT(rdfPort->Types))
  924. midiIns += 1;
  925. else if (LV2_IS_PORT_OUTPUT(rdfPort->Types))
  926. midiOuts += 1;
  927. }
  928. }
  929. if (LV2_IS_INSTRUMENT(rdfDescriptor->Type[0], rdfDescriptor->Type[1]))
  930. hints |= PLUGIN_IS_SYNTH;
  931. if (rdfDescriptor->UICount > 0)
  932. hints |= PLUGIN_HAS_CUSTOM_UI;
  933. DISCOVERY_OUT("init", "-----------");
  934. DISCOVERY_OUT("build", BINARY_NATIVE);
  935. DISCOVERY_OUT("hints", hints);
  936. if (rdfDescriptor->Name != nullptr)
  937. DISCOVERY_OUT("name", rdfDescriptor->Name);
  938. if (rdfDescriptor->Author != nullptr)
  939. DISCOVERY_OUT("maker", rdfDescriptor->Author);
  940. DISCOVERY_OUT("uri", rdfDescriptor->URI);
  941. DISCOVERY_OUT("uniqueId", rdfDescriptor->UniqueID);
  942. DISCOVERY_OUT("audio.ins", audioIns);
  943. DISCOVERY_OUT("audio.outs", audioOuts);
  944. DISCOVERY_OUT("midi.ins", midiIns);
  945. DISCOVERY_OUT("midi.outs", midiOuts);
  946. DISCOVERY_OUT("parameters.ins", parametersIns);
  947. DISCOVERY_OUT("parameters.outs", parametersOuts);
  948. DISCOVERY_OUT("end", "------------");
  949. delete rdfDescriptor;
  950. }
  951. }
  952. #ifndef CARLA_OS_MAC
  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. #endif // ! CARLA_OS_MAC
  1160. static void do_fluidsynth_check(const char* const filename, const bool doInit)
  1161. {
  1162. #ifdef HAVE_FLUIDSYNTH
  1163. const juce::String jfilename = juce::String(CharPointer_UTF8(filename));
  1164. const File file(jfilename);
  1165. if (! file.existsAsFile())
  1166. {
  1167. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1168. return;
  1169. }
  1170. if (! fluid_is_soundfont(filename))
  1171. {
  1172. DISCOVERY_OUT("error", "Not a SF2 file");
  1173. return;
  1174. }
  1175. int programs = 0;
  1176. if (doInit)
  1177. {
  1178. fluid_settings_t* const f_settings = new_fluid_settings();
  1179. CARLA_SAFE_ASSERT_RETURN(f_settings != nullptr,);
  1180. fluid_synth_t* const f_synth = new_fluid_synth(f_settings);
  1181. CARLA_SAFE_ASSERT_RETURN(f_synth != nullptr,);
  1182. const int f_id = fluid_synth_sfload(f_synth, filename, 0);
  1183. if (f_id < 0)
  1184. {
  1185. DISCOVERY_OUT("error", "Failed to load SF2 file");
  1186. return;
  1187. }
  1188. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(f_synth, static_cast<uint>(f_id)))
  1189. {
  1190. fluid_preset_t f_preset;
  1191. f_sfont->iteration_start(f_sfont);
  1192. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  1193. ++programs;
  1194. }
  1195. delete_fluid_synth(f_synth);
  1196. delete_fluid_settings(f_settings);
  1197. }
  1198. CarlaString name(file.getFileNameWithoutExtension().toRawUTF8());
  1199. CarlaString label(name);
  1200. // 2 channels
  1201. DISCOVERY_OUT("init", "-----------");
  1202. DISCOVERY_OUT("build", BINARY_NATIVE);
  1203. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1204. DISCOVERY_OUT("name", name.buffer());
  1205. DISCOVERY_OUT("label", label.buffer());
  1206. DISCOVERY_OUT("audio.outs", 2);
  1207. DISCOVERY_OUT("midi.ins", 1);
  1208. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1209. DISCOVERY_OUT("parameters.outs", 1);
  1210. DISCOVERY_OUT("end", "------------");
  1211. // 16 channels
  1212. if (doInit && (name.isEmpty() || programs <= 1))
  1213. return;
  1214. name += " (16 outputs)";
  1215. DISCOVERY_OUT("init", "-----------");
  1216. DISCOVERY_OUT("build", BINARY_NATIVE);
  1217. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1218. DISCOVERY_OUT("name", name.buffer());
  1219. DISCOVERY_OUT("label", label.buffer());
  1220. DISCOVERY_OUT("audio.outs", 32);
  1221. DISCOVERY_OUT("midi.ins", 1);
  1222. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1223. DISCOVERY_OUT("parameters.outs", 1);
  1224. DISCOVERY_OUT("end", "------------");
  1225. #else // HAVE_FLUIDSYNTH
  1226. DISCOVERY_OUT("error", "SF2 support not available");
  1227. return;
  1228. // unused
  1229. (void)filename;
  1230. (void)doInit;
  1231. #endif
  1232. }
  1233. static void do_linuxsampler_check(const char* const filename, const char* const stype, const bool doInit)
  1234. {
  1235. #ifdef HAVE_LINUXSAMPLER
  1236. const juce::String jfilename = juce::String(CharPointer_UTF8(filename));
  1237. const File file(jfilename);
  1238. if (! file.existsAsFile())
  1239. {
  1240. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1241. return;
  1242. }
  1243. if (doInit)
  1244. const LinuxSamplerScopedEngine engine(filename, stype);
  1245. else
  1246. LinuxSamplerScopedEngine::outputInfo(nullptr, file.getFileNameWithoutExtension().toRawUTF8(), std::strcmp(stype, "gig") == 0);
  1247. #else // HAVE_LINUXSAMPLER
  1248. DISCOVERY_OUT("error", stype << " support not available");
  1249. return;
  1250. // unused
  1251. (void)filename;
  1252. (void)doInit;
  1253. #endif
  1254. }
  1255. // ------------------------------ main entry point ------------------------------
  1256. int main(int argc, char* argv[])
  1257. {
  1258. if (argc != 3)
  1259. {
  1260. carla_stdout("usage: %s <type> </path/to/plugin>", argv[0]);
  1261. return 1;
  1262. }
  1263. const char* const stype = argv[1];
  1264. const char* const filename = argv[2];
  1265. const PluginType type = getPluginTypeFromString(stype);
  1266. CarlaString filenameCheck(filename);
  1267. filenameCheck.toLower();
  1268. if (type != PLUGIN_GIG && type != PLUGIN_SF2 && type != PLUGIN_SFZ)
  1269. {
  1270. if (filenameCheck.contains("fluidsynth", true))
  1271. {
  1272. DISCOVERY_OUT("info", "skipping fluidsynth based plugin");
  1273. return 0;
  1274. }
  1275. if (filenameCheck.contains("linuxsampler", true) || filenameCheck.endsWith("ls16.so"))
  1276. {
  1277. DISCOVERY_OUT("info", "skipping linuxsampler based plugin");
  1278. return 0;
  1279. }
  1280. }
  1281. bool openLib = false;
  1282. lib_t handle = nullptr;
  1283. switch (type)
  1284. {
  1285. case PLUGIN_LADSPA:
  1286. case PLUGIN_DSSI:
  1287. #ifndef CARLA_OS_MAC
  1288. case PLUGIN_VST2:
  1289. openLib = true;
  1290. #endif
  1291. default:
  1292. break;
  1293. }
  1294. if (openLib)
  1295. {
  1296. handle = lib_open(filename);
  1297. if (handle == nullptr)
  1298. {
  1299. print_lib_error(filename);
  1300. return 1;
  1301. }
  1302. }
  1303. // never do init for dssi-vst, takes too long and it's crashy
  1304. bool doInit = ! filenameCheck.contains("dssi-vst", true);
  1305. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS") != nullptr)
  1306. doInit = false;
  1307. if (doInit && handle != nullptr)
  1308. {
  1309. // test fast loading & unloading DLL without initializing the plugin(s)
  1310. if (! lib_close(handle))
  1311. {
  1312. print_lib_error(filename);
  1313. return 1;
  1314. }
  1315. handle = lib_open(filename);
  1316. if (handle == nullptr)
  1317. {
  1318. print_lib_error(filename);
  1319. return 1;
  1320. }
  1321. }
  1322. if (std::strcmp(filename, ":all") == 0)
  1323. {
  1324. do_cached_check(type);
  1325. return 0;
  1326. }
  1327. switch (type)
  1328. {
  1329. case PLUGIN_LADSPA:
  1330. do_ladspa_check(handle, filename, doInit);
  1331. break;
  1332. case PLUGIN_DSSI:
  1333. do_dssi_check(handle, filename, doInit);
  1334. break;
  1335. case PLUGIN_LV2:
  1336. do_lv2_check(filename, doInit);
  1337. break;
  1338. case PLUGIN_VST2:
  1339. do_vst_check(handle, doInit);
  1340. break;
  1341. case PLUGIN_GIG:
  1342. do_linuxsampler_check(filename, "gig", doInit);
  1343. break;
  1344. case PLUGIN_SF2:
  1345. do_fluidsynth_check(filename, doInit);
  1346. break;
  1347. case PLUGIN_SFZ:
  1348. do_linuxsampler_check(filename, "sfz", doInit);
  1349. break;
  1350. default:
  1351. break;
  1352. }
  1353. if (openLib && handle != nullptr)
  1354. lib_close(handle);
  1355. return 0;
  1356. }
  1357. // --------------------------------------------------------------------------