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.

1481 lines
46KB

  1. /*
  2. * Carla Plugin discovery
  3. * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaBackendUtils.hpp"
  18. #include "CarlaLibUtils.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "LinkedList.hpp"
  22. #ifdef BUILD_BRIDGE
  23. # undef HAVE_FLUIDSYNTH
  24. #endif
  25. #include "CarlaLadspaUtils.hpp"
  26. #include "CarlaLv2Utils.hpp"
  27. #include "CarlaVstUtils.hpp"
  28. #ifdef CARLA_OS_MAC
  29. # import <Foundation/Foundation.h>
  30. #endif
  31. #ifdef HAVE_FLUIDSYNTH
  32. # include <fluidsynth.h>
  33. #endif
  34. #include <iostream>
  35. #ifndef BUILD_BRIDGE
  36. # include "water/files/File.h"
  37. # include "water/text/StringArray.h"
  38. # include "CarlaDssiUtils.cpp"
  39. # include "../backend/utils/CachedPlugins.cpp"
  40. #else
  41. # include "CarlaDssiUtils.hpp"
  42. #endif
  43. #define DISCOVERY_OUT(x, y) std::cout << "\ncarla-discovery::" << x << "::" << y << std::endl;
  44. using water::CharPointer_UTF8;
  45. using water::File;
  46. using water::StringArray;
  47. CARLA_BACKEND_USE_NAMESPACE
  48. // --------------------------------------------------------------------------
  49. // Dummy values to test plugins with
  50. static const uint32_t kBufferSize = 512;
  51. static const double kSampleRate = 44100.0;
  52. static const int32_t kSampleRatei = 44100;
  53. static const float kSampleRatef = 44100.0f;
  54. // --------------------------------------------------------------------------
  55. // Don't print ELF/EXE related errors since discovery can find multi-architecture binaries
  56. static void print_lib_error(const char* const filename)
  57. {
  58. const char* const error(lib_error(filename));
  59. if (error != nullptr && std::strstr(error, "wrong ELF class") == nullptr && std::strstr(error, "Bad EXE format") == nullptr)
  60. DISCOVERY_OUT("error", error);
  61. }
  62. // --------------------------------------------------------------------------
  63. // VST stuff
  64. // Check if plugin is currently processing
  65. static bool gVstIsProcessing = false;
  66. // Check if plugin needs idle
  67. static bool gVstNeedsIdle = false;
  68. // Check if plugin wants midi
  69. static bool gVstWantsMidi = false;
  70. // Check if plugin wants time
  71. static bool gVstWantsTime = false;
  72. // Current uniqueId for VST shell plugins
  73. static intptr_t gVstCurrentUniqueId = 0;
  74. // Supported Carla features
  75. static intptr_t vstHostCanDo(const char* const feature)
  76. {
  77. carla_debug("vstHostCanDo(\"%s\")", feature);
  78. if (std::strcmp(feature, "supplyIdle") == 0)
  79. return 1;
  80. if (std::strcmp(feature, "sendVstEvents") == 0)
  81. return 1;
  82. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  83. return 1;
  84. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  85. return 1;
  86. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  87. {
  88. gVstWantsTime = true;
  89. return 1;
  90. }
  91. if (std::strcmp(feature, "receiveVstEvents") == 0)
  92. return 1;
  93. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  94. return 1;
  95. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  96. return -1;
  97. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  98. return -1;
  99. if (std::strcmp(feature, "acceptIOChanges") == 0)
  100. return 1;
  101. if (std::strcmp(feature, "sizeWindow") == 0)
  102. return 1;
  103. if (std::strcmp(feature, "offline") == 0)
  104. return -1;
  105. if (std::strcmp(feature, "openFileSelector") == 0)
  106. return -1;
  107. if (std::strcmp(feature, "closeFileSelector") == 0)
  108. return -1;
  109. if (std::strcmp(feature, "startStopProcess") == 0)
  110. return 1;
  111. if (std::strcmp(feature, "supportShell") == 0)
  112. return 1;
  113. if (std::strcmp(feature, "shellCategory") == 0)
  114. return 1;
  115. if (std::strcmp(feature, "NIMKPIVendorSpecificCallbacks") == 0)
  116. return -1;
  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. static intptr_t VSTCALLBACK vstHostCallback(AEffect* const effect, const int32_t opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  126. {
  127. carla_debug("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  128. static VstTimeInfo timeInfo;
  129. intptr_t ret = 0;
  130. switch (opcode)
  131. {
  132. case audioMasterAutomate:
  133. ret = 1;
  134. break;
  135. case audioMasterVersion:
  136. ret = kVstVersion;
  137. break;
  138. case audioMasterCurrentId:
  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. carla_zeroStruct(timeInfo);
  150. timeInfo.sampleRate = kSampleRate;
  151. // Tempo
  152. timeInfo.tempo = 120.0;
  153. timeInfo.flags |= kVstTempoValid;
  154. // Time Signature
  155. timeInfo.timeSigNumerator = 4;
  156. timeInfo.timeSigDenominator = 4;
  157. timeInfo.flags |= kVstTimeSigValid;
  158. ret = (intptr_t)&timeInfo;
  159. break;
  160. case DECLARE_VST_DEPRECATED(audioMasterTempoAt):
  161. ret = 120 * 10000;
  162. break;
  163. case DECLARE_VST_DEPRECATED(audioMasterGetNumAutomatableParameters):
  164. ret = carla_minPositive(effect->numParams, static_cast<int>(MAX_DEFAULT_PARAMETERS));
  165. break;
  166. case DECLARE_VST_DEPRECATED(audioMasterGetParameterQuantization):
  167. ret = 1; // full single float precision
  168. break;
  169. case DECLARE_VST_DEPRECATED(audioMasterNeedIdle):
  170. if (gVstNeedsIdle) { DISCOVERY_OUT("warning", "Plugin requested idle more than once"); }
  171. gVstNeedsIdle = true;
  172. ret = 1;
  173. break;
  174. case audioMasterGetSampleRate:
  175. ret = kSampleRatei;
  176. break;
  177. case audioMasterGetBlockSize:
  178. ret = kBufferSize;
  179. break;
  180. case DECLARE_VST_DEPRECATED(audioMasterWillReplaceOrAccumulate):
  181. ret = 1; // replace
  182. break;
  183. case audioMasterGetCurrentProcessLevel:
  184. ret = gVstIsProcessing ? kVstProcessLevelRealtime : kVstProcessLevelUser;
  185. break;
  186. case audioMasterGetAutomationState:
  187. ret = kVstAutomationOff;
  188. break;
  189. case audioMasterGetVendorString:
  190. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  191. std::strcpy((char*)ptr, "falkTX");
  192. ret = 1;
  193. break;
  194. case audioMasterGetProductString:
  195. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  196. std::strcpy((char*)ptr, "Carla-Discovery");
  197. ret = 1;
  198. break;
  199. case audioMasterGetVendorVersion:
  200. ret = CARLA_VERSION_HEX;
  201. break;
  202. case audioMasterCanDo:
  203. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  204. ret = vstHostCanDo((const char*)ptr);
  205. break;
  206. case audioMasterGetLanguage:
  207. ret = kVstLangEnglish;
  208. break;
  209. default:
  210. carla_stdout("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)", effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  211. break;
  212. }
  213. return ret;
  214. }
  215. // ------------------------------ Plugin Checks -----------------------------
  216. #ifndef BUILD_BRIDGE
  217. static void print_cached_plugin(const CarlaCachedPluginInfo* const pinfo)
  218. {
  219. if (! pinfo->valid)
  220. return;
  221. DISCOVERY_OUT("init", "-----------");
  222. DISCOVERY_OUT("build", BINARY_NATIVE);
  223. DISCOVERY_OUT("hints", pinfo->hints);
  224. DISCOVERY_OUT("name", pinfo->name);
  225. DISCOVERY_OUT("maker", pinfo->maker);
  226. DISCOVERY_OUT("label", pinfo->label);
  227. DISCOVERY_OUT("audio.ins", pinfo->audioIns);
  228. DISCOVERY_OUT("audio.outs", pinfo->audioOuts);
  229. DISCOVERY_OUT("midi.ins", pinfo->midiIns);
  230. DISCOVERY_OUT("midi.outs", pinfo->midiOuts);
  231. DISCOVERY_OUT("parameters.ins", pinfo->parameterIns);
  232. DISCOVERY_OUT("parameters.outs", pinfo->parameterOuts);
  233. DISCOVERY_OUT("end", "------------");
  234. }
  235. static void do_cached_check(const PluginType type)
  236. {
  237. const char* plugPath;
  238. switch (type)
  239. {
  240. case PLUGIN_LV2:
  241. plugPath = std::getenv("LV2_PATH");
  242. break;
  243. case PLUGIN_SFZ:
  244. plugPath = std::getenv("SFZ_PATH");
  245. break;
  246. default:
  247. plugPath = nullptr;
  248. break;
  249. }
  250. const uint count = carla_get_cached_plugin_count(type, plugPath);
  251. for (uint i=0; i<count; ++i)
  252. {
  253. const CarlaCachedPluginInfo* pinfo(carla_get_cached_plugin_info(type, i));
  254. CARLA_SAFE_ASSERT_CONTINUE(pinfo != nullptr);
  255. print_cached_plugin(pinfo);
  256. }
  257. }
  258. #endif
  259. static void do_ladspa_check(lib_t& libHandle, const char* const filename, const bool doInit)
  260. {
  261. LADSPA_Descriptor_Function descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  262. if (descFn == nullptr)
  263. {
  264. DISCOVERY_OUT("error", "Not a LADSPA plugin");
  265. return;
  266. }
  267. const LADSPA_Descriptor* descriptor;
  268. {
  269. descriptor = descFn(0);
  270. if (descriptor == nullptr)
  271. {
  272. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  273. return;
  274. }
  275. if (doInit && descriptor->instantiate != nullptr && descriptor->cleanup != nullptr)
  276. {
  277. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  278. if (handle == nullptr)
  279. {
  280. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  281. return;
  282. }
  283. descriptor->cleanup(handle);
  284. lib_close(libHandle);
  285. libHandle = lib_open(filename);
  286. if (libHandle == nullptr)
  287. {
  288. print_lib_error(filename);
  289. return;
  290. }
  291. descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  292. if (descFn == nullptr)
  293. {
  294. DISCOVERY_OUT("error", "Not a LADSPA plugin (#2)");
  295. return;
  296. }
  297. }
  298. }
  299. unsigned long i = 0;
  300. while ((descriptor = descFn(i++)) != nullptr)
  301. {
  302. if (descriptor->instantiate == nullptr)
  303. {
  304. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no instantiate()");
  305. continue;
  306. }
  307. if (descriptor->cleanup == nullptr)
  308. {
  309. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no cleanup()");
  310. continue;
  311. }
  312. if (descriptor->run == nullptr)
  313. {
  314. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no run()");
  315. continue;
  316. }
  317. if (! LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  318. {
  319. DISCOVERY_OUT("warning", "Plugin '" << descriptor->Name << "' is not hard real-time capable");
  320. }
  321. uint hints = 0x0;
  322. int audioIns = 0;
  323. int audioOuts = 0;
  324. int audioTotal = 0;
  325. int parametersIns = 0;
  326. int parametersOuts = 0;
  327. int parametersTotal = 0;
  328. if (LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  329. hints |= PLUGIN_IS_RTSAFE;
  330. for (unsigned long j=0; j < descriptor->PortCount; ++j)
  331. {
  332. CARLA_ASSERT(descriptor->PortNames[j] != nullptr);
  333. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  334. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  335. {
  336. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  337. audioIns += 1;
  338. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  339. audioOuts += 1;
  340. audioTotal += 1;
  341. }
  342. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  343. {
  344. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  345. parametersIns += 1;
  346. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(descriptor->PortNames[j], "latency") != 0 && std::strcmp(descriptor->PortNames[j], "_latency") != 0)
  347. parametersOuts += 1;
  348. parametersTotal += 1;
  349. }
  350. }
  351. if (doInit)
  352. {
  353. // -----------------------------------------------------------------------
  354. // start crash-free plugin test
  355. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  356. if (handle == nullptr)
  357. {
  358. DISCOVERY_OUT("error", "Failed to init LADSPA plugin");
  359. continue;
  360. }
  361. // Test quick init and cleanup
  362. descriptor->cleanup(handle);
  363. handle = descriptor->instantiate(descriptor, kSampleRatei);
  364. if (handle == nullptr)
  365. {
  366. DISCOVERY_OUT("error", "Failed to init LADSPA plugin (#2)");
  367. continue;
  368. }
  369. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  370. LADSPA_Data bufferParams[parametersTotal];
  371. LADSPA_Data min, max, def;
  372. for (unsigned long j=0, iA=0, iC=0; j < descriptor->PortCount; ++j)
  373. {
  374. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  375. const LADSPA_PortRangeHint portRangeHints = descriptor->PortRangeHints[j];
  376. const char* const portName = descriptor->PortNames[j];
  377. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  378. {
  379. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  380. descriptor->connect_port(handle, j, bufferAudio[iA++]);
  381. }
  382. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  383. {
  384. // min value
  385. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  386. min = portRangeHints.LowerBound;
  387. else
  388. min = 0.0f;
  389. // max value
  390. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  391. max = portRangeHints.UpperBound;
  392. else
  393. max = 1.0f;
  394. if (min > max)
  395. {
  396. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  397. max = min + 0.1f;
  398. }
  399. else if (carla_isEqual(min, max))
  400. {
  401. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  402. max = min + 0.1f;
  403. }
  404. // default value
  405. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  406. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  407. {
  408. min *= kSampleRatef;
  409. max *= kSampleRatef;
  410. def *= kSampleRatef;
  411. }
  412. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  413. {
  414. // latency parameter
  415. def = 0.0f;
  416. }
  417. else
  418. {
  419. if (def < min)
  420. def = min;
  421. else if (def > max)
  422. def = max;
  423. }
  424. bufferParams[iC] = def;
  425. descriptor->connect_port(handle, j, &bufferParams[iC++]);
  426. }
  427. }
  428. if (descriptor->activate != nullptr)
  429. descriptor->activate(handle);
  430. descriptor->run(handle, kBufferSize);
  431. if (descriptor->deactivate != nullptr)
  432. descriptor->deactivate(handle);
  433. descriptor->cleanup(handle);
  434. // end crash-free plugin test
  435. // -----------------------------------------------------------------------
  436. }
  437. DISCOVERY_OUT("init", "-----------");
  438. DISCOVERY_OUT("build", BINARY_NATIVE);
  439. DISCOVERY_OUT("hints", hints);
  440. DISCOVERY_OUT("name", descriptor->Name);
  441. DISCOVERY_OUT("label", descriptor->Label);
  442. DISCOVERY_OUT("maker", descriptor->Maker);
  443. DISCOVERY_OUT("uniqueId", descriptor->UniqueID);
  444. DISCOVERY_OUT("audio.ins", audioIns);
  445. DISCOVERY_OUT("audio.outs", audioOuts);
  446. DISCOVERY_OUT("parameters.ins", parametersIns);
  447. DISCOVERY_OUT("parameters.outs", parametersOuts);
  448. DISCOVERY_OUT("end", "------------");
  449. }
  450. }
  451. static void do_dssi_check(lib_t& libHandle, const char* const filename, const bool doInit)
  452. {
  453. DSSI_Descriptor_Function descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  454. if (descFn == nullptr)
  455. {
  456. DISCOVERY_OUT("error", "Not a DSSI plugin");
  457. return;
  458. }
  459. const DSSI_Descriptor* descriptor;
  460. {
  461. descriptor = descFn(0);
  462. if (descriptor == nullptr)
  463. {
  464. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  465. return;
  466. }
  467. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  468. if (ldescriptor == nullptr)
  469. {
  470. DISCOVERY_OUT("error", "DSSI plugin doesn't provide the LADSPA interface");
  471. return;
  472. }
  473. if (doInit && ldescriptor->instantiate != nullptr && ldescriptor->cleanup != nullptr)
  474. {
  475. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  476. if (handle == nullptr)
  477. {
  478. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  479. return;
  480. }
  481. ldescriptor->cleanup(handle);
  482. lib_close(libHandle);
  483. libHandle = lib_open(filename);
  484. if (libHandle == nullptr)
  485. {
  486. print_lib_error(filename);
  487. return;
  488. }
  489. descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  490. if (descFn == nullptr)
  491. {
  492. DISCOVERY_OUT("error", "Not a DSSI plugin (#2)");
  493. return;
  494. }
  495. }
  496. }
  497. unsigned long i = 0;
  498. while ((descriptor = descFn(i++)) != nullptr)
  499. {
  500. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  501. if (ldescriptor == nullptr)
  502. {
  503. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no LADSPA interface");
  504. continue;
  505. }
  506. if (descriptor->DSSI_API_Version != DSSI_VERSION_MAJOR)
  507. {
  508. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' uses an unsupported DSSI spec version " << descriptor->DSSI_API_Version);
  509. continue;
  510. }
  511. if (ldescriptor->instantiate == nullptr)
  512. {
  513. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no instantiate()");
  514. continue;
  515. }
  516. if (ldescriptor->cleanup == nullptr)
  517. {
  518. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no cleanup()");
  519. continue;
  520. }
  521. if (ldescriptor->run == nullptr && descriptor->run_synth == nullptr)
  522. {
  523. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no run() or run_synth()");
  524. continue;
  525. }
  526. if (descriptor->run_synth == nullptr && descriptor->run_multiple_synths != nullptr)
  527. {
  528. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' requires run_multiple_synths which is not supported");
  529. continue;
  530. }
  531. if (! LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  532. {
  533. DISCOVERY_OUT("warning", "Plugin '" << ldescriptor->Name << "' is not hard real-time capable");
  534. }
  535. uint hints = 0x0;
  536. int audioIns = 0;
  537. int audioOuts = 0;
  538. int audioTotal = 0;
  539. int midiIns = 0;
  540. int parametersIns = 0;
  541. int parametersOuts = 0;
  542. int parametersTotal = 0;
  543. if (LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  544. hints |= PLUGIN_IS_RTSAFE;
  545. for (unsigned long j=0; j < ldescriptor->PortCount; ++j)
  546. {
  547. CARLA_ASSERT(ldescriptor->PortNames[j] != nullptr);
  548. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  549. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  550. {
  551. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  552. audioIns += 1;
  553. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  554. audioOuts += 1;
  555. audioTotal += 1;
  556. }
  557. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  558. {
  559. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  560. parametersIns += 1;
  561. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(ldescriptor->PortNames[j], "latency") != 0 && std::strcmp(ldescriptor->PortNames[j], "_latency") != 0)
  562. parametersOuts += 1;
  563. parametersTotal += 1;
  564. }
  565. }
  566. if (descriptor->run_synth != nullptr)
  567. midiIns = 1;
  568. if (midiIns > 0 && audioIns == 0 && audioOuts > 0)
  569. hints |= PLUGIN_IS_SYNTH;
  570. #ifndef BUILD_BRIDGE
  571. if (const char* const ui = find_dssi_ui(filename, ldescriptor->Label))
  572. {
  573. hints |= PLUGIN_HAS_CUSTOM_UI;
  574. delete[] ui;
  575. }
  576. #endif
  577. if (doInit)
  578. {
  579. // -----------------------------------------------------------------------
  580. // start crash-free plugin test
  581. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  582. if (handle == nullptr)
  583. {
  584. DISCOVERY_OUT("error", "Failed to init DSSI plugin");
  585. continue;
  586. }
  587. // Test quick init and cleanup
  588. ldescriptor->cleanup(handle);
  589. handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  590. if (handle == nullptr)
  591. {
  592. DISCOVERY_OUT("error", "Failed to init DSSI plugin (#2)");
  593. continue;
  594. }
  595. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  596. LADSPA_Data bufferParams[parametersTotal];
  597. LADSPA_Data min, max, def;
  598. for (unsigned long j=0, iA=0, iC=0; j < ldescriptor->PortCount; ++j)
  599. {
  600. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  601. const LADSPA_PortRangeHint portRangeHints = ldescriptor->PortRangeHints[j];
  602. const char* const portName = ldescriptor->PortNames[j];
  603. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  604. {
  605. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  606. ldescriptor->connect_port(handle, j, bufferAudio[iA++]);
  607. }
  608. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  609. {
  610. // min value
  611. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  612. min = portRangeHints.LowerBound;
  613. else
  614. min = 0.0f;
  615. // max value
  616. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  617. max = portRangeHints.UpperBound;
  618. else
  619. max = 1.0f;
  620. if (min > max)
  621. {
  622. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  623. max = min + 0.1f;
  624. }
  625. else if (carla_isEqual(min, max))
  626. {
  627. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  628. max = min + 0.1f;
  629. }
  630. // default value
  631. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  632. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  633. {
  634. min *= kSampleRatef;
  635. max *= kSampleRatef;
  636. def *= kSampleRatef;
  637. }
  638. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  639. {
  640. // latency parameter
  641. def = 0.0f;
  642. }
  643. else
  644. {
  645. if (def < min)
  646. def = min;
  647. else if (def > max)
  648. def = max;
  649. }
  650. bufferParams[iC] = def;
  651. ldescriptor->connect_port(handle, j, &bufferParams[iC++]);
  652. }
  653. }
  654. // select first midi-program if available
  655. if (descriptor->get_program != nullptr && descriptor->select_program != nullptr)
  656. {
  657. if (const DSSI_Program_Descriptor* const pDesc = descriptor->get_program(handle, 0))
  658. descriptor->select_program(handle, pDesc->Bank, pDesc->Program);
  659. }
  660. if (ldescriptor->activate != nullptr)
  661. ldescriptor->activate(handle);
  662. if (descriptor->run_synth != nullptr)
  663. {
  664. snd_seq_event_t midiEvents[2];
  665. carla_zeroStructs(midiEvents, 2);
  666. const unsigned long midiEventCount = 2;
  667. midiEvents[0].type = SND_SEQ_EVENT_NOTEON;
  668. midiEvents[0].data.note.note = 64;
  669. midiEvents[0].data.note.velocity = 100;
  670. midiEvents[1].type = SND_SEQ_EVENT_NOTEOFF;
  671. midiEvents[1].data.note.note = 64;
  672. midiEvents[1].data.note.velocity = 0;
  673. midiEvents[1].time.tick = kBufferSize/2;
  674. descriptor->run_synth(handle, kBufferSize, midiEvents, midiEventCount);
  675. }
  676. else
  677. ldescriptor->run(handle, kBufferSize);
  678. if (ldescriptor->deactivate != nullptr)
  679. ldescriptor->deactivate(handle);
  680. ldescriptor->cleanup(handle);
  681. // end crash-free plugin test
  682. // -----------------------------------------------------------------------
  683. }
  684. DISCOVERY_OUT("init", "-----------");
  685. DISCOVERY_OUT("build", BINARY_NATIVE);
  686. DISCOVERY_OUT("hints", hints);
  687. DISCOVERY_OUT("name", ldescriptor->Name);
  688. DISCOVERY_OUT("label", ldescriptor->Label);
  689. DISCOVERY_OUT("maker", ldescriptor->Maker);
  690. DISCOVERY_OUT("uniqueId", ldescriptor->UniqueID);
  691. DISCOVERY_OUT("audio.ins", audioIns);
  692. DISCOVERY_OUT("audio.outs", audioOuts);
  693. DISCOVERY_OUT("midi.ins", midiIns);
  694. DISCOVERY_OUT("parameters.ins", parametersIns);
  695. DISCOVERY_OUT("parameters.outs", parametersOuts);
  696. DISCOVERY_OUT("end", "------------");
  697. }
  698. }
  699. #ifndef BUILD_BRIDGE
  700. static void do_lv2_check(const char* const bundle, const bool doInit)
  701. {
  702. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  703. Lilv::Node bundleNode(lv2World.new_file_uri(nullptr, bundle));
  704. CARLA_SAFE_ASSERT_RETURN(bundleNode.is_uri(),);
  705. CarlaString sBundle(bundleNode.as_uri());
  706. if (! sBundle.endsWith("/"))
  707. sBundle += "/";
  708. // Load bundle
  709. lv2World.load_bundle(sBundle);
  710. // Load plugins in this bundle
  711. const Lilv::Plugins lilvPlugins(lv2World.get_all_plugins());
  712. // Get all plugin URIs in this bundle
  713. StringArray URIs;
  714. LILV_FOREACH(plugins, it, lilvPlugins)
  715. {
  716. Lilv::Plugin lilvPlugin(lilv_plugins_get(lilvPlugins, it));
  717. if (const char* const uri = lilvPlugin.get_uri().as_string())
  718. URIs.addIfNotAlreadyThere(water::String(uri));
  719. }
  720. if (URIs.size() == 0)
  721. {
  722. DISCOVERY_OUT("warning", "LV2 Bundle doesn't provide any plugins");
  723. return;
  724. }
  725. // Get & check every plugin-instance
  726. for (int i=0, count=URIs.size(); i < count; ++i)
  727. {
  728. const char* const URI = URIs[i].toRawUTF8();
  729. ScopedPointer<const LV2_RDF_Descriptor> rdfDescriptor(lv2_rdf_new(URI, false));
  730. if (rdfDescriptor == nullptr || rdfDescriptor->URI == nullptr)
  731. {
  732. DISCOVERY_OUT("error", "Failed to find LV2 plugin '" << URI << "'");
  733. continue;
  734. }
  735. if (doInit)
  736. {
  737. // test if lib is loadable, twice
  738. const lib_t libHandle1 = lib_open(rdfDescriptor->Binary);
  739. if (libHandle1 == nullptr)
  740. {
  741. print_lib_error(rdfDescriptor->Binary);
  742. delete rdfDescriptor;
  743. continue;
  744. }
  745. lib_close(libHandle1);
  746. const lib_t libHandle2 = lib_open(rdfDescriptor->Binary);
  747. if (libHandle2 == nullptr)
  748. {
  749. print_lib_error(rdfDescriptor->Binary);
  750. delete rdfDescriptor;
  751. continue;
  752. }
  753. lib_close(libHandle2);
  754. }
  755. const LilvPlugin* const cPlugin(lv2World.getPluginFromURI(URI));
  756. CARLA_SAFE_ASSERT_CONTINUE(cPlugin != nullptr);
  757. Lilv::Plugin lilvPlugin(cPlugin);
  758. CARLA_SAFE_ASSERT_CONTINUE(lilvPlugin.get_uri().is_uri());
  759. print_cached_plugin(get_cached_plugin_lv2(lv2World, lilvPlugin));
  760. }
  761. }
  762. #endif
  763. static void do_vst_check(lib_t& libHandle, const char* const filename, const bool doInit)
  764. {
  765. VST_Function vstFn = nullptr;
  766. #ifdef CARLA_OS_MAC
  767. CFBundleRef bundleRef = nullptr;
  768. CFBundleRefNum resFileId = 0;
  769. if (libHandle == nullptr)
  770. {
  771. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)strlen(filename), true);
  772. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr,);
  773. bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
  774. CFRelease(urlRef);
  775. CARLA_SAFE_ASSERT_RETURN(bundleRef != nullptr,);
  776. if (! CFBundleLoadExecutable(bundleRef))
  777. {
  778. CFRelease(bundleRef);
  779. DISCOVERY_OUT("error", "Failed to load VST bundle executable");
  780. return;
  781. }
  782. vstFn = (VST_Function)CFBundleGetFunctionPointerForName(bundleRef, CFSTR("main_macho"));
  783. if (vstFn == nullptr)
  784. vstFn = (VST_Function)CFBundleGetFunctionPointerForName(bundleRef, CFSTR("VSTPluginMain"));
  785. if (vstFn == nullptr)
  786. {
  787. CFBundleUnloadExecutable(bundleRef);
  788. CFRelease(bundleRef);
  789. DISCOVERY_OUT("error", "Not a VST plugin");
  790. return;
  791. }
  792. resFileId = CFBundleOpenBundleResourceMap(bundleRef);
  793. }
  794. else
  795. #endif
  796. {
  797. vstFn = lib_symbol<VST_Function>(libHandle, "VSTPluginMain");
  798. if (vstFn == nullptr)
  799. {
  800. vstFn = lib_symbol<VST_Function>(libHandle, "main");
  801. if (vstFn == nullptr)
  802. {
  803. DISCOVERY_OUT("error", "Not a VST plugin");
  804. return;
  805. }
  806. }
  807. }
  808. AEffect* effect = vstFn(vstHostCallback);
  809. if (effect == nullptr || effect->magic != kEffectMagic)
  810. {
  811. DISCOVERY_OUT("error", "Failed to init VST plugin, or VST magic failed");
  812. return;
  813. }
  814. if (effect->uniqueID == 0)
  815. {
  816. DISCOVERY_OUT("error", "Plugin doesn't have an Unique ID");
  817. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  818. return;
  819. }
  820. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdentify), 0, 0, nullptr, 0.0f);
  821. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effSetBlockSizeAndSampleRate), 0, kBufferSize, nullptr, kSampleRatef);
  822. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, kSampleRatef);
  823. effect->dispatcher(effect, effSetBlockSize, 0, kBufferSize, nullptr, 0.0f);
  824. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  825. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  826. if (effect->numPrograms > 0)
  827. effect->dispatcher(effect, effSetProgram, 0, 0, nullptr, 0.0f);
  828. const bool isShell = (effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f) == kPlugCategShell);
  829. gVstCurrentUniqueId = effect->uniqueID;
  830. char strBuf[STR_MAX+1];
  831. CarlaString cName;
  832. CarlaString cProduct;
  833. CarlaString cVendor;
  834. LinkedList<intptr_t> uniqueIds;
  835. if (isShell)
  836. {
  837. for (;;)
  838. {
  839. carla_zeroChars(strBuf, STR_MAX+1);
  840. gVstCurrentUniqueId = effect->dispatcher(effect, effShellGetNextPlugin, 0, 0, strBuf, 0.0f);
  841. if (gVstCurrentUniqueId == 0)
  842. break;
  843. uniqueIds.append(gVstCurrentUniqueId);
  844. }
  845. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  846. effect = nullptr;
  847. }
  848. else
  849. {
  850. uniqueIds.append(gVstCurrentUniqueId);
  851. }
  852. for (LinkedList<intptr_t>::Itenerator it = uniqueIds.begin2(); it.valid(); it.next())
  853. {
  854. gVstCurrentUniqueId = it.getValue(0);
  855. if (effect == nullptr)
  856. {
  857. effect = vstFn(vstHostCallback);
  858. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdentify), 0, 0, nullptr, 0.0f);
  859. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effSetBlockSizeAndSampleRate), 0, kBufferSize, nullptr, kSampleRatef);
  860. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, kSampleRatef);
  861. effect->dispatcher(effect, effSetBlockSize, 0, kBufferSize, nullptr, 0.0f);
  862. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  863. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  864. if (effect->numPrograms > 0)
  865. effect->dispatcher(effect, effSetProgram, 0, 0, nullptr, 0.0f);
  866. }
  867. // get name
  868. carla_zeroChars(strBuf, STR_MAX+1);
  869. if (effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f) == 1)
  870. cName = strBuf;
  871. else
  872. cName.clear();
  873. // get product
  874. carla_zeroChars(strBuf, STR_MAX+1);
  875. if (effect->dispatcher(effect, effGetProductString, 0, 0, strBuf, 0.0f) == 1)
  876. cProduct = strBuf;
  877. else
  878. cProduct.clear();
  879. // get vendor
  880. carla_zeroChars(strBuf, STR_MAX+1);
  881. if (effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f) == 1)
  882. cVendor = strBuf;
  883. else
  884. cVendor.clear();
  885. // get everything else
  886. uint hints = 0x0;
  887. int audioIns = effect->numInputs;
  888. int audioOuts = effect->numOutputs;
  889. int midiIns = 0;
  890. int midiOuts = 0;
  891. int parameters = effect->numParams;
  892. if (effect->flags & effFlagsHasEditor)
  893. hints |= PLUGIN_HAS_CUSTOM_UI;
  894. if (effect->flags & effFlagsIsSynth)
  895. hints |= PLUGIN_IS_SYNTH;
  896. if (vstPluginCanDo(effect, "receiveVstEvents") || vstPluginCanDo(effect, "receiveVstMidiEvent") || (effect->flags & effFlagsIsSynth) != 0)
  897. midiIns = 1;
  898. if (vstPluginCanDo(effect, "sendVstEvents") || vstPluginCanDo(effect, "sendVstMidiEvent"))
  899. midiOuts = 1;
  900. // -----------------------------------------------------------------------
  901. // start crash-free plugin test
  902. if (doInit)
  903. {
  904. if (gVstNeedsIdle)
  905. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  906. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  907. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  908. if (gVstNeedsIdle)
  909. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  910. // Plugin might call wantMidi() during resume
  911. if (midiIns == 0 && gVstWantsMidi)
  912. {
  913. midiIns = 1;
  914. }
  915. float* bufferAudioIn[std::max(1, audioIns)];
  916. float* bufferAudioOut[std::max(1, audioOuts)];
  917. if (audioIns == 0)
  918. {
  919. bufferAudioIn[0] = nullptr;
  920. }
  921. else
  922. {
  923. for (int j=0; j < audioIns; ++j)
  924. {
  925. bufferAudioIn[j] = new float[kBufferSize];
  926. carla_zeroFloats(bufferAudioIn[j], kBufferSize);
  927. }
  928. }
  929. if (audioOuts == 0)
  930. {
  931. bufferAudioOut[0] = nullptr;
  932. }
  933. else
  934. {
  935. for (int j=0; j < audioOuts; ++j)
  936. {
  937. bufferAudioOut[j] = new float[kBufferSize];
  938. carla_zeroFloats(bufferAudioOut[j], kBufferSize);
  939. }
  940. }
  941. struct VstEventsFixed {
  942. int32_t numEvents;
  943. intptr_t reserved;
  944. VstEvent* data[2];
  945. VstEventsFixed()
  946. : numEvents(0),
  947. reserved(0)
  948. {
  949. data[0] = data[1] = nullptr;
  950. }
  951. } events;
  952. VstMidiEvent midiEvents[2];
  953. carla_zeroStructs(midiEvents, 2);
  954. midiEvents[0].type = kVstMidiType;
  955. midiEvents[0].byteSize = sizeof(VstMidiEvent);
  956. midiEvents[0].midiData[0] = char(MIDI_STATUS_NOTE_ON);
  957. midiEvents[0].midiData[1] = 64;
  958. midiEvents[0].midiData[2] = 100;
  959. midiEvents[1].type = kVstMidiType;
  960. midiEvents[1].byteSize = sizeof(VstMidiEvent);
  961. midiEvents[1].midiData[0] = char(MIDI_STATUS_NOTE_OFF);
  962. midiEvents[1].midiData[1] = 64;
  963. midiEvents[1].deltaFrames = kBufferSize/2;
  964. events.numEvents = 2;
  965. events.data[0] = (VstEvent*)&midiEvents[0];
  966. events.data[1] = (VstEvent*)&midiEvents[1];
  967. // processing
  968. gVstIsProcessing = true;
  969. if (midiIns > 0)
  970. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  971. if ((effect->flags & effFlagsCanReplacing) > 0 && effect->processReplacing != nullptr && effect->processReplacing != effect->DECLARE_VST_DEPRECATED(process))
  972. effect->processReplacing(effect, bufferAudioIn, bufferAudioOut, kBufferSize);
  973. else if (effect->DECLARE_VST_DEPRECATED(process) != nullptr)
  974. effect->DECLARE_VST_DEPRECATED(process)(effect, bufferAudioIn, bufferAudioOut, kBufferSize);
  975. else
  976. DISCOVERY_OUT("error", "Plugin doesn't have a process function");
  977. gVstIsProcessing = false;
  978. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  979. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  980. if (gVstNeedsIdle)
  981. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  982. for (int j=0; j < audioIns; ++j)
  983. delete[] bufferAudioIn[j];
  984. for (int j=0; j < audioOuts; ++j)
  985. delete[] bufferAudioOut[j];
  986. }
  987. // end crash-free plugin test
  988. // -----------------------------------------------------------------------
  989. DISCOVERY_OUT("init", "-----------");
  990. DISCOVERY_OUT("build", BINARY_NATIVE);
  991. DISCOVERY_OUT("hints", hints);
  992. DISCOVERY_OUT("name", cName.buffer());
  993. DISCOVERY_OUT("label", cProduct.buffer());
  994. DISCOVERY_OUT("maker", cVendor.buffer());
  995. DISCOVERY_OUT("uniqueId", gVstCurrentUniqueId);
  996. DISCOVERY_OUT("audio.ins", audioIns);
  997. DISCOVERY_OUT("audio.outs", audioOuts);
  998. DISCOVERY_OUT("midi.ins", midiIns);
  999. DISCOVERY_OUT("midi.outs", midiOuts);
  1000. DISCOVERY_OUT("parameters.ins", parameters);
  1001. DISCOVERY_OUT("end", "------------");
  1002. gVstWantsMidi = false;
  1003. gVstWantsTime = false;
  1004. if (! isShell)
  1005. break;
  1006. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1007. effect = nullptr;
  1008. }
  1009. uniqueIds.clear();
  1010. if (effect != nullptr)
  1011. {
  1012. if (gVstNeedsIdle)
  1013. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1014. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1015. }
  1016. #ifdef CARLA_OS_MAC
  1017. if (bundleRef != nullptr)
  1018. {
  1019. CFBundleCloseBundleResourceMap(bundleRef, resFileId);
  1020. CFBundleUnloadExecutable(bundleRef);
  1021. CFRelease(bundleRef);
  1022. }
  1023. #else
  1024. return;
  1025. // unused
  1026. (void)filename;
  1027. #endif
  1028. }
  1029. static void do_fluidsynth_check(const char* const filename, const bool doInit)
  1030. {
  1031. #ifdef HAVE_FLUIDSYNTH
  1032. const water::String jfilename = water::String(CharPointer_UTF8(filename));
  1033. const File file(jfilename);
  1034. if (! file.existsAsFile())
  1035. {
  1036. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1037. return;
  1038. }
  1039. if (! fluid_is_soundfont(filename))
  1040. {
  1041. DISCOVERY_OUT("error", "Not a SF2 file");
  1042. return;
  1043. }
  1044. int programs = 0;
  1045. if (doInit)
  1046. {
  1047. fluid_settings_t* const f_settings = new_fluid_settings();
  1048. CARLA_SAFE_ASSERT_RETURN(f_settings != nullptr,);
  1049. fluid_synth_t* const f_synth = new_fluid_synth(f_settings);
  1050. CARLA_SAFE_ASSERT_RETURN(f_synth != nullptr,);
  1051. const int f_id = fluid_synth_sfload(f_synth, filename, 0);
  1052. if (f_id < 0)
  1053. {
  1054. DISCOVERY_OUT("error", "Failed to load SF2 file");
  1055. return;
  1056. }
  1057. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(f_synth, static_cast<uint>(f_id)))
  1058. {
  1059. fluid_preset_t f_preset;
  1060. f_sfont->iteration_start(f_sfont);
  1061. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  1062. ++programs;
  1063. }
  1064. delete_fluid_synth(f_synth);
  1065. delete_fluid_settings(f_settings);
  1066. }
  1067. CarlaString name(file.getFileNameWithoutExtension().toRawUTF8());
  1068. CarlaString label(name);
  1069. // 2 channels
  1070. DISCOVERY_OUT("init", "-----------");
  1071. DISCOVERY_OUT("build", BINARY_NATIVE);
  1072. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1073. DISCOVERY_OUT("name", name.buffer());
  1074. DISCOVERY_OUT("label", label.buffer());
  1075. DISCOVERY_OUT("audio.outs", 2);
  1076. DISCOVERY_OUT("midi.ins", 1);
  1077. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1078. DISCOVERY_OUT("parameters.outs", 1);
  1079. DISCOVERY_OUT("end", "------------");
  1080. // 16 channels
  1081. if (doInit && (name.isEmpty() || programs <= 1))
  1082. return;
  1083. name += " (16 outputs)";
  1084. DISCOVERY_OUT("init", "-----------");
  1085. DISCOVERY_OUT("build", BINARY_NATIVE);
  1086. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1087. DISCOVERY_OUT("name", name.buffer());
  1088. DISCOVERY_OUT("label", label.buffer());
  1089. DISCOVERY_OUT("audio.outs", 32);
  1090. DISCOVERY_OUT("midi.ins", 1);
  1091. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1092. DISCOVERY_OUT("parameters.outs", 1);
  1093. DISCOVERY_OUT("end", "------------");
  1094. #else // HAVE_FLUIDSYNTH
  1095. DISCOVERY_OUT("error", "SF2 support not available");
  1096. return;
  1097. // unused
  1098. (void)filename;
  1099. (void)doInit;
  1100. #endif
  1101. }
  1102. // ------------------------------ main entry point ------------------------------
  1103. int main(int argc, char* argv[])
  1104. {
  1105. if (argc != 3)
  1106. {
  1107. carla_stdout("usage: %s <type> </path/to/plugin>", argv[0]);
  1108. return 1;
  1109. }
  1110. const char* const stype = argv[1];
  1111. const char* const filename = argv[2];
  1112. const PluginType type = getPluginTypeFromString(stype);
  1113. CarlaString filenameCheck(filename);
  1114. filenameCheck.toLower();
  1115. bool openLib = false;
  1116. lib_t handle = nullptr;
  1117. switch (type)
  1118. {
  1119. case PLUGIN_LADSPA:
  1120. case PLUGIN_DSSI:
  1121. case PLUGIN_VST2:
  1122. openLib = true;
  1123. default:
  1124. break;
  1125. }
  1126. if (type != PLUGIN_SF2 && filenameCheck.contains("fluidsynth", true))
  1127. {
  1128. DISCOVERY_OUT("info", "skipping fluidsynth based plugin");
  1129. return 0;
  1130. }
  1131. #ifdef CARLA_OS_MAC
  1132. if (type == PLUGIN_VST2 && (filenameCheck.endsWith(".vst") || filenameCheck.endsWith(".vst/")))
  1133. openLib = false;
  1134. #endif
  1135. if (openLib)
  1136. {
  1137. handle = lib_open(filename);
  1138. if (handle == nullptr)
  1139. {
  1140. print_lib_error(filename);
  1141. return 1;
  1142. }
  1143. }
  1144. // never do init for dssi-vst, takes too long and it's crashy
  1145. bool doInit = ! filenameCheck.contains("dssi-vst", true);
  1146. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS") != nullptr)
  1147. doInit = false;
  1148. if (doInit && openLib && handle != nullptr)
  1149. {
  1150. // test fast loading & unloading DLL without initializing the plugin(s)
  1151. if (! lib_close(handle))
  1152. {
  1153. print_lib_error(filename);
  1154. return 1;
  1155. }
  1156. handle = lib_open(filename);
  1157. if (handle == nullptr)
  1158. {
  1159. print_lib_error(filename);
  1160. return 1;
  1161. }
  1162. }
  1163. #ifndef BUILD_BRIDGE
  1164. if (std::strcmp(filename, ":all") == 0)
  1165. {
  1166. do_cached_check(type);
  1167. return 0;
  1168. }
  1169. #endif
  1170. switch (type)
  1171. {
  1172. case PLUGIN_LADSPA:
  1173. do_ladspa_check(handle, filename, doInit);
  1174. break;
  1175. case PLUGIN_DSSI:
  1176. do_dssi_check(handle, filename, doInit);
  1177. break;
  1178. #ifndef BUILD_BRIDGE
  1179. case PLUGIN_LV2:
  1180. do_lv2_check(filename, doInit);
  1181. break;
  1182. #endif
  1183. case PLUGIN_VST2:
  1184. do_vst_check(handle, filename, doInit);
  1185. break;
  1186. case PLUGIN_SF2:
  1187. do_fluidsynth_check(filename, doInit);
  1188. break;
  1189. default:
  1190. break;
  1191. }
  1192. if (openLib && handle != nullptr)
  1193. lib_close(handle);
  1194. return 0;
  1195. }
  1196. // --------------------------------------------------------------------------