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.

1740 lines
54KB

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