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.

1948 lines
60KB

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