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.

1812 lines
56KB

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