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.

1780 lines
56KB

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