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.

1797 lines
55KB

  1. /*
  2. * Carla Plugin discovery
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaBackendUtils.hpp"
  18. #include "CarlaLibUtils.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  22. # define 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::FloatVectorOperations;
  46. // FIXME
  47. #include <QtCore/QDir>
  48. #include <QtCore/QFileInfo>
  49. #include <QtCore/QUrl>
  50. #define DISCOVERY_OUT(x, y) std::cout << "\ncarla-discovery::" << x << "::" << y << std::endl;
  51. CARLA_BACKEND_USE_NAMESPACE
  52. // --------------------------------------------------------------------------
  53. // Dummy values to test plugins with
  54. static const uint32_t kBufferSize = 512;
  55. static const double kSampleRate = 44100.0;
  56. static const int32_t kSampleRatei = 44100;
  57. static const float kSampleRatef = 44100.0f;
  58. // --------------------------------------------------------------------------
  59. // Don't print ELF/EXE related errors since discovery can find multi-architecture binaries
  60. static void print_lib_error(const char* const filename)
  61. {
  62. const char* const error(lib_error(filename));
  63. if (error != nullptr && std::strstr(error, "wrong ELF class") == nullptr && std::strstr(error, "Bad EXE format") == nullptr)
  64. DISCOVERY_OUT("error", error);
  65. }
  66. #if defined(WANT_VST) && ! defined(CARLA_OS_MAC)
  67. // --------------------------------------------------------------------------
  68. // VST stuff
  69. // Check if plugin is currently processing
  70. static bool gVstIsProcessing = false;
  71. // Check if plugin needs idle
  72. static bool gVstNeedsIdle = false;
  73. // Check if plugin wants midi
  74. static bool gVstWantsMidi = false;
  75. // Check if plugin wants time
  76. static bool gVstWantsTime = false;
  77. // Current uniqueId for VST shell plugins
  78. static intptr_t gVstCurrentUniqueId = 0;
  79. // Supported Carla features
  80. static intptr_t vstHostCanDo(const char* const feature)
  81. {
  82. carla_debug("vstHostCanDo(\"%s\")", feature);
  83. if (std::strcmp(feature, "supplyIdle") == 0)
  84. return 1;
  85. if (std::strcmp(feature, "sendVstEvents") == 0)
  86. return 1;
  87. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  88. return 1;
  89. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  90. return 1;
  91. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  92. {
  93. gVstWantsTime = true;
  94. return 1;
  95. }
  96. if (std::strcmp(feature, "receiveVstEvents") == 0)
  97. return 1;
  98. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  99. return 1;
  100. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  101. return -1;
  102. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  103. return -1;
  104. if (std::strcmp(feature, "acceptIOChanges") == 0)
  105. return 1;
  106. if (std::strcmp(feature, "sizeWindow") == 0)
  107. return 1;
  108. if (std::strcmp(feature, "offline") == 0)
  109. return -1;
  110. if (std::strcmp(feature, "openFileSelector") == 0)
  111. return -1;
  112. if (std::strcmp(feature, "closeFileSelector") == 0)
  113. return -1;
  114. if (std::strcmp(feature, "startStopProcess") == 0)
  115. return 1;
  116. if (std::strcmp(feature, "supportShell") == 0)
  117. return 1;
  118. if (std::strcmp(feature, "shellCategory") == 0)
  119. return 1;
  120. // non-official features found in some plugins:
  121. // "asyncProcessing"
  122. // "editFile"
  123. // unimplemented
  124. carla_stderr("vstHostCanDo(\"%s\") - unknown feature", feature);
  125. return 0;
  126. }
  127. // Host-side callback
  128. 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)
  129. {
  130. carla_debug("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  131. static VstTimeInfo timeInfo;
  132. intptr_t ret = 0;
  133. switch (opcode)
  134. {
  135. case audioMasterAutomate:
  136. ret = 1;
  137. break;
  138. case audioMasterVersion:
  139. ret = kVstVersion;
  140. break;
  141. case audioMasterCurrentId:
  142. if (gVstCurrentUniqueId == 0) DISCOVERY_OUT("warning", "plugin asked for uniqueId, but it's currently 0");
  143. ret = gVstCurrentUniqueId;
  144. break;
  145. case DECLARE_VST_DEPRECATED(audioMasterWantMidi):
  146. if (gVstWantsMidi) DISCOVERY_OUT("warning", "plugin requested MIDI more than once");
  147. gVstWantsMidi = true;
  148. ret = 1;
  149. break;
  150. case audioMasterGetTime:
  151. if (! gVstIsProcessing) DISCOVERY_OUT("warning", "plugin requested timeInfo out of process");
  152. if (! gVstWantsTime) DISCOVERY_OUT("warning", "plugin requested timeInfo but didn't ask if host could do \"sendVstTimeInfo\"");
  153. carla_zeroStruct<VstTimeInfo>(timeInfo);
  154. timeInfo.sampleRate = kSampleRate;
  155. // Tempo
  156. timeInfo.tempo = 120.0;
  157. timeInfo.flags |= kVstTempoValid;
  158. // Time Signature
  159. timeInfo.timeSigNumerator = 4;
  160. timeInfo.timeSigDenominator = 4;
  161. timeInfo.flags |= kVstTimeSigValid;
  162. ret = (intptr_t)&timeInfo;
  163. break;
  164. case DECLARE_VST_DEPRECATED(audioMasterTempoAt):
  165. ret = 120 * 10000;
  166. break;
  167. case DECLARE_VST_DEPRECATED(audioMasterGetNumAutomatableParameters):
  168. ret = carla_fixValue<intptr_t>(0, MAX_DEFAULT_PARAMETERS, effect->numParams);
  169. break;
  170. case DECLARE_VST_DEPRECATED(audioMasterGetParameterQuantization):
  171. ret = 1; // full single float precision
  172. break;
  173. case DECLARE_VST_DEPRECATED(audioMasterNeedIdle):
  174. if (gVstNeedsIdle) DISCOVERY_OUT("warning", "plugin requested idle more than once");
  175. gVstNeedsIdle = true;
  176. ret = 1;
  177. break;
  178. case audioMasterGetSampleRate:
  179. ret = kSampleRatei;
  180. break;
  181. case audioMasterGetBlockSize:
  182. ret = kBufferSize;
  183. break;
  184. case DECLARE_VST_DEPRECATED(audioMasterWillReplaceOrAccumulate):
  185. ret = 1; // replace
  186. break;
  187. case audioMasterGetCurrentProcessLevel:
  188. ret = gVstIsProcessing ? kVstProcessLevelRealtime : kVstProcessLevelUser;
  189. break;
  190. case audioMasterGetAutomationState:
  191. ret = kVstAutomationOff;
  192. break;
  193. case audioMasterGetVendorString:
  194. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  195. std::strcpy((char*)ptr, "falkTX");
  196. ret = 1;
  197. break;
  198. case audioMasterGetProductString:
  199. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  200. std::strcpy((char*)ptr, "Carla-Discovery");
  201. ret = 1;
  202. break;
  203. case audioMasterGetVendorVersion:
  204. ret = CARLA_VERSION_HEX;
  205. break;
  206. case audioMasterCanDo:
  207. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  208. ret = vstHostCanDo((const char*)ptr);
  209. break;
  210. case audioMasterGetLanguage:
  211. ret = kVstLangEnglish;
  212. break;
  213. default:
  214. carla_stdout("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  215. break;
  216. }
  217. return ret;
  218. }
  219. #endif
  220. #ifdef WANT_LINUXSAMPLER
  221. // --------------------------------------------------------------------------
  222. // LinuxSampler stuff
  223. class LinuxSamplerScopedEngine
  224. {
  225. public:
  226. LinuxSamplerScopedEngine(const char* const filename, const char* const stype)
  227. : fEngine(nullptr)
  228. {
  229. using namespace LinuxSampler;
  230. try {
  231. fEngine = EngineFactory::Create(stype);
  232. }
  233. catch (const Exception& e)
  234. {
  235. DISCOVERY_OUT("error", e.what());
  236. return;
  237. }
  238. if (fEngine == nullptr)
  239. return;
  240. InstrumentManager* const insMan(fEngine->GetInstrumentManager());
  241. if (insMan == nullptr)
  242. {
  243. DISCOVERY_OUT("error", "Failed to get LinuxSampler instrument manager");
  244. return;
  245. }
  246. std::vector<InstrumentManager::instrument_id_t> ids;
  247. try {
  248. ids = insMan->GetInstrumentFileContent(filename);
  249. }
  250. catch (const InstrumentManagerException& e)
  251. {
  252. DISCOVERY_OUT("error", e.what());
  253. return;
  254. }
  255. if (ids.size() == 0)
  256. {
  257. DISCOVERY_OUT("error", "Failed to find any instruments");
  258. return;
  259. }
  260. InstrumentManager::instrument_info_t info;
  261. try {
  262. info = insMan->GetInstrumentInfo(ids[0]);
  263. }
  264. catch (const InstrumentManagerException& e)
  265. {
  266. DISCOVERY_OUT("error", e.what());
  267. return;
  268. }
  269. outputInfo(&info, ids.size());
  270. }
  271. ~LinuxSamplerScopedEngine()
  272. {
  273. if (fEngine != nullptr)
  274. {
  275. LinuxSampler::EngineFactory::Destroy(fEngine);
  276. fEngine = nullptr;
  277. }
  278. }
  279. static void outputInfo(const LinuxSampler::InstrumentManager::instrument_info_t* const info, const size_t programs, const char* const basename = nullptr)
  280. {
  281. CarlaString name;
  282. const char* label;
  283. if (info != nullptr)
  284. {
  285. name = info->InstrumentName.c_str();
  286. label = info->Product.c_str();
  287. }
  288. else
  289. {
  290. name = basename;
  291. label = basename;
  292. }
  293. // 2 channels
  294. DISCOVERY_OUT("init", "-----------");
  295. DISCOVERY_OUT("build", BINARY_NATIVE);
  296. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  297. DISCOVERY_OUT("name", name.buffer());
  298. DISCOVERY_OUT("label", label);
  299. if (info != nullptr)
  300. DISCOVERY_OUT("maker", info->Artists);
  301. DISCOVERY_OUT("audio.outs", 2);
  302. DISCOVERY_OUT("midi.ins", 1);
  303. DISCOVERY_OUT("end", "------------");
  304. // 16 channels
  305. if (name.isEmpty() || programs <= 1)
  306. return;
  307. name += " (16 outputs)";
  308. DISCOVERY_OUT("init", "-----------");
  309. DISCOVERY_OUT("build", BINARY_NATIVE);
  310. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  311. DISCOVERY_OUT("name", name.buffer());
  312. DISCOVERY_OUT("label", label);
  313. if (info != nullptr)
  314. DISCOVERY_OUT("maker", info->Artists);
  315. DISCOVERY_OUT("audio.outs", 32);
  316. DISCOVERY_OUT("midi.ins", 1);
  317. DISCOVERY_OUT("end", "------------");
  318. }
  319. private:
  320. LinuxSampler::Engine* fEngine;
  321. CARLA_PREVENT_HEAP_ALLOCATION
  322. CARLA_DECLARE_NON_COPY_CLASS(LinuxSamplerScopedEngine)
  323. };
  324. #endif
  325. // ------------------------------ Plugin Checks -----------------------------
  326. static void do_ladspa_check(void*& libHandle, const char* const filename, const bool init)
  327. {
  328. #ifdef WANT_LADSPA
  329. LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)lib_symbol(libHandle, "ladspa_descriptor");
  330. if (descFn == nullptr)
  331. {
  332. DISCOVERY_OUT("error", "Not a LADSPA plugin");
  333. return;
  334. }
  335. const LADSPA_Descriptor* descriptor;
  336. {
  337. descriptor = descFn(0);
  338. if (descriptor == nullptr)
  339. {
  340. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  341. return;
  342. }
  343. if (init && descriptor->instantiate != nullptr && descriptor->cleanup != nullptr)
  344. {
  345. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  346. if (handle == nullptr)
  347. {
  348. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  349. return;
  350. }
  351. descriptor->cleanup(handle);
  352. lib_close(libHandle);
  353. libHandle = lib_open(filename);
  354. if (libHandle == nullptr)
  355. {
  356. print_lib_error(filename);
  357. return;
  358. }
  359. descFn = (LADSPA_Descriptor_Function)lib_symbol(libHandle, "ladspa_descriptor");
  360. if (descFn == nullptr)
  361. {
  362. DISCOVERY_OUT("error", "Not a LADSPA plugin (#2)");
  363. return;
  364. }
  365. }
  366. }
  367. unsigned long i = 0;
  368. while ((descriptor = descFn(i++)) != nullptr)
  369. {
  370. if (descriptor->instantiate == nullptr)
  371. {
  372. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no instantiate()");
  373. continue;
  374. }
  375. if (descriptor->cleanup == nullptr)
  376. {
  377. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no cleanup()");
  378. continue;
  379. }
  380. if (descriptor->run == nullptr)
  381. {
  382. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no run()");
  383. continue;
  384. }
  385. if (! LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  386. {
  387. DISCOVERY_OUT("warning", "Plugin '" << descriptor->Name << "' is not hard real-time capable");
  388. }
  389. uint hints = 0x0;
  390. int audioIns = 0;
  391. int audioOuts = 0;
  392. int audioTotal = 0;
  393. int parametersIns = 0;
  394. int parametersOuts = 0;
  395. int parametersTotal = 0;
  396. if (LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  397. hints |= PLUGIN_IS_RTSAFE;
  398. for (unsigned long j=0; j < descriptor->PortCount; ++j)
  399. {
  400. CARLA_ASSERT(descriptor->PortNames[j] != nullptr);
  401. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  402. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  403. {
  404. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  405. audioIns += 1;
  406. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  407. audioOuts += 1;
  408. audioTotal += 1;
  409. }
  410. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  411. {
  412. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  413. parametersIns += 1;
  414. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(descriptor->PortNames[j], "latency") != 0 && std::strcmp(descriptor->PortNames[j], "_latency") != 0)
  415. parametersOuts += 1;
  416. parametersTotal += 1;
  417. }
  418. }
  419. if (init)
  420. {
  421. // -----------------------------------------------------------------------
  422. // start crash-free plugin test
  423. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  424. if (handle == nullptr)
  425. {
  426. DISCOVERY_OUT("error", "Failed to init LADSPA plugin");
  427. continue;
  428. }
  429. // Test quick init and cleanup
  430. descriptor->cleanup(handle);
  431. handle = descriptor->instantiate(descriptor, kSampleRatei);
  432. if (handle == nullptr)
  433. {
  434. DISCOVERY_OUT("error", "Failed to init LADSPA plugin (#2)");
  435. continue;
  436. }
  437. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  438. LADSPA_Data bufferParams[parametersTotal];
  439. LADSPA_Data min, max, def;
  440. for (unsigned long j=0, iA=0, iC=0; j < descriptor->PortCount; ++j)
  441. {
  442. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  443. const LADSPA_PortRangeHint portRangeHints = descriptor->PortRangeHints[j];
  444. const char* const portName = descriptor->PortNames[j];
  445. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  446. {
  447. FloatVectorOperations::clear(bufferAudio[iA], kBufferSize);
  448. descriptor->connect_port(handle, j, bufferAudio[iA++]);
  449. }
  450. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  451. {
  452. // min value
  453. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  454. min = portRangeHints.LowerBound;
  455. else
  456. min = 0.0f;
  457. // max value
  458. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  459. max = portRangeHints.UpperBound;
  460. else
  461. max = 1.0f;
  462. if (min > max)
  463. {
  464. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  465. max = min + 0.1f;
  466. }
  467. else if (max - min == 0.0f)
  468. {
  469. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max - min == 0");
  470. max = min + 0.1f;
  471. }
  472. // default value
  473. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  474. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  475. {
  476. min *= kSampleRatef;
  477. max *= kSampleRatef;
  478. def *= kSampleRatef;
  479. }
  480. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  481. {
  482. // latency parameter
  483. def = 0.0f;
  484. }
  485. else
  486. {
  487. if (def < min)
  488. def = min;
  489. else if (def > max)
  490. def = max;
  491. }
  492. bufferParams[iC] = def;
  493. descriptor->connect_port(handle, j, &bufferParams[iC++]);
  494. }
  495. }
  496. if (descriptor->activate != nullptr)
  497. descriptor->activate(handle);
  498. descriptor->run(handle, kBufferSize);
  499. if (descriptor->deactivate != nullptr)
  500. descriptor->deactivate(handle);
  501. descriptor->cleanup(handle);
  502. // end crash-free plugin test
  503. // -----------------------------------------------------------------------
  504. }
  505. DISCOVERY_OUT("init", "-----------");
  506. DISCOVERY_OUT("build", BINARY_NATIVE);
  507. DISCOVERY_OUT("hints", hints);
  508. DISCOVERY_OUT("name", descriptor->Name);
  509. DISCOVERY_OUT("label", descriptor->Label);
  510. DISCOVERY_OUT("maker", descriptor->Maker);
  511. DISCOVERY_OUT("uniqueId", descriptor->UniqueID);
  512. DISCOVERY_OUT("audio.ins", audioIns);
  513. DISCOVERY_OUT("audio.outs", audioOuts);
  514. DISCOVERY_OUT("parameters.ins", parametersIns);
  515. DISCOVERY_OUT("parameters.outs", parametersOuts);
  516. DISCOVERY_OUT("end", "------------");
  517. }
  518. #else
  519. DISCOVERY_OUT("error", "LADSPA support not available");
  520. return;
  521. // unused
  522. (void)libHandle;
  523. (void)filename;
  524. (void)init;
  525. #endif
  526. }
  527. static void do_dssi_check(void*& libHandle, const char* const filename, const bool init)
  528. {
  529. #ifdef WANT_DSSI
  530. DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)lib_symbol(libHandle, "dssi_descriptor");
  531. if (descFn == nullptr)
  532. {
  533. DISCOVERY_OUT("error", "Not a DSSI plugin");
  534. return;
  535. }
  536. const DSSI_Descriptor* descriptor;
  537. {
  538. descriptor = descFn(0);
  539. if (descriptor == nullptr)
  540. {
  541. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  542. return;
  543. }
  544. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  545. if (ldescriptor == nullptr)
  546. {
  547. DISCOVERY_OUT("error", "DSSI plugin doesn't provide the LADSPA interface");
  548. return;
  549. }
  550. if (init && ldescriptor->instantiate != nullptr && ldescriptor->cleanup != nullptr)
  551. {
  552. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  553. if (handle == nullptr)
  554. {
  555. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  556. return;
  557. }
  558. ldescriptor->cleanup(handle);
  559. lib_close(libHandle);
  560. libHandle = lib_open(filename);
  561. if (libHandle == nullptr)
  562. {
  563. print_lib_error(filename);
  564. return;
  565. }
  566. descFn = (DSSI_Descriptor_Function)lib_symbol(libHandle, "dssi_descriptor");
  567. if (descFn == nullptr)
  568. {
  569. DISCOVERY_OUT("error", "Not a DSSI plugin (#2)");
  570. return;
  571. }
  572. }
  573. }
  574. unsigned long i = 0;
  575. while ((descriptor = descFn(i++)) != nullptr)
  576. {
  577. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  578. if (ldescriptor == nullptr)
  579. {
  580. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no LADSPA interface");
  581. continue;
  582. }
  583. if (descriptor->DSSI_API_Version != DSSI_VERSION_MAJOR)
  584. {
  585. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' uses an unsupported DSSI spec version " << descriptor->DSSI_API_Version);
  586. continue;
  587. }
  588. if (ldescriptor->instantiate == nullptr)
  589. {
  590. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no instantiate()");
  591. continue;
  592. }
  593. if (ldescriptor->cleanup == nullptr)
  594. {
  595. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no cleanup()");
  596. continue;
  597. }
  598. if (ldescriptor->run == nullptr && descriptor->run_synth == nullptr && descriptor->run_multiple_synths == nullptr)
  599. {
  600. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no run(), run_synth() or run_multiple_synths()");
  601. continue;
  602. }
  603. if (! LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  604. {
  605. DISCOVERY_OUT("warning", "Plugin '" << ldescriptor->Name << "' is not hard real-time capable");
  606. }
  607. uint hints = 0x0;
  608. int audioIns = 0;
  609. int audioOuts = 0;
  610. int audioTotal = 0;
  611. int midiIns = 0;
  612. int parametersIns = 0;
  613. int parametersOuts = 0;
  614. int parametersTotal = 0;
  615. if (LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  616. hints |= PLUGIN_IS_RTSAFE;
  617. for (unsigned long j=0; j < ldescriptor->PortCount; ++j)
  618. {
  619. CARLA_ASSERT(ldescriptor->PortNames[j] != nullptr);
  620. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  621. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  622. {
  623. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  624. audioIns += 1;
  625. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  626. audioOuts += 1;
  627. audioTotal += 1;
  628. }
  629. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  630. {
  631. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  632. parametersIns += 1;
  633. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(ldescriptor->PortNames[j], "latency") != 0 && std::strcmp(ldescriptor->PortNames[j], "_latency") != 0)
  634. parametersOuts += 1;
  635. parametersTotal += 1;
  636. }
  637. }
  638. if (descriptor->run_synth != nullptr || descriptor->run_multiple_synths != nullptr)
  639. midiIns = 1;
  640. if (midiIns > 0 && audioIns == 0 && audioOuts > 0)
  641. hints |= PLUGIN_IS_SYNTH;
  642. if (const char* const ui = find_dssi_ui(filename, ldescriptor->Label))
  643. {
  644. hints |= PLUGIN_HAS_CUSTOM_UI;
  645. delete[] ui;
  646. }
  647. if (init)
  648. {
  649. // -----------------------------------------------------------------------
  650. // start crash-free plugin test
  651. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  652. if (handle == nullptr)
  653. {
  654. DISCOVERY_OUT("error", "Failed to init DSSI plugin");
  655. continue;
  656. }
  657. // Test quick init and cleanup
  658. ldescriptor->cleanup(handle);
  659. handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  660. if (handle == nullptr)
  661. {
  662. DISCOVERY_OUT("error", "Failed to init DSSI plugin (#2)");
  663. continue;
  664. }
  665. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  666. LADSPA_Data bufferParams[parametersTotal];
  667. LADSPA_Data min, max, def;
  668. for (unsigned long j=0, iA=0, iC=0; j < ldescriptor->PortCount; ++j)
  669. {
  670. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  671. const LADSPA_PortRangeHint portRangeHints = ldescriptor->PortRangeHints[j];
  672. const char* const portName = ldescriptor->PortNames[j];
  673. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  674. {
  675. FloatVectorOperations::clear(bufferAudio[iA], kBufferSize);
  676. ldescriptor->connect_port(handle, j, bufferAudio[iA++]);
  677. }
  678. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  679. {
  680. // min value
  681. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  682. min = portRangeHints.LowerBound;
  683. else
  684. min = 0.0f;
  685. // max value
  686. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  687. max = portRangeHints.UpperBound;
  688. else
  689. max = 1.0f;
  690. if (min > max)
  691. {
  692. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  693. max = min + 0.1f;
  694. }
  695. else if (max - min == 0.0f)
  696. {
  697. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max - min == 0");
  698. max = min + 0.1f;
  699. }
  700. // default value
  701. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  702. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  703. {
  704. min *= kSampleRatef;
  705. max *= kSampleRatef;
  706. def *= kSampleRatef;
  707. }
  708. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  709. {
  710. // latency parameter
  711. def = 0.0f;
  712. }
  713. else
  714. {
  715. if (def < min)
  716. def = min;
  717. else if (def > max)
  718. def = max;
  719. }
  720. bufferParams[iC] = def;
  721. ldescriptor->connect_port(handle, j, &bufferParams[iC++]);
  722. }
  723. }
  724. // select first midi-program if available
  725. if (descriptor->get_program != nullptr && descriptor->select_program != nullptr)
  726. {
  727. if (const DSSI_Program_Descriptor* const pDesc = descriptor->get_program(handle, 0))
  728. descriptor->select_program(handle, pDesc->Bank, pDesc->Program);
  729. }
  730. if (ldescriptor->activate != nullptr)
  731. ldescriptor->activate(handle);
  732. if (descriptor->run_synth != nullptr || descriptor->run_multiple_synths != nullptr)
  733. {
  734. snd_seq_event_t midiEvents[2];
  735. carla_zeroStruct<snd_seq_event_t>(midiEvents, 2);
  736. const unsigned long midiEventCount = 2;
  737. midiEvents[0].type = SND_SEQ_EVENT_NOTEON;
  738. midiEvents[0].data.note.note = 64;
  739. midiEvents[0].data.note.velocity = 100;
  740. midiEvents[1].type = SND_SEQ_EVENT_NOTEOFF;
  741. midiEvents[1].data.note.note = 64;
  742. midiEvents[1].data.note.velocity = 0;
  743. midiEvents[1].time.tick = kBufferSize/2;
  744. if (descriptor->run_multiple_synths != nullptr && descriptor->run_synth == nullptr)
  745. {
  746. LADSPA_Handle handlePtr[1] = { handle };
  747. snd_seq_event_t* midiEventsPtr[1] = { midiEvents };
  748. unsigned long midiEventCountPtr[1] = { midiEventCount };
  749. descriptor->run_multiple_synths(1, handlePtr, kBufferSize, midiEventsPtr, midiEventCountPtr);
  750. }
  751. else
  752. descriptor->run_synth(handle, kBufferSize, midiEvents, midiEventCount);
  753. }
  754. else
  755. ldescriptor->run(handle, kBufferSize);
  756. if (ldescriptor->deactivate != nullptr)
  757. ldescriptor->deactivate(handle);
  758. ldescriptor->cleanup(handle);
  759. // end crash-free plugin test
  760. // -----------------------------------------------------------------------
  761. }
  762. DISCOVERY_OUT("init", "-----------");
  763. DISCOVERY_OUT("build", BINARY_NATIVE);
  764. DISCOVERY_OUT("hints", hints);
  765. DISCOVERY_OUT("name", ldescriptor->Name);
  766. DISCOVERY_OUT("label", ldescriptor->Label);
  767. DISCOVERY_OUT("maker", ldescriptor->Maker);
  768. DISCOVERY_OUT("uniqueId", ldescriptor->UniqueID);
  769. DISCOVERY_OUT("audio.ins", audioIns);
  770. DISCOVERY_OUT("audio.outs", audioOuts);
  771. DISCOVERY_OUT("midi.ins", midiIns);
  772. DISCOVERY_OUT("parameters.ins", parametersIns);
  773. DISCOVERY_OUT("parameters.outs", parametersOuts);
  774. DISCOVERY_OUT("end", "------------");
  775. }
  776. #else
  777. DISCOVERY_OUT("error", "DSSI support not available");
  778. return;
  779. // unused
  780. (void)libHandle;
  781. (void)filename;
  782. (void)init;
  783. #endif
  784. }
  785. static void do_lv2_check(const char* const bundle, const bool init)
  786. {
  787. #ifdef WANT_LV2
  788. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  789. // Convert bundle filename to URI
  790. QString qBundle(QUrl::fromLocalFile(bundle).toString());
  791. if (! qBundle.endsWith(OS_SEP_STR))
  792. qBundle += OS_SEP_STR;
  793. // Load bundle
  794. lv2World.load_bundle(qBundle.toUtf8().constData());
  795. // Load plugins in this bundle
  796. const Lilv::Plugins lilvPlugins(lv2World.get_all_plugins());
  797. // Get all plugin URIs in this bundle
  798. QStringList 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.append(QString(uri));
  804. }
  805. if (URIs.count() == 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.count(); i < count; ++i)
  812. {
  813. const LV2_RDF_Descriptor* const rdfDescriptor(lv2_rdf_new(URIs.at(i).toUtf8().constData(), false));
  814. if (rdfDescriptor == nullptr || rdfDescriptor->URI == nullptr)
  815. {
  816. DISCOVERY_OUT("error", "Failed to find LV2 plugin '" << URIs.at(i).toUtf8().constData() << "'");
  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 QFileInfo file(filename);
  1313. if (! file.exists())
  1314. {
  1315. DISCOVERY_OUT("error", "Requested file does not exist");
  1316. return;
  1317. }
  1318. if (! file.isFile())
  1319. {
  1320. DISCOVERY_OUT("error", "Requested file is not valid");
  1321. return;
  1322. }
  1323. if (! file.isReadable())
  1324. {
  1325. DISCOVERY_OUT("error", "Requested file is not readable");
  1326. return;
  1327. }
  1328. if (init)
  1329. const LinuxSamplerScopedEngine engine(filename, stype);
  1330. else
  1331. LinuxSamplerScopedEngine::outputInfo(nullptr, 0, file.baseName().toUtf8().constData());
  1332. #else
  1333. DISCOVERY_OUT("error", stype << " support not available");
  1334. return;
  1335. // unused
  1336. (void)filename;
  1337. (void)init;
  1338. #endif
  1339. }
  1340. // --------------------------------------------------------------------------
  1341. class ScopedWorkingDirSet
  1342. {
  1343. public:
  1344. ScopedWorkingDirSet(const char* const filename)
  1345. : fPreviousPath(QDir::currentPath())
  1346. {
  1347. QDir dir(filename);
  1348. dir.cdUp();
  1349. QDir::setCurrent(dir.absolutePath());
  1350. }
  1351. ~ScopedWorkingDirSet()
  1352. {
  1353. QDir::setCurrent(fPreviousPath);
  1354. }
  1355. private:
  1356. const QString fPreviousPath;
  1357. };
  1358. // ------------------------------ main entry point ------------------------------
  1359. int main(int argc, char* argv[])
  1360. {
  1361. if (argc != 3)
  1362. {
  1363. carla_stdout("usage: %s <type> </path/to/plugin>", argv[0]);
  1364. return 1;
  1365. }
  1366. const char* const stype = argv[1];
  1367. const char* const filename = argv[2];
  1368. const PluginType type = getPluginTypeFromString(stype);
  1369. const ScopedWorkingDirSet swds(filename);
  1370. CarlaString filenameStr(filename);
  1371. filenameStr.toLower();
  1372. if (filenameStr.contains("fluidsynth", true))
  1373. {
  1374. DISCOVERY_OUT("info", "skipping fluidsynth based plugin");
  1375. return 0;
  1376. }
  1377. if (filenameStr.contains("linuxsampler", true) || filenameStr.endsWith("ls16.so"))
  1378. {
  1379. DISCOVERY_OUT("info", "skipping linuxsampler based plugin");
  1380. return 0;
  1381. }
  1382. bool openLib = false;
  1383. void* handle = nullptr;
  1384. switch (type)
  1385. {
  1386. case PLUGIN_LADSPA:
  1387. case PLUGIN_DSSI:
  1388. #ifndef CARLA_OS_MAC
  1389. case PLUGIN_VST:
  1390. openLib = true;
  1391. #endif
  1392. default:
  1393. break;
  1394. }
  1395. if (openLib)
  1396. {
  1397. handle = lib_open(filename);
  1398. if (handle == nullptr)
  1399. {
  1400. print_lib_error(filename);
  1401. return 1;
  1402. }
  1403. }
  1404. // never do init for dssi-vst, takes too long and it's crashy
  1405. bool doInit = ! filenameStr.contains("dssi-vst", true);
  1406. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS") != nullptr)
  1407. doInit = false;
  1408. if (doInit && handle != nullptr)
  1409. {
  1410. // test fast loading & unloading DLL without initializing the plugin(s)
  1411. if (! lib_close(handle))
  1412. {
  1413. print_lib_error(filename);
  1414. return 1;
  1415. }
  1416. handle = lib_open(filename);
  1417. if (handle == nullptr)
  1418. {
  1419. print_lib_error(filename);
  1420. return 1;
  1421. }
  1422. }
  1423. switch (type)
  1424. {
  1425. case PLUGIN_LADSPA:
  1426. do_ladspa_check(handle, filename, doInit);
  1427. break;
  1428. case PLUGIN_DSSI:
  1429. do_dssi_check(handle, filename, doInit);
  1430. break;
  1431. case PLUGIN_LV2:
  1432. do_lv2_check(filename, doInit);
  1433. break;
  1434. case PLUGIN_VST:
  1435. #ifdef CARLA_OS_MAC
  1436. do_juce_check(filename, "VST", doInit);
  1437. #else
  1438. do_vst_check(handle, doInit);
  1439. #endif
  1440. break;
  1441. case PLUGIN_VST3:
  1442. #ifdef WANT_JUCE_PROCESSORS
  1443. do_juce_check(filename, "VST3", doInit);
  1444. #else
  1445. DISCOVERY_OUT("error", "VST3 support not available");
  1446. #endif
  1447. break;
  1448. case PLUGIN_AU:
  1449. #ifdef WANT_JUCE_PROCESSORS
  1450. do_juce_check(filename, "AU", doInit);
  1451. #else
  1452. DISCOVERY_OUT("error", "AU support not available");
  1453. #endif
  1454. break;
  1455. case PLUGIN_GIG:
  1456. do_linuxsampler_check(filename, "gig", doInit);
  1457. break;
  1458. case PLUGIN_SF2:
  1459. do_fluidsynth_check(filename, doInit);
  1460. break;
  1461. case PLUGIN_SFZ:
  1462. do_linuxsampler_check(filename, "sfz", doInit);
  1463. break;
  1464. default:
  1465. break;
  1466. }
  1467. if (openLib && handle != nullptr)
  1468. lib_close(handle);
  1469. return 0;
  1470. }
  1471. // --------------------------------------------------------------------------