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.

1737 lines
54KB

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