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.

1883 lines
58KB

  1. /*
  2. * Carla Plugin discovery
  3. * Copyright (C) 2011-2020 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaBackendUtils.hpp"
  18. #include "CarlaLibUtils.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "LinkedList.hpp"
  22. #ifdef BUILD_BRIDGE
  23. # undef HAVE_FLUIDSYNTH
  24. #endif
  25. #ifdef USING_JUCE
  26. # if defined(__clang__)
  27. # pragma clang diagnostic push
  28. # pragma clang diagnostic ignored "-Wfloat-equal"
  29. # pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
  30. # elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  31. # pragma GCC diagnostic push
  32. # pragma GCC diagnostic ignored "-Wconversion"
  33. # pragma GCC diagnostic ignored "-Wdouble-promotion"
  34. # pragma GCC diagnostic ignored "-Weffc++"
  35. # pragma GCC diagnostic ignored "-Wfloat-equal"
  36. # endif
  37. # include "../backend/utils/JUCE.cpp"
  38. # include "AppConfig.h"
  39. # include "juce_audio_processors/juce_audio_processors.h"
  40. # if JUCE_PLUGINHOST_VST
  41. # define USING_JUCE_FOR_VST2
  42. # endif
  43. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  44. # pragma GCC diagnostic pop
  45. # endif
  46. #endif
  47. #include "CarlaLadspaUtils.hpp"
  48. #include "CarlaLv2Utils.hpp"
  49. #ifndef USING_JUCE_FOR_VST2
  50. # include "CarlaVst2Utils.hpp"
  51. #endif
  52. #ifdef CARLA_OS_MAC
  53. # define Component CocoaComponent
  54. # define MemoryBlock CocoaMemoryBlock
  55. # define Point CocoaPoint
  56. # import <Foundation/Foundation.h>
  57. # undef Component
  58. # undef MemoryBlock
  59. # undef Point
  60. # include "CarlaMacUtils.cpp"
  61. # include <spawn.h>
  62. # if defined(USING_JUCE) && defined(__aarch64__)
  63. # include <spawn.h>
  64. # endif
  65. #endif
  66. #ifdef CARLA_OS_WIN
  67. # include <pthread.h>
  68. # include <objbase.h>
  69. #endif
  70. #ifdef HAVE_FLUIDSYNTH
  71. # include <fluidsynth.h>
  72. #endif
  73. #include <iostream>
  74. #include "water/files/File.h"
  75. #ifndef BUILD_BRIDGE
  76. # include "water/text/StringArray.h"
  77. # include "CarlaDssiUtils.cpp"
  78. # include "../backend/utils/CachedPlugins.cpp"
  79. #else
  80. # include "CarlaDssiUtils.hpp"
  81. #endif
  82. #define DISCOVERY_OUT(x, y) std::cout << "\ncarla-discovery::" << x << "::" << y << std::endl;
  83. using water::CharPointer_UTF8;
  84. using water::File;
  85. using water::StringArray;
  86. CARLA_BACKEND_USE_NAMESPACE
  87. // -------------------------------------------------------------------------------------------------------------------
  88. // Dummy values to test plugins with
  89. static const uint32_t kBufferSize = 512;
  90. static const double kSampleRate = 44100.0;
  91. static const int32_t kSampleRatei = 44100;
  92. static const float kSampleRatef = 44100.0f;
  93. // -------------------------------------------------------------------------------------------------------------------
  94. // Don't print ELF/EXE related errors since discovery can find multi-architecture binaries
  95. static void print_lib_error(const char* const filename)
  96. {
  97. const char* const error(lib_error(filename));
  98. if (error != nullptr &&
  99. std::strstr(error, "wrong ELF class") == nullptr &&
  100. std::strstr(error, "invalid ELF header") == nullptr &&
  101. std::strstr(error, "Bad EXE format") == nullptr &&
  102. std::strstr(error, "no suitable image found") == nullptr &&
  103. std::strstr(error, "not a valid Win32 application") == nullptr)
  104. {
  105. DISCOVERY_OUT("error", error);
  106. }
  107. }
  108. // ------------------------------ Plugin Checks -----------------------------
  109. #ifndef BUILD_BRIDGE
  110. static void print_cached_plugin(const CarlaCachedPluginInfo* const pinfo)
  111. {
  112. if (! pinfo->valid)
  113. return;
  114. DISCOVERY_OUT("init", "-----------");
  115. DISCOVERY_OUT("build", BINARY_NATIVE);
  116. DISCOVERY_OUT("hints", pinfo->hints);
  117. DISCOVERY_OUT("category", getPluginCategoryAsString(pinfo->category));
  118. DISCOVERY_OUT("name", pinfo->name);
  119. DISCOVERY_OUT("maker", pinfo->maker);
  120. DISCOVERY_OUT("label", pinfo->label);
  121. DISCOVERY_OUT("audio.ins", pinfo->audioIns);
  122. DISCOVERY_OUT("audio.outs", pinfo->audioOuts);
  123. DISCOVERY_OUT("cv.ins", pinfo->cvIns);
  124. DISCOVERY_OUT("cv.outs", pinfo->cvOuts);
  125. DISCOVERY_OUT("midi.ins", pinfo->midiIns);
  126. DISCOVERY_OUT("midi.outs", pinfo->midiOuts);
  127. DISCOVERY_OUT("parameters.ins", pinfo->parameterIns);
  128. DISCOVERY_OUT("parameters.outs", pinfo->parameterOuts);
  129. DISCOVERY_OUT("end", "------------");
  130. }
  131. static void do_cached_check(const PluginType type)
  132. {
  133. const char* plugPath;
  134. switch (type)
  135. {
  136. case PLUGIN_LV2:
  137. plugPath = std::getenv("LV2_PATH");
  138. break;
  139. case PLUGIN_SFZ:
  140. plugPath = std::getenv("SFZ_PATH");
  141. break;
  142. default:
  143. plugPath = nullptr;
  144. break;
  145. }
  146. # ifdef USING_JUCE
  147. if (type == PLUGIN_AU)
  148. carla_juce_init();
  149. # endif
  150. const uint count = carla_get_cached_plugin_count(type, plugPath);
  151. for (uint i=0; i<count; ++i)
  152. {
  153. const CarlaCachedPluginInfo* pinfo(carla_get_cached_plugin_info(type, i));
  154. CARLA_SAFE_ASSERT_CONTINUE(pinfo != nullptr);
  155. print_cached_plugin(pinfo);
  156. }
  157. # ifdef USING_JUCE
  158. if (type == PLUGIN_AU)
  159. carla_juce_cleanup();
  160. # endif
  161. }
  162. #endif
  163. static void do_ladspa_check(lib_t& libHandle, const char* const filename, const bool doInit)
  164. {
  165. LADSPA_Descriptor_Function descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  166. if (descFn == nullptr)
  167. {
  168. DISCOVERY_OUT("error", "Not a LADSPA plugin");
  169. return;
  170. }
  171. const LADSPA_Descriptor* descriptor;
  172. {
  173. descriptor = descFn(0);
  174. if (descriptor == nullptr)
  175. {
  176. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  177. return;
  178. }
  179. if (doInit && descriptor->instantiate != nullptr && descriptor->cleanup != nullptr)
  180. {
  181. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  182. if (handle == nullptr)
  183. {
  184. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  185. return;
  186. }
  187. descriptor->cleanup(handle);
  188. lib_close(libHandle);
  189. libHandle = lib_open(filename);
  190. if (libHandle == nullptr)
  191. {
  192. print_lib_error(filename);
  193. return;
  194. }
  195. descFn = lib_symbol<LADSPA_Descriptor_Function>(libHandle, "ladspa_descriptor");
  196. if (descFn == nullptr)
  197. {
  198. DISCOVERY_OUT("error", "Not a LADSPA plugin (#2)");
  199. return;
  200. }
  201. }
  202. }
  203. unsigned long i = 0;
  204. while ((descriptor = descFn(i++)) != nullptr)
  205. {
  206. if (descriptor->instantiate == nullptr)
  207. {
  208. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no instantiate()");
  209. continue;
  210. }
  211. if (descriptor->cleanup == nullptr)
  212. {
  213. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no cleanup()");
  214. continue;
  215. }
  216. if (descriptor->run == nullptr)
  217. {
  218. DISCOVERY_OUT("error", "Plugin '" << descriptor->Name << "' has no run()");
  219. continue;
  220. }
  221. if (! LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  222. {
  223. DISCOVERY_OUT("warning", "Plugin '" << descriptor->Name << "' is not hard real-time capable");
  224. }
  225. uint hints = 0x0;
  226. uint audioIns = 0;
  227. uint audioOuts = 0;
  228. uint audioTotal = 0;
  229. uint parametersIns = 0;
  230. uint parametersOuts = 0;
  231. uint parametersTotal = 0;
  232. if (LADSPA_IS_HARD_RT_CAPABLE(descriptor->Properties))
  233. hints |= PLUGIN_IS_RTSAFE;
  234. for (unsigned long j=0; j < descriptor->PortCount; ++j)
  235. {
  236. CARLA_ASSERT(descriptor->PortNames[j] != nullptr);
  237. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  238. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  239. {
  240. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  241. audioIns += 1;
  242. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  243. audioOuts += 1;
  244. audioTotal += 1;
  245. }
  246. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  247. {
  248. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  249. parametersIns += 1;
  250. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(descriptor->PortNames[j], "latency") != 0 && std::strcmp(descriptor->PortNames[j], "_latency") != 0)
  251. parametersOuts += 1;
  252. parametersTotal += 1;
  253. }
  254. }
  255. if (doInit)
  256. {
  257. // -----------------------------------------------------------------------
  258. // start crash-free plugin test
  259. LADSPA_Handle handle = descriptor->instantiate(descriptor, kSampleRatei);
  260. if (handle == nullptr)
  261. {
  262. DISCOVERY_OUT("error", "Failed to init LADSPA plugin");
  263. continue;
  264. }
  265. // Test quick init and cleanup
  266. descriptor->cleanup(handle);
  267. handle = descriptor->instantiate(descriptor, kSampleRatei);
  268. if (handle == nullptr)
  269. {
  270. DISCOVERY_OUT("error", "Failed to init LADSPA plugin (#2)");
  271. continue;
  272. }
  273. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  274. LADSPA_Data bufferParams[parametersTotal];
  275. LADSPA_Data min, max, def;
  276. for (unsigned long j=0, iA=0, iC=0; j < descriptor->PortCount; ++j)
  277. {
  278. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  279. const LADSPA_PortRangeHint portRangeHints = descriptor->PortRangeHints[j];
  280. const char* const portName = descriptor->PortNames[j];
  281. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  282. {
  283. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  284. descriptor->connect_port(handle, j, bufferAudio[iA++]);
  285. }
  286. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  287. {
  288. // min value
  289. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  290. min = portRangeHints.LowerBound;
  291. else
  292. min = 0.0f;
  293. // max value
  294. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  295. max = portRangeHints.UpperBound;
  296. else
  297. max = 1.0f;
  298. if (min > max)
  299. {
  300. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  301. max = min + 0.1f;
  302. }
  303. else if (carla_isEqual(min, max))
  304. {
  305. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  306. max = min + 0.1f;
  307. }
  308. // default value
  309. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  310. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  311. {
  312. min *= kSampleRatef;
  313. max *= kSampleRatef;
  314. def *= kSampleRatef;
  315. }
  316. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  317. {
  318. // latency parameter
  319. def = 0.0f;
  320. }
  321. else
  322. {
  323. if (def < min)
  324. def = min;
  325. else if (def > max)
  326. def = max;
  327. }
  328. bufferParams[iC] = def;
  329. descriptor->connect_port(handle, j, &bufferParams[iC++]);
  330. }
  331. }
  332. if (descriptor->activate != nullptr)
  333. descriptor->activate(handle);
  334. descriptor->run(handle, kBufferSize);
  335. if (descriptor->deactivate != nullptr)
  336. descriptor->deactivate(handle);
  337. descriptor->cleanup(handle);
  338. // end crash-free plugin test
  339. // -----------------------------------------------------------------------
  340. }
  341. DISCOVERY_OUT("init", "-----------");
  342. DISCOVERY_OUT("build", BINARY_NATIVE);
  343. DISCOVERY_OUT("hints", hints);
  344. DISCOVERY_OUT("category", getPluginCategoryAsString(getPluginCategoryFromName(descriptor->Name)));
  345. DISCOVERY_OUT("name", descriptor->Name);
  346. DISCOVERY_OUT("label", descriptor->Label);
  347. DISCOVERY_OUT("maker", descriptor->Maker);
  348. DISCOVERY_OUT("uniqueId", descriptor->UniqueID);
  349. DISCOVERY_OUT("audio.ins", audioIns);
  350. DISCOVERY_OUT("audio.outs", audioOuts);
  351. DISCOVERY_OUT("parameters.ins", parametersIns);
  352. DISCOVERY_OUT("parameters.outs", parametersOuts);
  353. DISCOVERY_OUT("end", "------------");
  354. }
  355. }
  356. static void do_dssi_check(lib_t& libHandle, const char* const filename, const bool doInit)
  357. {
  358. DSSI_Descriptor_Function descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  359. if (descFn == nullptr)
  360. {
  361. DISCOVERY_OUT("error", "Not a DSSI plugin");
  362. return;
  363. }
  364. const DSSI_Descriptor* descriptor;
  365. {
  366. descriptor = descFn(0);
  367. if (descriptor == nullptr)
  368. {
  369. DISCOVERY_OUT("error", "Binary doesn't contain any plugins");
  370. return;
  371. }
  372. const LADSPA_Descriptor* const ldescriptor(descriptor->LADSPA_Plugin);
  373. if (ldescriptor == nullptr)
  374. {
  375. DISCOVERY_OUT("error", "DSSI plugin doesn't provide the LADSPA interface");
  376. return;
  377. }
  378. if (doInit && ldescriptor->instantiate != nullptr && ldescriptor->cleanup != nullptr)
  379. {
  380. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  381. if (handle == nullptr)
  382. {
  383. DISCOVERY_OUT("error", "Failed to init first LADSPA plugin");
  384. return;
  385. }
  386. ldescriptor->cleanup(handle);
  387. lib_close(libHandle);
  388. libHandle = lib_open(filename);
  389. if (libHandle == nullptr)
  390. {
  391. print_lib_error(filename);
  392. return;
  393. }
  394. descFn = lib_symbol<DSSI_Descriptor_Function>(libHandle, "dssi_descriptor");
  395. if (descFn == nullptr)
  396. {
  397. DISCOVERY_OUT("error", "Not a DSSI plugin (#2)");
  398. return;
  399. }
  400. }
  401. }
  402. unsigned long i = 0;
  403. while ((descriptor = descFn(i++)) != nullptr)
  404. {
  405. const LADSPA_Descriptor* const ldescriptor = descriptor->LADSPA_Plugin;
  406. if (ldescriptor == nullptr)
  407. {
  408. DISCOVERY_OUT("error", "Plugin has no LADSPA interface");
  409. continue;
  410. }
  411. if (descriptor->DSSI_API_Version != DSSI_VERSION_MAJOR)
  412. {
  413. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' uses an unsupported DSSI spec version " << descriptor->DSSI_API_Version);
  414. continue;
  415. }
  416. if (ldescriptor->instantiate == nullptr)
  417. {
  418. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no instantiate()");
  419. continue;
  420. }
  421. if (ldescriptor->cleanup == nullptr)
  422. {
  423. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no cleanup()");
  424. continue;
  425. }
  426. if (ldescriptor->run == nullptr && descriptor->run_synth == nullptr)
  427. {
  428. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' has no run() or run_synth()");
  429. continue;
  430. }
  431. if (descriptor->run_synth == nullptr && descriptor->run_multiple_synths != nullptr)
  432. {
  433. DISCOVERY_OUT("error", "Plugin '" << ldescriptor->Name << "' requires run_multiple_synths which is not supported");
  434. continue;
  435. }
  436. if (! LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  437. {
  438. DISCOVERY_OUT("warning", "Plugin '" << ldescriptor->Name << "' is not hard real-time capable");
  439. }
  440. uint hints = 0x0;
  441. uint audioIns = 0;
  442. uint audioOuts = 0;
  443. uint audioTotal = 0;
  444. uint midiIns = 0;
  445. uint parametersIns = 0;
  446. uint parametersOuts = 0;
  447. uint parametersTotal = 0;
  448. if (LADSPA_IS_HARD_RT_CAPABLE(ldescriptor->Properties))
  449. hints |= PLUGIN_IS_RTSAFE;
  450. for (unsigned long j=0; j < ldescriptor->PortCount; ++j)
  451. {
  452. CARLA_ASSERT(ldescriptor->PortNames[j] != nullptr);
  453. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  454. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  455. {
  456. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  457. audioIns += 1;
  458. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  459. audioOuts += 1;
  460. audioTotal += 1;
  461. }
  462. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  463. {
  464. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  465. parametersIns += 1;
  466. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && std::strcmp(ldescriptor->PortNames[j], "latency") != 0 && std::strcmp(ldescriptor->PortNames[j], "_latency") != 0)
  467. parametersOuts += 1;
  468. parametersTotal += 1;
  469. }
  470. }
  471. if (descriptor->run_synth != nullptr)
  472. midiIns = 1;
  473. if (midiIns > 0 && audioIns == 0 && audioOuts > 0)
  474. hints |= PLUGIN_IS_SYNTH;
  475. #ifndef BUILD_BRIDGE
  476. if (const char* const ui = find_dssi_ui(filename, ldescriptor->Label))
  477. {
  478. hints |= PLUGIN_HAS_CUSTOM_UI;
  479. delete[] ui;
  480. }
  481. #endif
  482. if (doInit)
  483. {
  484. // -----------------------------------------------------------------------
  485. // start crash-free plugin test
  486. LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  487. if (handle == nullptr)
  488. {
  489. DISCOVERY_OUT("error", "Failed to init DSSI plugin");
  490. continue;
  491. }
  492. // Test quick init and cleanup
  493. ldescriptor->cleanup(handle);
  494. handle = ldescriptor->instantiate(ldescriptor, kSampleRatei);
  495. if (handle == nullptr)
  496. {
  497. DISCOVERY_OUT("error", "Failed to init DSSI plugin (#2)");
  498. continue;
  499. }
  500. LADSPA_Data bufferAudio[kBufferSize][audioTotal];
  501. LADSPA_Data bufferParams[parametersTotal];
  502. LADSPA_Data min, max, def;
  503. for (unsigned long j=0, iA=0, iC=0; j < ldescriptor->PortCount; ++j)
  504. {
  505. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  506. const LADSPA_PortRangeHint portRangeHints = ldescriptor->PortRangeHints[j];
  507. const char* const portName = ldescriptor->PortNames[j];
  508. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  509. {
  510. carla_zeroFloats(bufferAudio[iA], kBufferSize);
  511. ldescriptor->connect_port(handle, j, bufferAudio[iA++]);
  512. }
  513. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  514. {
  515. // min value
  516. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  517. min = portRangeHints.LowerBound;
  518. else
  519. min = 0.0f;
  520. // max value
  521. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  522. max = portRangeHints.UpperBound;
  523. else
  524. max = 1.0f;
  525. if (min > max)
  526. {
  527. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: min > max");
  528. max = min + 0.1f;
  529. }
  530. else if (carla_isEqual(min, max))
  531. {
  532. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max == min");
  533. max = min + 0.1f;
  534. }
  535. // default value
  536. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  537. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  538. {
  539. min *= kSampleRatef;
  540. max *= kSampleRatef;
  541. def *= kSampleRatef;
  542. }
  543. if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && (std::strcmp(portName, "latency") == 0 || std::strcmp(portName, "_latency") == 0))
  544. {
  545. // latency parameter
  546. def = 0.0f;
  547. }
  548. else
  549. {
  550. if (def < min)
  551. def = min;
  552. else if (def > max)
  553. def = max;
  554. }
  555. bufferParams[iC] = def;
  556. ldescriptor->connect_port(handle, j, &bufferParams[iC++]);
  557. }
  558. }
  559. // select first midi-program if available
  560. if (descriptor->get_program != nullptr && descriptor->select_program != nullptr)
  561. {
  562. if (const DSSI_Program_Descriptor* const pDesc = descriptor->get_program(handle, 0))
  563. descriptor->select_program(handle, pDesc->Bank, pDesc->Program);
  564. }
  565. if (ldescriptor->activate != nullptr)
  566. ldescriptor->activate(handle);
  567. if (descriptor->run_synth != nullptr)
  568. {
  569. snd_seq_event_t midiEvents[2];
  570. carla_zeroStructs(midiEvents, 2);
  571. const unsigned long midiEventCount = 2;
  572. midiEvents[0].type = SND_SEQ_EVENT_NOTEON;
  573. midiEvents[0].data.note.note = 64;
  574. midiEvents[0].data.note.velocity = 100;
  575. midiEvents[1].type = SND_SEQ_EVENT_NOTEOFF;
  576. midiEvents[1].data.note.note = 64;
  577. midiEvents[1].data.note.velocity = 0;
  578. midiEvents[1].time.tick = kBufferSize/2;
  579. descriptor->run_synth(handle, kBufferSize, midiEvents, midiEventCount);
  580. }
  581. else
  582. ldescriptor->run(handle, kBufferSize);
  583. if (ldescriptor->deactivate != nullptr)
  584. ldescriptor->deactivate(handle);
  585. ldescriptor->cleanup(handle);
  586. // end crash-free plugin test
  587. // -----------------------------------------------------------------------
  588. }
  589. DISCOVERY_OUT("init", "-----------");
  590. DISCOVERY_OUT("build", BINARY_NATIVE);
  591. DISCOVERY_OUT("category", ((hints & PLUGIN_IS_SYNTH)
  592. ? "synth"
  593. : getPluginCategoryAsString(getPluginCategoryFromName(ldescriptor->Name))));
  594. DISCOVERY_OUT("hints", hints);
  595. DISCOVERY_OUT("name", ldescriptor->Name);
  596. DISCOVERY_OUT("label", ldescriptor->Label);
  597. DISCOVERY_OUT("maker", ldescriptor->Maker);
  598. DISCOVERY_OUT("uniqueId", ldescriptor->UniqueID);
  599. DISCOVERY_OUT("audio.ins", audioIns);
  600. DISCOVERY_OUT("audio.outs", audioOuts);
  601. DISCOVERY_OUT("midi.ins", midiIns);
  602. DISCOVERY_OUT("parameters.ins", parametersIns);
  603. DISCOVERY_OUT("parameters.outs", parametersOuts);
  604. DISCOVERY_OUT("end", "------------");
  605. }
  606. }
  607. #ifndef BUILD_BRIDGE
  608. static void do_lv2_check(const char* const bundle, const bool doInit)
  609. {
  610. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  611. Lilv::Node bundleNode(lv2World.new_file_uri(nullptr, bundle));
  612. CARLA_SAFE_ASSERT_RETURN(bundleNode.is_uri(),);
  613. CarlaString sBundle(bundleNode.as_uri());
  614. if (! sBundle.endsWith("/"))
  615. sBundle += "/";
  616. // Load bundle
  617. lv2World.load_bundle(sBundle);
  618. // Load plugins in this bundle
  619. const Lilv::Plugins lilvPlugins(lv2World.get_all_plugins());
  620. // Get all plugin URIs in this bundle
  621. water::StringArray URIs;
  622. LILV_FOREACH(plugins, it, lilvPlugins)
  623. {
  624. Lilv::Plugin lilvPlugin(lilv_plugins_get(lilvPlugins, it));
  625. if (const char* const uri = lilvPlugin.get_uri().as_string())
  626. URIs.addIfNotAlreadyThere(water::String(uri));
  627. }
  628. if (URIs.size() == 0)
  629. {
  630. DISCOVERY_OUT("warning", "LV2 Bundle doesn't provide any plugins");
  631. return;
  632. }
  633. // Get & check every plugin-instance
  634. for (int i=0, count=URIs.size(); i < count; ++i)
  635. {
  636. const char* const URI = URIs[i].toRawUTF8();
  637. CarlaScopedPointer<const LV2_RDF_Descriptor> rdfDescriptor(lv2_rdf_new(URI, false));
  638. if (rdfDescriptor == nullptr || rdfDescriptor->URI == nullptr)
  639. {
  640. DISCOVERY_OUT("error", "Failed to find LV2 plugin '" << URI << "'");
  641. continue;
  642. }
  643. if (doInit)
  644. {
  645. // test if lib is loadable, twice
  646. const lib_t libHandle1 = lib_open(rdfDescriptor->Binary);
  647. if (libHandle1 == nullptr)
  648. {
  649. print_lib_error(rdfDescriptor->Binary);
  650. delete rdfDescriptor;
  651. continue;
  652. }
  653. lib_close(libHandle1);
  654. const lib_t libHandle2 = lib_open(rdfDescriptor->Binary);
  655. if (libHandle2 == nullptr)
  656. {
  657. print_lib_error(rdfDescriptor->Binary);
  658. delete rdfDescriptor;
  659. continue;
  660. }
  661. lib_close(libHandle2);
  662. }
  663. const LilvPlugin* const cPlugin(lv2World.getPluginFromURI(URI));
  664. CARLA_SAFE_ASSERT_CONTINUE(cPlugin != nullptr);
  665. Lilv::Plugin lilvPlugin(cPlugin);
  666. CARLA_SAFE_ASSERT_CONTINUE(lilvPlugin.get_uri().is_uri());
  667. print_cached_plugin(get_cached_plugin_lv2(lv2World, lilvPlugin));
  668. }
  669. }
  670. #endif
  671. #ifndef USING_JUCE_FOR_VST2
  672. // -------------------------------------------------------------------------------------------------------------------
  673. // VST stuff
  674. // Check if plugin is currently processing
  675. static bool gVstIsProcessing = false;
  676. // Check if plugin needs idle
  677. static bool gVstNeedsIdle = false;
  678. // Check if plugin wants midi
  679. static bool gVstWantsMidi = false;
  680. // Check if plugin wants time
  681. static bool gVstWantsTime = false;
  682. // Current uniqueId for VST shell plugins
  683. static intptr_t gVstCurrentUniqueId = 0;
  684. // Supported Carla features
  685. static intptr_t vstHostCanDo(const char* const feature)
  686. {
  687. carla_debug("vstHostCanDo(\"%s\")", feature);
  688. if (std::strcmp(feature, "supplyIdle") == 0)
  689. return 1;
  690. if (std::strcmp(feature, "sendVstEvents") == 0)
  691. return 1;
  692. if (std::strcmp(feature, "sendVstMidiEvent") == 0)
  693. return 1;
  694. if (std::strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  695. return 1;
  696. if (std::strcmp(feature, "sendVstTimeInfo") == 0)
  697. {
  698. gVstWantsTime = true;
  699. return 1;
  700. }
  701. if (std::strcmp(feature, "receiveVstEvents") == 0)
  702. return 1;
  703. if (std::strcmp(feature, "receiveVstMidiEvent") == 0)
  704. return 1;
  705. if (std::strcmp(feature, "receiveVstTimeInfo") == 0)
  706. return -1;
  707. if (std::strcmp(feature, "reportConnectionChanges") == 0)
  708. return -1;
  709. if (std::strcmp(feature, "acceptIOChanges") == 0)
  710. return 1;
  711. if (std::strcmp(feature, "sizeWindow") == 0)
  712. return 1;
  713. if (std::strcmp(feature, "offline") == 0)
  714. return -1;
  715. if (std::strcmp(feature, "openFileSelector") == 0)
  716. return -1;
  717. if (std::strcmp(feature, "closeFileSelector") == 0)
  718. return -1;
  719. if (std::strcmp(feature, "startStopProcess") == 0)
  720. return 1;
  721. if (std::strcmp(feature, "supportShell") == 0)
  722. return 1;
  723. if (std::strcmp(feature, "shellCategory") == 0)
  724. return 1;
  725. if (std::strcmp(feature, "NIMKPIVendorSpecificCallbacks") == 0)
  726. return -1;
  727. // non-official features found in some plugins:
  728. // "asyncProcessing"
  729. // "editFile"
  730. // unimplemented
  731. carla_stderr("vstHostCanDo(\"%s\") - unknown feature", feature);
  732. return 0;
  733. }
  734. // Host-side callback
  735. 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)
  736. {
  737. carla_debug("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)",
  738. effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, static_cast<double>(opt));
  739. static VstTimeInfo timeInfo;
  740. intptr_t ret = 0;
  741. switch (opcode)
  742. {
  743. case audioMasterAutomate:
  744. ret = 1;
  745. break;
  746. case audioMasterVersion:
  747. ret = kVstVersion;
  748. break;
  749. case audioMasterCurrentId:
  750. ret = gVstCurrentUniqueId;
  751. break;
  752. case DECLARE_VST_DEPRECATED(audioMasterWantMidi):
  753. if (gVstWantsMidi) { DISCOVERY_OUT("warning", "Plugin requested MIDI more than once"); }
  754. gVstWantsMidi = true;
  755. ret = 1;
  756. break;
  757. case audioMasterGetTime:
  758. if (! gVstIsProcessing) { DISCOVERY_OUT("warning", "Plugin requested timeInfo out of process"); }
  759. if (! gVstWantsTime) { DISCOVERY_OUT("warning", "Plugin requested timeInfo but didn't ask if host could do \"sendVstTimeInfo\""); }
  760. carla_zeroStruct(timeInfo);
  761. timeInfo.sampleRate = kSampleRate;
  762. // Tempo
  763. timeInfo.tempo = 120.0;
  764. timeInfo.flags |= kVstTempoValid;
  765. // Time Signature
  766. timeInfo.timeSigNumerator = 4;
  767. timeInfo.timeSigDenominator = 4;
  768. timeInfo.flags |= kVstTimeSigValid;
  769. ret = (intptr_t)&timeInfo;
  770. break;
  771. case DECLARE_VST_DEPRECATED(audioMasterTempoAt):
  772. ret = 120 * 10000;
  773. break;
  774. case DECLARE_VST_DEPRECATED(audioMasterGetNumAutomatableParameters):
  775. ret = carla_minPositive(effect->numParams, static_cast<int>(MAX_DEFAULT_PARAMETERS));
  776. break;
  777. case DECLARE_VST_DEPRECATED(audioMasterGetParameterQuantization):
  778. ret = 1; // full single float precision
  779. break;
  780. case DECLARE_VST_DEPRECATED(audioMasterNeedIdle):
  781. if (gVstNeedsIdle) { DISCOVERY_OUT("warning", "Plugin requested idle more than once"); }
  782. gVstNeedsIdle = true;
  783. ret = 1;
  784. break;
  785. case audioMasterGetSampleRate:
  786. ret = kSampleRatei;
  787. break;
  788. case audioMasterGetBlockSize:
  789. ret = kBufferSize;
  790. break;
  791. case DECLARE_VST_DEPRECATED(audioMasterWillReplaceOrAccumulate):
  792. ret = 1; // replace
  793. break;
  794. case audioMasterGetCurrentProcessLevel:
  795. ret = gVstIsProcessing ? kVstProcessLevelRealtime : kVstProcessLevelUser;
  796. break;
  797. case audioMasterGetAutomationState:
  798. ret = kVstAutomationOff;
  799. break;
  800. case audioMasterGetVendorString:
  801. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  802. std::strcpy((char*)ptr, "falkTX");
  803. ret = 1;
  804. break;
  805. case audioMasterGetProductString:
  806. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  807. std::strcpy((char*)ptr, "Carla-Discovery");
  808. ret = 1;
  809. break;
  810. case audioMasterGetVendorVersion:
  811. ret = CARLA_VERSION_HEX;
  812. break;
  813. case audioMasterCanDo:
  814. CARLA_SAFE_ASSERT_BREAK(ptr != nullptr);
  815. ret = vstHostCanDo((const char*)ptr);
  816. break;
  817. case audioMasterGetLanguage:
  818. ret = kVstLangEnglish;
  819. break;
  820. default:
  821. carla_stdout("vstHostCallback(%p, %i:%s, %i, " P_INTPTR ", %p, %f)",
  822. effect, opcode, vstMasterOpcode2str(opcode), index, value, ptr, static_cast<double>(opt));
  823. break;
  824. }
  825. return ret;
  826. }
  827. static void do_vst_check(lib_t& libHandle, const char* const filename, const bool doInit)
  828. {
  829. VST_Function vstFn = nullptr;
  830. #ifdef CARLA_OS_MAC
  831. CFBundleRef bundleRef = nullptr;
  832. CFBundleRefNum resFileId = 0;
  833. if (libHandle == nullptr)
  834. {
  835. const CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)filename, (CFIndex)strlen(filename), true);
  836. CARLA_SAFE_ASSERT_RETURN(urlRef != nullptr,);
  837. bundleRef = CFBundleCreate(kCFAllocatorDefault, urlRef);
  838. CFRelease(urlRef);
  839. CARLA_SAFE_ASSERT_RETURN(bundleRef != nullptr,);
  840. if (! CFBundleLoadExecutable(bundleRef))
  841. {
  842. CFRelease(bundleRef);
  843. DISCOVERY_OUT("error", "Failed to load VST bundle executable");
  844. return;
  845. }
  846. vstFn = (VST_Function)CFBundleGetFunctionPointerForName(bundleRef, CFSTR("main_macho"));
  847. if (vstFn == nullptr)
  848. vstFn = (VST_Function)CFBundleGetFunctionPointerForName(bundleRef, CFSTR("VSTPluginMain"));
  849. if (vstFn == nullptr)
  850. {
  851. CFBundleUnloadExecutable(bundleRef);
  852. CFRelease(bundleRef);
  853. DISCOVERY_OUT("error", "Not a VST plugin");
  854. return;
  855. }
  856. resFileId = CFBundleOpenBundleResourceMap(bundleRef);
  857. }
  858. else
  859. #endif
  860. {
  861. vstFn = lib_symbol<VST_Function>(libHandle, "VSTPluginMain");
  862. if (vstFn == nullptr)
  863. {
  864. vstFn = lib_symbol<VST_Function>(libHandle, "main");
  865. if (vstFn == nullptr)
  866. {
  867. DISCOVERY_OUT("error", "Not a VST plugin");
  868. return;
  869. }
  870. }
  871. }
  872. AEffect* effect = vstFn(vstHostCallback);
  873. if (effect == nullptr || effect->magic != kEffectMagic)
  874. {
  875. DISCOVERY_OUT("error", "Failed to init VST plugin, or VST magic failed");
  876. return;
  877. }
  878. if (effect->uniqueID == 0)
  879. {
  880. DISCOVERY_OUT("warning", "Plugin doesn't have an Unique ID when first loaded");
  881. }
  882. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdentify), 0, 0, nullptr, 0.0f);
  883. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effSetBlockSizeAndSampleRate), 0, kBufferSize, nullptr, kSampleRatef);
  884. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, kSampleRatef);
  885. effect->dispatcher(effect, effSetBlockSize, 0, kBufferSize, nullptr, 0.0f);
  886. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  887. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  888. if (effect->numPrograms > 0)
  889. effect->dispatcher(effect, effSetProgram, 0, 0, nullptr, 0.0f);
  890. const bool isShell = (effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f) == kPlugCategShell);
  891. if (effect->uniqueID == 0 && !isShell)
  892. {
  893. DISCOVERY_OUT("error", "Plugin doesn't have an Unique ID after being open");
  894. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  895. return;
  896. }
  897. gVstCurrentUniqueId = effect->uniqueID;
  898. char strBuf[STR_MAX+1];
  899. CarlaString cName;
  900. CarlaString cProduct;
  901. CarlaString cVendor;
  902. PluginCategory category;
  903. LinkedList<intptr_t> uniqueIds;
  904. if (isShell)
  905. {
  906. for (;;)
  907. {
  908. carla_zeroChars(strBuf, STR_MAX+1);
  909. gVstCurrentUniqueId = effect->dispatcher(effect, effShellGetNextPlugin, 0, 0, strBuf, 0.0f);
  910. if (gVstCurrentUniqueId == 0)
  911. break;
  912. uniqueIds.append(gVstCurrentUniqueId);
  913. }
  914. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  915. effect = nullptr;
  916. }
  917. else
  918. {
  919. uniqueIds.append(gVstCurrentUniqueId);
  920. }
  921. for (LinkedList<intptr_t>::Itenerator it = uniqueIds.begin2(); it.valid(); it.next())
  922. {
  923. gVstCurrentUniqueId = it.getValue(0);
  924. if (effect == nullptr)
  925. {
  926. effect = vstFn(vstHostCallback);
  927. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdentify), 0, 0, nullptr, 0.0f);
  928. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effSetBlockSizeAndSampleRate), 0, kBufferSize, nullptr, kSampleRatef);
  929. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, kSampleRatef);
  930. effect->dispatcher(effect, effSetBlockSize, 0, kBufferSize, nullptr, 0.0f);
  931. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  932. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  933. if (effect->numPrograms > 0)
  934. effect->dispatcher(effect, effSetProgram, 0, 0, nullptr, 0.0f);
  935. }
  936. // get name
  937. carla_zeroChars(strBuf, STR_MAX+1);
  938. if (effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f) == 1)
  939. cName = strBuf;
  940. else
  941. cName.clear();
  942. // get product
  943. carla_zeroChars(strBuf, STR_MAX+1);
  944. if (effect->dispatcher(effect, effGetProductString, 0, 0, strBuf, 0.0f) == 1)
  945. cProduct = strBuf;
  946. else
  947. cProduct.clear();
  948. // get vendor
  949. carla_zeroChars(strBuf, STR_MAX+1);
  950. if (effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f) == 1)
  951. cVendor = strBuf;
  952. else
  953. cVendor.clear();
  954. // get category
  955. switch (effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f))
  956. {
  957. case kPlugCategSynth:
  958. category = PLUGIN_CATEGORY_SYNTH;
  959. break;
  960. case kPlugCategAnalysis:
  961. category = PLUGIN_CATEGORY_UTILITY;
  962. break;
  963. case kPlugCategMastering:
  964. category = PLUGIN_CATEGORY_DYNAMICS;
  965. break;
  966. case kPlugCategRoomFx:
  967. category = PLUGIN_CATEGORY_DELAY;
  968. break;
  969. case kPlugCategRestoration:
  970. category = PLUGIN_CATEGORY_UTILITY;
  971. break;
  972. case kPlugCategGenerator:
  973. category = PLUGIN_CATEGORY_SYNTH;
  974. break;
  975. default:
  976. if (effect->flags & effFlagsIsSynth)
  977. category = PLUGIN_CATEGORY_SYNTH;
  978. else
  979. category = PLUGIN_CATEGORY_NONE;
  980. break;
  981. }
  982. // get everything else
  983. uint hints = 0x0;
  984. uint audioIns = static_cast<uint>(std::max(0, effect->numInputs));
  985. uint audioOuts = static_cast<uint>(std::max(0, effect->numOutputs));
  986. uint midiIns = 0;
  987. uint midiOuts = 0;
  988. uint parameters = static_cast<uint>(std::max(0, effect->numParams));
  989. if (effect->flags & effFlagsHasEditor)
  990. hints |= PLUGIN_HAS_CUSTOM_UI;
  991. if (effect->flags & effFlagsIsSynth)
  992. {
  993. hints |= PLUGIN_IS_SYNTH;
  994. midiIns = 1;
  995. }
  996. if (vstPluginCanDo(effect, "receiveVstEvents") || vstPluginCanDo(effect, "receiveVstMidiEvent") || (effect->flags & effFlagsIsSynth) != 0)
  997. midiIns = 1;
  998. if (vstPluginCanDo(effect, "sendVstEvents") || vstPluginCanDo(effect, "sendVstMidiEvent"))
  999. midiOuts = 1;
  1000. // -----------------------------------------------------------------------
  1001. // start crash-free plugin test
  1002. if (doInit)
  1003. {
  1004. if (gVstNeedsIdle)
  1005. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1006. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  1007. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  1008. if (gVstNeedsIdle)
  1009. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1010. // Plugin might call wantMidi() during resume
  1011. if (midiIns == 0 && gVstWantsMidi)
  1012. {
  1013. midiIns = 1;
  1014. }
  1015. float* bufferAudioIn[std::max(1U, audioIns)];
  1016. float* bufferAudioOut[std::max(1U, audioOuts)];
  1017. if (audioIns == 0)
  1018. {
  1019. bufferAudioIn[0] = nullptr;
  1020. }
  1021. else
  1022. {
  1023. for (uint j=0; j < audioIns; ++j)
  1024. {
  1025. bufferAudioIn[j] = new float[kBufferSize];
  1026. carla_zeroFloats(bufferAudioIn[j], kBufferSize);
  1027. }
  1028. }
  1029. if (audioOuts == 0)
  1030. {
  1031. bufferAudioOut[0] = nullptr;
  1032. }
  1033. else
  1034. {
  1035. for (uint j=0; j < audioOuts; ++j)
  1036. {
  1037. bufferAudioOut[j] = new float[kBufferSize];
  1038. carla_zeroFloats(bufferAudioOut[j], kBufferSize);
  1039. }
  1040. }
  1041. struct VstEventsFixed {
  1042. int32_t numEvents;
  1043. intptr_t reserved;
  1044. VstEvent* data[2];
  1045. VstEventsFixed()
  1046. : numEvents(0),
  1047. reserved(0)
  1048. {
  1049. data[0] = data[1] = nullptr;
  1050. }
  1051. } events;
  1052. VstMidiEvent midiEvents[2];
  1053. carla_zeroStructs(midiEvents, 2);
  1054. midiEvents[0].type = kVstMidiType;
  1055. midiEvents[0].byteSize = sizeof(VstMidiEvent);
  1056. midiEvents[0].midiData[0] = char(MIDI_STATUS_NOTE_ON);
  1057. midiEvents[0].midiData[1] = 64;
  1058. midiEvents[0].midiData[2] = 100;
  1059. midiEvents[1].type = kVstMidiType;
  1060. midiEvents[1].byteSize = sizeof(VstMidiEvent);
  1061. midiEvents[1].midiData[0] = char(MIDI_STATUS_NOTE_OFF);
  1062. midiEvents[1].midiData[1] = 64;
  1063. midiEvents[1].deltaFrames = kBufferSize/2;
  1064. events.numEvents = 2;
  1065. events.data[0] = (VstEvent*)&midiEvents[0];
  1066. events.data[1] = (VstEvent*)&midiEvents[1];
  1067. // processing
  1068. gVstIsProcessing = true;
  1069. if (midiIns > 0)
  1070. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  1071. if ((effect->flags & effFlagsCanReplacing) > 0 && effect->processReplacing != nullptr && effect->processReplacing != effect->DECLARE_VST_DEPRECATED(process))
  1072. effect->processReplacing(effect, bufferAudioIn, bufferAudioOut, kBufferSize);
  1073. else if (effect->DECLARE_VST_DEPRECATED(process) != nullptr)
  1074. effect->DECLARE_VST_DEPRECATED(process)(effect, bufferAudioIn, bufferAudioOut, kBufferSize);
  1075. else
  1076. DISCOVERY_OUT("error", "Plugin doesn't have a process function");
  1077. gVstIsProcessing = false;
  1078. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  1079. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  1080. if (gVstNeedsIdle)
  1081. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1082. for (uint j=0; j < audioIns; ++j)
  1083. delete[] bufferAudioIn[j];
  1084. for (uint j=0; j < audioOuts; ++j)
  1085. delete[] bufferAudioOut[j];
  1086. }
  1087. // end crash-free plugin test
  1088. // -----------------------------------------------------------------------
  1089. DISCOVERY_OUT("init", "-----------");
  1090. DISCOVERY_OUT("build", BINARY_NATIVE);
  1091. DISCOVERY_OUT("hints", hints);
  1092. DISCOVERY_OUT("category", getPluginCategoryAsString(category));
  1093. DISCOVERY_OUT("name", cName.buffer());
  1094. DISCOVERY_OUT("label", cProduct.buffer());
  1095. DISCOVERY_OUT("maker", cVendor.buffer());
  1096. DISCOVERY_OUT("uniqueId", gVstCurrentUniqueId);
  1097. DISCOVERY_OUT("audio.ins", audioIns);
  1098. DISCOVERY_OUT("audio.outs", audioOuts);
  1099. DISCOVERY_OUT("midi.ins", midiIns);
  1100. DISCOVERY_OUT("midi.outs", midiOuts);
  1101. DISCOVERY_OUT("parameters.ins", parameters);
  1102. DISCOVERY_OUT("end", "------------");
  1103. gVstWantsMidi = false;
  1104. gVstWantsTime = false;
  1105. if (! isShell)
  1106. break;
  1107. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1108. effect = nullptr;
  1109. }
  1110. uniqueIds.clear();
  1111. if (effect != nullptr)
  1112. {
  1113. if (gVstNeedsIdle)
  1114. effect->dispatcher(effect, DECLARE_VST_DEPRECATED(effIdle), 0, 0, nullptr, 0.0f);
  1115. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  1116. }
  1117. #ifdef CARLA_OS_MAC
  1118. if (bundleRef != nullptr)
  1119. {
  1120. CFBundleCloseBundleResourceMap(bundleRef, resFileId);
  1121. CFBundleUnloadExecutable(bundleRef);
  1122. CFRelease(bundleRef);
  1123. }
  1124. #else
  1125. return;
  1126. // unused
  1127. (void)filename;
  1128. #endif
  1129. }
  1130. #endif // ! USING_JUCE_FOR_VST2
  1131. #ifdef USING_JUCE
  1132. // -------------------------------------------------------------------------------------------------------------------
  1133. // find all available plugin audio ports
  1134. static void findMaxTotalChannels(juce::AudioProcessor* const filter, int& maxTotalIns, int& maxTotalOuts)
  1135. {
  1136. filter->enableAllBuses();
  1137. int numInputBuses = filter->getBusCount(true);
  1138. int numOutputBuses = filter->getBusCount(false);
  1139. if (numInputBuses > 1 || numOutputBuses > 1)
  1140. {
  1141. maxTotalIns = maxTotalOuts = 0;
  1142. for (int i = 0; i < numInputBuses; ++i)
  1143. maxTotalIns += filter->getChannelCountOfBus(true, i);
  1144. for (int i = 0; i < numOutputBuses; ++i)
  1145. maxTotalOuts += filter->getChannelCountOfBus(false, i);
  1146. }
  1147. else
  1148. {
  1149. maxTotalIns = numInputBuses > 0 ? filter->getBus(true, 0)->getMaxSupportedChannels(64) : 0;
  1150. maxTotalOuts = numOutputBuses > 0 ? filter->getBus(false, 0)->getMaxSupportedChannels(64) : 0;
  1151. }
  1152. }
  1153. // -------------------------------------------------------------------------------------------------------------------
  1154. static bool do_juce_check(const char* const filename_, const char* const stype, const bool doInit)
  1155. {
  1156. CARLA_SAFE_ASSERT_RETURN(stype != nullptr && stype[0] != 0, false) // FIXME
  1157. carla_debug("do_juce_check(%s, %s, %s)", filename_, stype, bool2str(doInit));
  1158. carla_juce_init();
  1159. juce::String filename;
  1160. #ifdef CARLA_OS_WIN
  1161. // Fix for wine usage
  1162. if (juce::File("Z:\\usr\\").isDirectory() && filename_[0] == '/')
  1163. {
  1164. filename = filename_;
  1165. filename.replace("/", "\\");
  1166. filename = "Z:" + filename;
  1167. }
  1168. else
  1169. #endif
  1170. filename = juce::File(filename_).getFullPathName();
  1171. CarlaScopedPointer<juce::AudioPluginFormat> pluginFormat;
  1172. /* */ if (std::strcmp(stype, "VST2") == 0)
  1173. {
  1174. #if JUCE_PLUGINHOST_VST
  1175. pluginFormat = new juce::VSTPluginFormat();
  1176. #else
  1177. DISCOVERY_OUT("error", "VST2 support not available");
  1178. return false;
  1179. #endif
  1180. }
  1181. else if (std::strcmp(stype, "VST3") == 0)
  1182. {
  1183. #if JUCE_PLUGINHOST_VST3
  1184. pluginFormat = new juce::VST3PluginFormat();
  1185. #else
  1186. DISCOVERY_OUT("error", "VST3 support not available");
  1187. return false;
  1188. #endif
  1189. }
  1190. else if (std::strcmp(stype, "AU") == 0)
  1191. {
  1192. #if JUCE_PLUGINHOST_AU
  1193. pluginFormat = new juce::AudioUnitPluginFormat();
  1194. #else
  1195. DISCOVERY_OUT("error", "AU support not available");
  1196. return false;
  1197. #endif
  1198. }
  1199. if (pluginFormat == nullptr)
  1200. {
  1201. DISCOVERY_OUT("error", stype << " support not available");
  1202. return false;
  1203. }
  1204. #ifdef CARLA_OS_WIN
  1205. CARLA_CUSTOM_SAFE_ASSERT_RETURN("Plugin file/folder does not exist", juce::File(filename).exists(), false);
  1206. #endif
  1207. CARLA_SAFE_ASSERT_RETURN(pluginFormat->fileMightContainThisPluginType(filename), false);
  1208. juce::OwnedArray<juce::PluginDescription> results;
  1209. pluginFormat->findAllTypesForFile(results, filename);
  1210. if (results.size() == 0)
  1211. {
  1212. #if defined(CARLA_OS_MAC) && defined(__aarch64__)
  1213. if (std::strcmp(stype, "VST2") == 0 || std::strcmp(stype, "VST3") == 0)
  1214. return true;
  1215. #endif
  1216. DISCOVERY_OUT("error", "No plugins found");
  1217. return false;
  1218. }
  1219. for (juce::PluginDescription **it = results.begin(), **end = results.end(); it != end; ++it)
  1220. {
  1221. juce::PluginDescription* const desc(*it);
  1222. uint hints = 0x0;
  1223. int audioIns = desc->numInputChannels;
  1224. int audioOuts = desc->numOutputChannels;
  1225. int midiIns = 0;
  1226. int midiOuts = 0;
  1227. int parameters = 0;
  1228. if (desc->isInstrument)
  1229. {
  1230. hints |= PLUGIN_IS_SYNTH;
  1231. midiIns = 1;
  1232. }
  1233. if (doInit)
  1234. {
  1235. if (std::unique_ptr<juce::AudioPluginInstance> instance
  1236. = pluginFormat->createInstanceFromDescription(*desc, kSampleRate, kBufferSize))
  1237. {
  1238. carla_juce_idle();
  1239. findMaxTotalChannels(instance.get(), audioIns, audioOuts);
  1240. instance->refreshParameterList();
  1241. parameters = instance->getParameters().size();
  1242. if (instance->hasEditor())
  1243. hints |= PLUGIN_HAS_CUSTOM_UI;
  1244. if (instance->acceptsMidi())
  1245. midiIns = 1;
  1246. if (instance->producesMidi())
  1247. midiOuts = 1;
  1248. }
  1249. }
  1250. DISCOVERY_OUT("init", "-----------");
  1251. DISCOVERY_OUT("build", BINARY_NATIVE);
  1252. DISCOVERY_OUT("hints", hints);
  1253. DISCOVERY_OUT("category", getPluginCategoryAsString(getPluginCategoryFromName(desc->category.toRawUTF8())));
  1254. DISCOVERY_OUT("name", desc->descriptiveName);
  1255. DISCOVERY_OUT("label", desc->name);
  1256. DISCOVERY_OUT("maker", desc->manufacturerName);
  1257. DISCOVERY_OUT("uniqueId", desc->uid);
  1258. DISCOVERY_OUT("audio.ins", audioIns);
  1259. DISCOVERY_OUT("audio.outs", audioOuts);
  1260. DISCOVERY_OUT("midi.ins", midiIns);
  1261. DISCOVERY_OUT("midi.outs", midiOuts);
  1262. DISCOVERY_OUT("parameters.ins", parameters);
  1263. DISCOVERY_OUT("end", "------------");
  1264. }
  1265. carla_juce_idle();
  1266. carla_juce_cleanup();
  1267. return false;
  1268. }
  1269. #endif // USING_JUCE_FOR_VST2
  1270. static void do_fluidsynth_check(const char* const filename, const PluginType type, const bool doInit)
  1271. {
  1272. #ifdef HAVE_FLUIDSYNTH
  1273. const water::String jfilename = water::String(water::CharPointer_UTF8(filename));
  1274. const water::File file(jfilename);
  1275. if (! file.existsAsFile())
  1276. {
  1277. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1278. return;
  1279. }
  1280. if (type == PLUGIN_SF2 && ! fluid_is_soundfont(filename))
  1281. {
  1282. DISCOVERY_OUT("error", "Not a SF2 file");
  1283. return;
  1284. }
  1285. int programs = 0;
  1286. if (doInit)
  1287. {
  1288. fluid_settings_t* const f_settings = new_fluid_settings();
  1289. CARLA_SAFE_ASSERT_RETURN(f_settings != nullptr,);
  1290. fluid_synth_t* const f_synth = new_fluid_synth(f_settings);
  1291. CARLA_SAFE_ASSERT_RETURN(f_synth != nullptr,);
  1292. const int f_id_test = fluid_synth_sfload(f_synth, filename, 0);
  1293. if (f_id_test < 0)
  1294. {
  1295. DISCOVERY_OUT("error", "Failed to load SF2 file");
  1296. return;
  1297. }
  1298. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1299. const int f_id = f_id_test;
  1300. #else
  1301. const uint f_id = static_cast<uint>(f_id_test);
  1302. #endif
  1303. if (fluid_sfont_t* const f_sfont = fluid_synth_get_sfont_by_id(f_synth, f_id))
  1304. {
  1305. #if FLUIDSYNTH_VERSION_MAJOR >= 2
  1306. fluid_sfont_iteration_start(f_sfont);
  1307. for (; fluid_sfont_iteration_next(f_sfont);)
  1308. ++programs;
  1309. #else
  1310. fluid_preset_t f_preset;
  1311. f_sfont->iteration_start(f_sfont);
  1312. for (; f_sfont->iteration_next(f_sfont, &f_preset);)
  1313. ++programs;
  1314. #endif
  1315. }
  1316. delete_fluid_synth(f_synth);
  1317. delete_fluid_settings(f_settings);
  1318. }
  1319. CarlaString name(file.getFileNameWithoutExtension().toRawUTF8());
  1320. CarlaString label(name);
  1321. // 2 channels
  1322. DISCOVERY_OUT("init", "-----------");
  1323. DISCOVERY_OUT("build", BINARY_NATIVE);
  1324. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1325. DISCOVERY_OUT("category", "synth");
  1326. DISCOVERY_OUT("name", name.buffer());
  1327. DISCOVERY_OUT("label", label.buffer());
  1328. DISCOVERY_OUT("audio.outs", 2);
  1329. DISCOVERY_OUT("midi.ins", 1);
  1330. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1331. DISCOVERY_OUT("parameters.outs", 1);
  1332. DISCOVERY_OUT("end", "------------");
  1333. // 16 channels
  1334. if (doInit && (name.isEmpty() || programs <= 1))
  1335. return;
  1336. name += " (16 outputs)";
  1337. DISCOVERY_OUT("init", "-----------");
  1338. DISCOVERY_OUT("build", BINARY_NATIVE);
  1339. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1340. DISCOVERY_OUT("category", "synth");
  1341. DISCOVERY_OUT("name", name.buffer());
  1342. DISCOVERY_OUT("label", label.buffer());
  1343. DISCOVERY_OUT("audio.outs", 32);
  1344. DISCOVERY_OUT("midi.ins", 1);
  1345. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1346. DISCOVERY_OUT("parameters.outs", 1);
  1347. DISCOVERY_OUT("end", "------------");
  1348. #else // HAVE_FLUIDSYNTH
  1349. DISCOVERY_OUT("error", "SF2 support not available");
  1350. return;
  1351. // unused
  1352. (void)filename;
  1353. (void)type;
  1354. (void)doInit;
  1355. #endif
  1356. }
  1357. // ------------------------------ main entry point ------------------------------
  1358. int main(int argc, char* argv[])
  1359. {
  1360. if (argc != 3)
  1361. {
  1362. carla_stdout("usage: %s <type> </path/to/plugin>", argv[0]);
  1363. return 1;
  1364. }
  1365. const char* const stype = argv[1];
  1366. const char* const filename = argv[2];
  1367. const PluginType type = getPluginTypeFromString(stype);
  1368. CarlaString filenameCheck(filename);
  1369. filenameCheck.toLower();
  1370. bool openLib = false;
  1371. lib_t handle = nullptr;
  1372. switch (type)
  1373. {
  1374. case PLUGIN_LADSPA:
  1375. case PLUGIN_DSSI:
  1376. case PLUGIN_VST2:
  1377. openLib = true;
  1378. break;
  1379. case PLUGIN_VST3:
  1380. openLib = water::File(filename).existsAsFile();
  1381. break;
  1382. default:
  1383. break;
  1384. }
  1385. if (type != PLUGIN_SF2 && filenameCheck.contains("fluidsynth", true))
  1386. {
  1387. DISCOVERY_OUT("info", "skipping fluidsynth based plugin");
  1388. return 0;
  1389. }
  1390. #ifdef CARLA_OS_MAC
  1391. if (type == PLUGIN_VST2 && (filenameCheck.endsWith(".vst") || filenameCheck.endsWith(".vst/")))
  1392. openLib = false;
  1393. #endif
  1394. // ---------------------------------------------------------------------------------------------------------------
  1395. // Initialize OS features
  1396. // we want stuff in English so we can parse error messages
  1397. ::setlocale(LC_ALL, "C");
  1398. #ifndef CARLA_OS_WIN
  1399. carla_setenv("LC_ALL", "C");
  1400. #endif
  1401. #ifdef CARLA_OS_WIN
  1402. OleInitialize(nullptr);
  1403. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  1404. # ifndef __WINPTHREADS_VERSION
  1405. // (non-portable) initialization of statically linked pthread library
  1406. pthread_win32_process_attach_np();
  1407. pthread_win32_thread_attach_np();
  1408. # endif
  1409. #endif
  1410. // ---------------------------------------------------------------------------------------------------------------
  1411. if (openLib)
  1412. {
  1413. handle = lib_open(filename);
  1414. if (handle == nullptr)
  1415. {
  1416. print_lib_error(filename);
  1417. return 1;
  1418. }
  1419. }
  1420. // never do init for dssi-vst, takes too long and it's crashy
  1421. bool doInit = ! filenameCheck.contains("dssi-vst", true);
  1422. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS") != nullptr)
  1423. doInit = false;
  1424. // ---------------------------------------------------------------------------------------------------------------
  1425. if (doInit && openLib && handle != nullptr)
  1426. {
  1427. // test fast loading & unloading DLL without initializing the plugin(s)
  1428. if (! lib_close(handle))
  1429. {
  1430. print_lib_error(filename);
  1431. return 1;
  1432. }
  1433. handle = lib_open(filename);
  1434. if (handle == nullptr)
  1435. {
  1436. print_lib_error(filename);
  1437. return 1;
  1438. }
  1439. }
  1440. #ifndef BUILD_BRIDGE
  1441. if (std::strcmp(filename, ":all") == 0)
  1442. {
  1443. do_cached_check(type);
  1444. return 0;
  1445. }
  1446. #endif
  1447. #ifdef CARLA_OS_MAC
  1448. // Plugin might be in quarentine due to Apple stupid notarization rules, let's remove that if possible
  1449. switch (type)
  1450. {
  1451. case PLUGIN_LADSPA:
  1452. case PLUGIN_DSSI:
  1453. case PLUGIN_VST2:
  1454. case PLUGIN_VST3:
  1455. removeFileFromQuarantine(filename);
  1456. break;
  1457. default:
  1458. break;
  1459. }
  1460. #endif
  1461. #ifdef USING_JUCE
  1462. // some macOS plugins have not been yet ported to arm64, re-run them in x86_64 mode if discovery fails
  1463. bool retryJucePlugin = false;
  1464. #endif
  1465. switch (type)
  1466. {
  1467. case PLUGIN_LADSPA:
  1468. do_ladspa_check(handle, filename, doInit);
  1469. break;
  1470. case PLUGIN_DSSI:
  1471. do_dssi_check(handle, filename, doInit);
  1472. break;
  1473. #ifndef BUILD_BRIDGE
  1474. case PLUGIN_LV2:
  1475. do_lv2_check(filename, doInit);
  1476. break;
  1477. #endif
  1478. case PLUGIN_VST2:
  1479. #if defined(USING_JUCE) && JUCE_PLUGINHOST_VST
  1480. retryJucePlugin = do_juce_check(filename, "VST2", doInit);
  1481. #else
  1482. do_vst_check(handle, filename, doInit);
  1483. #endif
  1484. break;
  1485. case PLUGIN_VST3:
  1486. #if defined(USING_JUCE) && JUCE_PLUGINHOST_VST3
  1487. retryJucePlugin = do_juce_check(filename, "VST3", doInit);
  1488. #else
  1489. DISCOVERY_OUT("error", "VST3 support not available");
  1490. #endif
  1491. break;
  1492. case PLUGIN_AU:
  1493. #if defined(USING_JUCE) && JUCE_PLUGINHOST_AU
  1494. do_juce_check(filename, "AU", doInit);
  1495. #else
  1496. DISCOVERY_OUT("error", "AU support not available");
  1497. #endif
  1498. break;
  1499. case PLUGIN_DLS:
  1500. case PLUGIN_GIG:
  1501. case PLUGIN_SF2:
  1502. do_fluidsynth_check(filename, type, doInit);
  1503. break;
  1504. default:
  1505. break;
  1506. }
  1507. #if defined(CARLA_OS_MAC) && defined(USING_JUCE) && defined(__aarch64__)
  1508. if (retryJucePlugin)
  1509. {
  1510. DISCOVERY_OUT("warning", "No plugins found while scanning in arm64 mode, will try x86_64 now");
  1511. cpu_type_t pref = CPU_TYPE_X86_64;
  1512. pid_t pid = -1;
  1513. posix_spawnattr_t attr;
  1514. posix_spawnattr_init(&attr);
  1515. CARLA_SAFE_ASSERT_RETURN(posix_spawnattr_setbinpref_np(&attr, 1, &pref, nullptr) == 0, 1);
  1516. CARLA_SAFE_ASSERT_RETURN(posix_spawn(&pid, argv[0], nullptr, &attr, argv, nullptr) == 0, 1);
  1517. posix_spawnattr_destroy(&attr);
  1518. if (pid > 0)
  1519. {
  1520. int status;
  1521. waitpid(pid, &status, 0);
  1522. }
  1523. }
  1524. #endif
  1525. if (openLib && handle != nullptr)
  1526. lib_close(handle);
  1527. // ---------------------------------------------------------------------------------------------------------------
  1528. #ifdef CARLA_OS_WIN
  1529. #ifndef __WINPTHREADS_VERSION
  1530. pthread_win32_thread_detach_np();
  1531. pthread_win32_process_detach_np();
  1532. #endif
  1533. CoUninitialize();
  1534. OleUninitialize();
  1535. #endif
  1536. return 0;
  1537. #ifdef USING_JUCE
  1538. // might be unused
  1539. (void)retryJucePlugin;
  1540. #endif
  1541. }
  1542. // -------------------------------------------------------------------------------------------------------------------