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.

1714 lines
53KB

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