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.

2998 lines
100KB

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