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.

2680 lines
86KB

  1. /*
  2. * Carla Plugin discovery
  3. * Copyright (C) 2011-2023 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 "CarlaScopeUtils.hpp"
  21. #include "CarlaMIDI.h"
  22. #include "LinkedList.hpp"
  23. #include "CarlaLadspaUtils.hpp"
  24. #include "CarlaDssiUtils.hpp"
  25. #include "CarlaLv2Utils.hpp"
  26. #include "CarlaVst2Utils.hpp"
  27. #include "CarlaVst3Utils.hpp"
  28. #include "CarlaClapUtils.hpp"
  29. #ifdef CARLA_OS_MAC
  30. # include "CarlaMacUtils.cpp"
  31. # if defined(USING_JUCE) && defined(__aarch64__)
  32. # include <spawn.h>
  33. # endif
  34. #endif
  35. #ifdef CARLA_OS_WIN
  36. # include <pthread.h>
  37. # include <objbase.h>
  38. #endif
  39. #ifdef BUILD_BRIDGE
  40. # undef HAVE_FLUIDSYNTH
  41. # undef HAVE_YSFX
  42. #endif
  43. #ifdef HAVE_FLUIDSYNTH
  44. # include <fluidsynth.h>
  45. #endif
  46. #include <iostream>
  47. #include "water/files/File.h"
  48. #ifndef BUILD_BRIDGE
  49. # include "CarlaDssiUtils.cpp"
  50. # include "CarlaJsfxUtils.hpp"
  51. # include "../backend/utils/CachedPlugins.cpp"
  52. #endif
  53. #ifdef USING_JUCE
  54. # include "carla_juce/carla_juce.h"
  55. # pragma GCC diagnostic ignored "-Wdouble-promotion"
  56. # pragma GCC diagnostic ignored "-Wduplicated-branches"
  57. # pragma GCC diagnostic ignored "-Weffc++"
  58. # pragma GCC diagnostic ignored "-Wfloat-equal"
  59. # include "juce_audio_processors/juce_audio_processors.h"
  60. # if JUCE_PLUGINHOST_VST
  61. # define USING_JUCE_FOR_VST2
  62. # endif
  63. # if JUCE_PLUGINHOST_VST3
  64. # define USING_JUCE_FOR_VST3
  65. # endif
  66. # pragma GCC diagnostic pop
  67. #endif
  68. #define DISCOVERY_OUT(x, y) std::cout << "\ncarla-discovery::" << x << "::" << y << std::endl;
  69. using water::File;
  70. CARLA_BACKEND_USE_NAMESPACE
  71. // -------------------------------------------------------------------------------------------------------------------
  72. // Dummy values to test plugins with
  73. static constexpr const uint32_t kBufferSize = 512;
  74. static constexpr const double kSampleRate = 44100.0;
  75. static constexpr const int32_t kSampleRatei = 44100;
  76. static constexpr const float kSampleRatef = 44100.0f;
  77. // -------------------------------------------------------------------------------------------------------------------
  78. // Don't print ELF/EXE related errors since discovery can find multi-architecture binaries
  79. static void print_lib_error(const char* const filename)
  80. {
  81. const char* const error = lib_error(filename);
  82. if (error != nullptr &&
  83. std::strstr(error, "wrong ELF class") == nullptr &&
  84. std::strstr(error, "invalid ELF header") == nullptr &&
  85. std::strstr(error, "Bad EXE format") == nullptr &&
  86. std::strstr(error, "no suitable image found") == nullptr &&
  87. std::strstr(error, "not a valid Win32 application") == nullptr)
  88. {
  89. DISCOVERY_OUT("error", error);
  90. }
  91. }
  92. // ------------------------------ Plugin Checks -----------------------------
  93. #ifndef BUILD_BRIDGE
  94. static void print_cached_plugin(const CarlaCachedPluginInfo* const pinfo)
  95. {
  96. if (! pinfo->valid)
  97. return;
  98. DISCOVERY_OUT("init", "-----------");
  99. DISCOVERY_OUT("build", BINARY_NATIVE);
  100. DISCOVERY_OUT("hints", pinfo->hints);
  101. DISCOVERY_OUT("category", getPluginCategoryAsString(pinfo->category));
  102. DISCOVERY_OUT("name", pinfo->name);
  103. DISCOVERY_OUT("maker", pinfo->maker);
  104. DISCOVERY_OUT("label", pinfo->label);
  105. DISCOVERY_OUT("audio.ins", pinfo->audioIns);
  106. DISCOVERY_OUT("audio.outs", pinfo->audioOuts);
  107. DISCOVERY_OUT("cv.ins", pinfo->cvIns);
  108. DISCOVERY_OUT("cv.outs", pinfo->cvOuts);
  109. DISCOVERY_OUT("midi.ins", pinfo->midiIns);
  110. DISCOVERY_OUT("midi.outs", pinfo->midiOuts);
  111. DISCOVERY_OUT("parameters.ins", pinfo->parameterIns);
  112. DISCOVERY_OUT("parameters.outs", pinfo->parameterOuts);
  113. DISCOVERY_OUT("end", "------------");
  114. }
  115. static void do_cached_check(const PluginType type)
  116. {
  117. const char* plugPath;
  118. switch (type)
  119. {
  120. case PLUGIN_LV2:
  121. plugPath = std::getenv("LV2_PATH");
  122. break;
  123. case PLUGIN_SFZ:
  124. plugPath = std::getenv("SFZ_PATH");
  125. break;
  126. default:
  127. plugPath = nullptr;
  128. break;
  129. }
  130. #ifdef USING_JUCE
  131. if (type == PLUGIN_AU)
  132. CarlaJUCE::initialiseJuce_GUI();
  133. #endif
  134. const uint count = carla_get_cached_plugin_count(type, plugPath);
  135. for (uint i=0; i<count; ++i)
  136. {
  137. const CarlaCachedPluginInfo* pinfo = carla_get_cached_plugin_info(type, i);
  138. CARLA_SAFE_ASSERT_CONTINUE(pinfo != nullptr);
  139. print_cached_plugin(pinfo);
  140. }
  141. #ifdef USING_JUCE
  142. if (type == PLUGIN_AU)
  143. CarlaJUCE::shutdownJuce_GUI();
  144. #endif
  145. }
  146. #endif
  147. static void do_ladspa_check(lib_t& libHandle, const char* const filename, const bool doInit)
  148. {
  149. LADSPA_Descriptor_Function descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  150. if (descFn == nullptr)
  151. {
  152. DISCOVERY_OUT("error", "Not a LADSPA plugin");
  153. return;
  154. }
  155. const LADSPA_Descriptor* descriptor;
  156. {
  157. descriptor = descFn(0);
  158. if (descriptor == nullptr)
  159. {
  160. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  161. return;
  162. }
  163. if (doInit && descriptor->instantiate != nullptr && descriptor->cleanup != nullptr)
  164. {
  165. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  166. if (handle == nullptr)
  167. {
  168. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  169. return;
  170. }
  171. descriptor->cleanup(handle);
  172. lib_close(libHandle);
  173. libHandle = lib_open(filename);
  174. if (libHandle == nullptr)
  175. {
  176. print_lib_error(filename);
  177. return;
  178. }
  179. descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  180. if (descFn == nullptr)
  181. {
  182. DISCOVERY_OUT("error", "Not a LADSPA plugin (#2)");
  183. return;
  184. }
  185. }
  186. }
  187. unsigned long i = 0;
  188. while ((descriptor = descFn(i++)) != nullptr)
  189. {
  190. if (descriptor->instantiate == nullptr)
  191. {
  192. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no instantiate()");
  193. continue;
  194. }
  195. if (descriptor->cleanup == nullptr)
  196. {
  197. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no cleanup()");
  198. continue;
  199. }
  200. if (descriptor->run == nullptr)
  201. {
  202. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no run()");
  203. continue;
  204. }
  205. if (! LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  206. {
  207. DISCOVERY_OUT("warning", "Plugin '" << descriptor->Name << "' is not hard real-time capable");
  208. }
  209. uint hints = 0x0;
  210. uint audioIns = 0;
  211. uint audioOuts = 0;
  212. uint audioTotal = 0;
  213. uint parametersIns = 0;
  214. uint parametersOuts = 0;
  215. uint parametersTotal = 0;
  216. if (LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  217. hints |= PLUGIN_IS_RTSAFE;
  218. for (unsigned long j=0; j < descriptor->PortCount; ++j)
  219. {
  220. CARLA_ASSERT(descriptor->PortNames[j] != nullptr);
  221. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  222. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  223. {
  224. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  225. audioIns += 1;
  226. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  227. audioOuts += 1;
  228. audioTotal += 1;
  229. }
  230. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  231. {
  232. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  233. parametersIns += 1;
  234. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(descriptor->PortNames[j], "latency") != 0 && std::strcmp(descriptor->PortNames[j], "_latency") != 0)
  235. parametersOuts += 1;
  236. parametersTotal += 1;
  237. }
  238. }
  239. if (doInit)
  240. {
  241. // -----------------------------------------------------------------------
  242. // start crash-free plugin test
  243. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  244. if (handle == nullptr)
  245. {
  246. DISCOVERY_OUT("error", "Failed to init LADSPA plugin");
  247. continue;
  248. }
  249. // Test quick init and cleanup
  250. descriptor->cleanup(handle);
  251. handle = descriptor->instantiate(descriptor, kSampleRatei);
  252. if (handle == nullptr)
  253. {
  254. DISCOVERY_OUT("error", "Failed to init LADSPA plugin (#2)");
  255. continue;
  256. }
  257. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  258. LADSPA_Data bufferParams[parametersTotal];
  259. LADSPA_Data min, max, def;
  260. for (unsigned long j=0, iA=0, iC=0; j < descriptor->PortCount; ++j)
  261. {
  262. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  263. const LADSPA_PortRangeHint portRangeHints = descriptor->PortRangeHints[j];
  264. const char* const portName = descriptor->PortNames[j];
  265. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  266. {
  267. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  268. descriptor->connect_port(handle, j, bufferAudio[iA++]);
  269. }
  270. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  271. {
  272. // min value
  273. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  274. min = portRangeHints.LowerBound;
  275. else
  276. min = 0.0f;
  277. // max value
  278. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  279. max = portRangeHints.UpperBound;
  280. else
  281. max = 1.0f;
  282. if (min > max)
  283. {
  284. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  285. max = min + 0.1f;
  286. }
  287. else if (carla_isEqual(min, max))
  288. {
  289. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  290. max = min + 0.1f;
  291. }
  292. // default value
  293. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  294. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  295. {
  296. min *= kSampleRatef;
  297. max *= kSampleRatef;
  298. def *= kSampleRatef;
  299. }
  300. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  301. {
  302. // latency parameter
  303. def = 0.0f;
  304. }
  305. else
  306. {
  307. if (def < min)
  308. def = min;
  309. else if (def > max)
  310. def = max;
  311. }
  312. bufferParams[iC] = def;
  313. descriptor->connect_port(handle, j, &bufferParams[iC++]);
  314. }
  315. }
  316. if (descriptor->activate != nullptr)
  317. descriptor->activate(handle);
  318. descriptor->run(handle, kBufferSize);
  319. if (descriptor->deactivate != nullptr)
  320. descriptor->deactivate(handle);
  321. descriptor->cleanup(handle);
  322. // end crash-free plugin test
  323. // -----------------------------------------------------------------------
  324. }
  325. DISCOVERY_OUT("init", "-----------");
  326. DISCOVERY_OUT("build", BINARY_NATIVE);
  327. DISCOVERY_OUT("hints", hints);
  328. DISCOVERY_OUT("category", getPluginCategoryAsString(getPluginCategoryFromName(descriptor->Name)));
  329. DISCOVERY_OUT("name", descriptor->Name);
  330. DISCOVERY_OUT("label", descriptor->Label);
  331. DISCOVERY_OUT("maker", descriptor->Maker);
  332. DISCOVERY_OUT("uniqueId", descriptor->UniqueID);
  333. DISCOVERY_OUT("audio.ins", audioIns);
  334. DISCOVERY_OUT("audio.outs", audioOuts);
  335. DISCOVERY_OUT("parameters.ins", parametersIns);
  336. DISCOVERY_OUT("parameters.outs", parametersOuts);
  337. DISCOVERY_OUT("end", "------------");
  338. }
  339. }
  340. static void do_dssi_check(lib_t& libHandle, const char* const filename, const bool doInit)
  341. {
  342. DSSI_Descriptor_Function descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  343. if (descFn == nullptr)
  344. {
  345. DISCOVERY_OUT("error", "Not a DSSI plugin");
  346. return;
  347. }
  348. const DSSI_Descriptor* descriptor;
  349. {
  350. descriptor = descFn(0);
  351. if (descriptor == nullptr)
  352. {
  353. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  354. return;
  355. }
  356. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  357. if (ldescriptor == nullptr)
  358. {
  359. DISCOVERY_OUT("error", "DSSI plugin doesn't provide the LADSPA interface");
  360. return;
  361. }
  362. if (doInit && ldescriptor->instantiate != nullptr && ldescriptor->cleanup != nullptr)
  363. {
  364. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  365. if (handle == nullptr)
  366. {
  367. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  368. return;
  369. }
  370. ldescriptor->cleanup(handle);
  371. lib_close(libHandle);
  372. libHandle = lib_open(filename);
  373. if (libHandle == nullptr)
  374. {
  375. print_lib_error(filename);
  376. return;
  377. }
  378. descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  379. if (descFn == nullptr)
  380. {
  381. DISCOVERY_OUT("error", "Not a DSSI plugin (#2)");
  382. return;
  383. }
  384. }
  385. }
  386. unsigned long i = 0;
  387. while ((descriptor = descFn(i++)) != nullptr)
  388. {
  389. const LADSPA_Descriptor* const ldescriptor = descriptor->LADSPA_Plugin;
  390. if (ldescriptor == nullptr)
  391. {
  392. DISCOVERY_OUT("error", "Plugin has no LADSPA interface");
  393. continue;
  394. }
  395. if (descriptor->DSSI_API_Version != DSSI_VERSION_MAJOR)
  396. {
  397. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' uses an unsupported DSSI spec version " << descriptor->DSSI_API_Version);
  398. continue;
  399. }
  400. if (ldescriptor->instantiate == nullptr)
  401. {
  402. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no instantiate()");
  403. continue;
  404. }
  405. if (ldescriptor->cleanup == nullptr)
  406. {
  407. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no cleanup()");
  408. continue;
  409. }
  410. if (ldescriptor->run == nullptr && descriptor->run_synth == nullptr)
  411. {
  412. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no run() or run_synth()");
  413. continue;
  414. }
  415. if (descriptor->run_synth == nullptr && descriptor->run_multiple_synths != nullptr)
  416. {
  417. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' requires run_multiple_synths which is not supported");
  418. continue;
  419. }
  420. if (! LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  421. {
  422. DISCOVERY_OUT("warning", "Plugin '" << ldescriptor->Name << "' is not hard real-time capable");
  423. }
  424. uint hints = 0x0;
  425. uint audioIns = 0;
  426. uint audioOuts = 0;
  427. uint audioTotal = 0;
  428. uint midiIns = 0;
  429. uint parametersIns = 0;
  430. uint parametersOuts = 0;
  431. uint parametersTotal = 0;
  432. if (LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  433. hints |= PLUGIN_IS_RTSAFE;
  434. for (unsigned long j=0; j < ldescriptor->PortCount; ++j)
  435. {
  436. CARLA_ASSERT(ldescriptor->PortNames[j] != nullptr);
  437. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  438. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  439. {
  440. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  441. audioIns += 1;
  442. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  443. audioOuts += 1;
  444. audioTotal += 1;
  445. }
  446. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  447. {
  448. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  449. parametersIns += 1;
  450. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(ldescriptor->PortNames[j], "latency") != 0 && std::strcmp(ldescriptor->PortNames[j], "_latency") != 0)
  451. parametersOuts += 1;
  452. parametersTotal += 1;
  453. }
  454. }
  455. if (descriptor->run_synth != nullptr)
  456. midiIns = 1;
  457. if (midiIns > 0 && audioIns == 0 && audioOuts > 0)
  458. hints |= PLUGIN_IS_SYNTH;
  459. #ifndef BUILD_BRIDGE
  460. if (const char* const ui = find_dssi_ui(filename, ldescriptor->Label))
  461. {
  462. hints |= PLUGIN_HAS_CUSTOM_UI;
  463. delete[] ui;
  464. }
  465. #endif
  466. if (doInit)
  467. {
  468. // -----------------------------------------------------------------------
  469. // start crash-free plugin test
  470. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  471. if (handle == nullptr)
  472. {
  473. DISCOVERY_OUT("error", "Failed to init DSSI plugin");
  474. continue;
  475. }
  476. // Test quick init and cleanup
  477. ldescriptor->cleanup(handle);
  478. handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  479. if (handle == nullptr)
  480. {
  481. DISCOVERY_OUT("error", "Failed to init DSSI plugin (#2)");
  482. continue;
  483. }
  484. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  485. LADSPA_Data bufferParams[parametersTotal];
  486. LADSPA_Data min, max, def;
  487. for (unsigned long j=0, iA=0, iC=0; j < ldescriptor->PortCount; ++j)
  488. {
  489. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  490. const LADSPA_PortRangeHint portRangeHints = ldescriptor->PortRangeHints[j];
  491. const char* const portName = ldescriptor->PortNames[j];
  492. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  493. {
  494. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  495. ldescriptor->connect_port(handle, j, bufferAudio[iA++]);
  496. }
  497. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  498. {
  499. // min value
  500. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  501. min = portRangeHints.LowerBound;
  502. else
  503. min = 0.0f;
  504. // max value
  505. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  506. max = portRangeHints.UpperBound;
  507. else
  508. max = 1.0f;
  509. if (min > max)
  510. {
  511. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  512. max = min + 0.1f;
  513. }
  514. else if (carla_isEqual(min, max))
  515. {
  516. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  517. max = min + 0.1f;
  518. }
  519. // default value
  520. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  521. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  522. {
  523. min *= kSampleRatef;
  524. max *= kSampleRatef;
  525. def *= kSampleRatef;
  526. }
  527. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  528. {
  529. // latency parameter
  530. def = 0.0f;
  531. }
  532. else
  533. {
  534. if (def < min)
  535. def = min;
  536. else if (def > max)
  537. def = max;
  538. }
  539. bufferParams[iC] = def;
  540. ldescriptor->connect_port(handle, j, &bufferParams[iC++]);
  541. }
  542. }
  543. // select first midi-program if available
  544. if (descriptor->get_program != nullptr && descriptor->select_program != nullptr)
  545. {
  546. if (const DSSI_Program_Descriptor* const pDesc = descriptor->get_program(handle, 0))
  547. descriptor->select_program(handle, pDesc->Bank, pDesc->Program);
  548. }
  549. if (ldescriptor->activate != nullptr)
  550. ldescriptor->activate(handle);
  551. if (descriptor->run_synth != nullptr)
  552. {
  553. snd_seq_event_t midiEvents[2];
  554. carla_zeroStructs(midiEvents, 2);
  555. const unsigned long midiEventCount = 2;
  556. midiEvents[0].type = SND_SEQ_EVENT_NOTEON;
  557. midiEvents[0].data.note.note = 64;
  558. midiEvents[0].data.note.velocity = 100;
  559. midiEvents[1].type = SND_SEQ_EVENT_NOTEOFF;
  560. midiEvents[1].data.note.note = 64;
  561. midiEvents[1].data.note.velocity = 0;
  562. midiEvents[1].time.tick = kBufferSize/2;
  563. descriptor->run_synth(handle, kBufferSize, midiEvents, midiEventCount);
  564. }
  565. else
  566. ldescriptor->run(handle, kBufferSize);
  567. if (ldescriptor->deactivate != nullptr)
  568. ldescriptor->deactivate(handle);
  569. ldescriptor->cleanup(handle);
  570. // end crash-free plugin test
  571. // -----------------------------------------------------------------------
  572. }
  573. DISCOVERY_OUT("init", "-----------");
  574. DISCOVERY_OUT("build", BINARY_NATIVE);
  575. DISCOVERY_OUT("category", ((hints & PLUGIN_IS_SYNTH)
  576. ? "synth"
  577. : getPluginCategoryAsString(getPluginCategoryFromName(ldescriptor->Name))));
  578. DISCOVERY_OUT("hints", hints);
  579. DISCOVERY_OUT("name", ldescriptor->Name);
  580. DISCOVERY_OUT("label", ldescriptor->Label);
  581. DISCOVERY_OUT("maker", ldescriptor->Maker);
  582. DISCOVERY_OUT("uniqueId", ldescriptor->UniqueID);
  583. DISCOVERY_OUT("audio.ins", audioIns);
  584. DISCOVERY_OUT("audio.outs", audioOuts);
  585. DISCOVERY_OUT("midi.ins", midiIns);
  586. DISCOVERY_OUT("parameters.ins", parametersIns);
  587. DISCOVERY_OUT("parameters.outs", parametersOuts);
  588. DISCOVERY_OUT("end", "------------");
  589. }
  590. }
  591. #ifndef BUILD_BRIDGE
  592. static void do_lv2_check(const char* const bundle, const bool doInit)
  593. {
  594. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  595. Lilv::Node bundleNode(lv2World.new_file_uri(nullptr, bundle));
  596. CARLA_SAFE_ASSERT_RETURN(bundleNode.is_uri(),);
  597. CarlaString sBundle(bundleNode.as_uri());
  598. if (! sBundle.endsWith("/"))
  599. sBundle += "/";
  600. // Load bundle
  601. lv2World.load_bundle(sBundle);
  602. // Load plugins in this bundle
  603. const Lilv::Plugins lilvPlugins(lv2World.get_all_plugins());
  604. // Get all plugin URIs in this bundle
  605. CarlaStringList URIs;
  606. LILV_FOREACH(plugins, it, lilvPlugins)
  607. {
  608. Lilv::Plugin lilvPlugin(lilv_plugins_get(lilvPlugins, it));
  609. if (const char* const uri = lilvPlugin.get_uri().as_string())
  610. URIs.appendUnique(uri);
  611. }
  612. if (URIs.isEmpty())
  613. {
  614. DISCOVERY_OUT("warning", "LV2 Bundle doesn't provide any plugins");
  615. return;
  616. }
  617. // Get & check every plugin-instance
  618. for (CarlaStringList::Itenerator it=URIs.begin2(); it.valid(); it.next())
  619. {
  620. const char* const URI = it.getValue(nullptr);
  621. CARLA_SAFE_ASSERT_CONTINUE(URI != nullptr);
  622. CarlaScopedPointer<const LV2_RDF_Descriptor> rdfDescriptor(lv2_rdf_new(URI, false));
  623. if (rdfDescriptor == nullptr || rdfDescriptor->URI == nullptr)
  624. {
  625. DISCOVERY_OUT("error", "Failed to find LV2 plugin '" << URI << "'");
  626. continue;
  627. }
  628. if (doInit)
  629. {
  630. // test if lib is loadable, twice
  631. const lib_t libHandle1 = lib_open(rdfDescriptor->Binary);
  632. if (libHandle1 == nullptr)
  633. {
  634. print_lib_error(rdfDescriptor->Binary);
  635. delete rdfDescriptor;
  636. continue;
  637. }
  638. lib_close(libHandle1);
  639. const lib_t libHandle2 = lib_open(rdfDescriptor->Binary);
  640. if (libHandle2 == nullptr)
  641. {
  642. print_lib_error(rdfDescriptor->Binary);
  643. delete rdfDescriptor;
  644. continue;
  645. }
  646. lib_close(libHandle2);
  647. }
  648. const LilvPlugin* const cPlugin(lv2World.getPluginFromURI(URI));
  649. CARLA_SAFE_ASSERT_CONTINUE(cPlugin != nullptr);
  650. Lilv::Plugin lilvPlugin(cPlugin);
  651. CARLA_SAFE_ASSERT_CONTINUE(lilvPlugin.get_uri().is_uri());
  652. print_cached_plugin(get_cached_plugin_lv2(lv2World, lilvPlugin));
  653. }
  654. }
  655. #endif
  656. #ifndef USING_JUCE_FOR_VST2
  657. // -------------------------------------------------------------------------------------------------------------------
  658. // VST stuff
  659. // Check if plugin is currently processing
  660. static bool gVstIsProcessing = false;
  661. // Check if plugin needs idle
  662. static bool gVstNeedsIdle = false;
  663. // Check if plugin wants midi
  664. static bool gVstWantsMidi = false;
  665. // Check if plugin wants time
  666. static bool gVstWantsTime = false;
  667. // Current uniqueId for VST shell plugins
  668. static intptr_t gVstCurrentUniqueId = 0;
  669. // Supported Carla features
  670. static intptr_t vstHostCanDo(const char* const feature)
  671. {
  672. carla_debug("vstHostCanDo(\"%s\")", feature);
  673. if (std::strcmp(feature, "supplyIdle") == 0)
  674. return 1;
  675. if (std::strcmp(feature, "sendVstEvents") == 0)
  676. return 1;
  677. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  678. return 1;
  679. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  680. return 1;
  681. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  682. {
  683. gVstWantsTime = true;
  684. return 1;
  685. }
  686. if (std::strcmp(feature, "receiveVstEvents") == 0)
  687. return 1;
  688. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  689. return 1;
  690. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  691. return -1;
  692. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  693. return -1;
  694. if (std::strcmp(feature, "acceptIOChanges") == 0)
  695. return 1;
  696. if (std::strcmp(feature, "sizeWindow") == 0)
  697. return 1;
  698. if (std::strcmp(feature, "offline") == 0)
  699. return -1;
  700. if (std::strcmp(feature, "openFileSelector") == 0)
  701. return -1;
  702. if (std::strcmp(feature, "closeFileSelector") == 0)
  703. return -1;
  704. if (std::strcmp(feature, "startStopProcess") == 0)
  705. return 1;
  706. if (std::strcmp(feature, "supportShell") == 0)
  707. return 1;
  708. if (std::strcmp(feature, "shellCategory") == 0)
  709. return 1;
  710. if (std::strcmp(feature, "NIMKPIVendorSpecificCallbacks") == 0)
  711. return -1;
  712. // non-official features found in some plugins:
  713. // "asyncProcessing"
  714. // "editFile"
  715. // unimplemented
  716. carla_stderr("vstHostCanDo(\"%s\") - unknown feature", feature);
  717. return 0;
  718. }
  719. // Host-side callback
  720. 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)
  721. {
  722. carla_debug("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)",
  723. effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, static_cast<double>(opt));
  724. static VstTimeInfo timeInfo;
  725. intptr_t ret = 0;
  726. switch (opcode)
  727. {
  728. case audioMasterAutomate:
  729. ret = 1;
  730. break;
  731. case audioMasterVersion:
  732. ret = kVstVersion;
  733. break;
  734. case audioMasterCurrentId:
  735. ret = gVstCurrentUniqueId;
  736. break;
  737. case DECLARE_VST_DEPRECATED(audioMasterWantMidi):
  738. if (gVstWantsMidi) { DISCOVERY_OUT("warning", "Plugin requested MIDI more than once"); }
  739. gVstWantsMidi = true;
  740. ret = 1;
  741. break;
  742. case audioMasterGetTime:
  743. if (! gVstIsProcessing) { DISCOVERY_OUT("warning", "Plugin requested timeInfo out of process"); }
  744. if (! gVstWantsTime) { DISCOVERY_OUT("warning", "Plugin requested timeInfo but didn't ask if host could do \"sendVstTimeInfo\""); }
  745. carla_zeroStruct(timeInfo);
  746. timeInfo.sampleRate = kSampleRate;
  747. // Tempo
  748. timeInfo.tempo = 120.0;
  749. timeInfo.flags |= kVstTempoValid;
  750. // Time Signature
  751. timeInfo.timeSigNumerator = 4;
  752. timeInfo.timeSigDenominator = 4;
  753. timeInfo.flags |= kVstTimeSigValid;
  754. ret = (intptr_t)&timeInfo;
  755. break;
  756. case DECLARE_VST_DEPRECATED(audioMasterTempoAt):
  757. ret = 120 * 10000;
  758. break;
  759. case DECLARE_VST_DEPRECATED(audioMasterGetNumAutomatableParameters):
  760. ret = carla_minPositive(effect->numParams, static_cast<int>(MAX_DEFAULT_PARAMETERS));
  761. break;
  762. case DECLARE_VST_DEPRECATED(audioMasterGetParameterQuantization):
  763. ret = 1; // full single float precision
  764. break;
  765. case DECLARE_VST_DEPRECATED(audioMasterNeedIdle):
  766. if (gVstNeedsIdle) { DISCOVERY_OUT("warning", "Plugin requested idle more than once"); }
  767. gVstNeedsIdle = true;
  768. ret = 1;
  769. break;
  770. case audioMasterGetSampleRate:
  771. ret = kSampleRatei;
  772. break;
  773. case audioMasterGetBlockSize:
  774. ret = kBufferSize;
  775. break;
  776. case DECLARE_VST_DEPRECATED(audioMasterWillReplaceOrAccumulate):
  777. ret = 1; // replace
  778. break;
  779. case audioMasterGetCurrentProcessLevel:
  780. ret = gVstIsProcessing ? kVstProcessLevelRealtime : kVstProcessLevelUser;
  781. break;
  782. case audioMasterGetAutomationState:
  783. ret = kVstAutomationOff;
  784. break;
  785. case audioMasterGetVendorString:
  786. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  787. std::strcpy((char*)ptr, "falkTX");
  788. ret = 1;
  789. break;
  790. case audioMasterGetProductString:
  791. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  792. std::strcpy((char*)ptr, "Carla-Discovery");
  793. ret = 1;
  794. break;
  795. case audioMasterGetVendorVersion:
  796. ret = CARLA_VERSION_HEX;
  797. break;
  798. case audioMasterCanDo:
  799. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  800. ret = vstHostCanDo((const char*)ptr);
  801. break;
  802. case audioMasterGetLanguage:
  803. ret = kVstLangEnglish;
  804. break;
  805. default:
  806. carla_stdout("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)",
  807. effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, static_cast<double>(opt));
  808. break;
  809. }
  810. return ret;
  811. }
  812. static void do_vst2_check(lib_t& libHandle, const char* const filename, const bool doInit)
  813. {
  814. VST_Function vstFn = nullptr;
  815. #ifdef CARLA_OS_MAC
  816. BundleLoader bundleLoader;
  817. if (libHandle == nullptr)
  818. {
  819. if (! bundleLoader.load(filename))
  820. {
  821. DISCOVERY_OUT("error", "Failed to load VST2 bundle executable");
  822. return;
  823. }
  824. vstFn = bundleLoader.getSymbol<VST_Function>(CFSTR("main_macho"));
  825. if (vstFn == nullptr)
  826. vstFn = bundleLoader.getSymbol<VST_Function>(CFSTR("VSTPluginMain"));
  827. if (vstFn == nullptr)
  828. {
  829. DISCOVERY_OUT("error", "Not a VST2 plugin");
  830. return;
  831. }
  832. }
  833. else
  834. #endif
  835. {
  836. vstFn = lib_symbol<VST_Function>(libHandle, "VSTPluginMain");
  837. if (vstFn == nullptr)
  838. {
  839. vstFn = lib_symbol<VST_Function>(libHandle, "main");
  840. if (vstFn == nullptr)
  841. {
  842. DISCOVERY_OUT("error", "Not a VST plugin");
  843. return;
  844. }
  845. }
  846. }
  847. AEffect* effect = vstFn(vstHostCallback);
  848. if (effect == nullptr || effect->magic != kEffectMagic)
  849. {
  850. DISCOVERY_OUT("error", "Failed to init VST plugin, or VST magic failed");
  851. return;
  852. }
  853. if (effect->uniqueID == 0)
  854. {
  855. DISCOVERY_OUT("warning", "Plugin doesn't have an Unique ID when first loaded");
  856. }
  857. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdentify), 0, 0, nullptr, 0.0f);
  858. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effSetBlockSizeAndSampleRate), 0, kBufferSize, nullptr, kSampleRatef);
  859. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, kSampleRatef);
  860. effect->dispatcher(effect, effSetBlockSize, 0, kBufferSize, nullptr, 0.0f);
  861. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  862. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  863. if (effect->numPrograms > 0)
  864. effect->dispatcher(effect, effSetProgram, 0, 0, nullptr, 0.0f);
  865. const bool isShell = (effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f) == kPlugCategShell);
  866. if (effect->uniqueID == 0 && !isShell)
  867. {
  868. DISCOVERY_OUT("error", "Plugin doesn't have an Unique ID after being open");
  869. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  870. return;
  871. }
  872. gVstCurrentUniqueId = effect->uniqueID;
  873. char strBuf[STR_MAX+1];
  874. CarlaString cName;
  875. CarlaString cProduct;
  876. CarlaString cVendor;
  877. PluginCategory category;
  878. LinkedList<intptr_t> uniqueIds;
  879. if (isShell)
  880. {
  881. for (;;)
  882. {
  883. carla_zeroChars(strBuf, STR_MAX+1);
  884. gVstCurrentUniqueId = effect->dispatcher(effect, effShellGetNextPlugin, 0, 0, strBuf, 0.0f);
  885. if (gVstCurrentUniqueId == 0)
  886. break;
  887. uniqueIds.append(gVstCurrentUniqueId);
  888. }
  889. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  890. effect = nullptr;
  891. }
  892. else
  893. {
  894. uniqueIds.append(gVstCurrentUniqueId);
  895. }
  896. for (LinkedList<intptr_t>::Itenerator it = uniqueIds.begin2(); it.valid(); it.next())
  897. {
  898. gVstCurrentUniqueId = it.getValue(0);
  899. if (effect == nullptr)
  900. {
  901. effect = vstFn(vstHostCallback);
  902. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdentify), 0, 0, nullptr, 0.0f);
  903. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effSetBlockSizeAndSampleRate), 0, kBufferSize, nullptr, kSampleRatef);
  904. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, kSampleRatef);
  905. effect->dispatcher(effect, effSetBlockSize, 0, kBufferSize, nullptr, 0.0f);
  906. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  907. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  908. if (effect->numPrograms > 0)
  909. effect->dispatcher(effect, effSetProgram, 0, 0, nullptr, 0.0f);
  910. }
  911. // get name
  912. carla_zeroChars(strBuf, STR_MAX+1);
  913. if (effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f) == 1)
  914. cName = strBuf;
  915. else
  916. cName.clear();
  917. // get product
  918. carla_zeroChars(strBuf, STR_MAX+1);
  919. if (effect->dispatcher(effect, effGetProductString, 0, 0, strBuf, 0.0f) == 1)
  920. cProduct = strBuf;
  921. else
  922. cProduct.clear();
  923. // get vendor
  924. carla_zeroChars(strBuf, STR_MAX+1);
  925. if (effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f) == 1)
  926. cVendor = strBuf;
  927. else
  928. cVendor.clear();
  929. // get category
  930. switch (effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f))
  931. {
  932. case kPlugCategSynth:
  933. category = PLUGIN_CATEGORY_SYNTH;
  934. break;
  935. case kPlugCategAnalysis:
  936. category = PLUGIN_CATEGORY_UTILITY;
  937. break;
  938. case kPlugCategMastering:
  939. category = PLUGIN_CATEGORY_DYNAMICS;
  940. break;
  941. case kPlugCategRoomFx:
  942. category = PLUGIN_CATEGORY_DELAY;
  943. break;
  944. case kPlugCategRestoration:
  945. category = PLUGIN_CATEGORY_UTILITY;
  946. break;
  947. case kPlugCategGenerator:
  948. category = PLUGIN_CATEGORY_SYNTH;
  949. break;
  950. default:
  951. if (effect->flags & effFlagsIsSynth)
  952. category = PLUGIN_CATEGORY_SYNTH;
  953. else
  954. category = PLUGIN_CATEGORY_NONE;
  955. break;
  956. }
  957. // get everything else
  958. uint hints = 0x0;
  959. uint audioIns = static_cast<uint>(std::max(0, effect->numInputs));
  960. uint audioOuts = static_cast<uint>(std::max(0, effect->numOutputs));
  961. uint midiIns = 0;
  962. uint midiOuts = 0;
  963. uint parameters = static_cast<uint>(std::max(0, effect->numParams));
  964. if (effect->flags & effFlagsHasEditor)
  965. hints |= PLUGIN_HAS_CUSTOM_UI;
  966. if (effect->flags & effFlagsIsSynth)
  967. {
  968. hints |= PLUGIN_IS_SYNTH;
  969. midiIns = 1;
  970. }
  971. if (vstPluginCanDo(effect, "receiveVstEvents") || vstPluginCanDo(effect, "receiveVstMidiEvent") || (effect->flags & effFlagsIsSynth) != 0)
  972. midiIns = 1;
  973. if (vstPluginCanDo(effect, "sendVstEvents") || vstPluginCanDo(effect, "sendVstMidiEvent"))
  974. midiOuts = 1;
  975. // -----------------------------------------------------------------------
  976. // start crash-free plugin test
  977. if (doInit)
  978. {
  979. if (gVstNeedsIdle)
  980. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  981. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  982. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  983. if (gVstNeedsIdle)
  984. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  985. // Plugin might call wantMidi() during resume
  986. if (midiIns == 0 && gVstWantsMidi)
  987. {
  988. midiIns = 1;
  989. }
  990. float* bufferAudioIn[std::max(1U, audioIns)];
  991. float* bufferAudioOut[std::max(1U, audioOuts)];
  992. if (audioIns == 0)
  993. {
  994. bufferAudioIn[0] = nullptr;
  995. }
  996. else
  997. {
  998. for (uint j=0; j < audioIns; ++j)
  999. {
  1000. bufferAudioIn[j] = new float[kBufferSize];
  1001. carla_zeroFloats(bufferAudioIn[j], kBufferSize);
  1002. }
  1003. }
  1004. if (audioOuts == 0)
  1005. {
  1006. bufferAudioOut[0] = nullptr;
  1007. }
  1008. else
  1009. {
  1010. for (uint j=0; j < audioOuts; ++j)
  1011. {
  1012. bufferAudioOut[j] = new float[kBufferSize];
  1013. carla_zeroFloats(bufferAudioOut[j], kBufferSize);
  1014. }
  1015. }
  1016. struct VstEventsFixed {
  1017. int32_t numEvents;
  1018. intptr_t reserved;
  1019. VstEvent* data[2];
  1020. VstEventsFixed()
  1021. : numEvents(0),
  1022. reserved(0)
  1023. {
  1024. data[0] = data[1] = nullptr;
  1025. }
  1026. } events;
  1027. VstMidiEvent midiEvents[2];
  1028. carla_zeroStructs(midiEvents, 2);
  1029. midiEvents[0].type = kVstMidiType;
  1030. midiEvents[0].byteSize = sizeof(VstMidiEvent);
  1031. midiEvents[0].midiData[0] = char(MIDI_STATUS_NOTE_ON);
  1032. midiEvents[0].midiData[1] = 64;
  1033. midiEvents[0].midiData[2] = 100;
  1034. midiEvents[1].type = kVstMidiType;
  1035. midiEvents[1].byteSize = sizeof(VstMidiEvent);
  1036. midiEvents[1].midiData[0] = char(MIDI_STATUS_NOTE_OFF);
  1037. midiEvents[1].midiData[1] = 64;
  1038. midiEvents[1].deltaFrames = kBufferSize/2;
  1039. events.numEvents = 2;
  1040. events.data[0] = (VstEvent*)&midiEvents[0];
  1041. events.data[1] = (VstEvent*)&midiEvents[1];
  1042. // processing
  1043. gVstIsProcessing = true;
  1044. if (midiIns > 0)
  1045. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  1046. if ((effect->flags & effFlagsCanReplacing) > 0 && effect->processReplacing != nullptr && effect->processReplacing != effect->DECLARE_VST_DEPRECATED(process))
  1047. effect->processReplacing(effect, bufferAudioIn, bufferAudioOut, kBufferSize);
  1048. else if (effect->DECLARE_VST_DEPRECATED(process) != nullptr)
  1049. effect->DECLARE_VST_DEPRECATED(process)(effect, bufferAudioIn, bufferAudioOut, kBufferSize);
  1050. else
  1051. DISCOVERY_OUT("error", "Plugin doesn't have a process function");
  1052. gVstIsProcessing = false;
  1053. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1054. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1055. if (gVstNeedsIdle)
  1056. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1057. for (uint j=0; j < audioIns; ++j)
  1058. delete[] bufferAudioIn[j];
  1059. for (uint j=0; j < audioOuts; ++j)
  1060. delete[] bufferAudioOut[j];
  1061. }
  1062. // end crash-free plugin test
  1063. // -----------------------------------------------------------------------
  1064. DISCOVERY_OUT("init", "-----------");
  1065. DISCOVERY_OUT("build", BINARY_NATIVE);
  1066. DISCOVERY_OUT("hints", hints);
  1067. DISCOVERY_OUT("category", getPluginCategoryAsString(category));
  1068. DISCOVERY_OUT("name", cName.buffer());
  1069. DISCOVERY_OUT("label", cProduct.buffer());
  1070. DISCOVERY_OUT("maker", cVendor.buffer());
  1071. DISCOVERY_OUT("uniqueId", gVstCurrentUniqueId);
  1072. DISCOVERY_OUT("audio.ins", audioIns);
  1073. DISCOVERY_OUT("audio.outs", audioOuts);
  1074. DISCOVERY_OUT("midi.ins", midiIns);
  1075. DISCOVERY_OUT("midi.outs", midiOuts);
  1076. DISCOVERY_OUT("parameters.ins", parameters);
  1077. DISCOVERY_OUT("end", "------------");
  1078. gVstWantsMidi = false;
  1079. gVstWantsTime = false;
  1080. if (! isShell)
  1081. break;
  1082. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1083. effect = nullptr;
  1084. }
  1085. uniqueIds.clear();
  1086. if (effect != nullptr)
  1087. {
  1088. if (gVstNeedsIdle)
  1089. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1090. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1091. }
  1092. #ifndef CARLA_OS_MAC
  1093. return;
  1094. // unused
  1095. (void)filename;
  1096. #endif
  1097. }
  1098. #endif // ! USING_JUCE_FOR_VST2
  1099. #ifndef USING_JUCE_FOR_VST3
  1100. static uint32_t V3_API v3_ref_static(void*) { return 1; }
  1101. static uint32_t V3_API v3_unref_static(void*) { return 0; }
  1102. struct carla_v3_host_application : v3_host_application_cpp {
  1103. carla_v3_host_application()
  1104. {
  1105. query_interface = carla_query_interface;
  1106. ref = v3_ref_static;
  1107. unref = v3_unref_static;
  1108. app.get_name = carla_get_name;
  1109. app.create_instance = carla_create_instance;
  1110. }
  1111. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  1112. {
  1113. if (v3_tuid_match(iid, v3_funknown_iid) ||
  1114. v3_tuid_match(iid, v3_host_application_iid))
  1115. {
  1116. *iface = self;
  1117. return V3_OK;
  1118. }
  1119. *iface = nullptr;
  1120. return V3_NO_INTERFACE;
  1121. }
  1122. static v3_result V3_API carla_get_name(void*, v3_str_128 name)
  1123. {
  1124. static const char hostname[] = "Carla-Discovery\0";
  1125. for (size_t i=0; i<sizeof(hostname); ++i)
  1126. name[i] = hostname[i];
  1127. return V3_OK;
  1128. }
  1129. static v3_result V3_API carla_create_instance(void*, v3_tuid, v3_tuid, void**) { return V3_NOT_IMPLEMENTED; }
  1130. };
  1131. struct carla_v3_param_changes : v3_param_changes_cpp {
  1132. carla_v3_param_changes()
  1133. {
  1134. query_interface = carla_query_interface;
  1135. ref = v3_ref_static;
  1136. unref = v3_unref_static;
  1137. changes.get_param_count = carla_get_param_count;
  1138. changes.get_param_data = carla_get_param_data;
  1139. changes.add_param_data = carla_add_param_data;
  1140. }
  1141. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  1142. {
  1143. if (v3_tuid_match(iid, v3_funknown_iid) ||
  1144. v3_tuid_match(iid, v3_param_changes_iid))
  1145. {
  1146. *iface = self;
  1147. return V3_OK;
  1148. }
  1149. *iface = nullptr;
  1150. return V3_NO_INTERFACE;
  1151. }
  1152. static int32_t V3_API carla_get_param_count(void*) { return 0; }
  1153. static v3_param_value_queue** V3_API carla_get_param_data(void*, int32_t) { return nullptr; }
  1154. static v3_param_value_queue** V3_API carla_add_param_data(void*, v3_param_id*, int32_t*) { return nullptr; }
  1155. };
  1156. struct carla_v3_event_list : v3_event_list_cpp {
  1157. carla_v3_event_list()
  1158. {
  1159. query_interface = carla_query_interface;
  1160. ref = v3_ref_static;
  1161. unref = v3_unref_static;
  1162. list.get_event_count = carla_get_event_count;
  1163. list.get_event = carla_get_event;
  1164. list.add_event = carla_add_event;
  1165. }
  1166. static v3_result V3_API carla_query_interface(void* const self, const v3_tuid iid, void** const iface)
  1167. {
  1168. if (v3_tuid_match(iid, v3_funknown_iid) ||
  1169. v3_tuid_match(iid, v3_event_list_iid))
  1170. {
  1171. *iface = self;
  1172. return V3_OK;
  1173. }
  1174. *iface = nullptr;
  1175. return V3_NO_INTERFACE;
  1176. }
  1177. static uint32_t V3_API carla_get_event_count(void*) { return 0; }
  1178. static v3_result V3_API carla_get_event(void*, int32_t, v3_event*) { return V3_NOT_IMPLEMENTED; }
  1179. static v3_result V3_API carla_add_event(void*, v3_event*) { return V3_NOT_IMPLEMENTED; }
  1180. };
  1181. static void do_vst3_check(lib_t& libHandle, const char* const filename, const bool doInit)
  1182. {
  1183. V3_ENTRYFN v3_entry = nullptr;
  1184. V3_EXITFN v3_exit = nullptr;
  1185. V3_GETFN v3_get = nullptr;
  1186. #ifdef CARLA_OS_MAC
  1187. BundleLoader bundleLoader;
  1188. #endif
  1189. // if passed filename is not a plugin binary directly, inspect bundle and find one
  1190. if (libHandle == nullptr)
  1191. {
  1192. #ifdef CARLA_OS_MAC
  1193. if (! bundleLoader.load(filename))
  1194. {
  1195. DISCOVERY_OUT("error", "Failed to load VST3 bundle executable");
  1196. return;
  1197. }
  1198. v3_entry = bundleLoader.getSymbol<V3_ENTRYFN>(CFSTR(V3_ENTRYFNNAME));
  1199. v3_exit = bundleLoader.getSymbol<V3_EXITFN>(CFSTR(V3_EXITFNNAME));
  1200. v3_get = bundleLoader.getSymbol<V3_GETFN>(CFSTR(V3_GETFNNAME));
  1201. #else
  1202. water::String binaryfilename = filename;
  1203. if (!binaryfilename.endsWithChar(CARLA_OS_SEP))
  1204. binaryfilename += CARLA_OS_SEP_STR;
  1205. binaryfilename += "Contents" CARLA_OS_SEP_STR V3_CONTENT_DIR CARLA_OS_SEP_STR;
  1206. binaryfilename += water::File(filename).getFileNameWithoutExtension();
  1207. # ifdef CARLA_OS_WIN
  1208. binaryfilename += ".vst3";
  1209. # else
  1210. binaryfilename += ".so";
  1211. # endif
  1212. if (! water::File(binaryfilename).existsAsFile())
  1213. {
  1214. DISCOVERY_OUT("error", "Failed to find a suitable VST3 bundle binary");
  1215. return;
  1216. }
  1217. libHandle = lib_open(binaryfilename.toRawUTF8());
  1218. if (libHandle == nullptr)
  1219. {
  1220. print_lib_error(filename);
  1221. return;
  1222. }
  1223. #endif
  1224. }
  1225. #ifndef CARLA_OS_MAC
  1226. v3_entry = lib_symbol<V3_ENTRYFN>(libHandle, V3_ENTRYFNNAME);
  1227. v3_exit = lib_symbol<V3_EXITFN>(libHandle, V3_EXITFNNAME);
  1228. v3_get = lib_symbol<V3_GETFN>(libHandle, V3_GETFNNAME);
  1229. #endif
  1230. // ensure entry and exit points are available
  1231. if (v3_entry == nullptr || v3_exit == nullptr || v3_get == nullptr)
  1232. {
  1233. DISCOVERY_OUT("error", "Not a VST3 plugin");
  1234. return;
  1235. }
  1236. // call entry point
  1237. #if defined(CARLA_OS_MAC)
  1238. v3_entry(bundleLoader.getRef());
  1239. #elif defined(CARLA_OS_WIN)
  1240. v3_entry();
  1241. #else
  1242. v3_entry(libHandle);
  1243. #endif
  1244. carla_v3_host_application hostApplication;
  1245. carla_v3_host_application* hostApplicationPtr = &hostApplication;
  1246. v3_funknown** const hostContext = (v3_funknown**)&hostApplicationPtr;
  1247. // fetch initial factory
  1248. v3_plugin_factory** factory1 = v3_get();
  1249. CARLA_SAFE_ASSERT_RETURN(factory1 != nullptr, v3_exit());
  1250. // get factory info
  1251. v3_factory_info factoryInfo = {};
  1252. CARLA_SAFE_ASSERT_RETURN(v3_cpp_obj(factory1)->get_factory_info(factory1, &factoryInfo) == V3_OK, v3_exit());
  1253. // get num classes
  1254. const int32_t numClasses = v3_cpp_obj(factory1)->num_classes(factory1);
  1255. CARLA_SAFE_ASSERT_RETURN(numClasses > 0, v3_exit());
  1256. // query 2nd factory
  1257. v3_plugin_factory_2** factory2 = nullptr;
  1258. if (v3_cpp_obj_query_interface(factory1, v3_plugin_factory_2_iid, &factory2) == V3_OK)
  1259. {
  1260. CARLA_SAFE_ASSERT_RETURN(factory2 != nullptr, v3_exit());
  1261. }
  1262. else
  1263. {
  1264. CARLA_SAFE_ASSERT(factory2 == nullptr);
  1265. factory2 = nullptr;
  1266. }
  1267. // query 3rd factory
  1268. v3_plugin_factory_3** factory3 = nullptr;
  1269. if (factory2 != nullptr && v3_cpp_obj_query_interface(factory2, v3_plugin_factory_3_iid, &factory3) == V3_OK)
  1270. {
  1271. CARLA_SAFE_ASSERT_RETURN(factory3 != nullptr, v3_exit());
  1272. }
  1273. else
  1274. {
  1275. CARLA_SAFE_ASSERT(factory3 == nullptr);
  1276. factory3 = nullptr;
  1277. }
  1278. // set host context (application) if 3rd factory provided
  1279. if (factory3 != nullptr)
  1280. v3_cpp_obj(factory3)->set_host_context(factory3, hostContext);
  1281. // go through all relevant classes
  1282. for (int32_t i=0; i<numClasses; ++i)
  1283. {
  1284. // v3_class_info_2 is ABI compatible with v3_class_info
  1285. union {
  1286. v3_class_info v1;
  1287. v3_class_info_2 v2;
  1288. } classInfo = {};
  1289. if (factory2 != nullptr)
  1290. v3_cpp_obj(factory2)->get_class_info_2(factory2, i, &classInfo.v2);
  1291. else
  1292. v3_cpp_obj(factory1)->get_class_info(factory1, i, &classInfo.v1);
  1293. // safety check
  1294. CARLA_SAFE_ASSERT_CONTINUE(classInfo.v1.cardinality == 0x7FFFFFFF);
  1295. // only check for audio plugins
  1296. if (std::strcmp(classInfo.v1.category, "Audio Module Class") != 0)
  1297. continue;
  1298. // create instance
  1299. void* instance = nullptr;
  1300. CARLA_SAFE_ASSERT_CONTINUE(v3_cpp_obj(factory1)->create_instance(factory1, classInfo.v1.class_id, v3_component_iid, &instance) == V3_OK);
  1301. CARLA_SAFE_ASSERT_CONTINUE(instance != nullptr);
  1302. // initialize instance
  1303. v3_component** const component = static_cast<v3_component**>(instance);
  1304. CARLA_SAFE_ASSERT_CONTINUE(v3_cpp_obj_initialize(component, hostContext) == V3_OK);
  1305. // create edit controller
  1306. v3_edit_controller** controller = nullptr;
  1307. bool shouldTerminateController;
  1308. if (v3_cpp_obj_query_interface(component, v3_edit_controller_iid, &controller) != V3_OK)
  1309. controller = nullptr;
  1310. if (controller != nullptr)
  1311. {
  1312. // got edit controller from casting component, assume they belong to the same object
  1313. shouldTerminateController = false;
  1314. }
  1315. else
  1316. {
  1317. // try to create edit controller from factory
  1318. v3_tuid uid = {};
  1319. if (v3_cpp_obj(component)->get_controller_class_id(component, uid) == V3_OK)
  1320. {
  1321. instance = nullptr;
  1322. if (v3_cpp_obj(factory1)->create_instance(factory1, uid, v3_edit_controller_iid, &instance) == V3_OK && instance != nullptr)
  1323. controller = static_cast<v3_edit_controller**>(instance);
  1324. }
  1325. if (controller == nullptr)
  1326. {
  1327. DISCOVERY_OUT("warning", "Plugin '" << classInfo.v1.name << "' does not have an edit controller");
  1328. v3_cpp_obj_terminate(component);
  1329. v3_cpp_obj_unref(component);
  1330. continue;
  1331. }
  1332. // component is separate from controller, needs its dedicated initialize and terminate
  1333. shouldTerminateController = true;
  1334. v3_cpp_obj_initialize(controller, hostContext);
  1335. }
  1336. // fill in all the details
  1337. uint hints = 0x0;
  1338. int audioIns = 0;
  1339. int audioOuts = 0;
  1340. int cvIns = 0;
  1341. int cvOuts = 0;
  1342. int parameterIns = 0;
  1343. int parameterOuts = 0;
  1344. const int32_t numAudioInputBuses = v3_cpp_obj(component)->get_bus_count(component, V3_AUDIO, V3_INPUT);
  1345. const int32_t numEventInputBuses = v3_cpp_obj(component)->get_bus_count(component, V3_EVENT, V3_INPUT);
  1346. const int32_t numAudioOutputBuses = v3_cpp_obj(component)->get_bus_count(component, V3_AUDIO, V3_OUTPUT);
  1347. const int32_t numEventOutputBuses = v3_cpp_obj(component)->get_bus_count(component, V3_EVENT, V3_OUTPUT);
  1348. const int32_t numParameters = v3_cpp_obj(controller)->get_parameter_count(controller);
  1349. CARLA_SAFE_ASSERT(numAudioInputBuses >= 0);
  1350. CARLA_SAFE_ASSERT(numEventInputBuses >= 0);
  1351. CARLA_SAFE_ASSERT(numAudioOutputBuses >= 0);
  1352. CARLA_SAFE_ASSERT(numEventOutputBuses >= 0);
  1353. CARLA_SAFE_ASSERT(numParameters >= 0);
  1354. for (int32_t j=0; j<numAudioInputBuses; ++j)
  1355. {
  1356. v3_bus_info busInfo = {};
  1357. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->get_bus_info(component, V3_AUDIO, V3_INPUT, j, &busInfo) == V3_OK);
  1358. if (busInfo.flags & V3_IS_CONTROL_VOLTAGE)
  1359. cvIns += busInfo.channel_count;
  1360. else
  1361. audioIns += busInfo.channel_count;
  1362. }
  1363. for (int32_t j=0; j<numAudioOutputBuses; ++j)
  1364. {
  1365. v3_bus_info busInfo = {};
  1366. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->get_bus_info(component, V3_AUDIO, V3_OUTPUT, j, &busInfo) == V3_OK);
  1367. if (busInfo.flags & V3_IS_CONTROL_VOLTAGE)
  1368. cvOuts += busInfo.channel_count;
  1369. else
  1370. audioOuts += busInfo.channel_count;
  1371. }
  1372. for (int32_t j=0; j<numParameters; ++j)
  1373. {
  1374. v3_param_info paramInfo = {};
  1375. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(controller)->get_parameter_info(controller, j, &paramInfo) == V3_OK);
  1376. if (paramInfo.flags & (V3_PARAM_IS_BYPASS|V3_PARAM_IS_HIDDEN|V3_PARAM_PROGRAM_CHANGE))
  1377. continue;
  1378. if (paramInfo.flags & V3_PARAM_READ_ONLY)
  1379. ++parameterOuts;
  1380. else
  1381. ++parameterIns;
  1382. }
  1383. if (v3_plugin_view** const view = v3_cpp_obj(controller)->create_view(controller, "view"))
  1384. {
  1385. if (v3_cpp_obj(view)->is_platform_type_supported(view, V3_VIEW_PLATFORM_TYPE_NATIVE) == V3_TRUE)
  1386. hints |= PLUGIN_HAS_CUSTOM_UI;
  1387. v3_cpp_obj_unref(view);
  1388. }
  1389. if (factory2 != nullptr && std::strstr(classInfo.v2.sub_categories, "Instrument") != nullptr)
  1390. hints |= PLUGIN_IS_SYNTH;
  1391. if (doInit)
  1392. {
  1393. v3_audio_processor** processor = nullptr;
  1394. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj_query_interface(component, v3_audio_processor_iid, &processor) == V3_OK);
  1395. CARLA_SAFE_ASSERT_BREAK(processor != nullptr);
  1396. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(processor)->can_process_sample_size(processor, V3_SAMPLE_32) == V3_OK);
  1397. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->set_active(component, true) == V3_OK);
  1398. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->set_active(component, false) == V3_OK);
  1399. v3_process_setup setup = { V3_REALTIME, V3_SAMPLE_32, kBufferSize, kSampleRate };
  1400. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(processor)->setup_processing(processor, &setup) == V3_OK);
  1401. for (int32_t j=0; j<numAudioInputBuses; ++j)
  1402. {
  1403. v3_bus_info busInfo = {};
  1404. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->get_bus_info(component, V3_AUDIO, V3_INPUT, j, &busInfo) == V3_OK);
  1405. if ((busInfo.flags & V3_DEFAULT_ACTIVE) == 0x0) {
  1406. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->activate_bus(component, V3_AUDIO, V3_INPUT, j, true) == V3_OK);
  1407. }
  1408. }
  1409. for (int32_t j=0; j<numAudioOutputBuses; ++j)
  1410. {
  1411. v3_bus_info busInfo = {};
  1412. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->get_bus_info(component, V3_AUDIO, V3_OUTPUT, j, &busInfo) == V3_OK);
  1413. if ((busInfo.flags & V3_DEFAULT_ACTIVE) == 0x0) {
  1414. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->activate_bus(component, V3_AUDIO, V3_OUTPUT, j, true) == V3_OK);
  1415. }
  1416. }
  1417. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->set_active(component, true) == V3_OK);
  1418. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(processor)->set_processing(processor, true) == V3_OK);
  1419. float* bufferAudioIn[(uint)std::max(1, audioIns + cvIns)];
  1420. float* bufferAudioOut[(uint)std::max(1, audioOuts + cvOuts)];
  1421. if (audioIns + cvIns == 0)
  1422. {
  1423. bufferAudioIn[0] = nullptr;
  1424. }
  1425. else
  1426. {
  1427. for (int j=0; j < audioIns + cvIns; ++j)
  1428. {
  1429. bufferAudioIn[j] = new float[kBufferSize];
  1430. carla_zeroFloats(bufferAudioIn[j], kBufferSize);
  1431. }
  1432. }
  1433. if (audioOuts + cvOuts == 0)
  1434. {
  1435. bufferAudioOut[0] = nullptr;
  1436. }
  1437. else
  1438. {
  1439. for (int j=0; j < audioOuts + cvOuts; ++j)
  1440. {
  1441. bufferAudioOut[j] = new float[kBufferSize];
  1442. carla_zeroFloats(bufferAudioOut[j], kBufferSize);
  1443. }
  1444. }
  1445. carla_v3_event_list eventList;
  1446. carla_v3_event_list* eventListPtr = &eventList;
  1447. carla_v3_param_changes paramChanges;
  1448. carla_v3_param_changes* paramChangesPtr = &paramChanges;
  1449. v3_audio_bus_buffers processInputs = { audioIns + cvIns, 0, { bufferAudioIn } };
  1450. v3_audio_bus_buffers processOutputs = { audioOuts + cvOuts, 0, { bufferAudioOut } };
  1451. v3_process_context processContext = {};
  1452. processContext.sample_rate = kSampleRate;
  1453. v3_process_data processData = {
  1454. V3_REALTIME,
  1455. V3_SAMPLE_32,
  1456. kBufferSize,
  1457. audioIns + cvIns,
  1458. audioOuts + cvOuts,
  1459. &processInputs,
  1460. &processOutputs,
  1461. (v3_param_changes**)&paramChangesPtr,
  1462. (v3_param_changes**)&paramChangesPtr,
  1463. (v3_event_list**)&eventListPtr,
  1464. (v3_event_list**)&eventListPtr,
  1465. &processContext
  1466. };
  1467. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(processor)->process(processor, &processData) == V3_OK);
  1468. for (int j=0; j < audioIns + cvIns; ++j)
  1469. delete[] bufferAudioIn[j];
  1470. for (int j=0; j < audioOuts + cvOuts; ++j)
  1471. delete[] bufferAudioOut[j];
  1472. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(processor)->set_processing(processor, false) == V3_OK);
  1473. CARLA_SAFE_ASSERT_BREAK(v3_cpp_obj(component)->set_active(component, false) == V3_OK);
  1474. v3_cpp_obj_unref(processor);
  1475. }
  1476. if (shouldTerminateController)
  1477. v3_cpp_obj_terminate(controller);
  1478. v3_cpp_obj_unref(controller);
  1479. v3_cpp_obj_terminate(component);
  1480. v3_cpp_obj_unref(component);
  1481. DISCOVERY_OUT("init", "-----------");
  1482. DISCOVERY_OUT("build", BINARY_NATIVE);
  1483. DISCOVERY_OUT("hints", hints);
  1484. DISCOVERY_OUT("category", getPluginCategoryAsString(factory2 != nullptr ? getPluginCategoryFromV3SubCategories(classInfo.v2.sub_categories)
  1485. : getPluginCategoryFromName(classInfo.v1.name)));
  1486. DISCOVERY_OUT("name", classInfo.v1.name);
  1487. DISCOVERY_OUT("label", tuid2str(classInfo.v1.class_id));
  1488. DISCOVERY_OUT("maker", (factory2 != nullptr ? classInfo.v2.vendor : factoryInfo.vendor));
  1489. DISCOVERY_OUT("audio.ins", audioIns);
  1490. DISCOVERY_OUT("audio.outs", audioOuts);
  1491. DISCOVERY_OUT("cv.ins", cvIns);
  1492. DISCOVERY_OUT("cv.outs", cvOuts);
  1493. DISCOVERY_OUT("midi.ins", numEventInputBuses);
  1494. DISCOVERY_OUT("midi.outs", numEventOutputBuses);
  1495. DISCOVERY_OUT("parameters.ins", parameterIns);
  1496. DISCOVERY_OUT("parameters.outs", parameterOuts);
  1497. DISCOVERY_OUT("end", "------------");
  1498. }
  1499. // unref interfaces
  1500. if (factory3 != nullptr)
  1501. v3_cpp_obj_unref(factory3);
  1502. if (factory2 != nullptr)
  1503. v3_cpp_obj_unref(factory2);
  1504. v3_cpp_obj_unref(factory1);
  1505. v3_exit();
  1506. }
  1507. #endif // ! USING_JUCE_FOR_VST3
  1508. struct carla_clap_host : clap_host_t {
  1509. carla_clap_host()
  1510. {
  1511. clap_version = CLAP_VERSION;
  1512. host_data = this;
  1513. name = "Carla-Discovery";
  1514. vendor = "falkTX";
  1515. url = "https://kx.studio/carla";
  1516. version = CARLA_VERSION_STRING;
  1517. get_extension = carla_get_extension;
  1518. request_restart = carla_request_restart;
  1519. request_process = carla_request_process;
  1520. request_callback = carla_request_callback;
  1521. }
  1522. static CLAP_ABI const void* carla_get_extension(const clap_host_t* const host, const char* const extension_id)
  1523. {
  1524. carla_stdout("carla_get_extension %p %s", host, extension_id);
  1525. return nullptr;
  1526. }
  1527. static CLAP_ABI void carla_request_restart(const clap_host_t* const host)
  1528. {
  1529. carla_stdout("carla_request_restart %p", host);
  1530. }
  1531. static CLAP_ABI void carla_request_process(const clap_host_t* const host)
  1532. {
  1533. carla_stdout("carla_request_process %p", host);
  1534. }
  1535. static CLAP_ABI void carla_request_callback(const clap_host_t* const host)
  1536. {
  1537. carla_stdout("carla_request_callback %p", host);
  1538. }
  1539. };
  1540. static void do_clap_check(lib_t& libHandle, const char* const filename, const bool doInit)
  1541. {
  1542. const clap_plugin_entry_t* entry = nullptr;
  1543. #ifdef CARLA_OS_MAC
  1544. BundleLoader bundleLoader;
  1545. // if passed filename is not a plugin binary directly, inspect bundle and find one
  1546. if (libHandle == nullptr)
  1547. {
  1548. if (! bundleLoader.load(filename))
  1549. {
  1550. DISCOVERY_OUT("error", "Failed to load CLAP bundle executable");
  1551. return;
  1552. }
  1553. entry = bundleLoader.getSymbol<const clap_plugin_entry_t*>(CFSTR("clap_entry"));
  1554. }
  1555. else
  1556. #endif
  1557. {
  1558. entry = lib_symbol<const clap_plugin_entry_t*>(libHandle, "clap_entry");
  1559. }
  1560. // ensure entry points are available
  1561. if (entry == nullptr || entry->init == nullptr || entry->deinit == nullptr || entry->get_factory == nullptr)
  1562. {
  1563. DISCOVERY_OUT("error", "Not a CLAP plugin");
  1564. return;
  1565. }
  1566. // ensure compatible version
  1567. if (!clap_version_is_compatible(entry->clap_version))
  1568. {
  1569. DISCOVERY_OUT("error", "Incompatible CLAP plugin");
  1570. return;
  1571. }
  1572. const water::String pluginPath(water::File(filename).getParentDirectory().getFullPathName());
  1573. if (!entry->init(pluginPath.toRawUTF8()))
  1574. {
  1575. DISCOVERY_OUT("error", "CLAP plugin failed to initialize");
  1576. return;
  1577. }
  1578. const clap_plugin_factory_t* const factory = static_cast<const clap_plugin_factory_t*>(
  1579. entry->get_factory(CLAP_PLUGIN_FACTORY_ID));
  1580. CARLA_SAFE_ASSERT_RETURN(factory != nullptr
  1581. && factory->get_plugin_count != nullptr
  1582. && factory->get_plugin_descriptor != nullptr
  1583. && factory->create_plugin != nullptr, entry->deinit());
  1584. if (const uint32_t count = factory->get_plugin_count(factory))
  1585. {
  1586. const carla_clap_host host;
  1587. for (uint32_t i=0; i<count; ++i)
  1588. {
  1589. const clap_plugin_descriptor_t* const desc = factory->get_plugin_descriptor(factory, i);
  1590. CARLA_SAFE_ASSERT_CONTINUE(desc != nullptr);
  1591. const clap_plugin_t* const plugin = factory->create_plugin(factory, &host, desc->id);
  1592. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  1593. // FIXME this is not needed per spec, but JUCE-based CLAP plugins crash without it :(
  1594. if (!plugin->init(plugin))
  1595. {
  1596. plugin->destroy(plugin);
  1597. continue;
  1598. }
  1599. uint hints = 0x0;
  1600. uint audioIns = 0;
  1601. uint audioOuts = 0;
  1602. // uint audioTotal = 0;
  1603. uint midiIns = 0;
  1604. uint midiOuts = 0;
  1605. // uint midiTotal = 0;
  1606. uint parametersIns = 0;
  1607. uint parametersOuts = 0;
  1608. // uint parametersTotal = 0;
  1609. PluginCategory category = PLUGIN_CATEGORY_NONE;
  1610. const clap_plugin_audio_ports_t* const audioPorts = static_cast<const clap_plugin_audio_ports_t*>(
  1611. plugin->get_extension(plugin, CLAP_EXT_AUDIO_PORTS));
  1612. const clap_plugin_note_ports_t* const notePorts = static_cast<const clap_plugin_note_ports_t*>(
  1613. plugin->get_extension(plugin, CLAP_EXT_NOTE_PORTS));
  1614. const clap_plugin_params_t* const params = static_cast<const clap_plugin_params_t*>(
  1615. plugin->get_extension(plugin, CLAP_EXT_PARAMS));
  1616. #ifdef CLAP_WINDOW_API_NATIVE
  1617. const clap_plugin_gui_t* const gui = static_cast<const clap_plugin_gui_t*>(
  1618. plugin->get_extension(plugin, CLAP_EXT_GUI));
  1619. #endif
  1620. if (audioPorts != nullptr)
  1621. {
  1622. clap_audio_port_info_t info;
  1623. const uint32_t inPorts = audioPorts->count(plugin, true);
  1624. for (uint32_t j=0; j<inPorts; ++j)
  1625. {
  1626. if (!audioPorts->get(plugin, j, true, &info))
  1627. break;
  1628. audioIns += info.channel_count;
  1629. }
  1630. const uint32_t outPorts = audioPorts->count(plugin, false);
  1631. for (uint32_t j=0; j<outPorts; ++j)
  1632. {
  1633. if (!audioPorts->get(plugin, j, false, &info))
  1634. break;
  1635. audioOuts += info.channel_count;
  1636. }
  1637. // audioTotal = audioIns + audioOuts;
  1638. }
  1639. if (notePorts != nullptr)
  1640. {
  1641. clap_note_port_info_t info;
  1642. const uint32_t inPorts = notePorts->count(plugin, true);
  1643. for (uint32_t j=0; j<inPorts; ++j)
  1644. {
  1645. if (!notePorts->get(plugin, j, true, &info))
  1646. break;
  1647. if (info.supported_dialects & CLAP_NOTE_DIALECT_MIDI)
  1648. ++midiIns;
  1649. }
  1650. const uint32_t outPorts = notePorts->count(plugin, false);
  1651. for (uint32_t j=0; j<outPorts; ++j)
  1652. {
  1653. if (!notePorts->get(plugin, j, false, &info))
  1654. break;
  1655. if (info.supported_dialects & CLAP_NOTE_DIALECT_MIDI)
  1656. ++midiOuts;
  1657. }
  1658. // midiTotal = midiIns + midiOuts;
  1659. }
  1660. if (params != nullptr)
  1661. {
  1662. clap_param_info_t info;
  1663. const uint32_t numParams = params->count(plugin);
  1664. for (uint32_t j=0; j<numParams; ++j)
  1665. {
  1666. if (!params->get_info(plugin, j, &info))
  1667. break;
  1668. if (info.flags & (CLAP_PARAM_IS_HIDDEN|CLAP_PARAM_IS_BYPASS))
  1669. continue;
  1670. if (info.flags & CLAP_PARAM_IS_READONLY)
  1671. ++parametersOuts;
  1672. else
  1673. ++parametersIns;
  1674. }
  1675. // parametersTotal = parametersIns + parametersOuts;
  1676. }
  1677. if (desc->features != nullptr)
  1678. category = getPluginCategoryFromClapFeatures(desc->features);
  1679. if (category == PLUGIN_CATEGORY_SYNTH)
  1680. hints |= PLUGIN_IS_SYNTH;
  1681. #ifdef CLAP_WINDOW_API_NATIVE
  1682. if (gui != nullptr)
  1683. hints |= PLUGIN_HAS_CUSTOM_UI;
  1684. #endif
  1685. if (doInit)
  1686. {
  1687. // -----------------------------------------------------------------------
  1688. // start crash-free plugin test
  1689. // FIXME already initiated before, because broken plugins etc
  1690. // plugin->init(plugin);
  1691. // TODO
  1692. // end crash-free plugin test
  1693. // -----------------------------------------------------------------------
  1694. }
  1695. plugin->destroy(plugin);
  1696. DISCOVERY_OUT("init", "-----------");
  1697. DISCOVERY_OUT("build", BINARY_NATIVE);
  1698. DISCOVERY_OUT("hints", hints);
  1699. DISCOVERY_OUT("category", getPluginCategoryAsString(category));
  1700. DISCOVERY_OUT("name", desc->name);
  1701. DISCOVERY_OUT("label", desc->id);
  1702. DISCOVERY_OUT("maker", desc->vendor);
  1703. DISCOVERY_OUT("audio.ins", audioIns);
  1704. DISCOVERY_OUT("audio.outs", audioOuts);
  1705. DISCOVERY_OUT("midi.ins", midiIns);
  1706. DISCOVERY_OUT("midi.outs", midiOuts);
  1707. DISCOVERY_OUT("parameters.ins", parametersIns);
  1708. DISCOVERY_OUT("parameters.outs", parametersOuts);
  1709. DISCOVERY_OUT("end", "------------");
  1710. }
  1711. }
  1712. entry->deinit();
  1713. }
  1714. #ifdef USING_JUCE
  1715. // -------------------------------------------------------------------------------------------------------------------
  1716. // find all available plugin audio ports
  1717. static void findMaxTotalChannels(juce::AudioProcessor* const filter, int& maxTotalIns, int& maxTotalOuts)
  1718. {
  1719. filter->enableAllBuses();
  1720. const int numInputBuses = filter->getBusCount(true);
  1721. const int numOutputBuses = filter->getBusCount(false);
  1722. if (numInputBuses > 1 || numOutputBuses > 1)
  1723. {
  1724. maxTotalIns = maxTotalOuts = 0;
  1725. for (int i = 0; i < numInputBuses; ++i)
  1726. maxTotalIns += filter->getChannelCountOfBus(true, i);
  1727. for (int i = 0; i < numOutputBuses; ++i)
  1728. maxTotalOuts += filter->getChannelCountOfBus(false, i);
  1729. }
  1730. else
  1731. {
  1732. maxTotalIns = numInputBuses > 0 ? filter->getBus(true, 0)->getMaxSupportedChannels(64) : 0;
  1733. maxTotalOuts = numOutputBuses > 0 ? filter->getBus(false, 0)->getMaxSupportedChannels(64) : 0;
  1734. }
  1735. }
  1736. // -------------------------------------------------------------------------------------------------------------------
  1737. static bool do_juce_check(const char* const filename_, const char* const stype, const bool doInit)
  1738. {
  1739. CARLA_SAFE_ASSERT_RETURN(stype != nullptr && stype[0] != 0, false) // FIXME
  1740. carla_debug("do_juce_check(%s, %s, %s)", filename_, stype, bool2str(doInit));
  1741. CarlaJUCE::initialiseJuce_GUI();
  1742. juce::String filename;
  1743. #ifdef CARLA_OS_WIN
  1744. // Fix for wine usage
  1745. if (juce::File("Z:\\usr\\").isDirectory() && filename_[0] == '/')
  1746. {
  1747. filename = filename_;
  1748. filename.replace("/", "\\");
  1749. filename = "Z:" + filename;
  1750. }
  1751. else
  1752. #endif
  1753. filename = juce::File(filename_).getFullPathName();
  1754. CarlaScopedPointer<juce::AudioPluginFormat> pluginFormat;
  1755. /* */ if (std::strcmp(stype, "VST2") == 0)
  1756. {
  1757. #if JUCE_PLUGINHOST_VST
  1758. pluginFormat = new juce::VSTPluginFormat();
  1759. #else
  1760. DISCOVERY_OUT("error", "VST2 support not available");
  1761. return false;
  1762. #endif
  1763. }
  1764. else if (std::strcmp(stype, "VST3") == 0)
  1765. {
  1766. #if JUCE_PLUGINHOST_VST3
  1767. pluginFormat = new juce::VST3PluginFormat();
  1768. #else
  1769. DISCOVERY_OUT("error", "VST3 support not available");
  1770. return false;
  1771. #endif
  1772. }
  1773. else if (std::strcmp(stype, "AU") == 0)
  1774. {
  1775. #if JUCE_PLUGINHOST_AU
  1776. pluginFormat = new juce::AudioUnitPluginFormat();
  1777. #else
  1778. DISCOVERY_OUT("error", "AU support not available");
  1779. return false;
  1780. #endif
  1781. }
  1782. if (pluginFormat == nullptr)
  1783. {
  1784. DISCOVERY_OUT("error", stype << " support not available");
  1785. return false;
  1786. }
  1787. #ifdef CARLA_OS_WIN
  1788. CARLA_CUSTOM_SAFE_ASSERT_RETURN("Plugin file/folder does not exist", juce::File(filename).exists(), false);
  1789. #endif
  1790. CARLA_SAFE_ASSERT_RETURN(pluginFormat->fileMightContainThisPluginType(filename), false);
  1791. juce::OwnedArray<juce::PluginDescription> results;
  1792. pluginFormat->findAllTypesForFile(results, filename);
  1793. if (results.size() == 0)
  1794. {
  1795. #if defined(CARLA_OS_MAC) && defined(__aarch64__)
  1796. if (std::strcmp(stype, "VST2") == 0 || std::strcmp(stype, "VST3") == 0)
  1797. return true;
  1798. #endif
  1799. DISCOVERY_OUT("error", "No plugins found");
  1800. return false;
  1801. }
  1802. for (juce::PluginDescription **it = results.begin(), **end = results.end(); it != end; ++it)
  1803. {
  1804. juce::PluginDescription* const desc(*it);
  1805. uint hints = 0x0;
  1806. int audioIns = desc->numInputChannels;
  1807. int audioOuts = desc->numOutputChannels;
  1808. int midiIns = 0;
  1809. int midiOuts = 0;
  1810. int parameters = 0;
  1811. if (desc->isInstrument)
  1812. {
  1813. hints |= PLUGIN_IS_SYNTH;
  1814. midiIns = 1;
  1815. }
  1816. if (doInit)
  1817. {
  1818. if (std::unique_ptr<juce::AudioPluginInstance> instance
  1819. = pluginFormat->createInstanceFromDescription(*desc, kSampleRate, kBufferSize))
  1820. {
  1821. CarlaJUCE::idleJuce_GUI();
  1822. findMaxTotalChannels(instance.get(), audioIns, audioOuts);
  1823. instance->refreshParameterList();
  1824. parameters = instance->getParameters().size();
  1825. if (instance->hasEditor())
  1826. hints |= PLUGIN_HAS_CUSTOM_UI;
  1827. if (instance->acceptsMidi())
  1828. midiIns = 1;
  1829. if (instance->producesMidi())
  1830. midiOuts = 1;
  1831. }
  1832. }
  1833. DISCOVERY_OUT("init", "-----------");
  1834. DISCOVERY_OUT("build", BINARY_NATIVE);
  1835. DISCOVERY_OUT("hints", hints);
  1836. DISCOVERY_OUT("category", getPluginCategoryAsString(getPluginCategoryFromName(desc->category.toRawUTF8())));
  1837. DISCOVERY_OUT("name", desc->descriptiveName);
  1838. DISCOVERY_OUT("label", desc->name);
  1839. DISCOVERY_OUT("maker", desc->manufacturerName);
  1840. DISCOVERY_OUT("uniqueId", desc->uniqueId);
  1841. DISCOVERY_OUT("audio.ins", audioIns);
  1842. DISCOVERY_OUT("audio.outs", audioOuts);
  1843. DISCOVERY_OUT("midi.ins", midiIns);
  1844. DISCOVERY_OUT("midi.outs", midiOuts);
  1845. DISCOVERY_OUT("parameters.ins", parameters);
  1846. DISCOVERY_OUT("end", "------------");
  1847. }
  1848. CarlaJUCE::idleJuce_GUI();
  1849. CarlaJUCE::shutdownJuce_GUI();
  1850. return false;
  1851. }
  1852. #endif // USING_JUCE_FOR_VST2
  1853. static void do_fluidsynth_check(const char* const filename, const PluginType type, const bool doInit)
  1854. {
  1855. #ifdef HAVE_FLUIDSYNTH
  1856. const water::File file(filename);
  1857. if (! file.existsAsFile())
  1858. {
  1859. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1860. return;
  1861. }
  1862. if (type == PLUGIN_SF2 && ! fluid_is_soundfont(filename))
  1863. {
  1864. DISCOVERY_OUT("error", "Not a SF2 file");
  1865. return;
  1866. }
  1867. int programs = 0;
  1868. if (doInit)
  1869. {
  1870. fluid_settings_t* const f_settings = new_fluid_settings();
  1871. CARLA_SAFE_ASSERT_RETURN(f_settings != nullptr,);
  1872. fluid_synth_t* const f_synth = new_fluid_synth(f_settings);
  1873. CARLA_SAFE_ASSERT_RETURN(f_synth != nullptr,);
  1874. const int f_id_test = fluid_synth_sfload(f_synth, filename, 0);
  1875. if (f_id_test < 0)
  1876. {
  1877. DISCOVERY_OUT("error", "Failed to load SF2 file");
  1878. return;
  1879. }
  1880. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1881. const int f_id = f_id_test;
  1882. #else
  1883. const uint f_id = static_cast<uint>(f_id_test);
  1884. #endif
  1885. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(f_synth, f_id))
  1886. {
  1887. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1888. fluid_sfont_iteration_start(f_sfont);
  1889. for (; fluid_sfont_iteration_next(f_sfont);)
  1890. ++programs;
  1891. #else
  1892. fluid_preset_t f_preset;
  1893. f_sfont->iteration_start(f_sfont);
  1894. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  1895. ++programs;
  1896. #endif
  1897. }
  1898. delete_fluid_synth(f_synth);
  1899. delete_fluid_settings(f_settings);
  1900. }
  1901. CarlaString name(file.getFileNameWithoutExtension().toRawUTF8());
  1902. CarlaString label(name);
  1903. // 2 channels
  1904. DISCOVERY_OUT("init", "-----------");
  1905. DISCOVERY_OUT("build", BINARY_NATIVE);
  1906. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1907. DISCOVERY_OUT("category", "synth");
  1908. DISCOVERY_OUT("name", name.buffer());
  1909. DISCOVERY_OUT("label", label.buffer());
  1910. DISCOVERY_OUT("audio.outs", 2);
  1911. DISCOVERY_OUT("midi.ins", 1);
  1912. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1913. DISCOVERY_OUT("parameters.outs", 1);
  1914. DISCOVERY_OUT("end", "------------");
  1915. // 16 channels
  1916. if (doInit && (name.isEmpty() || programs <= 1))
  1917. return;
  1918. name += " (16 outputs)";
  1919. DISCOVERY_OUT("init", "-----------");
  1920. DISCOVERY_OUT("build", BINARY_NATIVE);
  1921. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1922. DISCOVERY_OUT("category", "synth");
  1923. DISCOVERY_OUT("name", name.buffer());
  1924. DISCOVERY_OUT("label", label.buffer());
  1925. DISCOVERY_OUT("audio.outs", 32);
  1926. DISCOVERY_OUT("midi.ins", 1);
  1927. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1928. DISCOVERY_OUT("parameters.outs", 1);
  1929. DISCOVERY_OUT("end", "------------");
  1930. #else // HAVE_FLUIDSYNTH
  1931. DISCOVERY_OUT("error", "SF2 support not available");
  1932. return;
  1933. // unused
  1934. (void)filename;
  1935. (void)type;
  1936. (void)doInit;
  1937. #endif
  1938. }
  1939. // -------------------------------------------------------------------------------------------------------------------
  1940. static void do_jsfx_check(const char* const filename, bool doInit)
  1941. {
  1942. #ifdef HAVE_YSFX
  1943. const water::File file(filename);
  1944. ysfx_config_u config(ysfx_config_new());
  1945. ysfx_register_builtin_audio_formats(config.get());
  1946. ysfx_guess_file_roots(config.get(), filename);
  1947. ysfx_set_log_reporter(config.get(), &CarlaJsfxLogging::logErrorsOnly);
  1948. ysfx_u effect(ysfx_new(config.get()));
  1949. uint hints = 0;
  1950. // do not attempt to compile it, because the import path is not known
  1951. (void)doInit;
  1952. if (! ysfx_load_file(effect.get(), filename, 0))
  1953. {
  1954. DISCOVERY_OUT("error", "Cannot read the JSFX header");
  1955. return;
  1956. }
  1957. const char* const name = ysfx_get_name(effect.get());
  1958. // author and category are extracted from the pseudo-tags
  1959. const char* const author = ysfx_get_author(effect.get());
  1960. const CB::PluginCategory category = CarlaJsfxCategories::getFromEffect(effect.get());
  1961. const uint32_t audioIns = ysfx_get_num_inputs(effect.get());
  1962. const uint32_t audioOuts = ysfx_get_num_outputs(effect.get());
  1963. const uint32_t midiIns = 1;
  1964. const uint32_t midiOuts = 1;
  1965. uint32_t parameters = 0;
  1966. for (uint32_t sliderIndex = 0; sliderIndex < ysfx_max_sliders; ++sliderIndex)
  1967. {
  1968. if (ysfx_slider_exists(effect.get(), sliderIndex))
  1969. ++parameters;
  1970. }
  1971. DISCOVERY_OUT("init", "-----------");
  1972. DISCOVERY_OUT("build", BINARY_NATIVE);
  1973. DISCOVERY_OUT("hints", hints);
  1974. DISCOVERY_OUT("category", getPluginCategoryAsString(category));
  1975. DISCOVERY_OUT("name", name);
  1976. DISCOVERY_OUT("maker", author);
  1977. DISCOVERY_OUT("label", filename);
  1978. DISCOVERY_OUT("audio.ins", audioIns);
  1979. DISCOVERY_OUT("audio.outs", audioOuts);
  1980. DISCOVERY_OUT("midi.ins", midiIns);
  1981. DISCOVERY_OUT("midi.outs", midiOuts);
  1982. DISCOVERY_OUT("parameters.ins", parameters);
  1983. DISCOVERY_OUT("end", "------------");
  1984. #else // HAVE_YSFX
  1985. DISCOVERY_OUT("error", "JSFX support not available");
  1986. return;
  1987. // unused
  1988. (void)filename;
  1989. (void)doInit;
  1990. #endif
  1991. }
  1992. // ------------------------------ main entry point ------------------------------
  1993. int main(int argc, char* argv[])
  1994. {
  1995. if (argc != 3)
  1996. {
  1997. carla_stdout("usage: %s <type> </path/to/plugin>", argv[0]);
  1998. return 1;
  1999. }
  2000. const char* const stype = argv[1];
  2001. const char* const filename = argv[2];
  2002. const PluginType type = getPluginTypeFromString(stype);
  2003. CarlaString filenameCheck(filename);
  2004. filenameCheck.toLower();
  2005. bool openLib = false;
  2006. lib_t handle = nullptr;
  2007. switch (type)
  2008. {
  2009. case PLUGIN_LADSPA:
  2010. case PLUGIN_DSSI:
  2011. case PLUGIN_VST2:
  2012. openLib = true;
  2013. break;
  2014. case PLUGIN_VST3:
  2015. case PLUGIN_CLAP:
  2016. openLib = water::File(filename).existsAsFile();
  2017. break;
  2018. default:
  2019. break;
  2020. }
  2021. if (type != PLUGIN_SF2 && filenameCheck.contains("fluidsynth", true))
  2022. {
  2023. DISCOVERY_OUT("info", "skipping fluidsynth based plugin");
  2024. return 0;
  2025. }
  2026. #ifdef CARLA_OS_MAC
  2027. if (type == PLUGIN_VST2 && (filenameCheck.endsWith(".vst") || filenameCheck.endsWith(".vst/")))
  2028. openLib = false;
  2029. #endif
  2030. // ---------------------------------------------------------------------------------------------------------------
  2031. // Initialize OS features
  2032. // we want stuff in English so we can parse error messages
  2033. ::setlocale(LC_ALL, "C");
  2034. #ifndef CARLA_OS_WIN
  2035. carla_setenv("LC_ALL", "C");
  2036. #endif
  2037. #ifdef CARLA_OS_WIN
  2038. OleInitialize(nullptr);
  2039. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  2040. # ifndef __WINPTHREADS_VERSION
  2041. // (non-portable) initialization of statically linked pthread library
  2042. pthread_win32_process_attach_np();
  2043. pthread_win32_thread_attach_np();
  2044. # endif
  2045. #endif
  2046. // ---------------------------------------------------------------------------------------------------------------
  2047. if (openLib)
  2048. {
  2049. handle = lib_open(filename);
  2050. if (handle == nullptr)
  2051. {
  2052. print_lib_error(filename);
  2053. return 1;
  2054. }
  2055. }
  2056. // never do init for dssi-vst, takes too long and it's crashy
  2057. bool doInit = ! filenameCheck.contains("dssi-vst", true);
  2058. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS") != nullptr)
  2059. doInit = false;
  2060. // ---------------------------------------------------------------------------------------------------------------
  2061. if (doInit && openLib && handle != nullptr)
  2062. {
  2063. // test fast loading & unloading DLL without initializing the plugin(s)
  2064. if (! lib_close(handle))
  2065. {
  2066. print_lib_error(filename);
  2067. return 1;
  2068. }
  2069. handle = lib_open(filename);
  2070. if (handle == nullptr)
  2071. {
  2072. print_lib_error(filename);
  2073. return 1;
  2074. }
  2075. }
  2076. #ifndef BUILD_BRIDGE
  2077. if (std::strcmp(filename, ":all") == 0)
  2078. {
  2079. do_cached_check(type);
  2080. return 0;
  2081. }
  2082. #endif
  2083. #ifdef CARLA_OS_MAC
  2084. // Plugin might be in quarentine due to Apple stupid notarization rules, let's remove that if possible
  2085. switch (type)
  2086. {
  2087. case PLUGIN_LADSPA:
  2088. case PLUGIN_DSSI:
  2089. case PLUGIN_VST2:
  2090. case PLUGIN_VST3:
  2091. case PLUGIN_CLAP:
  2092. removeFileFromQuarantine(filename);
  2093. break;
  2094. default:
  2095. break;
  2096. }
  2097. #endif
  2098. #ifdef USING_JUCE
  2099. // some macOS plugins have not been yet ported to arm64, re-run them in x86_64 mode if discovery fails
  2100. bool retryJucePlugin = false;
  2101. #endif
  2102. switch (type)
  2103. {
  2104. case PLUGIN_LADSPA:
  2105. do_ladspa_check(handle, filename, doInit);
  2106. break;
  2107. case PLUGIN_DSSI:
  2108. do_dssi_check(handle, filename, doInit);
  2109. break;
  2110. #ifndef BUILD_BRIDGE
  2111. case PLUGIN_LV2:
  2112. do_lv2_check(filename, doInit);
  2113. break;
  2114. #endif
  2115. case PLUGIN_VST2:
  2116. #if defined(USING_JUCE) && JUCE_PLUGINHOST_VST
  2117. retryJucePlugin = do_juce_check(filename, "VST2", doInit);
  2118. #else
  2119. do_vst2_check(handle, filename, doInit);
  2120. #endif
  2121. break;
  2122. case PLUGIN_VST3:
  2123. #if defined(USING_JUCE) && JUCE_PLUGINHOST_VST3
  2124. retryJucePlugin = do_juce_check(filename, "VST3", doInit);
  2125. #else
  2126. do_vst3_check(handle, filename, doInit);
  2127. #endif
  2128. break;
  2129. case PLUGIN_AU:
  2130. #if defined(USING_JUCE) && JUCE_PLUGINHOST_AU
  2131. do_juce_check(filename, "AU", doInit);
  2132. #else
  2133. DISCOVERY_OUT("error", "AU support not available");
  2134. #endif
  2135. break;
  2136. #ifndef BUILD_BRIDGE
  2137. case PLUGIN_JSFX:
  2138. do_jsfx_check(filename, doInit);
  2139. break;
  2140. #endif
  2141. case PLUGIN_CLAP:
  2142. do_clap_check(handle, filename, doInit);
  2143. break;
  2144. case PLUGIN_DLS:
  2145. case PLUGIN_GIG:
  2146. case PLUGIN_SF2:
  2147. do_fluidsynth_check(filename, type, doInit);
  2148. break;
  2149. default:
  2150. break;
  2151. }
  2152. #if defined(CARLA_OS_MAC) && defined(USING_JUCE) && defined(__aarch64__)
  2153. if (retryJucePlugin)
  2154. {
  2155. DISCOVERY_OUT("warning", "No plugins found while scanning in arm64 mode, will try x86_64 now");
  2156. cpu_type_t pref = CPU_TYPE_X86_64;
  2157. pid_t pid = -1;
  2158. posix_spawnattr_t attr;
  2159. posix_spawnattr_init(&attr);
  2160. CARLA_SAFE_ASSERT_RETURN(posix_spawnattr_setbinpref_np(&attr, 1, &pref, nullptr) == 0, 1);
  2161. CARLA_SAFE_ASSERT_RETURN(posix_spawn(&pid, argv[0], nullptr, &attr, argv, nullptr) == 0, 1);
  2162. posix_spawnattr_destroy(&attr);
  2163. if (pid > 0)
  2164. {
  2165. int status;
  2166. waitpid(pid, &status, 0);
  2167. }
  2168. }
  2169. #endif
  2170. if (openLib && handle != nullptr)
  2171. lib_close(handle);
  2172. // ---------------------------------------------------------------------------------------------------------------
  2173. #ifdef CARLA_OS_WIN
  2174. #ifndef __WINPTHREADS_VERSION
  2175. pthread_win32_thread_detach_np();
  2176. pthread_win32_process_detach_np();
  2177. #endif
  2178. CoUninitialize();
  2179. OleUninitialize();
  2180. #endif
  2181. return 0;
  2182. #ifdef USING_JUCE
  2183. // might be unused
  2184. (void)retryJucePlugin;
  2185. #endif
  2186. }
  2187. // -------------------------------------------------------------------------------------------------------------------