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.

1922 lines
59KB

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