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.

1735 lines
54KB

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