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.

1778 lines
55KB

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