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.

1720 lines
54KB

  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(void*& libHandle, const char* const filename, const bool doInit)
  321. {
  322. LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)lib_symbol(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 = (LADSPA_Descriptor_Function)lib_symbol(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(void*& libHandle, const char* const filename, const bool doInit)
  513. {
  514. DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)lib_symbol(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 = (DSSI_Descriptor_Function)lib_symbol(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. void* const 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. void* const 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_GENERATOR(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(void*& libHandle, const bool doInit)
  934. {
  935. VST_Function vstFn = (VST_Function)lib_symbol(libHandle, "VSTPluginMain");
  936. if (vstFn == nullptr)
  937. {
  938. vstFn = (VST_Function)lib_symbol(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. using namespace juce;
  1116. ScopedPointer<AudioPluginFormat> pluginFormat;
  1117. if (stype == nullptr)
  1118. return;
  1119. else if (std::strcmp(stype, "VST") == 0)
  1120. {
  1121. #if JUCE_PLUGINHOST_VST
  1122. pluginFormat = new VSTPluginFormat();
  1123. #else
  1124. DISCOVERY_OUT("error", "VST support not available");
  1125. #endif
  1126. }
  1127. else if (std::strcmp(stype, "VST3") == 0)
  1128. {
  1129. #if JUCE_PLUGINHOST_VST3
  1130. pluginFormat = new VST3PluginFormat();
  1131. #else
  1132. DISCOVERY_OUT("error", "VST3 support not available");
  1133. #endif
  1134. }
  1135. else if (std::strcmp(stype, "AU") == 0)
  1136. {
  1137. #if JUCE_PLUGINHOST_AU
  1138. pluginFormat = new AudioUnitPluginFormat();
  1139. #else
  1140. DISCOVERY_OUT("error", "AU support not available");
  1141. #endif
  1142. }
  1143. if (pluginFormat == nullptr)
  1144. {
  1145. DISCOVERY_OUT("error", stype << " support not available");
  1146. return;
  1147. }
  1148. OwnedArray<PluginDescription> results;
  1149. pluginFormat->findAllTypesForFile(results, filename);
  1150. for (PluginDescription **it = results.begin(), **end = results.end(); it != end; ++it)
  1151. {
  1152. static int iv=0;
  1153. carla_stderr2("LOOKING FOR PLUGIN %i", iv++);
  1154. PluginDescription* const desc(*it);
  1155. uint hints = 0x0;
  1156. int audioIns = desc->numInputChannels;
  1157. int audioOuts = desc->numOutputChannels;
  1158. int midiIns = 0;
  1159. int midiOuts = 0;
  1160. int parameters = 0;
  1161. if (desc->isInstrument)
  1162. hints |= PLUGIN_IS_SYNTH;
  1163. if (doInit)
  1164. {
  1165. if (AudioPluginInstance* const instance = pluginFormat->createInstanceFromDescription(*desc, kSampleRate, kBufferSize))
  1166. {
  1167. instance->refreshParameterList();
  1168. parameters = instance->getNumParameters();
  1169. if (instance->hasEditor())
  1170. hints |= PLUGIN_HAS_CUSTOM_UI;
  1171. if (instance->acceptsMidi())
  1172. midiIns = 1;
  1173. if (instance->producesMidi())
  1174. midiOuts = 1;
  1175. delete instance;
  1176. }
  1177. }
  1178. DISCOVERY_OUT("init", "-----------");
  1179. DISCOVERY_OUT("build", BINARY_NATIVE);
  1180. DISCOVERY_OUT("hints", hints);
  1181. DISCOVERY_OUT("name", desc->name);
  1182. DISCOVERY_OUT("label", desc->descriptiveName);
  1183. DISCOVERY_OUT("maker", desc->manufacturerName);
  1184. DISCOVERY_OUT("uniqueId", desc->uid);
  1185. DISCOVERY_OUT("audio.ins", audioIns);
  1186. DISCOVERY_OUT("audio.outs", audioOuts);
  1187. DISCOVERY_OUT("midi.ins", midiIns);
  1188. DISCOVERY_OUT("midi.outs", midiOuts);
  1189. DISCOVERY_OUT("parameters.ins", parameters);
  1190. DISCOVERY_OUT("end", "------------");
  1191. }
  1192. }
  1193. #endif
  1194. static void do_fluidsynth_check(const char* const filename, const bool doInit)
  1195. {
  1196. #ifdef HAVE_FLUIDSYNTH
  1197. const File file(filename);
  1198. if (! file.existsAsFile())
  1199. {
  1200. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1201. return;
  1202. }
  1203. if (! fluid_is_soundfont(filename))
  1204. {
  1205. DISCOVERY_OUT("error", "Not a SF2 file");
  1206. return;
  1207. }
  1208. int programs = 0;
  1209. if (doInit)
  1210. {
  1211. fluid_settings_t* const f_settings = new_fluid_settings();
  1212. CARLA_SAFE_ASSERT_RETURN(f_settings != nullptr,);
  1213. fluid_synth_t* const f_synth = new_fluid_synth(f_settings);
  1214. CARLA_SAFE_ASSERT_RETURN(f_synth != nullptr,);
  1215. const int f_id = fluid_synth_sfload(f_synth, filename, 0);
  1216. if (f_id < 0)
  1217. {
  1218. DISCOVERY_OUT("error", "Failed to load SF2 file");
  1219. return;
  1220. }
  1221. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(f_synth, static_cast<uint>(f_id)))
  1222. {
  1223. fluid_preset_t f_preset;
  1224. f_sfont->iteration_start(f_sfont);
  1225. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  1226. ++programs;
  1227. }
  1228. delete_fluid_synth(f_synth);
  1229. delete_fluid_settings(f_settings);
  1230. }
  1231. CarlaString name(file.getFileNameWithoutExtension().toRawUTF8());
  1232. CarlaString label(name);
  1233. // 2 channels
  1234. DISCOVERY_OUT("init", "-----------");
  1235. DISCOVERY_OUT("build", BINARY_NATIVE);
  1236. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1237. DISCOVERY_OUT("name", name.buffer());
  1238. DISCOVERY_OUT("label", label.buffer());
  1239. DISCOVERY_OUT("audio.outs", 2);
  1240. DISCOVERY_OUT("midi.ins", 1);
  1241. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1242. DISCOVERY_OUT("parameters.outs", 1);
  1243. DISCOVERY_OUT("end", "------------");
  1244. // 16 channels
  1245. if (doInit && (name.isEmpty() || programs <= 1))
  1246. return;
  1247. name += " (16 outputs)";
  1248. DISCOVERY_OUT("init", "-----------");
  1249. DISCOVERY_OUT("build", BINARY_NATIVE);
  1250. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1251. DISCOVERY_OUT("name", name.buffer());
  1252. DISCOVERY_OUT("label", label.buffer());
  1253. DISCOVERY_OUT("audio.outs", 32);
  1254. DISCOVERY_OUT("midi.ins", 1);
  1255. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1256. DISCOVERY_OUT("parameters.outs", 1);
  1257. DISCOVERY_OUT("end", "------------");
  1258. #else // HAVE_FLUIDSYNTH
  1259. DISCOVERY_OUT("error", "SF2 support not available");
  1260. return;
  1261. // unused
  1262. (void)filename;
  1263. (void)doInit;
  1264. #endif
  1265. }
  1266. static void do_linuxsampler_check(const char* const filename, const char* const stype, const bool doInit)
  1267. {
  1268. #ifdef HAVE_LINUXSAMPLER
  1269. const File file(filename);
  1270. if (! file.existsAsFile())
  1271. {
  1272. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1273. return;
  1274. }
  1275. if (doInit)
  1276. const LinuxSamplerScopedEngine engine(filename, stype);
  1277. else
  1278. LinuxSamplerScopedEngine::outputInfo(nullptr, file.getFileNameWithoutExtension().toRawUTF8(), std::strcmp(stype, "gig") == 0);
  1279. #else // HAVE_LINUXSAMPLER
  1280. DISCOVERY_OUT("error", stype << " support not available");
  1281. return;
  1282. // unused
  1283. (void)filename;
  1284. (void)doInit;
  1285. #endif
  1286. }
  1287. // ------------------------------ main entry point ------------------------------
  1288. int main(int argc, char* argv[])
  1289. {
  1290. if (argc != 3)
  1291. {
  1292. carla_stdout("usage: %s <type> </path/to/plugin>", argv[0]);
  1293. return 1;
  1294. }
  1295. const char* const stype = argv[1];
  1296. const char* const filename = argv[2];
  1297. const PluginType type = getPluginTypeFromString(stype);
  1298. CarlaString filenameStr(filename);
  1299. filenameStr.toLower();
  1300. if (type != PLUGIN_GIG && type != PLUGIN_SF2 && type != PLUGIN_SFZ)
  1301. {
  1302. if (filenameStr.contains("fluidsynth", true))
  1303. {
  1304. DISCOVERY_OUT("info", "skipping fluidsynth based plugin");
  1305. return 0;
  1306. }
  1307. if (filenameStr.contains("linuxsampler", true) || filenameStr.endsWith("ls16.so"))
  1308. {
  1309. DISCOVERY_OUT("info", "skipping linuxsampler based plugin");
  1310. return 0;
  1311. }
  1312. }
  1313. bool openLib = false;
  1314. void* handle = nullptr;
  1315. switch (type)
  1316. {
  1317. case PLUGIN_LADSPA:
  1318. case PLUGIN_DSSI:
  1319. #ifndef CARLA_OS_MAC
  1320. case PLUGIN_VST:
  1321. openLib = true;
  1322. #endif
  1323. default:
  1324. break;
  1325. }
  1326. if (openLib)
  1327. {
  1328. handle = lib_open(filename);
  1329. if (handle == nullptr)
  1330. {
  1331. print_lib_error(filename);
  1332. return 1;
  1333. }
  1334. }
  1335. // never do init for dssi-vst, takes too long and it's crashy
  1336. bool doInit = ! filenameStr.contains("dssi-vst", true);
  1337. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS") != nullptr)
  1338. doInit = false;
  1339. if (doInit && handle != nullptr)
  1340. {
  1341. // test fast loading & unloading DLL without initializing the plugin(s)
  1342. if (! lib_close(handle))
  1343. {
  1344. print_lib_error(filename);
  1345. return 1;
  1346. }
  1347. handle = lib_open(filename);
  1348. if (handle == nullptr)
  1349. {
  1350. print_lib_error(filename);
  1351. return 1;
  1352. }
  1353. }
  1354. switch (type)
  1355. {
  1356. case PLUGIN_LADSPA:
  1357. do_ladspa_check(handle, filename, doInit);
  1358. break;
  1359. case PLUGIN_DSSI:
  1360. do_dssi_check(handle, filename, doInit);
  1361. break;
  1362. case PLUGIN_LV2:
  1363. do_lv2_check(filename, doInit);
  1364. break;
  1365. case PLUGIN_VST:
  1366. #ifdef CARLA_OS_MAC
  1367. do_juce_check(filename, "VST", doInit);
  1368. #else
  1369. do_vst_check(handle, doInit);
  1370. #endif
  1371. break;
  1372. case PLUGIN_VST3:
  1373. #ifdef USE_JUCE_PROCESSORS
  1374. do_juce_check(filename, "VST3", doInit);
  1375. #else
  1376. DISCOVERY_OUT("error", "VST3 support not available");
  1377. #endif
  1378. break;
  1379. case PLUGIN_AU:
  1380. #ifdef USE_JUCE_PROCESSORS
  1381. do_juce_check(filename, "AU", doInit);
  1382. #else
  1383. DISCOVERY_OUT("error", "AU support not available");
  1384. #endif
  1385. break;
  1386. case PLUGIN_GIG:
  1387. do_linuxsampler_check(filename, "gig", doInit);
  1388. break;
  1389. case PLUGIN_SF2:
  1390. do_fluidsynth_check(filename, doInit);
  1391. break;
  1392. case PLUGIN_SFZ:
  1393. do_linuxsampler_check(filename, "sfz", doInit);
  1394. break;
  1395. default:
  1396. break;
  1397. }
  1398. if (openLib && handle != nullptr)
  1399. lib_close(handle);
  1400. return 0;
  1401. }
  1402. // --------------------------------------------------------------------------