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.

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