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.

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