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.

carla-discovery.cpp 54KB

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