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.

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