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.

1563 lines
50KB

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