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.

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