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.

1758 lines
54KB

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