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.

1970 lines
60KB

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