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.

2820 lines
92KB

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