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.

1722 lines
53KB

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