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