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 53KB

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
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
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
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
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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, ids.size());
  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 size_t programs, const char* const basename = nullptr)
  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() || programs <= 1)
  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 init)
  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 (init && 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 (init)
  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 (max - min == 0.0f)
  462. {
  463. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max - min == 0");
  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 init)
  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 (init && 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 (init)
  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 (max - min == 0.0f)
  681. {
  682. DISCOVERY_OUT("warning", "Parameter '" << portName << "' is broken: max - min == 0");
  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 init)
  763. {
  764. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  765. // Convert bundle filename to URI
  766. CarlaString sBundle("file://");
  767. sBundle += bundle;
  768. if (! sBundle.endsWith(OS_SEP))
  769. sBundle += OS_SEP_STR;
  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 (init)
  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* const rdfFeature(&rdfDescriptor->Features[j]);
  836. if (is_lv2_feature_supported(rdfFeature->URI))
  837. {
  838. pass();
  839. }
  840. else if (LV2_IS_FEATURE_REQUIRED(rdfFeature->Type))
  841. {
  842. DISCOVERY_OUT("error", "Plugin '" << rdfDescriptor->URI << "' requires a non-supported feature '" << rdfFeature->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 init)
  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 (init)
  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 init)
  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 (init)
  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 init)
  1196. {
  1197. #ifdef HAVE_FLUIDSYNTH
  1198. if (! fluid_is_soundfont(filename))
  1199. {
  1200. DISCOVERY_OUT("error", "Not a SF2 file");
  1201. return;
  1202. }
  1203. int programs = 0;
  1204. if (init)
  1205. {
  1206. fluid_settings_t* const f_settings = new_fluid_settings();
  1207. fluid_synth_t* const f_synth = new_fluid_synth(f_settings);
  1208. const int f_id = fluid_synth_sfload(f_synth, filename, 0);
  1209. if (f_id < 0)
  1210. {
  1211. DISCOVERY_OUT("error", "Failed to load SF2 file");
  1212. return;
  1213. }
  1214. fluid_sfont_t* f_sfont;
  1215. fluid_preset_t f_preset;
  1216. f_sfont = fluid_synth_get_sfont_by_id(f_synth, static_cast<uint>(f_id));
  1217. f_sfont->iteration_start(f_sfont);
  1218. while (f_sfont->iteration_next(f_sfont, &f_preset))
  1219. programs += 1;
  1220. delete_fluid_synth(f_synth);
  1221. delete_fluid_settings(f_settings);
  1222. }
  1223. CarlaString name;
  1224. if (const char* const shortname = std::strrchr(filename, OS_SEP))
  1225. name = shortname+1;
  1226. else
  1227. name = filename;
  1228. name.truncate(name.rfind('.'));
  1229. CarlaString label(name);
  1230. // 2 channels
  1231. DISCOVERY_OUT("init", "-----------");
  1232. DISCOVERY_OUT("build", BINARY_NATIVE);
  1233. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1234. DISCOVERY_OUT("name", name.buffer());
  1235. DISCOVERY_OUT("label", label.buffer());
  1236. DISCOVERY_OUT("audio.outs", 2);
  1237. DISCOVERY_OUT("midi.ins", 1);
  1238. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1239. DISCOVERY_OUT("parameters.outs", 1);
  1240. DISCOVERY_OUT("end", "------------");
  1241. // 16 channels
  1242. if (name.isEmpty() || programs <= 1)
  1243. return;
  1244. name += " (16 outputs)";
  1245. DISCOVERY_OUT("init", "-----------");
  1246. DISCOVERY_OUT("build", BINARY_NATIVE);
  1247. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  1248. DISCOVERY_OUT("name", name.buffer());
  1249. DISCOVERY_OUT("label", label.buffer());
  1250. DISCOVERY_OUT("audio.outs", 32);
  1251. DISCOVERY_OUT("midi.ins", 1);
  1252. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  1253. DISCOVERY_OUT("parameters.outs", 1);
  1254. DISCOVERY_OUT("end", "------------");
  1255. #else // HAVE_FLUIDSYNTH
  1256. DISCOVERY_OUT("error", "SF2 support not available");
  1257. return;
  1258. // unused
  1259. (void)filename;
  1260. (void)init;
  1261. #endif
  1262. }
  1263. static void do_linuxsampler_check(const char* const filename, const char* const stype, const bool init)
  1264. {
  1265. #ifdef HAVE_LINUXSAMPLER
  1266. const File file(filename);
  1267. if (! file.existsAsFile())
  1268. {
  1269. DISCOVERY_OUT("error", "Requested file is not valid or does not exist");
  1270. return;
  1271. }
  1272. if (init)
  1273. const LinuxSamplerScopedEngine engine(filename, stype);
  1274. else
  1275. LinuxSamplerScopedEngine::outputInfo(nullptr, 0, file.getFileNameWithoutExtension().toRawUTF8());
  1276. #else // HAVE_LINUXSAMPLER
  1277. DISCOVERY_OUT("error", stype << " support not available");
  1278. return;
  1279. // unused
  1280. (void)filename;
  1281. (void)init;
  1282. #endif
  1283. }
  1284. // ------------------------------ main entry point ------------------------------
  1285. int main(int argc, char* argv[])
  1286. {
  1287. if (argc != 3)
  1288. {
  1289. carla_stdout("usage: %s <type> </path/to/plugin>", argv[0]);
  1290. return 1;
  1291. }
  1292. const char* const stype = argv[1];
  1293. const char* const filename = argv[2];
  1294. const PluginType type = getPluginTypeFromString(stype);
  1295. CarlaString filenameStr(filename);
  1296. filenameStr.toLower();
  1297. if (filenameStr.contains("fluidsynth", true))
  1298. {
  1299. DISCOVERY_OUT("info", "skipping fluidsynth based plugin");
  1300. return 0;
  1301. }
  1302. if (filenameStr.contains("linuxsampler", true) || filenameStr.endsWith("ls16.so"))
  1303. {
  1304. DISCOVERY_OUT("info", "skipping linuxsampler based plugin");
  1305. return 0;
  1306. }
  1307. bool openLib = false;
  1308. void* handle = nullptr;
  1309. switch (type)
  1310. {
  1311. case PLUGIN_LADSPA:
  1312. case PLUGIN_DSSI:
  1313. #ifndef CARLA_OS_MAC
  1314. case PLUGIN_VST:
  1315. openLib = true;
  1316. #endif
  1317. default:
  1318. break;
  1319. }
  1320. if (openLib)
  1321. {
  1322. handle = lib_open(filename);
  1323. if (handle == nullptr)
  1324. {
  1325. print_lib_error(filename);
  1326. return 1;
  1327. }
  1328. }
  1329. // never do init for dssi-vst, takes too long and it's crashy
  1330. bool doInit = ! filenameStr.contains("dssi-vst", true);
  1331. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS") != nullptr)
  1332. doInit = false;
  1333. if (doInit && handle != nullptr)
  1334. {
  1335. // test fast loading & unloading DLL without initializing the plugin(s)
  1336. if (! lib_close(handle))
  1337. {
  1338. print_lib_error(filename);
  1339. return 1;
  1340. }
  1341. handle = lib_open(filename);
  1342. if (handle == nullptr)
  1343. {
  1344. print_lib_error(filename);
  1345. return 1;
  1346. }
  1347. }
  1348. switch (type)
  1349. {
  1350. case PLUGIN_LADSPA:
  1351. do_ladspa_check(handle, filename, doInit);
  1352. break;
  1353. case PLUGIN_DSSI:
  1354. do_dssi_check(handle, filename, doInit);
  1355. break;
  1356. case PLUGIN_LV2:
  1357. do_lv2_check(filename, doInit);
  1358. break;
  1359. case PLUGIN_VST:
  1360. #ifdef CARLA_OS_MAC
  1361. do_juce_check(filename, "VST", doInit);
  1362. #else
  1363. do_vst_check(handle, doInit);
  1364. #endif
  1365. break;
  1366. case PLUGIN_VST3:
  1367. #ifdef USE_JUCE_PROCESSORS
  1368. do_juce_check(filename, "VST3", doInit);
  1369. #else
  1370. DISCOVERY_OUT("error", "VST3 support not available");
  1371. #endif
  1372. break;
  1373. case PLUGIN_AU:
  1374. #ifdef USE_JUCE_PROCESSORS
  1375. do_juce_check(filename, "AU", doInit);
  1376. #else
  1377. DISCOVERY_OUT("error", "AU support not available");
  1378. #endif
  1379. break;
  1380. case PLUGIN_GIG:
  1381. do_linuxsampler_check(filename, "gig", doInit);
  1382. break;
  1383. case PLUGIN_SF2:
  1384. do_fluidsynth_check(filename, doInit);
  1385. break;
  1386. case PLUGIN_SFZ:
  1387. do_linuxsampler_check(filename, "sfz", doInit);
  1388. break;
  1389. default:
  1390. break;
  1391. }
  1392. if (openLib && handle != nullptr)
  1393. lib_close(handle);
  1394. return 0;
  1395. }
  1396. // --------------------------------------------------------------------------