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