Collection of tools useful for audio production
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.

1212 lines
38KB

  1. /*
  2. * Carla Plugin discovery code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #include "carla_includes.h"
  18. #include "carla_lib_includes.h"
  19. #include <cstdint>
  20. #include <cstdlib>
  21. #include <cstring>
  22. #include <iostream>
  23. #include <QtCore/QDir>
  24. #include <QtCore/QFileInfo>
  25. #include <QtCore/QUrl>
  26. #include "carla_backend.h"
  27. #include "carla_ladspa.h"
  28. #include "carla_dssi.h"
  29. #include "carla_lv2.h"
  30. #include "carla_vst.h"
  31. #ifdef BUILD_NATIVE
  32. #ifdef WANT_FLUIDSYNTH
  33. #include "carla_fluidsynth.h"
  34. #endif
  35. #ifdef WANT_LINUXSAMPLER
  36. #include "carla_linuxsampler.h"
  37. #endif
  38. #endif
  39. #define DISCOVERY_OUT(x, y) std::cout << "\ncarla-discovery::" << x << "::" << y << std::endl;
  40. // fake values to test plugins with
  41. const uint32_t bufferSize = 512;
  42. const double sampleRate = 44100.0;
  43. // Since discovery can find multi-architecture binaries, don't print ELF/EXE related errors
  44. void print_lib_error(const char* const filename)
  45. {
  46. const char* const error = lib_error(filename);
  47. if (error && strstr(error, "wrong ELF class") == nullptr && strstr(error, "Bad EXE format") == nullptr)
  48. DISCOVERY_OUT("error", error);
  49. }
  50. using namespace CarlaBackend;
  51. // ------------------------------ VST Stuff ------------------------------
  52. intptr_t vstCurrentUniqueId = 0;
  53. intptr_t vstHostCanDo(const char* const feature)
  54. {
  55. qDebug("vstHostCanDo(\"%s\")", feature);
  56. if (strcmp(feature, "supplyIdle") == 0)
  57. return 1;
  58. if (strcmp(feature, "sendVstEvents") == 0)
  59. return 1;
  60. if (strcmp(feature, "sendVstMidiEvent") == 0)
  61. return 1;
  62. if (strcmp(feature, "sendVstMidiEventFlagIsRealtime") == 0)
  63. return -1;
  64. if (strcmp(feature, "sendVstTimeInfo") == 0)
  65. return 1;
  66. if (strcmp(feature, "receiveVstEvents") == 0)
  67. return 1;
  68. if (strcmp(feature, "receiveVstMidiEvent") == 0)
  69. return 1;
  70. if (strcmp(feature, "receiveVstTimeInfo") == 0)
  71. return -1;
  72. if (strcmp(feature, "reportConnectionChanges") == 0)
  73. return -1;
  74. if (strcmp(feature, "acceptIOChanges") == 0)
  75. return 1;
  76. if (strcmp(feature, "sizeWindow") == 0)
  77. return 1;
  78. if (strcmp(feature, "offline") == 0)
  79. return -1;
  80. if (strcmp(feature, "openFileSelector") == 0)
  81. return -1;
  82. if (strcmp(feature, "closeFileSelector") == 0)
  83. return -1;
  84. if (strcmp(feature, "startStopProcess") == 0)
  85. return 1;
  86. if (strcmp(feature, "supportShell") == 0)
  87. return 1;
  88. if (strcmp(feature, "shellCategory") == 0)
  89. return 1;
  90. // unimplemented
  91. qWarning("vstHostCanDo(\"%s\") - unknown feature", feature);
  92. return 0;
  93. }
  94. 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)
  95. {
  96. #if DEBUG
  97. qDebug("vstHostCallback(%p, %s, %i, " P_INTPTR ", %p, %f", effect, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  98. #endif
  99. intptr_t ret = 0;
  100. switch (opcode)
  101. {
  102. case audioMasterAutomate:
  103. if (effect)
  104. effect->setParameter(effect, index, opt);
  105. break;
  106. case audioMasterVersion:
  107. ret = kVstVersion;
  108. break;
  109. case audioMasterCurrentId:
  110. ret = vstCurrentUniqueId;
  111. break;
  112. case audioMasterGetTime:
  113. static VstTimeInfo_R timeInfo;
  114. memset(&timeInfo, 0, sizeof(VstTimeInfo_R));
  115. timeInfo.sampleRate = sampleRate;
  116. // Tempo
  117. timeInfo.tempo = 120.0;
  118. timeInfo.flags |= kVstTempoValid;
  119. // Time Signature
  120. timeInfo.timeSigNumerator = 4;
  121. timeInfo.timeSigDenominator = 4;
  122. timeInfo.flags |= kVstTimeSigValid;
  123. ret = (intptr_t)&timeInfo;
  124. break;
  125. case audioMasterTempoAt:
  126. // Deprecated in VST SDK 2.4
  127. ret = 120 * 10000;
  128. break;
  129. case audioMasterGetNumAutomatableParameters:
  130. ret = MAX_PARAMETERS;
  131. break;
  132. case audioMasterGetSampleRate:
  133. ret = sampleRate;
  134. break;
  135. case audioMasterGetBlockSize:
  136. ret = bufferSize;
  137. break;
  138. case audioMasterGetCurrentProcessLevel:
  139. ret = kVstProcessLevelUser;
  140. break;
  141. case audioMasterGetVendorString:
  142. if (ptr)
  143. strcpy((char*)ptr, "Cadence");
  144. break;
  145. case audioMasterGetProductString:
  146. if (ptr)
  147. strcpy((char*)ptr, "Carla-Discovery");
  148. break;
  149. case audioMasterGetVendorVersion:
  150. ret = 0x050; // 0.5.0
  151. break;
  152. case audioMasterCanDo:
  153. if (ptr)
  154. ret = vstHostCanDo((const char*)ptr);
  155. break;
  156. case audioMasterGetLanguage:
  157. ret = kVstLangEnglish;
  158. break;
  159. default:
  160. qDebug("vstHostCallback(%p, %s, %i, " P_INTPTR ", %p, %f", effect, vstMasterOpcode2str(opcode), index, value, ptr, opt);
  161. break;
  162. }
  163. return ret;
  164. }
  165. // ------------------------------ Plugin Checks -----------------------------
  166. void do_ladspa_check(void* const libHandle, const bool init)
  167. {
  168. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)lib_symbol(libHandle, "ladspa_descriptor");
  169. if (! descFn)
  170. {
  171. DISCOVERY_OUT("error", "Not a LADSPA plugin");
  172. return;
  173. }
  174. unsigned long i = 0;
  175. const LADSPA_Descriptor* descriptor;
  176. while ((descriptor = descFn(i++)))
  177. {
  178. Q_ASSERT(descriptor->run);
  179. int hints = 0;
  180. int audioIns = 0;
  181. int audioOuts = 0;
  182. int audioTotal = 0;
  183. int parametersIns = 0;
  184. int parametersOuts = 0;
  185. int parametersTotal = 0;
  186. for (unsigned long j=0; j < descriptor->PortCount; j++)
  187. {
  188. const LADSPA_PortDescriptor portDescriptor = descriptor->PortDescriptors[j];
  189. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  190. {
  191. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  192. audioIns += 1;
  193. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  194. audioOuts += 1;
  195. audioTotal += 1;
  196. }
  197. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  198. {
  199. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  200. parametersIns += 1;
  201. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && strcmp(descriptor->PortNames[j], "latency") && strcmp(descriptor->PortNames[j], "_latency") && strcmp(descriptor->PortNames[j], "_sample-rate"))
  202. parametersOuts += 1;
  203. parametersTotal += 1;
  204. }
  205. }
  206. if (init)
  207. {
  208. // -----------------------------------------------------------------------
  209. // start crash-free plugin test
  210. const LADSPA_Handle handle = descriptor->instantiate(descriptor, sampleRate);
  211. if (! handle)
  212. {
  213. DISCOVERY_OUT("error", "Failed to init LADSPA plugin");
  214. continue;
  215. }
  216. LADSPA_Data bufferAudio[bufferSize][audioTotal];
  217. memset(bufferAudio, 0, sizeof(float)*bufferSize*audioTotal);
  218. LADSPA_Data bufferParams[parametersTotal];
  219. memset(bufferParams, 0, sizeof(float)*parametersTotal);
  220. LADSPA_Data min, max, def;
  221. for (unsigned long j=0, iA=0, iP=0; j < descriptor->PortCount; j++)
  222. {
  223. const LADSPA_PortDescriptor portType = descriptor->PortDescriptors[j];
  224. const LADSPA_PortRangeHint portHints = descriptor->PortRangeHints[j];
  225. if (LADSPA_IS_PORT_AUDIO(portType))
  226. {
  227. descriptor->connect_port(handle, j, bufferAudio[iA++]);
  228. }
  229. else if (LADSPA_IS_PORT_CONTROL(portType))
  230. {
  231. // min value
  232. if (LADSPA_IS_HINT_BOUNDED_BELOW(portHints.HintDescriptor))
  233. min = portHints.LowerBound;
  234. else
  235. min = 0.0f;
  236. // max value
  237. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portHints.HintDescriptor))
  238. max = portHints.UpperBound;
  239. else
  240. max = 1.0f;
  241. if (min > max)
  242. max = min;
  243. else if (max < min)
  244. min = max;
  245. if (max - min == 0.0f)
  246. {
  247. DISCOVERY_OUT("error", "Broken parameter: max - min == 0");
  248. max = min + 0.1f;
  249. }
  250. // default value
  251. def = get_default_ladspa_port_value(portHints.HintDescriptor, min, max);
  252. if (def < min)
  253. def = min;
  254. else if (def > max)
  255. def = max;
  256. if (LADSPA_IS_HINT_SAMPLE_RATE(portHints.HintDescriptor))
  257. {
  258. min *= sampleRate;
  259. max *= sampleRate;
  260. def *= sampleRate;
  261. }
  262. if (LADSPA_IS_PORT_OUTPUT(portType) && (strcmp(descriptor->PortNames[j], "latency") == 0 || strcmp(descriptor->PortNames[j], "_latency") == 0))
  263. {
  264. // latency parameter
  265. min = 0.0f;
  266. max = sampleRate;
  267. def = 0.0f;
  268. }
  269. bufferParams[iP] = def;
  270. descriptor->connect_port(handle, j, &bufferParams[iP++]);
  271. }
  272. }
  273. if (descriptor->activate)
  274. descriptor->activate(handle);
  275. descriptor->run(handle, bufferSize);
  276. if (descriptor->deactivate)
  277. descriptor->deactivate(handle);
  278. if (descriptor->cleanup)
  279. descriptor->cleanup(handle);
  280. // end crash-free plugin test
  281. // -----------------------------------------------------------------------
  282. }
  283. DISCOVERY_OUT("init", "-----------");
  284. DISCOVERY_OUT("name", descriptor->Name);
  285. DISCOVERY_OUT("label", descriptor->Label);
  286. DISCOVERY_OUT("maker", descriptor->Maker);
  287. DISCOVERY_OUT("copyright", descriptor->Copyright);
  288. DISCOVERY_OUT("unique_id", descriptor->UniqueID);
  289. DISCOVERY_OUT("hints", hints);
  290. DISCOVERY_OUT("audio.ins", audioIns);
  291. DISCOVERY_OUT("audio.outs", audioOuts);
  292. DISCOVERY_OUT("audio.total", audioTotal);
  293. DISCOVERY_OUT("parameters.ins", parametersIns);
  294. DISCOVERY_OUT("parameters.outs", parametersOuts);
  295. DISCOVERY_OUT("parameters.total", parametersTotal);
  296. DISCOVERY_OUT("build", BINARY_NATIVE);
  297. DISCOVERY_OUT("end", "------------");
  298. }
  299. }
  300. void do_dssi_check(void* const libHandle, const bool init)
  301. {
  302. const DSSI_Descriptor_Function descFn = (DSSI_Descriptor_Function)lib_symbol(libHandle, "dssi_descriptor");
  303. if (! descFn)
  304. {
  305. DISCOVERY_OUT("error", "Not a DSSI plugin");
  306. return;
  307. }
  308. unsigned long i = 0;
  309. const DSSI_Descriptor* descriptor;
  310. while ((descriptor = descFn(i++)))
  311. {
  312. const LADSPA_Descriptor* const ldescriptor = descriptor->LADSPA_Plugin;
  313. Q_ASSERT(ldescriptor);
  314. Q_ASSERT(ldescriptor->run || descriptor->run_synth || descriptor->run_multiple_synths);
  315. int hints = 0;
  316. int audioIns = 0;
  317. int audioOuts = 0;
  318. int audioTotal = 0;
  319. int midiIns = 0;
  320. int midiTotal = 0;
  321. int parametersIns = 0;
  322. int parametersOuts = 0;
  323. int parametersTotal = 0;
  324. int programsTotal = 0;
  325. for (unsigned long j=0; j < ldescriptor->PortCount; j++)
  326. {
  327. const LADSPA_PortDescriptor portDescriptor = ldescriptor->PortDescriptors[j];
  328. if (LADSPA_IS_PORT_AUDIO(portDescriptor))
  329. {
  330. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  331. audioIns += 1;
  332. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor))
  333. audioOuts += 1;
  334. audioTotal += 1;
  335. }
  336. else if (LADSPA_IS_PORT_CONTROL(portDescriptor))
  337. {
  338. if (LADSPA_IS_PORT_INPUT(portDescriptor))
  339. parametersIns += 1;
  340. else if (LADSPA_IS_PORT_OUTPUT(portDescriptor) && strcmp(ldescriptor->PortNames[j], "latency") && strcmp(ldescriptor->PortNames[j], "_latency") && strcmp(ldescriptor->PortNames[j], "_sample-rate"))
  341. parametersOuts += 1;
  342. parametersTotal += 1;
  343. }
  344. }
  345. if (descriptor->run_synth || descriptor->run_multiple_synths)
  346. midiIns = midiTotal = 1;
  347. if (midiIns > 0 && audioOuts > 0)
  348. hints |= PLUGIN_IS_SYNTH;
  349. if (init)
  350. {
  351. // -----------------------------------------------------------------------
  352. // start crash-free plugin test
  353. const LADSPA_Handle handle = ldescriptor->instantiate(ldescriptor, sampleRate);
  354. if (! handle)
  355. {
  356. DISCOVERY_OUT("error", "Failed to init DSSI plugin");
  357. continue;
  358. }
  359. // we can only get program info per-handle
  360. if (descriptor->get_program)
  361. {
  362. while ((descriptor->get_program(handle, programsTotal++)))
  363. continue;
  364. }
  365. LADSPA_Data bufferAudio[bufferSize][audioTotal];
  366. memset(bufferAudio, 0, sizeof(float)*bufferSize*audioTotal);
  367. LADSPA_Data bufferParams[parametersTotal];
  368. memset(bufferParams, 0, sizeof(float)*parametersTotal);
  369. LADSPA_Data min, max, def;
  370. for (unsigned long j=0, iA=0, iP=0; j < ldescriptor->PortCount; j++)
  371. {
  372. const LADSPA_PortDescriptor portType = ldescriptor->PortDescriptors[j];
  373. const LADSPA_PortRangeHint portHints = ldescriptor->PortRangeHints[j];
  374. if (LADSPA_IS_PORT_AUDIO(portType))
  375. {
  376. ldescriptor->connect_port(handle, j, bufferAudio[iA++]);
  377. }
  378. else if (LADSPA_IS_PORT_CONTROL(portType))
  379. {
  380. // min value
  381. if (LADSPA_IS_HINT_BOUNDED_BELOW(portHints.HintDescriptor))
  382. min = portHints.LowerBound;
  383. else
  384. min = 0.0f;
  385. // max value
  386. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portHints.HintDescriptor))
  387. max = portHints.UpperBound;
  388. else
  389. max = 1.0f;
  390. if (min > max)
  391. max = min;
  392. else if (max < min)
  393. min = max;
  394. if (max - min == 0.0f)
  395. {
  396. DISCOVERY_OUT("error", "Broken parameter: max - min == 0");
  397. max = min + 0.1f;
  398. }
  399. // default value
  400. def = get_default_ladspa_port_value(portHints.HintDescriptor, min, max);
  401. if (def < min)
  402. def = min;
  403. else if (def > max)
  404. def = max;
  405. if (LADSPA_IS_HINT_SAMPLE_RATE(portHints.HintDescriptor))
  406. {
  407. min *= sampleRate;
  408. max *= sampleRate;
  409. def *= sampleRate;
  410. }
  411. if (LADSPA_IS_PORT_OUTPUT(portType) && (strcmp(ldescriptor->PortNames[j], "latency") == 0 || strcmp(ldescriptor->PortNames[j], "_latency") == 0))
  412. {
  413. // latency parameter
  414. min = 0.0f;
  415. max = sampleRate;
  416. def = 0.0f;
  417. }
  418. bufferParams[iP] = def;
  419. ldescriptor->connect_port(handle, j, &bufferParams[iP++]);
  420. }
  421. }
  422. if (ldescriptor->activate)
  423. ldescriptor->activate(handle);
  424. if (descriptor->run_synth || descriptor->run_multiple_synths)
  425. {
  426. snd_seq_event_t midiEvents[2];
  427. memset(midiEvents, 0, sizeof(snd_seq_event_t)*2);
  428. const unsigned long midiEventCount = 2;
  429. midiEvents[0].type = SND_SEQ_EVENT_NOTEON;
  430. midiEvents[0].data.note.note = 64;
  431. midiEvents[0].data.note.velocity = 100;
  432. midiEvents[1].type = SND_SEQ_EVENT_NOTEOFF;
  433. midiEvents[1].data.note.note = 64;
  434. midiEvents[1].data.note.velocity = 0;
  435. midiEvents[1].time.tick = bufferSize/2;
  436. if (descriptor->run_multiple_synths && ! descriptor->run_synth)
  437. {
  438. LADSPA_Handle handlePtr[1] = { handle };
  439. snd_seq_event_t* midiEventsPtr[1] = { midiEvents };
  440. unsigned long midiEventCountPtr[1] = { midiEventCount };
  441. descriptor->run_multiple_synths(1, handlePtr, bufferSize, midiEventsPtr, midiEventCountPtr);
  442. }
  443. else
  444. descriptor->run_synth(handle, bufferSize, midiEvents, midiEventCount);
  445. }
  446. else
  447. ldescriptor->run(handle, bufferSize);
  448. if (ldescriptor->deactivate)
  449. ldescriptor->deactivate(handle);
  450. if (ldescriptor->cleanup)
  451. ldescriptor->cleanup(handle);
  452. // end crash-free plugin test
  453. // -----------------------------------------------------------------------
  454. }
  455. DISCOVERY_OUT("init", "-----------");
  456. DISCOVERY_OUT("name", ldescriptor->Name);
  457. DISCOVERY_OUT("label", ldescriptor->Label);
  458. DISCOVERY_OUT("maker", ldescriptor->Maker);
  459. DISCOVERY_OUT("copyright", ldescriptor->Copyright);
  460. DISCOVERY_OUT("unique_id", ldescriptor->UniqueID);
  461. DISCOVERY_OUT("hints", hints);
  462. DISCOVERY_OUT("audio.ins", audioIns);
  463. DISCOVERY_OUT("audio.outs", audioOuts);
  464. DISCOVERY_OUT("audio.total", audioTotal);
  465. DISCOVERY_OUT("midi.ins", midiIns);
  466. DISCOVERY_OUT("midi.total", midiTotal);
  467. DISCOVERY_OUT("parameters.ins", parametersIns);
  468. DISCOVERY_OUT("parameters.outs", parametersOuts);
  469. DISCOVERY_OUT("parameters.total", parametersTotal);
  470. DISCOVERY_OUT("programs.total", programsTotal);
  471. DISCOVERY_OUT("build", BINARY_NATIVE);
  472. DISCOVERY_OUT("end", "------------");
  473. }
  474. }
  475. void do_lv2_check(const char* const bundle, const bool init)
  476. {
  477. // Convert bundle filename to URI
  478. QString qBundle(QUrl::fromLocalFile(bundle).toString());
  479. if (! qBundle.endsWith(QDir::separator()))
  480. qBundle += QDir::separator();
  481. // Load bundle
  482. Lilv::Node lilvBundle(Lv2World.new_uri(qBundle.toUtf8().constData()));
  483. Lv2World.load_bundle(lilvBundle);
  484. // Load plugins in this bundle
  485. const Lilv::Plugins lilvPlugins = Lv2World.get_all_plugins();
  486. // Get all plugin URIs in this bundle
  487. QStringList URIs;
  488. LILV_FOREACH(plugins, i, lilvPlugins)
  489. {
  490. Lilv::Plugin lilvPlugin(lilv_plugins_get(lilvPlugins, i));
  491. URIs.append(QString(lilvPlugin.get_uri().as_string()));
  492. }
  493. // Get & check every plugin-instance
  494. for (int i=0; i < URIs.count(); i++)
  495. {
  496. const LV2_RDF_Descriptor* const rdf_descriptor = lv2_rdf_new(URIs.at(i).toUtf8().constData());
  497. Q_ASSERT(rdf_descriptor && rdf_descriptor->URI);
  498. if (init)
  499. {
  500. // test if DLL is loadable
  501. void* const libHandle = lib_open(rdf_descriptor->Binary);
  502. if (! libHandle)
  503. {
  504. print_lib_error(rdf_descriptor->Binary);
  505. continue;
  506. }
  507. lib_close(libHandle);
  508. // test if we support all required ports and features
  509. bool supported = true;
  510. for (uint32_t j=0; j < rdf_descriptor->PortCount; j++)
  511. {
  512. const LV2_RDF_Port* const port = &rdf_descriptor->Ports[j];
  513. bool validPort = (LV2_IS_PORT_CONTROL(port->Type) || LV2_IS_PORT_AUDIO(port->Type) || LV2_IS_PORT_ATOM_SEQUENCE(port->Type) /*|| LV2_IS_PORT_CV(port->Type)*/ || LV2_IS_PORT_EVENT(port->Type) || LV2_IS_PORT_MIDI_LL(port->Type));
  514. if (! (validPort || LV2_IS_PORT_OPTIONAL(port->Properties)))
  515. {
  516. DISCOVERY_OUT("error", "plugin requires a non-supported port type, port-name: " << port->Name);
  517. supported = false;
  518. break;
  519. }
  520. }
  521. for (uint32_t j=0; j < rdf_descriptor->FeatureCount && supported; j++)
  522. {
  523. const LV2_RDF_Feature* const feature = &rdf_descriptor->Features[j];
  524. if (LV2_IS_FEATURE_REQUIRED(feature->Type) && ! is_lv2_feature_supported(feature->URI))
  525. {
  526. DISCOVERY_OUT("error", "plugin requires a non-supported feature " << feature->URI);
  527. supported = false;
  528. break;
  529. }
  530. }
  531. if (! supported)
  532. continue;
  533. }
  534. int hints = 0;
  535. int audioIns = 0;
  536. int audioOuts = 0;
  537. int audioTotal = 0;
  538. int midiIns = 0;
  539. int midiOuts = 0;
  540. int midiTotal = 0;
  541. int parametersIns = 0;
  542. int parametersOuts = 0;
  543. int parametersTotal = 0;
  544. for (uint32_t j=0; j < rdf_descriptor->PortCount; j++)
  545. {
  546. const LV2_RDF_Port* const port = &rdf_descriptor->Ports[j];
  547. if (LV2_IS_PORT_AUDIO(port->Type))
  548. {
  549. if (LV2_IS_PORT_INPUT(port->Type))
  550. audioIns += 1;
  551. else if (LV2_IS_PORT_OUTPUT(port->Type))
  552. audioOuts += 1;
  553. audioTotal += 1;
  554. }
  555. else if (LV2_IS_PORT_CONTROL(port->Type))
  556. {
  557. if (LV2_IS_PORT_DESIGNATION_LATENCY(port->Designation) || LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(port->Designation) ||
  558. LV2_IS_PORT_DESIGNATION_FREEWHEELING(port->Designation) || LV2_IS_PORT_DESIGNATION_TIME(port->Designation))
  559. {
  560. pass();
  561. }
  562. else
  563. {
  564. if (LV2_IS_PORT_INPUT(port->Type))
  565. parametersIns += 1;
  566. else if (LV2_IS_PORT_OUTPUT(port->Type))
  567. parametersOuts += 1;
  568. parametersTotal += 1;
  569. }
  570. }
  571. else if (port->Type & LV2_PORT_SUPPORTS_MIDI_EVENT)
  572. {
  573. if (LV2_IS_PORT_INPUT(port->Type))
  574. midiIns += 1;
  575. else if (LV2_IS_PORT_OUTPUT(port->Type))
  576. midiOuts += 1;
  577. midiTotal += 1;
  578. }
  579. }
  580. if (rdf_descriptor->Type & LV2_CLASS_INSTRUMENT)
  581. hints |= PLUGIN_IS_SYNTH;
  582. if (rdf_descriptor->UICount > 0)
  583. hints |= PLUGIN_HAS_GUI;
  584. DISCOVERY_OUT("init", "-----------");
  585. DISCOVERY_OUT("label", rdf_descriptor->URI);
  586. if (rdf_descriptor->Name)
  587. DISCOVERY_OUT("name", rdf_descriptor->Name);
  588. if (rdf_descriptor->Author)
  589. DISCOVERY_OUT("maker", rdf_descriptor->Author);
  590. if (rdf_descriptor->License)
  591. DISCOVERY_OUT("copyright", rdf_descriptor->License);
  592. DISCOVERY_OUT("unique_id", rdf_descriptor->UniqueID);
  593. DISCOVERY_OUT("hints", hints);
  594. DISCOVERY_OUT("audio.ins", audioIns);
  595. DISCOVERY_OUT("audio.outs", audioOuts);
  596. DISCOVERY_OUT("audio.total", audioTotal);
  597. DISCOVERY_OUT("midi.ins", midiIns);
  598. DISCOVERY_OUT("midi.outs", midiOuts);
  599. DISCOVERY_OUT("midi.total", midiTotal);
  600. DISCOVERY_OUT("parameters.ins", parametersIns);
  601. DISCOVERY_OUT("parameters.outs", parametersOuts);
  602. DISCOVERY_OUT("parameters.total", parametersTotal);
  603. DISCOVERY_OUT("build", BINARY_NATIVE);
  604. DISCOVERY_OUT("end", "------------");
  605. }
  606. }
  607. void do_vst_check(void* const libHandle, const bool init)
  608. {
  609. VST_Function vstFn = (VST_Function)lib_symbol(libHandle, "VSTPluginMain");
  610. if (! vstFn)
  611. vstFn = (VST_Function)lib_symbol(libHandle, "main");
  612. if (! vstFn)
  613. {
  614. DISCOVERY_OUT("error", "Not a VST plugin");
  615. return;
  616. }
  617. AEffect* const effect = vstFn(vstHostCallback);
  618. if (! (effect && effect->magic == kEffectMagic))
  619. {
  620. DISCOVERY_OUT("error", "Failed to init VST plugin");
  621. return;
  622. }
  623. const char* cName;
  624. const char* cProduct;
  625. const char* cVendor;
  626. char strBuf[255] = { 0 };
  627. effect->dispatcher(effect, effGetEffectName, 0, 0, strBuf, 0.0f);
  628. cName = strdup((strBuf[0] != 0) ? strBuf : "");
  629. strBuf[0] = 0;
  630. effect->dispatcher(effect, effGetProductString, 0, 0, strBuf, 0.0f);
  631. cProduct = strdup((strBuf[0] != 0) ? strBuf : "");
  632. strBuf[0] = 0;
  633. effect->dispatcher(effect, effGetVendorString, 0, 0, strBuf, 0.0f);
  634. cVendor = strdup((strBuf[0] != 0) ? strBuf : "");
  635. vstCurrentUniqueId = effect->uniqueID;
  636. intptr_t vstCategory = effect->dispatcher(effect, effGetPlugCategory, 0, 0, nullptr, 0.0f);
  637. // only init if required
  638. if (init || vstCategory == kPlugCategShell)
  639. effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
  640. while (true)
  641. {
  642. int hints = 0;
  643. int audioIns = effect->numInputs;
  644. int audioOuts = effect->numOutputs;
  645. int audioTotal = audioIns + audioOuts;
  646. int midiIns = 0;
  647. int midiOuts = 0;
  648. int midiTotal = 0;
  649. int parametersIns = effect->numParams;
  650. int parametersTotal = parametersIns;
  651. int programsTotal = effect->numPrograms;
  652. if (effect->flags & effFlagsHasEditor)
  653. hints |= PLUGIN_HAS_GUI;
  654. if (effect->flags & effFlagsIsSynth)
  655. hints |= PLUGIN_IS_SYNTH;
  656. if (vstPluginCanDo(effect, "receiveVstEvents") || vstPluginCanDo(effect, "receiveVstMidiEvent") || (effect->flags & effFlagsIsSynth) > 0)
  657. midiIns = 1;
  658. if (vstPluginCanDo(effect, "sendVstEvents") || vstPluginCanDo(effect, "sendVstMidiEvent"))
  659. midiOuts = 1;
  660. midiTotal = midiIns + midiOuts;
  661. // -----------------------------------------------------------------------
  662. // start crash-free plugin test
  663. if (init)
  664. {
  665. float** bufferAudioIn = new float* [audioIns];
  666. for (int j=0; j < audioIns; j++)
  667. {
  668. bufferAudioIn[j] = new float [bufferSize];
  669. memset(bufferAudioIn[j], 0, sizeof(float)*bufferSize);
  670. }
  671. float** bufferAudioOut = new float* [audioOuts];
  672. for (int j=0; j < audioOuts; j++)
  673. {
  674. bufferAudioOut[j] = new float [bufferSize];
  675. memset(bufferAudioOut[j], 0, sizeof(float)*bufferSize);
  676. }
  677. struct {
  678. int32_t numEvents;
  679. intptr_t reserved;
  680. VstEvent* data[2];
  681. } events;
  682. VstMidiEvent midiEvents[2];
  683. memset(midiEvents, 0, sizeof(VstMidiEvent)*2);
  684. midiEvents[0].type = kVstMidiType;
  685. midiEvents[0].byteSize = sizeof(VstMidiEvent);
  686. midiEvents[0].midiData[0] = 0x90;
  687. midiEvents[0].midiData[1] = 64;
  688. midiEvents[0].midiData[2] = 100;
  689. midiEvents[1].type = kVstMidiType;
  690. midiEvents[1].byteSize = sizeof(VstMidiEvent);
  691. midiEvents[1].midiData[0] = 0x80;
  692. midiEvents[1].midiData[1] = 64;
  693. midiEvents[1].deltaFrames = bufferSize/2;
  694. events.numEvents = 2;
  695. events.reserved = 0;
  696. events.data[0] = (VstEvent*)&midiEvents[0];
  697. events.data[1] = (VstEvent*)&midiEvents[1];
  698. #if ! VST_FORCE_DEPRECATED
  699. effect->dispatcher(effect, effSetBlockSizeAndSampleRate, 0, bufferSize, nullptr, sampleRate);
  700. #endif
  701. effect->dispatcher(effect, effSetBlockSize, 0, bufferSize, nullptr, 0.0f);
  702. effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, sampleRate);
  703. effect->dispatcher(effect, effSetProcessPrecision, 0, kVstProcessPrecision32, nullptr, 0.0f);
  704. effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0.0f);
  705. effect->dispatcher(effect, effStartProcess, 0, 0, nullptr, 0.0f);
  706. if (midiIns > 0)
  707. effect->dispatcher(effect, effProcessEvents, 0, 0, &events, 0.0f);
  708. if ((effect->flags & effFlagsCanReplacing) > 0 && effect->processReplacing != effect->process)
  709. effect->processReplacing(effect, bufferAudioIn, bufferAudioOut, bufferSize);
  710. #if ! VST_FORCE_DEPRECATED
  711. else
  712. effect->process(effect, bufferAudioIn, bufferAudioOut, bufferSize);
  713. #endif
  714. effect->dispatcher(effect, effStopProcess, 0, 0, nullptr, 0.0f);
  715. effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0.0f);
  716. for (int j=0; j < audioIns; j++)
  717. delete[] bufferAudioIn[j];
  718. for (int j=0; j < audioOuts; j++)
  719. delete[] bufferAudioOut[j];
  720. delete[] bufferAudioIn;
  721. delete[] bufferAudioOut;
  722. }
  723. // end crash-free plugin test
  724. // -----------------------------------------------------------------------
  725. DISCOVERY_OUT("init", "-----------");
  726. DISCOVERY_OUT("name", cName);
  727. DISCOVERY_OUT("label", cProduct);
  728. DISCOVERY_OUT("maker", cVendor);
  729. DISCOVERY_OUT("copyright", cVendor);
  730. DISCOVERY_OUT("unique_id", vstCurrentUniqueId);
  731. DISCOVERY_OUT("hints", hints);
  732. DISCOVERY_OUT("audio.ins", audioIns);
  733. DISCOVERY_OUT("audio.outs", audioOuts);
  734. DISCOVERY_OUT("audio.total", audioTotal);
  735. DISCOVERY_OUT("midi.ins", midiIns);
  736. DISCOVERY_OUT("midi.outs", midiOuts);
  737. DISCOVERY_OUT("midi.total", midiTotal);
  738. DISCOVERY_OUT("parameters.ins", parametersIns);
  739. DISCOVERY_OUT("parameters.total", parametersTotal);
  740. DISCOVERY_OUT("programs.total", programsTotal);
  741. DISCOVERY_OUT("build", BINARY_NATIVE);
  742. DISCOVERY_OUT("end", "------------");
  743. if (vstCategory != kPlugCategShell)
  744. break;
  745. strBuf[0] = 0;
  746. vstCurrentUniqueId = effect->dispatcher(effect, effShellGetNextPlugin, 0, 0, strBuf, 0.0f);
  747. if (vstCurrentUniqueId != 0)
  748. {
  749. free((void*)cName);
  750. cName = strdup((strBuf[0] != 0) ? strBuf : "");
  751. }
  752. else
  753. break;
  754. }
  755. // only close if required
  756. if (init || vstCategory == kPlugCategShell)
  757. effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
  758. free((void*)cName);
  759. free((void*)cProduct);
  760. free((void*)cVendor);
  761. }
  762. void do_fluidsynth_check(const char* const filename, const bool init)
  763. {
  764. #ifdef WANT_FLUIDSYNTH
  765. if (! fluid_is_soundfont(filename))
  766. {
  767. DISCOVERY_OUT("error", "Not a SF2 file");
  768. return;
  769. }
  770. int programs = 0;
  771. if (init)
  772. {
  773. fluid_settings_t* const f_settings = new_fluid_settings();
  774. fluid_synth_t* const f_synth = new_fluid_synth(f_settings);
  775. const int f_id = fluid_synth_sfload(f_synth, filename, 0);
  776. if (f_id < 0)
  777. {
  778. DISCOVERY_OUT("error", "Failed to load SF2 file");
  779. return;
  780. }
  781. fluid_sfont_t* f_sfont;
  782. fluid_preset_t f_preset;
  783. f_sfont = fluid_synth_get_sfont_by_id(f_synth, f_id);
  784. f_sfont->iteration_start(f_sfont);
  785. while (f_sfont->iteration_next(f_sfont, &f_preset))
  786. programs += 1;
  787. delete_fluid_synth(f_synth);
  788. delete_fluid_settings(f_settings);
  789. }
  790. DISCOVERY_OUT("init", "-----------");
  791. DISCOVERY_OUT("name", "");
  792. DISCOVERY_OUT("label", "");
  793. DISCOVERY_OUT("maker", "");
  794. DISCOVERY_OUT("copyright", "");
  795. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  796. DISCOVERY_OUT("audio.outs", 2);
  797. DISCOVERY_OUT("audio.total", 2);
  798. DISCOVERY_OUT("midi.ins", 1);
  799. DISCOVERY_OUT("midi.total", 1);
  800. DISCOVERY_OUT("programs.total", programs);
  801. DISCOVERY_OUT("parameters.ins", 13); // defined in Carla
  802. DISCOVERY_OUT("parameters.outs", 1);
  803. DISCOVERY_OUT("parameters.total", 14);
  804. DISCOVERY_OUT("build", BINARY_NATIVE);
  805. DISCOVERY_OUT("end", "------------");
  806. #else
  807. DISCOVERY_OUT("error", "SF2 support not available");
  808. Q_UNUSED(filename);
  809. Q_UNUSED(init);
  810. #endif
  811. }
  812. void do_linuxsampler_check(const char* const filename, const char* const stype, const bool init)
  813. {
  814. #ifdef WANT_LINUXSAMPLER
  815. const QFileInfo file(filename);
  816. if (! file.exists())
  817. {
  818. DISCOVERY_OUT("error", "Requested file does not exist");
  819. return;
  820. }
  821. if (! file.isFile())
  822. {
  823. DISCOVERY_OUT("error", "Requested file is not valid");
  824. return;
  825. }
  826. if (! file.isReadable())
  827. {
  828. DISCOVERY_OUT("error", "Requested file is not readable");
  829. return;
  830. }
  831. using namespace LinuxSampler;
  832. class LinuxSamplerScopedEngine {
  833. public:
  834. LinuxSamplerScopedEngine(const char* const filename, const char* const stype)
  835. {
  836. engine = nullptr;
  837. try {
  838. engine = EngineFactory::Create(stype);
  839. }
  840. catch (const Exception& e)
  841. {
  842. DISCOVERY_OUT("error", e.what());
  843. return;
  844. }
  845. if (! engine)
  846. return;
  847. ins = engine->GetInstrumentManager();
  848. if (! ins)
  849. {
  850. DISCOVERY_OUT("error", "Failed to get LinuxSampeler instrument manager");
  851. return;
  852. }
  853. std::vector<InstrumentManager::instrument_id_t> ids;
  854. try {
  855. ids = ins->GetInstrumentFileContent(filename);
  856. }
  857. catch (const Exception& e)
  858. {
  859. DISCOVERY_OUT("error", e.what());
  860. return;
  861. }
  862. if (ids.size() > 0)
  863. {
  864. InstrumentManager::instrument_info_t info = ins->GetInstrumentInfo(ids[0]);
  865. outputInfo(&info, ids.size());
  866. }
  867. }
  868. ~LinuxSamplerScopedEngine()
  869. {
  870. if (engine)
  871. EngineFactory::Destroy(engine);
  872. }
  873. static void outputInfo(InstrumentManager::instrument_info_t* const info, const int programs, const char* const basename = nullptr)
  874. {
  875. DISCOVERY_OUT("init", "-----------");
  876. if (info)
  877. {
  878. DISCOVERY_OUT("name", info->InstrumentName);
  879. DISCOVERY_OUT("label", info->Product);
  880. DISCOVERY_OUT("maker", info->Artists);
  881. DISCOVERY_OUT("copyright", info->Artists);
  882. }
  883. else
  884. {
  885. DISCOVERY_OUT("name", basename);
  886. DISCOVERY_OUT("label", basename);
  887. }
  888. DISCOVERY_OUT("hints", PLUGIN_IS_SYNTH);
  889. DISCOVERY_OUT("audio.outs", 2);
  890. DISCOVERY_OUT("audio.total", 2);
  891. DISCOVERY_OUT("midi.ins", 1);
  892. DISCOVERY_OUT("midi.total", 1);
  893. DISCOVERY_OUT("programs.total", programs);
  894. //DISCOVERY_OUT("parameters.ins", 13); // defined in Carla - TODO
  895. //DISCOVERY_OUT("parameters.outs", 1);
  896. //DISCOVERY_OUT("parameters.total", 14);
  897. DISCOVERY_OUT("build", BINARY_NATIVE);
  898. DISCOVERY_OUT("end", "------------");
  899. }
  900. private:
  901. Engine* engine;
  902. InstrumentManager* ins;
  903. };
  904. if (init)
  905. const LinuxSamplerScopedEngine engine(filename, stype);
  906. else
  907. LinuxSamplerScopedEngine::outputInfo(nullptr, 0, file.baseName().toUtf8().constData());
  908. #else
  909. DISCOVERY_OUT("error", stype << " support not available");
  910. Q_UNUSED(filename);
  911. Q_UNUSED(init);
  912. #endif
  913. }
  914. // ------------------------------ main entry point ------------------------------
  915. int main(int argc, char* argv[])
  916. {
  917. if (argc != 3)
  918. {
  919. qWarning("usage: %s <type> </path/to/plugin>", argv[0]);
  920. return 1;
  921. }
  922. const char* const stype = argv[1];
  923. const char* const filename = argv[2];
  924. bool openLib;
  925. PluginType type;
  926. void* handle = nullptr;
  927. if (strcmp(stype, "LADSPA") == 0)
  928. {
  929. openLib = true;
  930. type = PLUGIN_LADSPA;
  931. }
  932. else if (strcmp(stype, "DSSI") == 0)
  933. {
  934. openLib = true;
  935. type = PLUGIN_DSSI;
  936. }
  937. else if (strcmp(stype, "LV2") == 0)
  938. {
  939. openLib = false;
  940. type = PLUGIN_LV2;
  941. }
  942. else if (strcmp(stype, "VST") == 0)
  943. {
  944. openLib = true;
  945. type = PLUGIN_VST;
  946. }
  947. else if (strcmp(stype, "GIG") == 0)
  948. {
  949. openLib = false;
  950. type = PLUGIN_GIG;
  951. }
  952. else if (strcmp(stype, "SF2") == 0)
  953. {
  954. openLib = false;
  955. type = PLUGIN_SF2;
  956. }
  957. else if (strcmp(stype, "SFZ") == 0)
  958. {
  959. openLib = false;
  960. type = PLUGIN_SFZ;
  961. }
  962. else
  963. {
  964. DISCOVERY_OUT("error", "Invalid plugin type");
  965. return 1;
  966. }
  967. if (openLib)
  968. {
  969. handle = lib_open(filename);
  970. if (! handle)
  971. {
  972. print_lib_error(filename);
  973. return 1;
  974. }
  975. }
  976. bool doInit = ! QString(filename).endsWith("dssi-vst.so", Qt::CaseInsensitive);
  977. if (doInit && getenv("CARLA_DISCOVERY_NO_PROCESSING_CHECKS"))
  978. doInit = false;
  979. switch (type)
  980. {
  981. case PLUGIN_LADSPA:
  982. do_ladspa_check(handle, doInit);
  983. break;
  984. case PLUGIN_DSSI:
  985. do_dssi_check(handle, doInit);
  986. break;
  987. case PLUGIN_LV2:
  988. do_lv2_check(filename, doInit);
  989. break;
  990. case PLUGIN_VST:
  991. do_vst_check(handle, doInit);
  992. break;
  993. case PLUGIN_GIG:
  994. do_linuxsampler_check(filename, "gig", doInit);
  995. break;
  996. case PLUGIN_SF2:
  997. do_fluidsynth_check(filename, doInit);
  998. break;
  999. case PLUGIN_SFZ:
  1000. do_linuxsampler_check(filename, "sfz", doInit);
  1001. break;
  1002. default:
  1003. break;
  1004. }
  1005. if (openLib)
  1006. lib_close(handle);
  1007. return 0;
  1008. }