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.

1708 lines
53KB

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