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.

1923 lines
59KB

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