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.

1727 lines
53KB

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