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.

1542 lines
51KB

  1. /*
  2. * Carla LADSPA Plugin
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_LADSPA
  19. #include "CarlaLadspaUtils.hpp"
  20. CARLA_BACKEND_START_NAMESPACE
  21. #if 0
  22. }
  23. #endif
  24. class LadspaPlugin : public CarlaPlugin
  25. {
  26. public:
  27. LadspaPlugin(CarlaEngine* const engine, const unsigned int id)
  28. : CarlaPlugin(engine, id),
  29. fHandle(nullptr),
  30. fHandle2(nullptr),
  31. fDescriptor(nullptr),
  32. fRdfDescriptor(nullptr),
  33. fAudioInBuffers(nullptr),
  34. fAudioOutBuffers(nullptr),
  35. fParamBuffers(nullptr)
  36. {
  37. carla_debug("LadspaPlugin::LadspaPlugin(%p, %i)", engine, id);
  38. }
  39. ~LadspaPlugin() override
  40. {
  41. carla_debug("LadspaPlugin::~LadspaPlugin()");
  42. kData->singleMutex.lock();
  43. kData->masterMutex.lock();
  44. if (kData->client != nullptr && kData->client->isActive())
  45. kData->client->deactivate();
  46. if (kData->active)
  47. {
  48. deactivate();
  49. kData->active = false;
  50. }
  51. if (fDescriptor != nullptr)
  52. {
  53. if (fDescriptor->cleanup != nullptr)
  54. {
  55. if (fHandle != nullptr)
  56. fDescriptor->cleanup(fHandle);
  57. if (fHandle2 != nullptr)
  58. fDescriptor->cleanup(fHandle2);
  59. }
  60. fHandle = nullptr;
  61. fHandle2 = nullptr;
  62. fDescriptor = nullptr;
  63. }
  64. if (fRdfDescriptor != nullptr)
  65. {
  66. delete fRdfDescriptor;
  67. fRdfDescriptor = nullptr;
  68. }
  69. clearBuffers();
  70. }
  71. // -------------------------------------------------------------------
  72. // Information (base)
  73. PluginType type() const override
  74. {
  75. return PLUGIN_LADSPA;
  76. }
  77. PluginCategory category() override
  78. {
  79. if (fRdfDescriptor != nullptr)
  80. {
  81. const LADSPA_PluginType category(fRdfDescriptor->Type);
  82. // Specific Types
  83. if (category & (LADSPA_PLUGIN_DELAY|LADSPA_PLUGIN_REVERB))
  84. return PLUGIN_CATEGORY_DELAY;
  85. if (category & (LADSPA_PLUGIN_PHASER|LADSPA_PLUGIN_FLANGER|LADSPA_PLUGIN_CHORUS))
  86. return PLUGIN_CATEGORY_MODULATOR;
  87. if (category & (LADSPA_PLUGIN_AMPLIFIER))
  88. return PLUGIN_CATEGORY_DYNAMICS;
  89. if (category & (LADSPA_PLUGIN_UTILITY|LADSPA_PLUGIN_SPECTRAL|LADSPA_PLUGIN_FREQUENCY_METER))
  90. return PLUGIN_CATEGORY_UTILITY;
  91. // Pre-set LADSPA Types
  92. if (LADSPA_IS_PLUGIN_DYNAMICS(category))
  93. return PLUGIN_CATEGORY_DYNAMICS;
  94. if (LADSPA_IS_PLUGIN_AMPLITUDE(category))
  95. return PLUGIN_CATEGORY_MODULATOR;
  96. if (LADSPA_IS_PLUGIN_EQ(category))
  97. return PLUGIN_CATEGORY_EQ;
  98. if (LADSPA_IS_PLUGIN_FILTER(category))
  99. return PLUGIN_CATEGORY_FILTER;
  100. if (LADSPA_IS_PLUGIN_FREQUENCY(category))
  101. return PLUGIN_CATEGORY_UTILITY;
  102. if (LADSPA_IS_PLUGIN_SIMULATOR(category))
  103. return PLUGIN_CATEGORY_OTHER;
  104. if (LADSPA_IS_PLUGIN_TIME(category))
  105. return PLUGIN_CATEGORY_DELAY;
  106. if (LADSPA_IS_PLUGIN_GENERATOR(category))
  107. return PLUGIN_CATEGORY_SYNTH;
  108. }
  109. return getPluginCategoryFromName(fName);
  110. }
  111. long uniqueId() const override
  112. {
  113. CARLA_ASSERT(fDescriptor != nullptr);
  114. return fDescriptor->UniqueID;
  115. }
  116. // -------------------------------------------------------------------
  117. // Information (count)
  118. uint32_t parameterScalePointCount(const uint32_t parameterId) const override
  119. {
  120. CARLA_ASSERT(parameterId < kData->param.count);
  121. const int32_t rindex(kData->param.data[parameterId].rindex);
  122. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  123. {
  124. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  125. return port.ScalePointCount;
  126. }
  127. return 0;
  128. }
  129. // -------------------------------------------------------------------
  130. // Information (current data)
  131. // nothing
  132. // -------------------------------------------------------------------
  133. // Information (per-plugin data)
  134. unsigned int availableOptions() override
  135. {
  136. #ifdef __USE_GNU
  137. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  138. #else
  139. const bool isDssiVst = fFilename.contains("dssi-vst");
  140. #endif
  141. unsigned int options = 0x0;
  142. if (! isDssiVst)
  143. options |= PLUGIN_OPTION_FIXED_BUFFER;
  144. if (kData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  145. {
  146. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  147. options |= PLUGIN_OPTION_FORCE_STEREO;
  148. else if (kData->audioIn.count <= 1 && kData->audioOut.count <= 1 && (kData->audioIn.count != 0 || kData->audioOut.count != 0))
  149. options |= PLUGIN_OPTION_FORCE_STEREO;
  150. }
  151. return options;
  152. }
  153. float getParameterValue(const uint32_t parameterId) override
  154. {
  155. CARLA_ASSERT(fParamBuffers != nullptr);
  156. CARLA_ASSERT(parameterId < kData->param.count);
  157. return fParamBuffers[parameterId];
  158. }
  159. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) override
  160. {
  161. CARLA_ASSERT(fRdfDescriptor != nullptr);
  162. CARLA_ASSERT(parameterId < kData->param.count);
  163. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  164. const int32_t rindex(kData->param.data[parameterId].rindex);
  165. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  166. {
  167. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  168. if (scalePointId < port.ScalePointCount)
  169. {
  170. const LADSPA_RDF_ScalePoint& scalePoint(port.ScalePoints[scalePointId]);
  171. return scalePoint.Value;
  172. }
  173. }
  174. return 0.0f;
  175. }
  176. void getLabel(char* const strBuf) override
  177. {
  178. CARLA_ASSERT(fDescriptor != nullptr);
  179. if (fDescriptor->Label != nullptr)
  180. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  181. else
  182. CarlaPlugin::getLabel(strBuf);
  183. }
  184. void getMaker(char* const strBuf) override
  185. {
  186. CARLA_ASSERT(fDescriptor != nullptr);
  187. if (fRdfDescriptor != nullptr && fRdfDescriptor->Creator != nullptr)
  188. std::strncpy(strBuf, fRdfDescriptor->Creator, STR_MAX);
  189. else if (fDescriptor->Maker != nullptr)
  190. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  191. else
  192. CarlaPlugin::getMaker(strBuf);
  193. }
  194. void getCopyright(char* const strBuf) override
  195. {
  196. CARLA_ASSERT(fDescriptor != nullptr);
  197. if (fDescriptor->Copyright != nullptr)
  198. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  199. else
  200. CarlaPlugin::getCopyright(strBuf);
  201. }
  202. void getRealName(char* const strBuf) override
  203. {
  204. CARLA_ASSERT(fDescriptor != nullptr);
  205. if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  206. std::strncpy(strBuf, fRdfDescriptor->Title, STR_MAX);
  207. else if (fDescriptor->Name != nullptr)
  208. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  209. else
  210. CarlaPlugin::getRealName(strBuf);
  211. }
  212. void getParameterName(const uint32_t parameterId, char* const strBuf) override
  213. {
  214. CARLA_ASSERT(fDescriptor != nullptr);
  215. CARLA_ASSERT(parameterId < kData->param.count);
  216. const int32_t rindex(kData->param.data[parameterId].rindex);
  217. if (rindex < static_cast<int32_t>(fDescriptor->PortCount))
  218. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  219. else
  220. CarlaPlugin::getParameterName(parameterId, strBuf);
  221. }
  222. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) override
  223. {
  224. CARLA_ASSERT(parameterId < kData->param.count);
  225. const int32_t rindex(kData->param.data[parameterId].rindex);
  226. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  227. {
  228. const LADSPA_RDF_Port& port = fRdfDescriptor->Ports[rindex];
  229. if (LADSPA_PORT_HAS_LABEL(port.Hints) && port.Label != nullptr)
  230. {
  231. std::strncpy(strBuf, port.Label, STR_MAX);
  232. return;
  233. }
  234. }
  235. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  236. }
  237. void getParameterUnit(const uint32_t parameterId, char* const strBuf) override
  238. {
  239. CARLA_ASSERT(parameterId < kData->param.count);
  240. const int32_t rindex(kData->param.data[parameterId].rindex);
  241. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  242. {
  243. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  244. if (LADSPA_PORT_HAS_UNIT(port.Hints))
  245. {
  246. switch (port.Unit)
  247. {
  248. case LADSPA_UNIT_DB:
  249. std::strncpy(strBuf, "dB", STR_MAX);
  250. return;
  251. case LADSPA_UNIT_COEF:
  252. std::strncpy(strBuf, "(coef)", STR_MAX);
  253. return;
  254. case LADSPA_UNIT_HZ:
  255. std::strncpy(strBuf, "Hz", STR_MAX);
  256. return;
  257. case LADSPA_UNIT_S:
  258. std::strncpy(strBuf, "s", STR_MAX);
  259. return;
  260. case LADSPA_UNIT_MS:
  261. std::strncpy(strBuf, "ms", STR_MAX);
  262. return;
  263. case LADSPA_UNIT_MIN:
  264. std::strncpy(strBuf, "min", STR_MAX);
  265. return;
  266. }
  267. }
  268. }
  269. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  270. }
  271. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) override
  272. {
  273. CARLA_ASSERT(fRdfDescriptor != nullptr);
  274. CARLA_ASSERT(parameterId < kData->param.count);
  275. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  276. const int32_t rindex(kData->param.data[parameterId].rindex);
  277. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  278. {
  279. const LADSPA_RDF_Port& port(fRdfDescriptor->Ports[rindex]);
  280. if (scalePointId < port.ScalePointCount)
  281. {
  282. const LADSPA_RDF_ScalePoint& scalePoint(port.ScalePoints[scalePointId]);
  283. if (scalePoint.Label != nullptr)
  284. {
  285. std::strncpy(strBuf, scalePoint.Label, STR_MAX);
  286. return;
  287. }
  288. }
  289. }
  290. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  291. }
  292. // -------------------------------------------------------------------
  293. // Set data (state)
  294. // nothing
  295. // -------------------------------------------------------------------
  296. // Set data (internal stuff)
  297. // nothing
  298. // -------------------------------------------------------------------
  299. // Set data (plugin-specific stuff)
  300. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  301. {
  302. CARLA_ASSERT(parameterId < kData->param.count);
  303. const float fixedValue(kData->param.fixValue(parameterId, value));
  304. fParamBuffers[parameterId] = fixedValue;
  305. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  306. }
  307. // -------------------------------------------------------------------
  308. // Set gui stuff
  309. // nothing
  310. // -------------------------------------------------------------------
  311. // Plugin state
  312. void reload() override
  313. {
  314. carla_debug("LadspaPlugin::reload() - start");
  315. CARLA_ASSERT(kData->engine != nullptr);
  316. CARLA_ASSERT(fDescriptor != nullptr);
  317. CARLA_ASSERT(fHandle != nullptr);
  318. if (kData->engine == nullptr)
  319. return;
  320. if (fDescriptor == nullptr)
  321. return;
  322. if (fHandle == nullptr)
  323. return;
  324. const ProcessMode processMode(kData->engine->getProccessMode());
  325. // Safely disable plugin for reload
  326. const ScopedDisabler sd(this);
  327. if (kData->active)
  328. deactivate();
  329. clearBuffers();
  330. const float sampleRate(static_cast<float>(kData->engine->getSampleRate()));
  331. const uint32_t portCount(static_cast<uint32_t>(fDescriptor->PortCount));
  332. uint32_t aIns, aOuts, params, j;
  333. aIns = aOuts = params = 0;
  334. bool forcedStereoIn, forcedStereoOut;
  335. forcedStereoIn = forcedStereoOut = false;
  336. bool needsCtrlIn, needsCtrlOut;
  337. needsCtrlIn = needsCtrlOut = false;
  338. if (portCount > 0)
  339. {
  340. CARLA_ASSERT(fDescriptor->PortDescriptors != nullptr);
  341. CARLA_ASSERT(fDescriptor->PortRangeHints != nullptr);
  342. CARLA_ASSERT(fDescriptor->PortNames != nullptr);
  343. for (uint32_t i=0; i < portCount; ++i)
  344. {
  345. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  346. if (LADSPA_IS_PORT_AUDIO(portType))
  347. {
  348. if (LADSPA_IS_PORT_INPUT(portType))
  349. aIns += 1;
  350. else if (LADSPA_IS_PORT_OUTPUT(portType))
  351. aOuts += 1;
  352. }
  353. else if (LADSPA_IS_PORT_CONTROL(portType))
  354. params += 1;
  355. }
  356. }
  357. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  358. {
  359. if (fHandle2 == nullptr)
  360. fHandle2 = fDescriptor->instantiate(fDescriptor, (unsigned long)sampleRate);
  361. if (fHandle2 != nullptr)
  362. {
  363. if (aIns == 1)
  364. {
  365. aIns = 2;
  366. forcedStereoIn = true;
  367. }
  368. if (aOuts == 1)
  369. {
  370. aOuts = 2;
  371. forcedStereoOut = true;
  372. }
  373. }
  374. }
  375. if (aIns > 0)
  376. {
  377. kData->audioIn.createNew(aIns);
  378. fAudioInBuffers = new float*[aIns];
  379. for (uint32_t i=0; i < aIns; ++i)
  380. fAudioInBuffers[i] = nullptr;
  381. }
  382. if (aOuts > 0)
  383. {
  384. kData->audioOut.createNew(aOuts);
  385. fAudioOutBuffers = new float*[aOuts];
  386. needsCtrlIn = true;
  387. for (uint32_t i=0; i < aOuts; ++i)
  388. fAudioOutBuffers[i] = nullptr;
  389. }
  390. if (params > 0)
  391. {
  392. kData->param.createNew(params);
  393. fParamBuffers = new float[params];
  394. for (uint32_t i=0; i < params; ++i)
  395. fParamBuffers[i] = 0.0f;
  396. }
  397. const uint portNameSize(kData->engine->maxPortNameSize());
  398. CarlaString portName;
  399. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  400. {
  401. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  402. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  403. const bool hasPortRDF = (fRdfDescriptor != nullptr && i < fRdfDescriptor->PortCount);
  404. CARLA_ASSERT(fDescriptor->PortNames[i] != nullptr);
  405. if (LADSPA_IS_PORT_AUDIO(portType))
  406. {
  407. portName.clear();
  408. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  409. {
  410. portName = fName;
  411. portName += ":";
  412. }
  413. portName += fDescriptor->PortNames[i];
  414. portName.truncate(portNameSize);
  415. if (LADSPA_IS_PORT_INPUT(portType))
  416. {
  417. j = iAudioIn++;
  418. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  419. kData->audioIn.ports[j].rindex = i;
  420. if (forcedStereoIn)
  421. {
  422. portName += "_2";
  423. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  424. kData->audioIn.ports[1].rindex = i;
  425. }
  426. }
  427. else if (LADSPA_IS_PORT_OUTPUT(portType))
  428. {
  429. j = iAudioOut++;
  430. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  431. kData->audioOut.ports[j].rindex = i;
  432. if (forcedStereoOut)
  433. {
  434. portName += "_2";
  435. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  436. kData->audioOut.ports[1].rindex = i;
  437. }
  438. }
  439. else
  440. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  441. }
  442. else if (LADSPA_IS_PORT_CONTROL(portType))
  443. {
  444. j = iCtrl++;
  445. kData->param.data[j].index = j;
  446. kData->param.data[j].rindex = i;
  447. kData->param.data[j].hints = 0x0;
  448. kData->param.data[j].midiChannel = 0;
  449. kData->param.data[j].midiCC = -1;
  450. float min, max, def, step, stepSmall, stepLarge;
  451. // min value
  452. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  453. min = portRangeHints.LowerBound;
  454. else
  455. min = 0.0f;
  456. // max value
  457. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  458. max = portRangeHints.UpperBound;
  459. else
  460. max = 1.0f;
  461. if (min > max)
  462. max = min;
  463. else if (max < min)
  464. min = max;
  465. if (max - min == 0.0f)
  466. {
  467. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  468. max = min + 0.1f;
  469. }
  470. // default value
  471. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  472. def = fRdfDescriptor->Ports[i].Default;
  473. else
  474. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  475. if (def < min)
  476. def = min;
  477. else if (def > max)
  478. def = max;
  479. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  480. {
  481. min *= sampleRate;
  482. max *= sampleRate;
  483. def *= sampleRate;
  484. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  485. }
  486. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  487. {
  488. step = max - min;
  489. stepSmall = step;
  490. stepLarge = step;
  491. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  492. }
  493. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  494. {
  495. step = 1.0f;
  496. stepSmall = 1.0f;
  497. stepLarge = 10.0f;
  498. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  499. }
  500. else
  501. {
  502. float range = max - min;
  503. step = range/100.0f;
  504. stepSmall = range/1000.0f;
  505. stepLarge = range/10.0f;
  506. }
  507. if (LADSPA_IS_PORT_INPUT(portType))
  508. {
  509. kData->param.data[j].type = PARAMETER_INPUT;
  510. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  511. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  512. needsCtrlIn = true;
  513. }
  514. else if (LADSPA_IS_PORT_OUTPUT(portType))
  515. {
  516. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  517. {
  518. min = 0.0f;
  519. max = sampleRate;
  520. def = 0.0f;
  521. step = 1.0f;
  522. stepSmall = 1.0f;
  523. stepLarge = 1.0f;
  524. kData->param.data[j].type = PARAMETER_LATENCY;
  525. kData->param.data[j].hints = 0;
  526. }
  527. else if (std::strcmp(fDescriptor->PortNames[i], "_sample-rate") == 0)
  528. {
  529. def = sampleRate;
  530. step = 1.0f;
  531. stepSmall = 1.0f;
  532. stepLarge = 1.0f;
  533. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  534. kData->param.data[j].hints = 0;
  535. }
  536. else
  537. {
  538. kData->param.data[j].type = PARAMETER_OUTPUT;
  539. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  540. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  541. needsCtrlOut = true;
  542. }
  543. }
  544. else
  545. {
  546. kData->param.data[j].type = PARAMETER_UNKNOWN;
  547. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  548. }
  549. // extra parameter hints
  550. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  551. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  552. // check for scalepoints, require at least 2 to make it useful
  553. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount > 1)
  554. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  555. kData->param.ranges[j].min = min;
  556. kData->param.ranges[j].max = max;
  557. kData->param.ranges[j].def = def;
  558. kData->param.ranges[j].step = step;
  559. kData->param.ranges[j].stepSmall = stepSmall;
  560. kData->param.ranges[j].stepLarge = stepLarge;
  561. // Start parameters in their default values
  562. fParamBuffers[j] = def;
  563. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  564. if (fHandle2 != nullptr)
  565. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  566. }
  567. else
  568. {
  569. // Not Audio or Control
  570. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  571. fDescriptor->connect_port(fHandle, i, nullptr);
  572. if (fHandle2 != nullptr)
  573. fDescriptor->connect_port(fHandle2, i, nullptr);
  574. }
  575. }
  576. if (needsCtrlIn)
  577. {
  578. portName.clear();
  579. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  580. {
  581. portName = fName;
  582. portName += ":";
  583. }
  584. portName += "events-in";
  585. portName.truncate(portNameSize);
  586. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  587. }
  588. if (needsCtrlOut)
  589. {
  590. portName.clear();
  591. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  592. {
  593. portName = fName;
  594. portName += ":";
  595. }
  596. portName += "events-out";
  597. portName.truncate(portNameSize);
  598. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  599. }
  600. if (forcedStereoIn || forcedStereoOut)
  601. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  602. else
  603. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  604. // plugin hints
  605. fHints = 0x0;
  606. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  607. fHints |= PLUGIN_IS_RTSAFE;
  608. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  609. fHints |= PLUGIN_CAN_DRYWET;
  610. if (aOuts > 0)
  611. fHints |= PLUGIN_CAN_VOLUME;
  612. if (aOuts >= 2 && aOuts % 2 == 0)
  613. fHints |= PLUGIN_CAN_BALANCE;
  614. // extra plugin hints
  615. kData->extraHints = 0x0;
  616. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  617. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  618. // check latency
  619. if (fHints & PLUGIN_CAN_DRYWET)
  620. {
  621. for (uint32_t i=0; i < kData->param.count; ++i)
  622. {
  623. if (kData->param.data[i].type != PARAMETER_LATENCY)
  624. continue;
  625. // we need to pre-run the plugin so it can update its latency control-port
  626. float tmpIn[aIns][2];
  627. float tmpOut[aOuts][2];
  628. for (j=0; j < aIns; ++j)
  629. {
  630. tmpIn[j][0] = 0.0f;
  631. tmpIn[j][1] = 0.0f;
  632. fDescriptor->connect_port(fHandle, kData->audioIn.ports[j].rindex, tmpIn[j]);
  633. }
  634. for (j=0; j < aOuts; ++j)
  635. {
  636. tmpOut[j][0] = 0.0f;
  637. tmpOut[j][1] = 0.0f;
  638. fDescriptor->connect_port(fHandle, kData->audioOut.ports[j].rindex, tmpOut[j]);
  639. }
  640. if (fDescriptor->activate != nullptr)
  641. fDescriptor->activate(fHandle);
  642. fDescriptor->run(fHandle, 2);
  643. if (fDescriptor->deactivate != nullptr)
  644. fDescriptor->deactivate(fHandle);
  645. const uint32_t latency = (uint32_t)fParamBuffers[i];
  646. if (kData->latency != latency)
  647. {
  648. kData->latency = latency;
  649. kData->client->setLatency(latency);
  650. kData->recreateLatencyBuffers();
  651. }
  652. break;
  653. }
  654. }
  655. bufferSizeChanged(kData->engine->getBufferSize());
  656. if (kData->active)
  657. activate();
  658. carla_debug("LadspaPlugin::reload() - end");
  659. }
  660. // -------------------------------------------------------------------
  661. // Plugin processing
  662. void activate() override
  663. {
  664. CARLA_ASSERT(fDescriptor != nullptr);
  665. CARLA_ASSERT(fHandle != nullptr);
  666. if (fDescriptor->activate != nullptr)
  667. {
  668. fDescriptor->activate(fHandle);
  669. if (fHandle2 != nullptr)
  670. fDescriptor->activate(fHandle2);
  671. }
  672. }
  673. void deactivate() override
  674. {
  675. CARLA_ASSERT(fDescriptor != nullptr);
  676. CARLA_ASSERT(fHandle != nullptr);
  677. if (fDescriptor->deactivate != nullptr)
  678. {
  679. fDescriptor->deactivate(fHandle);
  680. if (fHandle2 != nullptr)
  681. fDescriptor->deactivate(fHandle2);
  682. }
  683. }
  684. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  685. {
  686. uint32_t i, k;
  687. // --------------------------------------------------------------------------------------------------------
  688. // Check if active
  689. if (! kData->active)
  690. {
  691. // disable any output sound
  692. for (i=0; i < kData->audioOut.count; ++i)
  693. carla_zeroFloat(outBuffer[i], frames);
  694. return;
  695. }
  696. // --------------------------------------------------------------------------------------------------------
  697. // Check if needs reset
  698. if (kData->needsReset)
  699. {
  700. if (kData->latency > 0)
  701. {
  702. for (i=0; i < kData->audioIn.count; ++i)
  703. carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  704. }
  705. kData->needsReset = false;
  706. }
  707. // --------------------------------------------------------------------------------------------------------
  708. // Event Input and Processing
  709. if (kData->event.portIn != nullptr)
  710. {
  711. // ----------------------------------------------------------------------------------------------------
  712. // Event Input (System)
  713. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  714. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  715. uint32_t timeOffset = 0;
  716. for (i=0; i < nEvents; ++i)
  717. {
  718. const EngineEvent& event(kData->event.portIn->getEvent(i));
  719. time = event.time;
  720. if (time >= frames)
  721. continue;
  722. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  723. if (time > timeOffset && sampleAccurate)
  724. {
  725. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  726. timeOffset = time;
  727. }
  728. // Control change
  729. switch (event.type)
  730. {
  731. case kEngineEventTypeNull:
  732. break;
  733. case kEngineEventTypeControl:
  734. {
  735. const EngineControlEvent& ctrlEvent = event.ctrl;
  736. switch (ctrlEvent.type)
  737. {
  738. case kEngineControlEventTypeNull:
  739. break;
  740. case kEngineControlEventTypeParameter:
  741. {
  742. #ifndef BUILD_BRIDGE
  743. // Control backend stuff
  744. if (event.channel == kData->ctrlChannel)
  745. {
  746. float value;
  747. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  748. {
  749. value = ctrlEvent.value;
  750. setDryWet(value, false, false);
  751. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  752. }
  753. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  754. {
  755. value = ctrlEvent.value*127.0f/100.0f;
  756. setVolume(value, false, false);
  757. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  758. }
  759. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  760. {
  761. float left, right;
  762. value = ctrlEvent.value/0.5f - 1.0f;
  763. if (value < 0.0f)
  764. {
  765. left = -1.0f;
  766. right = (value*2.0f)+1.0f;
  767. }
  768. else if (value > 0.0f)
  769. {
  770. left = (value*2.0f)-1.0f;
  771. right = 1.0f;
  772. }
  773. else
  774. {
  775. left = -1.0f;
  776. right = 1.0f;
  777. }
  778. setBalanceLeft(left, false, false);
  779. setBalanceRight(right, false, false);
  780. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  781. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  782. }
  783. }
  784. #endif
  785. // Control plugin parameters
  786. for (k=0; k < kData->param.count; ++k)
  787. {
  788. if (kData->param.data[k].midiChannel != event.channel)
  789. continue;
  790. if (kData->param.data[k].midiCC != ctrlEvent.param)
  791. continue;
  792. if (kData->param.data[k].type != PARAMETER_INPUT)
  793. continue;
  794. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  795. continue;
  796. float value;
  797. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  798. {
  799. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  800. }
  801. else
  802. {
  803. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  804. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  805. value = std::rint(value);
  806. }
  807. setParameterValue(k, value, false, false, false);
  808. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  809. }
  810. break;
  811. }
  812. case kEngineControlEventTypeMidiBank:
  813. case kEngineControlEventTypeMidiProgram:
  814. case kEngineControlEventTypeAllSoundOff:
  815. case kEngineControlEventTypeAllNotesOff:
  816. break;
  817. }
  818. break;
  819. }
  820. case kEngineEventTypeMidi:
  821. // ignored in LADSPA
  822. break;
  823. }
  824. }
  825. kData->postRtEvents.trySplice();
  826. if (frames > timeOffset)
  827. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  828. } // End of Event Input and Processing
  829. // --------------------------------------------------------------------------------------------------------
  830. // Plugin processing (no events)
  831. else
  832. {
  833. processSingle(inBuffer, outBuffer, frames, 0);
  834. } // End of Plugin processing (no events)
  835. CARLA_PROCESS_CONTINUE_CHECK;
  836. // --------------------------------------------------------------------------------------------------------
  837. // Control Output
  838. if (kData->event.portOut != nullptr)
  839. {
  840. uint8_t channel;
  841. uint16_t param;
  842. float value;
  843. for (k=0; k < kData->param.count; ++k)
  844. {
  845. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  846. continue;
  847. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  848. if (kData->param.data[k].midiCC > 0)
  849. {
  850. channel = kData->param.data[k].midiChannel;
  851. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  852. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  853. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  854. }
  855. }
  856. } // End of Control Output
  857. }
  858. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  859. {
  860. CARLA_ASSERT(frames > 0);
  861. if (frames == 0)
  862. return false;
  863. if (kData->audioIn.count > 0)
  864. {
  865. CARLA_ASSERT(inBuffer != nullptr);
  866. if (inBuffer == nullptr)
  867. return false;
  868. }
  869. if (kData->audioOut.count > 0)
  870. {
  871. CARLA_ASSERT(outBuffer != nullptr);
  872. if (outBuffer == nullptr)
  873. return false;
  874. }
  875. uint32_t i, k;
  876. // --------------------------------------------------------------------------------------------------------
  877. // Try lock, silence otherwise
  878. if (kData->engine->isOffline())
  879. {
  880. kData->singleMutex.lock();
  881. }
  882. else if (! kData->singleMutex.tryLock())
  883. {
  884. for (i=0; i < kData->audioOut.count; ++i)
  885. {
  886. for (k=0; k < frames; ++k)
  887. outBuffer[i][k+timeOffset] = 0.0f;
  888. }
  889. return false;
  890. }
  891. // --------------------------------------------------------------------------------------------------------
  892. // Reset audio buffers
  893. for (i=0; i < kData->audioIn.count; ++i)
  894. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  895. for (i=0; i < kData->audioOut.count; ++i)
  896. carla_zeroFloat(fAudioOutBuffers[i], frames);
  897. // --------------------------------------------------------------------------------------------------------
  898. // Run plugin
  899. fDescriptor->run(fHandle, frames);
  900. if (fHandle2 != nullptr)
  901. fDescriptor->run(fHandle2, frames);
  902. #ifndef BUILD_BRIDGE
  903. // --------------------------------------------------------------------------------------------------------
  904. // Post-processing (dry/wet, volume and balance)
  905. {
  906. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  907. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  908. bool isPair;
  909. float bufValue, oldBufLeft[doBalance ? frames : 1];
  910. for (i=0; i < kData->audioOut.count; ++i)
  911. {
  912. // Dry/Wet
  913. if (doDryWet)
  914. {
  915. for (k=0; k < frames; ++k)
  916. {
  917. // TODO
  918. //if (k < kData->latency && kData->latency < frames)
  919. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  920. //else
  921. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  922. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  923. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  924. }
  925. }
  926. // Balance
  927. if (doBalance)
  928. {
  929. isPair = (i % 2 == 0);
  930. if (isPair)
  931. {
  932. CARLA_ASSERT(i+1 < kData->audioOut.count);
  933. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  934. }
  935. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  936. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  937. for (k=0; k < frames; ++k)
  938. {
  939. if (isPair)
  940. {
  941. // left
  942. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  943. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  944. }
  945. else
  946. {
  947. // right
  948. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  949. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  950. }
  951. }
  952. }
  953. // Volume (and buffer copy)
  954. {
  955. for (k=0; k < frames; ++k)
  956. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  957. }
  958. }
  959. #if 0
  960. // Latency, save values for next callback, TODO
  961. if (kData->latency > 0 && kData->latency < frames)
  962. {
  963. for (i=0; i < kData->audioIn.count; ++i)
  964. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  965. }
  966. #endif
  967. } // End of Post-processing
  968. #else
  969. for (i=0; i < kData->audioOut.count; ++i)
  970. {
  971. for (k=0; k < frames; ++k)
  972. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  973. }
  974. #endif
  975. // --------------------------------------------------------------------------------------------------------
  976. kData->singleMutex.unlock();
  977. return true;
  978. }
  979. void bufferSizeChanged(const uint32_t newBufferSize) override
  980. {
  981. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  982. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  983. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  984. {
  985. if (fAudioInBuffers[i] != nullptr)
  986. delete[] fAudioInBuffers[i];
  987. fAudioInBuffers[i] = new float[newBufferSize];
  988. }
  989. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  990. {
  991. if (fAudioOutBuffers[i] != nullptr)
  992. delete[] fAudioOutBuffers[i];
  993. fAudioOutBuffers[i] = new float[newBufferSize];
  994. }
  995. if (fHandle2 == nullptr)
  996. {
  997. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  998. {
  999. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1000. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1001. }
  1002. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1003. {
  1004. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1005. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1006. }
  1007. }
  1008. else
  1009. {
  1010. if (kData->audioIn.count > 0)
  1011. {
  1012. CARLA_ASSERT(kData->audioIn.count == 2);
  1013. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1014. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1015. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1016. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1017. }
  1018. if (kData->audioOut.count > 0)
  1019. {
  1020. CARLA_ASSERT(kData->audioOut.count == 2);
  1021. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1022. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1023. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1024. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1025. }
  1026. }
  1027. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1028. }
  1029. void sampleRateChanged(const double newSampleRate) override
  1030. {
  1031. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1032. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1033. // TODO
  1034. (void)newSampleRate;
  1035. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1036. }
  1037. // -------------------------------------------------------------------
  1038. // Plugin buffers
  1039. void clearBuffers() override
  1040. {
  1041. carla_debug("LadspaPlugin::clearBuffers() - start");
  1042. if (fAudioInBuffers != nullptr)
  1043. {
  1044. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1045. {
  1046. if (fAudioInBuffers[i] != nullptr)
  1047. {
  1048. delete[] fAudioInBuffers[i];
  1049. fAudioInBuffers[i] = nullptr;
  1050. }
  1051. }
  1052. delete[] fAudioInBuffers;
  1053. fAudioInBuffers = nullptr;
  1054. }
  1055. if (fAudioOutBuffers != nullptr)
  1056. {
  1057. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1058. {
  1059. if (fAudioOutBuffers[i] != nullptr)
  1060. {
  1061. delete[] fAudioOutBuffers[i];
  1062. fAudioOutBuffers[i] = nullptr;
  1063. }
  1064. }
  1065. delete[] fAudioOutBuffers;
  1066. fAudioOutBuffers = nullptr;
  1067. }
  1068. if (fParamBuffers != nullptr)
  1069. {
  1070. delete[] fParamBuffers;
  1071. fParamBuffers = nullptr;
  1072. }
  1073. CarlaPlugin::clearBuffers();
  1074. carla_debug("LadspaPlugin::clearBuffers() - end");
  1075. }
  1076. // -------------------------------------------------------------------
  1077. const void* getExtraStuff() const override
  1078. {
  1079. return fRdfDescriptor;
  1080. }
  1081. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1082. {
  1083. CARLA_ASSERT(kData->engine != nullptr);
  1084. CARLA_ASSERT(kData->client == nullptr);
  1085. CARLA_ASSERT(filename != nullptr);
  1086. CARLA_ASSERT(label != nullptr);
  1087. // ---------------------------------------------------------------
  1088. // first checks
  1089. if (kData->engine == nullptr)
  1090. {
  1091. return false;
  1092. }
  1093. if (kData->client != nullptr)
  1094. {
  1095. kData->engine->setLastError("Plugin client is already registered");
  1096. return false;
  1097. }
  1098. if (filename == nullptr)
  1099. {
  1100. kData->engine->setLastError("null filename");
  1101. return false;
  1102. }
  1103. if (label == nullptr)
  1104. {
  1105. kData->engine->setLastError("null label");
  1106. return false;
  1107. }
  1108. // ---------------------------------------------------------------
  1109. // open DLL
  1110. if (! kData->libOpen(filename))
  1111. {
  1112. kData->engine->setLastError(kData->libError(filename));
  1113. return false;
  1114. }
  1115. // ---------------------------------------------------------------
  1116. // get DLL main entry
  1117. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)kData->libSymbol("ladspa_descriptor");
  1118. if (descFn == nullptr)
  1119. {
  1120. kData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1121. return false;
  1122. }
  1123. // ---------------------------------------------------------------
  1124. // get descriptor that matches label
  1125. unsigned long i = 0;
  1126. while ((fDescriptor = descFn(i++)) != nullptr)
  1127. {
  1128. if (fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1129. break;
  1130. }
  1131. if (fDescriptor == nullptr)
  1132. {
  1133. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1134. return false;
  1135. }
  1136. // ---------------------------------------------------------------
  1137. // get info
  1138. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1139. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1140. if (name != nullptr)
  1141. fName = kData->engine->getUniquePluginName(name);
  1142. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  1143. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1144. else if (fDescriptor->Name != nullptr)
  1145. fName = kData->engine->getUniquePluginName(fDescriptor->Name);
  1146. else
  1147. fName = kData->engine->getUniquePluginName(fDescriptor->Label);
  1148. fFilename = filename;
  1149. // ---------------------------------------------------------------
  1150. // register client
  1151. kData->client = kData->engine->addClient(this);
  1152. if (kData->client == nullptr || ! kData->client->isOk())
  1153. {
  1154. kData->engine->setLastError("Failed to register plugin client");
  1155. return false;
  1156. }
  1157. // ---------------------------------------------------------------
  1158. // initialize plugin
  1159. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)kData->engine->getSampleRate());
  1160. if (fHandle == nullptr)
  1161. {
  1162. kData->engine->setLastError("Plugin failed to initialize");
  1163. return false;
  1164. }
  1165. // ---------------------------------------------------------------
  1166. // load plugin settings
  1167. {
  1168. #ifdef __USE_GNU
  1169. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  1170. #else
  1171. const bool isDssiVst = fFilename.contains("dssi-vst");
  1172. #endif
  1173. // set default options
  1174. fOptions = 0x0;
  1175. if (isDssiVst)
  1176. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1177. if (kData->engine->getOptions().forceStereo)
  1178. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1179. // load settings
  1180. kData->idStr = "LADSPA/";
  1181. kData->idStr += std::strrchr(filename, OS_SEP)+1;
  1182. kData->idStr += "/";
  1183. kData->idStr += CarlaString(uniqueId());
  1184. kData->idStr += "/";
  1185. kData->idStr += label;
  1186. fOptions = kData->loadSettings(fOptions, availableOptions());
  1187. // ignore settings, we need this anyway
  1188. if (isDssiVst)
  1189. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1190. }
  1191. return true;
  1192. }
  1193. private:
  1194. LADSPA_Handle fHandle;
  1195. LADSPA_Handle fHandle2;
  1196. const LADSPA_Descriptor* fDescriptor;
  1197. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1198. float** fAudioInBuffers;
  1199. float** fAudioOutBuffers;
  1200. float* fParamBuffers;
  1201. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1202. };
  1203. CARLA_BACKEND_END_NAMESPACE
  1204. #else // WANT_LADSPA
  1205. # warning Building without LADSPA support
  1206. #endif
  1207. CARLA_BACKEND_START_NAMESPACE
  1208. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1209. {
  1210. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  1211. #ifdef WANT_LADSPA
  1212. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1213. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1214. {
  1215. delete plugin;
  1216. return nullptr;
  1217. }
  1218. plugin->reload();
  1219. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1220. {
  1221. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1222. delete plugin;
  1223. return nullptr;
  1224. }
  1225. return plugin;
  1226. #else
  1227. init.engine->setLastError("LADSPA support not available");
  1228. return nullptr;
  1229. #endif
  1230. }
  1231. CARLA_BACKEND_END_NAMESPACE