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.

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