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.

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