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.

1541 lines
52KB

  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 doc/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. pData->singleMutex.lock();
  43. pData->masterMutex.lock();
  44. if (pData->client != nullptr && pData->client->isActive())
  45. pData->client->deactivate();
  46. if (pData->active)
  47. {
  48. deactivate();
  49. pData->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 getType() const noexcept override
  74. {
  75. return PLUGIN_LADSPA;
  76. }
  77. PluginCategory getCategory() const 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 getUniqueId() const override
  112. {
  113. CARLA_ASSERT(fDescriptor != nullptr);
  114. return fDescriptor->UniqueID;
  115. }
  116. // -------------------------------------------------------------------
  117. // Information (count)
  118. uint32_t getParameterScalePointCount(const uint32_t parameterId) const override
  119. {
  120. CARLA_ASSERT(parameterId < pData->param.count);
  121. const int32_t rindex(pData->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 getAvailableOptions() const 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_BUFFERS;
  144. if (pData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  145. {
  146. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  147. options |= PLUGIN_OPTION_FORCE_STEREO;
  148. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  149. options |= PLUGIN_OPTION_FORCE_STEREO;
  150. }
  151. return options;
  152. }
  153. float getParameterValue(const uint32_t parameterId) const override
  154. {
  155. CARLA_ASSERT(fParamBuffers != nullptr);
  156. CARLA_ASSERT(parameterId < pData->param.count);
  157. return fParamBuffers[parameterId];
  158. }
  159. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const override
  160. {
  161. CARLA_ASSERT(fRdfDescriptor != nullptr);
  162. CARLA_ASSERT(parameterId < pData->param.count);
  163. CARLA_ASSERT(scalePointId < getParameterScalePointCount(parameterId));
  164. const int32_t rindex(pData->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) const 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) const 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) const 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) const 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) const override
  213. {
  214. CARLA_ASSERT(fDescriptor != nullptr);
  215. CARLA_ASSERT(parameterId < pData->param.count);
  216. const int32_t rindex(pData->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) const override
  223. {
  224. CARLA_ASSERT(parameterId < pData->param.count);
  225. const int32_t rindex(pData->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) const override
  238. {
  239. CARLA_ASSERT(parameterId < pData->param.count);
  240. const int32_t rindex(pData->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) const override
  272. {
  273. CARLA_ASSERT(fRdfDescriptor != nullptr);
  274. CARLA_ASSERT(parameterId < pData->param.count);
  275. CARLA_ASSERT(scalePointId < getParameterScalePointCount(parameterId));
  276. const int32_t rindex(pData->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 < pData->param.count);
  303. const float fixedValue(pData->param.getFixedValue(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(pData->engine != nullptr);
  316. CARLA_ASSERT(fDescriptor != nullptr);
  317. CARLA_ASSERT(fHandle != nullptr);
  318. if (pData->engine == nullptr)
  319. return;
  320. if (fDescriptor == nullptr)
  321. return;
  322. if (fHandle == nullptr)
  323. return;
  324. const ProcessMode processMode(pData->engine->getProccessMode());
  325. // Safely disable plugin for reload
  326. const ScopedDisabler sd(this);
  327. if (pData->active)
  328. deactivate();
  329. clearBuffers();
  330. const float sampleRate(static_cast<float>(pData->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. pData->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. pData->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. pData->param.createNew(params);
  393. fParamBuffers = new float[params];
  394. carla_zeroFloat(fParamBuffers, params);
  395. }
  396. const uint portNameSize(pData->engine->getMaxPortNameSize());
  397. CarlaString portName;
  398. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  399. {
  400. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  401. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  402. const bool hasPortRDF = (fRdfDescriptor != nullptr && i < fRdfDescriptor->PortCount);
  403. CARLA_ASSERT(fDescriptor->PortNames[i] != nullptr);
  404. if (LADSPA_IS_PORT_AUDIO(portType))
  405. {
  406. portName.clear();
  407. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  408. {
  409. portName = fName;
  410. portName += ":";
  411. }
  412. portName += fDescriptor->PortNames[i];
  413. portName.truncate(portNameSize);
  414. if (LADSPA_IS_PORT_INPUT(portType))
  415. {
  416. j = iAudioIn++;
  417. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  418. pData->audioIn.ports[j].rindex = i;
  419. if (forcedStereoIn)
  420. {
  421. portName += "_2";
  422. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  423. pData->audioIn.ports[1].rindex = i;
  424. }
  425. }
  426. else if (LADSPA_IS_PORT_OUTPUT(portType))
  427. {
  428. j = iAudioOut++;
  429. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  430. pData->audioOut.ports[j].rindex = i;
  431. if (forcedStereoOut)
  432. {
  433. portName += "_2";
  434. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  435. pData->audioOut.ports[1].rindex = i;
  436. }
  437. }
  438. else
  439. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  440. }
  441. else if (LADSPA_IS_PORT_CONTROL(portType))
  442. {
  443. j = iCtrl++;
  444. pData->param.data[j].index = j;
  445. pData->param.data[j].rindex = i;
  446. pData->param.data[j].hints = 0x0;
  447. pData->param.data[j].midiChannel = 0;
  448. pData->param.data[j].midiCC = -1;
  449. float min, max, def, step, stepSmall, stepLarge;
  450. // min value
  451. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  452. min = portRangeHints.LowerBound;
  453. else
  454. min = 0.0f;
  455. // max value
  456. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  457. max = portRangeHints.UpperBound;
  458. else
  459. max = 1.0f;
  460. if (min > max)
  461. max = min;
  462. else if (max < min)
  463. min = max;
  464. if (max - min == 0.0f)
  465. {
  466. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  467. max = min + 0.1f;
  468. }
  469. // default value
  470. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  471. def = fRdfDescriptor->Ports[i].Default;
  472. else
  473. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  474. if (def < min)
  475. def = min;
  476. else if (def > max)
  477. def = max;
  478. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  479. {
  480. min *= sampleRate;
  481. max *= sampleRate;
  482. def *= sampleRate;
  483. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  484. }
  485. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  486. {
  487. step = max - min;
  488. stepSmall = step;
  489. stepLarge = step;
  490. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  491. }
  492. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  493. {
  494. step = 1.0f;
  495. stepSmall = 1.0f;
  496. stepLarge = 10.0f;
  497. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  498. }
  499. else
  500. {
  501. float range = max - min;
  502. step = range/100.0f;
  503. stepSmall = range/1000.0f;
  504. stepLarge = range/10.0f;
  505. }
  506. if (LADSPA_IS_PORT_INPUT(portType))
  507. {
  508. pData->param.data[j].type = PARAMETER_INPUT;
  509. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  510. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  511. needsCtrlIn = true;
  512. }
  513. else if (LADSPA_IS_PORT_OUTPUT(portType))
  514. {
  515. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  516. {
  517. min = 0.0f;
  518. max = sampleRate;
  519. def = 0.0f;
  520. step = 1.0f;
  521. stepSmall = 1.0f;
  522. stepLarge = 1.0f;
  523. pData->param.data[j].type = PARAMETER_LATENCY;
  524. pData->param.data[j].hints = 0;
  525. }
  526. else if (std::strcmp(fDescriptor->PortNames[i], "_sample-rate") == 0)
  527. {
  528. def = sampleRate;
  529. step = 1.0f;
  530. stepSmall = 1.0f;
  531. stepLarge = 1.0f;
  532. pData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  533. pData->param.data[j].hints = 0;
  534. }
  535. else
  536. {
  537. pData->param.data[j].type = PARAMETER_OUTPUT;
  538. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  539. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  540. needsCtrlOut = true;
  541. }
  542. }
  543. else
  544. {
  545. pData->param.data[j].type = PARAMETER_UNKNOWN;
  546. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  547. }
  548. // extra parameter hints
  549. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  550. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  551. // check for scalepoints, require at least 2 to make it useful
  552. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount > 1)
  553. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  554. pData->param.ranges[j].min = min;
  555. pData->param.ranges[j].max = max;
  556. pData->param.ranges[j].def = def;
  557. pData->param.ranges[j].step = step;
  558. pData->param.ranges[j].stepSmall = stepSmall;
  559. pData->param.ranges[j].stepLarge = stepLarge;
  560. // Start parameters in their default values
  561. fParamBuffers[j] = def;
  562. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  563. if (fHandle2 != nullptr)
  564. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  565. }
  566. else
  567. {
  568. // Not Audio or Control
  569. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  570. fDescriptor->connect_port(fHandle, i, nullptr);
  571. if (fHandle2 != nullptr)
  572. fDescriptor->connect_port(fHandle2, i, nullptr);
  573. }
  574. }
  575. if (needsCtrlIn)
  576. {
  577. portName.clear();
  578. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  579. {
  580. portName = fName;
  581. portName += ":";
  582. }
  583. portName += "events-in";
  584. portName.truncate(portNameSize);
  585. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  586. }
  587. if (needsCtrlOut)
  588. {
  589. portName.clear();
  590. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  591. {
  592. portName = fName;
  593. portName += ":";
  594. }
  595. portName += "events-out";
  596. portName.truncate(portNameSize);
  597. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  598. }
  599. if (forcedStereoIn || forcedStereoOut)
  600. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  601. else
  602. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  603. // plugin hints
  604. fHints = 0x0;
  605. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  606. fHints |= PLUGIN_IS_RTSAFE;
  607. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  608. fHints |= PLUGIN_CAN_DRYWET;
  609. if (aOuts > 0)
  610. fHints |= PLUGIN_CAN_VOLUME;
  611. if (aOuts >= 2 && aOuts % 2 == 0)
  612. fHints |= PLUGIN_CAN_BALANCE;
  613. // extra plugin hints
  614. pData->extraHints = 0x0;
  615. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  616. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  617. // check latency
  618. if (fHints & PLUGIN_CAN_DRYWET)
  619. {
  620. for (uint32_t i=0; i < pData->param.count; ++i)
  621. {
  622. if (pData->param.data[i].type != PARAMETER_LATENCY)
  623. continue;
  624. // we need to pre-run the plugin so it can update its latency control-port
  625. float tmpIn[aIns][2];
  626. float tmpOut[aOuts][2];
  627. for (j=0; j < aIns; ++j)
  628. {
  629. tmpIn[j][0] = 0.0f;
  630. tmpIn[j][1] = 0.0f;
  631. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  632. }
  633. for (j=0; j < aOuts; ++j)
  634. {
  635. tmpOut[j][0] = 0.0f;
  636. tmpOut[j][1] = 0.0f;
  637. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  638. }
  639. if (fDescriptor->activate != nullptr)
  640. fDescriptor->activate(fHandle);
  641. fDescriptor->run(fHandle, 2);
  642. if (fDescriptor->deactivate != nullptr)
  643. fDescriptor->deactivate(fHandle);
  644. const uint32_t latency = (uint32_t)fParamBuffers[i];
  645. if (pData->latency != latency)
  646. {
  647. pData->latency = latency;
  648. pData->client->setLatency(latency);
  649. pData->recreateLatencyBuffers();
  650. }
  651. break;
  652. }
  653. }
  654. bufferSizeChanged(pData->engine->getBufferSize());
  655. if (pData->active)
  656. activate();
  657. carla_debug("LadspaPlugin::reload() - end");
  658. }
  659. // -------------------------------------------------------------------
  660. // Plugin processing
  661. void activate() override
  662. {
  663. CARLA_ASSERT(fDescriptor != nullptr);
  664. CARLA_ASSERT(fHandle != nullptr);
  665. if (fDescriptor->activate != nullptr)
  666. {
  667. fDescriptor->activate(fHandle);
  668. if (fHandle2 != nullptr)
  669. fDescriptor->activate(fHandle2);
  670. }
  671. }
  672. void deactivate() override
  673. {
  674. CARLA_ASSERT(fDescriptor != nullptr);
  675. CARLA_ASSERT(fHandle != nullptr);
  676. if (fDescriptor->deactivate != nullptr)
  677. {
  678. fDescriptor->deactivate(fHandle);
  679. if (fHandle2 != nullptr)
  680. fDescriptor->deactivate(fHandle2);
  681. }
  682. }
  683. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  684. {
  685. uint32_t i, k;
  686. // --------------------------------------------------------------------------------------------------------
  687. // Check if active
  688. if (! pData->active)
  689. {
  690. // disable any output sound
  691. for (i=0; i < pData->audioOut.count; ++i)
  692. carla_zeroFloat(outBuffer[i], frames);
  693. return;
  694. }
  695. // --------------------------------------------------------------------------------------------------------
  696. // Check if needs reset
  697. if (pData->needsReset)
  698. {
  699. if (pData->latency > 0)
  700. {
  701. for (i=0; i < pData->audioIn.count; ++i)
  702. carla_zeroFloat(pData->latencyBuffers[i], pData->latency);
  703. }
  704. pData->needsReset = false;
  705. }
  706. // --------------------------------------------------------------------------------------------------------
  707. // Event Input and Processing
  708. if (pData->event.portIn != nullptr)
  709. {
  710. // ----------------------------------------------------------------------------------------------------
  711. // Event Input (System)
  712. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  713. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  714. uint32_t timeOffset = 0;
  715. for (i=0; i < nEvents; ++i)
  716. {
  717. const EngineEvent& event(pData->event.portIn->getEvent(i));
  718. time = event.time;
  719. if (time >= frames)
  720. continue;
  721. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  722. if (time > timeOffset && sampleAccurate)
  723. {
  724. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  725. timeOffset = time;
  726. }
  727. // Control change
  728. switch (event.type)
  729. {
  730. case kEngineEventTypeNull:
  731. break;
  732. case kEngineEventTypeControl:
  733. {
  734. const EngineControlEvent& ctrlEvent = event.ctrl;
  735. switch (ctrlEvent.type)
  736. {
  737. case kEngineControlEventTypeNull:
  738. break;
  739. case kEngineControlEventTypeParameter:
  740. {
  741. #ifndef BUILD_BRIDGE
  742. // Control backend stuff
  743. if (event.channel == pData->ctrlChannel)
  744. {
  745. float value;
  746. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  747. {
  748. value = ctrlEvent.value;
  749. setDryWet(value, false, false);
  750. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  751. }
  752. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  753. {
  754. value = ctrlEvent.value*127.0f/100.0f;
  755. setVolume(value, false, false);
  756. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  757. }
  758. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  759. {
  760. float left, right;
  761. value = ctrlEvent.value/0.5f - 1.0f;
  762. if (value < 0.0f)
  763. {
  764. left = -1.0f;
  765. right = (value*2.0f)+1.0f;
  766. }
  767. else if (value > 0.0f)
  768. {
  769. left = (value*2.0f)-1.0f;
  770. right = 1.0f;
  771. }
  772. else
  773. {
  774. left = -1.0f;
  775. right = 1.0f;
  776. }
  777. setBalanceLeft(left, false, false);
  778. setBalanceRight(right, false, false);
  779. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  780. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  781. }
  782. }
  783. #endif
  784. // Control plugin parameters
  785. for (k=0; k < pData->param.count; ++k)
  786. {
  787. if (pData->param.data[k].midiChannel != event.channel)
  788. continue;
  789. if (pData->param.data[k].midiCC != ctrlEvent.param)
  790. continue;
  791. if (pData->param.data[k].type != PARAMETER_INPUT)
  792. continue;
  793. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  794. continue;
  795. float value;
  796. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  797. {
  798. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  799. }
  800. else
  801. {
  802. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  803. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  804. value = std::rint(value);
  805. }
  806. setParameterValue(k, value, false, false, false);
  807. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  808. }
  809. break;
  810. }
  811. case kEngineControlEventTypeMidiBank:
  812. case kEngineControlEventTypeMidiProgram:
  813. case kEngineControlEventTypeAllSoundOff:
  814. case kEngineControlEventTypeAllNotesOff:
  815. break;
  816. }
  817. break;
  818. }
  819. case kEngineEventTypeMidi:
  820. // ignored in LADSPA
  821. break;
  822. }
  823. }
  824. pData->postRtEvents.trySplice();
  825. if (frames > timeOffset)
  826. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  827. } // End of Event Input and Processing
  828. // --------------------------------------------------------------------------------------------------------
  829. // Plugin processing (no events)
  830. else
  831. {
  832. processSingle(inBuffer, outBuffer, frames, 0);
  833. } // End of Plugin processing (no events)
  834. CARLA_PROCESS_CONTINUE_CHECK;
  835. // --------------------------------------------------------------------------------------------------------
  836. // Control Output
  837. if (pData->event.portOut != nullptr)
  838. {
  839. uint8_t channel;
  840. uint16_t param;
  841. float value;
  842. for (k=0; k < pData->param.count; ++k)
  843. {
  844. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  845. continue;
  846. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  847. if (pData->param.data[k].midiCC > 0)
  848. {
  849. channel = pData->param.data[k].midiChannel;
  850. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  851. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  852. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  853. }
  854. }
  855. } // End of Control Output
  856. }
  857. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  858. {
  859. CARLA_ASSERT(frames > 0);
  860. if (frames == 0)
  861. return false;
  862. if (pData->audioIn.count > 0)
  863. {
  864. CARLA_ASSERT(inBuffer != nullptr);
  865. if (inBuffer == nullptr)
  866. return false;
  867. }
  868. if (pData->audioOut.count > 0)
  869. {
  870. CARLA_ASSERT(outBuffer != nullptr);
  871. if (outBuffer == nullptr)
  872. return false;
  873. }
  874. uint32_t i, k;
  875. // --------------------------------------------------------------------------------------------------------
  876. // Try lock, silence otherwise
  877. if (pData->engine->isOffline())
  878. {
  879. pData->singleMutex.lock();
  880. }
  881. else if (! pData->singleMutex.tryLock())
  882. {
  883. for (i=0; i < pData->audioOut.count; ++i)
  884. {
  885. for (k=0; k < frames; ++k)
  886. outBuffer[i][k+timeOffset] = 0.0f;
  887. }
  888. return false;
  889. }
  890. // --------------------------------------------------------------------------------------------------------
  891. // Reset audio buffers
  892. for (i=0; i < pData->audioIn.count; ++i)
  893. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  894. for (i=0; i < pData->audioOut.count; ++i)
  895. carla_zeroFloat(fAudioOutBuffers[i], frames);
  896. // --------------------------------------------------------------------------------------------------------
  897. // Run plugin
  898. fDescriptor->run(fHandle, frames);
  899. if (fHandle2 != nullptr)
  900. fDescriptor->run(fHandle2, frames);
  901. #ifndef BUILD_BRIDGE
  902. // --------------------------------------------------------------------------------------------------------
  903. // Post-processing (dry/wet, volume and balance)
  904. {
  905. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  906. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  907. bool isPair;
  908. float bufValue, oldBufLeft[doBalance ? frames : 1];
  909. for (i=0; i < pData->audioOut.count; ++i)
  910. {
  911. // Dry/Wet
  912. if (doDryWet)
  913. {
  914. for (k=0; k < frames; ++k)
  915. {
  916. // TODO
  917. //if (k < pData->latency && pData->latency < frames)
  918. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  919. //else
  920. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  921. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  922. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  923. }
  924. }
  925. // Balance
  926. if (doBalance)
  927. {
  928. isPair = (i % 2 == 0);
  929. if (isPair)
  930. {
  931. CARLA_ASSERT(i+1 < pData->audioOut.count);
  932. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  933. }
  934. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  935. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  936. for (k=0; k < frames; ++k)
  937. {
  938. if (isPair)
  939. {
  940. // left
  941. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  942. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  943. }
  944. else
  945. {
  946. // right
  947. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  948. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  949. }
  950. }
  951. }
  952. // Volume (and buffer copy)
  953. {
  954. for (k=0; k < frames; ++k)
  955. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  956. }
  957. }
  958. #if 0
  959. // Latency, save values for next callback, TODO
  960. if (pData->latency > 0 && pData->latency < frames)
  961. {
  962. for (i=0; i < pData->audioIn.count; ++i)
  963. carla_copyFloat(pData->latencyBuffers[i], inBuffer[i] + (frames - pData->latency), pData->latency);
  964. }
  965. #endif
  966. } // End of Post-processing
  967. #else
  968. for (i=0; i < pData->audioOut.count; ++i)
  969. {
  970. for (k=0; k < frames; ++k)
  971. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  972. }
  973. #endif
  974. // --------------------------------------------------------------------------------------------------------
  975. pData->singleMutex.unlock();
  976. return true;
  977. }
  978. void bufferSizeChanged(const uint32_t newBufferSize) override
  979. {
  980. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  981. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  982. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  983. {
  984. if (fAudioInBuffers[i] != nullptr)
  985. delete[] fAudioInBuffers[i];
  986. fAudioInBuffers[i] = new float[newBufferSize];
  987. }
  988. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  989. {
  990. if (fAudioOutBuffers[i] != nullptr)
  991. delete[] fAudioOutBuffers[i];
  992. fAudioOutBuffers[i] = new float[newBufferSize];
  993. }
  994. if (fHandle2 == nullptr)
  995. {
  996. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  997. {
  998. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  999. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1000. }
  1001. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1002. {
  1003. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1004. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1005. }
  1006. }
  1007. else
  1008. {
  1009. if (pData->audioIn.count > 0)
  1010. {
  1011. CARLA_ASSERT(pData->audioIn.count == 2);
  1012. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1013. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1014. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1015. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1016. }
  1017. if (pData->audioOut.count > 0)
  1018. {
  1019. CARLA_ASSERT(pData->audioOut.count == 2);
  1020. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1021. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1022. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1023. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1024. }
  1025. }
  1026. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1027. }
  1028. void sampleRateChanged(const double newSampleRate) override
  1029. {
  1030. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1031. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1032. // TODO
  1033. (void)newSampleRate;
  1034. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1035. }
  1036. // -------------------------------------------------------------------
  1037. // Plugin buffers
  1038. void clearBuffers() override
  1039. {
  1040. carla_debug("LadspaPlugin::clearBuffers() - start");
  1041. if (fAudioInBuffers != nullptr)
  1042. {
  1043. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1044. {
  1045. if (fAudioInBuffers[i] != nullptr)
  1046. {
  1047. delete[] fAudioInBuffers[i];
  1048. fAudioInBuffers[i] = nullptr;
  1049. }
  1050. }
  1051. delete[] fAudioInBuffers;
  1052. fAudioInBuffers = nullptr;
  1053. }
  1054. if (fAudioOutBuffers != nullptr)
  1055. {
  1056. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1057. {
  1058. if (fAudioOutBuffers[i] != nullptr)
  1059. {
  1060. delete[] fAudioOutBuffers[i];
  1061. fAudioOutBuffers[i] = nullptr;
  1062. }
  1063. }
  1064. delete[] fAudioOutBuffers;
  1065. fAudioOutBuffers = nullptr;
  1066. }
  1067. if (fParamBuffers != nullptr)
  1068. {
  1069. delete[] fParamBuffers;
  1070. fParamBuffers = nullptr;
  1071. }
  1072. CarlaPlugin::clearBuffers();
  1073. carla_debug("LadspaPlugin::clearBuffers() - end");
  1074. }
  1075. // -------------------------------------------------------------------
  1076. const void* getExtraStuff() const noexcept override
  1077. {
  1078. return fRdfDescriptor;
  1079. }
  1080. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1081. {
  1082. CARLA_ASSERT(pData->engine != nullptr);
  1083. CARLA_ASSERT(pData->client == nullptr);
  1084. CARLA_ASSERT(filename != nullptr);
  1085. CARLA_ASSERT(label != nullptr);
  1086. // ---------------------------------------------------------------
  1087. // first checks
  1088. if (pData->engine == nullptr)
  1089. {
  1090. return false;
  1091. }
  1092. if (pData->client != nullptr)
  1093. {
  1094. pData->engine->setLastError("Plugin client is already registered");
  1095. return false;
  1096. }
  1097. if (filename == nullptr)
  1098. {
  1099. pData->engine->setLastError("null filename");
  1100. return false;
  1101. }
  1102. if (label == nullptr)
  1103. {
  1104. pData->engine->setLastError("null label");
  1105. return false;
  1106. }
  1107. // ---------------------------------------------------------------
  1108. // open DLL
  1109. if (! pData->libOpen(filename))
  1110. {
  1111. pData->engine->setLastError(pData->libError(filename));
  1112. return false;
  1113. }
  1114. // ---------------------------------------------------------------
  1115. // get DLL main entry
  1116. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)pData->libSymbol("ladspa_descriptor");
  1117. if (descFn == nullptr)
  1118. {
  1119. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1120. return false;
  1121. }
  1122. // ---------------------------------------------------------------
  1123. // get descriptor that matches label
  1124. unsigned long i = 0;
  1125. while ((fDescriptor = descFn(i++)) != nullptr)
  1126. {
  1127. if (fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1128. break;
  1129. }
  1130. if (fDescriptor == nullptr)
  1131. {
  1132. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1133. return false;
  1134. }
  1135. // ---------------------------------------------------------------
  1136. // get info
  1137. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1138. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1139. if (name != nullptr)
  1140. fName = pData->engine->getUniquePluginName(name);
  1141. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  1142. fName = pData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1143. else if (fDescriptor->Name != nullptr)
  1144. fName = pData->engine->getUniquePluginName(fDescriptor->Name);
  1145. else
  1146. fName = pData->engine->getUniquePluginName(fDescriptor->Label);
  1147. fFilename = filename;
  1148. // ---------------------------------------------------------------
  1149. // register client
  1150. pData->client = pData->engine->addClient(this);
  1151. if (pData->client == nullptr || ! pData->client->isOk())
  1152. {
  1153. pData->engine->setLastError("Failed to register plugin client");
  1154. return false;
  1155. }
  1156. // ---------------------------------------------------------------
  1157. // initialize plugin
  1158. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)pData->engine->getSampleRate());
  1159. if (fHandle == nullptr)
  1160. {
  1161. pData->engine->setLastError("Plugin failed to initialize");
  1162. return false;
  1163. }
  1164. // ---------------------------------------------------------------
  1165. // load plugin settings
  1166. {
  1167. #ifdef __USE_GNU
  1168. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  1169. #else
  1170. const bool isDssiVst = fFilename.contains("dssi-vst");
  1171. #endif
  1172. // set default options
  1173. fOptions = 0x0;
  1174. if (isDssiVst)
  1175. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1176. if (pData->engine->getOptions().forceStereo)
  1177. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1178. // load settings
  1179. pData->idStr = "LADSPA/";
  1180. pData->idStr += std::strrchr(filename, OS_SEP)+1;
  1181. pData->idStr += "/";
  1182. pData->idStr += CarlaString(getUniqueId());
  1183. pData->idStr += "/";
  1184. pData->idStr += label;
  1185. fOptions = pData->loadSettings(fOptions, getAvailableOptions());
  1186. // ignore settings, we need this anyway
  1187. if (isDssiVst)
  1188. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1189. }
  1190. return true;
  1191. }
  1192. private:
  1193. LADSPA_Handle fHandle;
  1194. LADSPA_Handle fHandle2;
  1195. const LADSPA_Descriptor* fDescriptor;
  1196. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1197. float** fAudioInBuffers;
  1198. float** fAudioOutBuffers;
  1199. float* fParamBuffers;
  1200. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1201. };
  1202. CARLA_BACKEND_END_NAMESPACE
  1203. #else // WANT_LADSPA
  1204. # warning Building without LADSPA support
  1205. #endif
  1206. CARLA_BACKEND_START_NAMESPACE
  1207. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1208. {
  1209. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  1210. #ifdef WANT_LADSPA
  1211. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1212. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1213. {
  1214. delete plugin;
  1215. return nullptr;
  1216. }
  1217. plugin->reload();
  1218. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1219. {
  1220. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1221. delete plugin;
  1222. return nullptr;
  1223. }
  1224. return plugin;
  1225. #else
  1226. init.engine->setLastError("LADSPA support not available");
  1227. return nullptr;
  1228. #endif
  1229. }
  1230. CARLA_BACKEND_END_NAMESPACE