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

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