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.

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