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.

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