Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1735 lines
54KB

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