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.

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