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.

1806 lines
56KB

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