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.

3110 lines
104KB

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