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.

1685 lines
52KB

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