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.

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