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.

1742 lines
55KB

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