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.

1533 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->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 (aOuts > 0 && (aIns == aOuts || aIns == 1))
  602. fHints |= PLUGIN_CAN_DRYWET;
  603. if (aOuts > 0)
  604. fHints |= PLUGIN_CAN_VOLUME;
  605. if (aOuts >= 2 && aOuts % 2 == 0)
  606. fHints |= PLUGIN_CAN_BALANCE;
  607. // extra plugin hints
  608. kData->extraHints = 0x0;
  609. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  610. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  611. // check latency
  612. if (fHints & PLUGIN_CAN_DRYWET)
  613. {
  614. for (uint32_t i=0; i < kData->param.count; ++i)
  615. {
  616. if (kData->param.data[i].type != PARAMETER_LATENCY)
  617. continue;
  618. // we need to pre-run the plugin so it can update its latency control-port
  619. float tmpIn[aIns][2];
  620. float tmpOut[aOuts][2];
  621. for (j=0; j < aIns; ++j)
  622. {
  623. tmpIn[j][0] = 0.0f;
  624. tmpIn[j][1] = 0.0f;
  625. fDescriptor->connect_port(fHandle, kData->audioIn.ports[j].rindex, tmpIn[j]);
  626. }
  627. for (j=0; j < aOuts; ++j)
  628. {
  629. tmpOut[j][0] = 0.0f;
  630. tmpOut[j][1] = 0.0f;
  631. fDescriptor->connect_port(fHandle, kData->audioOut.ports[j].rindex, tmpOut[j]);
  632. }
  633. if (fDescriptor->activate != nullptr)
  634. fDescriptor->activate(fHandle);
  635. fDescriptor->run(fHandle, 2);
  636. if (fDescriptor->deactivate != nullptr)
  637. fDescriptor->deactivate(fHandle);
  638. const uint32_t latency = (uint32_t)fParamBuffers[i];
  639. if (kData->latency != latency)
  640. {
  641. kData->latency = latency;
  642. kData->client->setLatency(latency);
  643. kData->recreateLatencyBuffers();
  644. }
  645. break;
  646. }
  647. }
  648. bufferSizeChanged(kData->engine->getBufferSize());
  649. if (kData->active)
  650. activate();
  651. carla_debug("LadspaPlugin::reload() - end");
  652. }
  653. // -------------------------------------------------------------------
  654. // Plugin processing
  655. void activate() override
  656. {
  657. CARLA_ASSERT(fDescriptor != nullptr);
  658. if (fDescriptor->activate != nullptr)
  659. {
  660. fDescriptor->activate(fHandle);
  661. if (fHandle2 != nullptr)
  662. fDescriptor->activate(fHandle2);
  663. }
  664. }
  665. void deactivate() override
  666. {
  667. CARLA_ASSERT(fDescriptor != nullptr);
  668. if (fDescriptor->deactivate != nullptr)
  669. {
  670. fDescriptor->deactivate(fHandle);
  671. if (fHandle2 != nullptr)
  672. fDescriptor->deactivate(fHandle2);
  673. }
  674. }
  675. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  676. {
  677. uint32_t i, k;
  678. // --------------------------------------------------------------------------------------------------------
  679. // Check if active
  680. if (! kData->active)
  681. {
  682. // disable any output sound
  683. for (i=0; i < kData->audioOut.count; ++i)
  684. carla_zeroFloat(outBuffer[i], frames);
  685. return;
  686. }
  687. // --------------------------------------------------------------------------------------------------------
  688. // Check if needs reset
  689. if (kData->needsReset)
  690. {
  691. if (kData->latency > 0)
  692. {
  693. for (i=0; i < kData->audioIn.count; ++i)
  694. carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  695. }
  696. kData->needsReset = false;
  697. }
  698. // --------------------------------------------------------------------------------------------------------
  699. // Event Input and Processing
  700. if (kData->event.portIn != nullptr)
  701. {
  702. // ----------------------------------------------------------------------------------------------------
  703. // Event Input (System)
  704. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  705. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  706. uint32_t timeOffset = 0;
  707. for (i=0; i < nEvents; ++i)
  708. {
  709. const EngineEvent& event = kData->event.portIn->getEvent(i);
  710. time = event.time;
  711. if (time >= frames)
  712. continue;
  713. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  714. if (time > timeOffset && sampleAccurate)
  715. {
  716. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  717. timeOffset = time;
  718. }
  719. // Control change
  720. switch (event.type)
  721. {
  722. case kEngineEventTypeNull:
  723. break;
  724. case kEngineEventTypeControl:
  725. {
  726. const EngineControlEvent& ctrlEvent = event.ctrl;
  727. switch (ctrlEvent.type)
  728. {
  729. case kEngineControlEventTypeNull:
  730. break;
  731. case kEngineControlEventTypeParameter:
  732. {
  733. // Control backend stuff
  734. if (event.channel == kData->ctrlChannel)
  735. {
  736. float value;
  737. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  738. {
  739. value = ctrlEvent.value;
  740. setDryWet(value, false, false);
  741. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  742. continue;
  743. }
  744. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  745. {
  746. value = ctrlEvent.value*127.0f/100.0f;
  747. setVolume(value, false, false);
  748. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  749. continue;
  750. }
  751. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  752. {
  753. float left, right;
  754. value = ctrlEvent.value/0.5f - 1.0f;
  755. if (value < 0.0f)
  756. {
  757. left = -1.0f;
  758. right = (value*2.0f)+1.0f;
  759. }
  760. else if (value > 0.0f)
  761. {
  762. left = (value*2.0f)-1.0f;
  763. right = 1.0f;
  764. }
  765. else
  766. {
  767. left = -1.0f;
  768. right = 1.0f;
  769. }
  770. setBalanceLeft(left, false, false);
  771. setBalanceRight(right, false, false);
  772. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  773. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  774. continue;
  775. }
  776. }
  777. // Control plugin parameters
  778. for (k=0; k < kData->param.count; ++k)
  779. {
  780. if (kData->param.data[k].midiChannel != event.channel)
  781. continue;
  782. if (kData->param.data[k].midiCC != ctrlEvent.param)
  783. continue;
  784. if (kData->param.data[k].type != PARAMETER_INPUT)
  785. continue;
  786. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  787. continue;
  788. float value;
  789. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  790. {
  791. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  792. }
  793. else
  794. {
  795. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  796. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  797. value = std::rint(value);
  798. }
  799. setParameterValue(k, value, false, false, false);
  800. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  801. }
  802. break;
  803. }
  804. case kEngineControlEventTypeMidiBank:
  805. case kEngineControlEventTypeMidiProgram:
  806. break;
  807. case kEngineControlEventTypeAllSoundOff:
  808. if (event.channel == kData->ctrlChannel)
  809. {
  810. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  811. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  812. }
  813. break;
  814. case kEngineControlEventTypeAllNotesOff:
  815. break;
  816. }
  817. break;
  818. }
  819. case kEngineEventTypeMidi:
  820. // ignored in LADSPA
  821. break;
  822. }
  823. }
  824. kData->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 (kData->event.portOut != nullptr)
  838. {
  839. uint8_t channel;
  840. uint16_t param;
  841. float value;
  842. for (k=0; k < kData->param.count; ++k)
  843. {
  844. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  845. continue;
  846. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  847. if (kData->param.data[k].midiCC > 0)
  848. {
  849. channel = kData->param.data[k].midiChannel;
  850. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  851. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  852. kData->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 (kData->audioIn.count > 0)
  863. {
  864. CARLA_ASSERT(inBuffer != nullptr);
  865. if (inBuffer == nullptr)
  866. return false;
  867. }
  868. if (kData->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 (kData->engine->isOffline())
  878. {
  879. kData->singleMutex.lock();
  880. }
  881. else if (! kData->singleMutex.tryLock())
  882. {
  883. for (i=0; i < kData->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 < kData->audioIn.count; ++i)
  893. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  894. for (i=0; i < kData->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. // --------------------------------------------------------------------------------------------------------
  902. // Post-processing (dry/wet, volume and balance)
  903. {
  904. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  905. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  906. bool isPair;
  907. float bufValue, oldBufLeft[doBalance ? frames : 1];
  908. for (i=0; i < kData->audioOut.count; ++i)
  909. {
  910. // Dry/Wet
  911. if (doDryWet)
  912. {
  913. for (k=0; k < frames; ++k)
  914. {
  915. // TODO
  916. //if (k < kData->latency && kData->latency < frames)
  917. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  918. //else
  919. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  920. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  921. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  922. }
  923. }
  924. // Balance
  925. if (doBalance)
  926. {
  927. isPair = (i % 2 == 0);
  928. if (isPair)
  929. {
  930. CARLA_ASSERT(i+1 < kData->audioOut.count);
  931. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  932. }
  933. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  934. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  935. for (k=0; k < frames; ++k)
  936. {
  937. if (isPair)
  938. {
  939. // left
  940. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  941. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  942. }
  943. else
  944. {
  945. // right
  946. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  947. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  948. }
  949. }
  950. }
  951. // Volume (and buffer copy)
  952. {
  953. for (k=0; k < frames; ++k)
  954. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  955. }
  956. }
  957. #if 0
  958. // Latency, save values for next callback, TODO
  959. if (kData->latency > 0 && kData->latency < frames)
  960. {
  961. for (i=0; i < kData->audioIn.count; ++i)
  962. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  963. }
  964. #endif
  965. } // End of Post-processing
  966. // --------------------------------------------------------------------------------------------------------
  967. kData->singleMutex.unlock();
  968. return true;
  969. }
  970. void bufferSizeChanged(const uint32_t newBufferSize) override
  971. {
  972. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  973. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  974. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  975. {
  976. if (fAudioInBuffers[i] != nullptr)
  977. delete[] fAudioInBuffers[i];
  978. fAudioInBuffers[i] = new float[newBufferSize];
  979. }
  980. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  981. {
  982. if (fAudioOutBuffers[i] != nullptr)
  983. delete[] fAudioOutBuffers[i];
  984. fAudioOutBuffers[i] = new float[newBufferSize];
  985. }
  986. if (fHandle2 == nullptr)
  987. {
  988. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  989. {
  990. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  991. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  992. }
  993. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  994. {
  995. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  996. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  997. }
  998. }
  999. else
  1000. {
  1001. if (kData->audioIn.count > 0)
  1002. {
  1003. CARLA_ASSERT(kData->audioIn.count == 2);
  1004. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1005. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1006. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1007. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1008. }
  1009. if (kData->audioOut.count > 0)
  1010. {
  1011. CARLA_ASSERT(kData->audioOut.count == 2);
  1012. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1013. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1014. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1015. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1016. }
  1017. }
  1018. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1019. }
  1020. void sampleRateChanged(const double newSampleRate) override
  1021. {
  1022. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1023. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1024. // TODO
  1025. (void)newSampleRate;
  1026. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1027. }
  1028. // -------------------------------------------------------------------
  1029. // Plugin buffers
  1030. void clearBuffers() override
  1031. {
  1032. carla_debug("LadspaPlugin::clearBuffers() - start");
  1033. if (fAudioInBuffers != nullptr)
  1034. {
  1035. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1036. {
  1037. if (fAudioInBuffers[i] != nullptr)
  1038. {
  1039. delete[] fAudioInBuffers[i];
  1040. fAudioInBuffers[i] = nullptr;
  1041. }
  1042. }
  1043. delete[] fAudioInBuffers;
  1044. fAudioInBuffers = nullptr;
  1045. }
  1046. if (fAudioOutBuffers != nullptr)
  1047. {
  1048. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1049. {
  1050. if (fAudioOutBuffers[i] != nullptr)
  1051. {
  1052. delete[] fAudioOutBuffers[i];
  1053. fAudioOutBuffers[i] = nullptr;
  1054. }
  1055. }
  1056. delete[] fAudioOutBuffers;
  1057. fAudioOutBuffers = nullptr;
  1058. }
  1059. if (fParamBuffers != nullptr)
  1060. {
  1061. delete[] fParamBuffers;
  1062. fParamBuffers = nullptr;
  1063. }
  1064. CarlaPlugin::clearBuffers();
  1065. carla_debug("LadspaPlugin::clearBuffers() - end");
  1066. }
  1067. // -------------------------------------------------------------------
  1068. const void* getExtraStuff() override
  1069. {
  1070. return fRdfDescriptor;
  1071. }
  1072. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1073. {
  1074. CARLA_ASSERT(kData->engine != nullptr);
  1075. CARLA_ASSERT(kData->client == nullptr);
  1076. CARLA_ASSERT(filename != nullptr);
  1077. CARLA_ASSERT(label != nullptr);
  1078. // ---------------------------------------------------------------
  1079. // first checks
  1080. if (kData->engine == nullptr)
  1081. {
  1082. return false;
  1083. }
  1084. if (kData->client != nullptr)
  1085. {
  1086. kData->engine->setLastError("Plugin client is already registered");
  1087. return false;
  1088. }
  1089. if (filename == nullptr)
  1090. {
  1091. kData->engine->setLastError("null filename");
  1092. return false;
  1093. }
  1094. if (label == nullptr)
  1095. {
  1096. kData->engine->setLastError("null label");
  1097. return false;
  1098. }
  1099. // ---------------------------------------------------------------
  1100. // open DLL
  1101. if (! kData->libOpen(filename))
  1102. {
  1103. kData->engine->setLastError(kData->libError(filename));
  1104. return false;
  1105. }
  1106. // ---------------------------------------------------------------
  1107. // get DLL main entry
  1108. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)kData->libSymbol("ladspa_descriptor");
  1109. if (descFn == nullptr)
  1110. {
  1111. kData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1112. return false;
  1113. }
  1114. // ---------------------------------------------------------------
  1115. // get descriptor that matches label
  1116. unsigned long i = 0;
  1117. while ((fDescriptor = descFn(i++)) != nullptr)
  1118. {
  1119. if (fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1120. break;
  1121. }
  1122. if (fDescriptor == nullptr)
  1123. {
  1124. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1125. return false;
  1126. }
  1127. // ---------------------------------------------------------------
  1128. // get info
  1129. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1130. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1131. if (name != nullptr)
  1132. fName = kData->engine->getUniquePluginName(name);
  1133. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  1134. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1135. else if (fDescriptor->Name != nullptr)
  1136. fName = kData->engine->getUniquePluginName(fDescriptor->Name);
  1137. else
  1138. fName = kData->engine->getUniquePluginName(fDescriptor->Label);
  1139. fFilename = filename;
  1140. // ---------------------------------------------------------------
  1141. // register client
  1142. kData->client = kData->engine->addClient(this);
  1143. if (kData->client == nullptr || ! kData->client->isOk())
  1144. {
  1145. kData->engine->setLastError("Failed to register plugin client");
  1146. return false;
  1147. }
  1148. // ---------------------------------------------------------------
  1149. // initialize plugin
  1150. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)kData->engine->getSampleRate());
  1151. if (fHandle == nullptr)
  1152. {
  1153. kData->engine->setLastError("Plugin failed to initialize");
  1154. return false;
  1155. }
  1156. // ---------------------------------------------------------------
  1157. // load plugin settings
  1158. {
  1159. #ifdef __USE_GNU
  1160. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  1161. #else
  1162. const bool isDssiVst = fFilename.contains("dssi-vst");
  1163. #endif
  1164. // set default options
  1165. fOptions = 0x0;
  1166. if (isDssiVst)
  1167. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1168. if (kData->engine->getOptions().forceStereo)
  1169. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1170. // load settings
  1171. kData->idStr = "LADSPA/";
  1172. kData->idStr += std::strrchr(filename, OS_SEP)+1;
  1173. kData->idStr += "/";
  1174. kData->idStr += CarlaString(uniqueId());
  1175. kData->idStr += "/";
  1176. kData->idStr += label;
  1177. fOptions = kData->loadSettings(fOptions, availableOptions());
  1178. // ignore settings, we need this anyway
  1179. if (isDssiVst)
  1180. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1181. }
  1182. return true;
  1183. }
  1184. private:
  1185. LADSPA_Handle fHandle;
  1186. LADSPA_Handle fHandle2;
  1187. const LADSPA_Descriptor* fDescriptor;
  1188. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1189. float** fAudioInBuffers;
  1190. float** fAudioOutBuffers;
  1191. float* fParamBuffers;
  1192. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1193. };
  1194. CARLA_BACKEND_END_NAMESPACE
  1195. #else // WANT_LADSPA
  1196. # warning Building without LADSPA support
  1197. #endif
  1198. CARLA_BACKEND_START_NAMESPACE
  1199. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1200. {
  1201. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  1202. #ifdef WANT_LADSPA
  1203. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1204. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1205. {
  1206. delete plugin;
  1207. return nullptr;
  1208. }
  1209. plugin->reload();
  1210. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1211. {
  1212. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1213. delete plugin;
  1214. return nullptr;
  1215. }
  1216. return plugin;
  1217. #else
  1218. init.engine->setLastError("LADSPA support not available");
  1219. return nullptr;
  1220. #endif
  1221. }
  1222. CARLA_BACKEND_END_NAMESPACE