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.

2508 lines
80KB

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