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.

1824 lines
57KB

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