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.

1813 lines
56KB

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