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.

1816 lines
56KB

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