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.

1785 lines
55KB

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