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->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(static_cast<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. #ifndef BUILD_BRIDGE
  738. // Control backend stuff
  739. if (event.channel == kData->ctrlChannel)
  740. {
  741. float value;
  742. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  743. {
  744. value = ctrlEvent.value;
  745. setDryWet(value, false, false);
  746. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  747. continue;
  748. }
  749. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  750. {
  751. value = ctrlEvent.value*127.0f/100.0f;
  752. setVolume(value, false, false);
  753. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  754. continue;
  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. continue;
  780. }
  781. }
  782. #endif
  783. // Control plugin parameters
  784. for (k=0; k < kData->param.count; ++k)
  785. {
  786. if (kData->param.data[k].midiChannel != event.channel)
  787. continue;
  788. if (kData->param.data[k].midiCC != ctrlEvent.param)
  789. continue;
  790. if (kData->param.data[k].type != PARAMETER_INPUT)
  791. continue;
  792. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  793. continue;
  794. float value;
  795. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  796. {
  797. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  798. }
  799. else
  800. {
  801. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  802. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  803. value = std::rint(value);
  804. }
  805. setParameterValue(k, value, false, false, false);
  806. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  807. }
  808. break;
  809. }
  810. case kEngineControlEventTypeMidiBank:
  811. case kEngineControlEventTypeMidiProgram:
  812. case kEngineControlEventTypeAllSoundOff:
  813. case kEngineControlEventTypeAllNotesOff:
  814. break;
  815. }
  816. break;
  817. }
  818. case kEngineEventTypeMidi:
  819. // ignored in LADSPA
  820. break;
  821. }
  822. }
  823. kData->postRtEvents.trySplice();
  824. if (frames > timeOffset)
  825. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  826. } // End of Event Input and Processing
  827. // --------------------------------------------------------------------------------------------------------
  828. // Plugin processing (no events)
  829. else
  830. {
  831. processSingle(inBuffer, outBuffer, frames, 0);
  832. } // End of Plugin processing (no events)
  833. CARLA_PROCESS_CONTINUE_CHECK;
  834. // --------------------------------------------------------------------------------------------------------
  835. // Control Output
  836. if (kData->event.portOut != nullptr)
  837. {
  838. uint8_t channel;
  839. uint16_t param;
  840. float value;
  841. for (k=0; k < kData->param.count; ++k)
  842. {
  843. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  844. continue;
  845. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  846. if (kData->param.data[k].midiCC > 0)
  847. {
  848. channel = kData->param.data[k].midiChannel;
  849. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  850. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  851. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  852. }
  853. }
  854. } // End of Control Output
  855. }
  856. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  857. {
  858. CARLA_ASSERT(frames > 0);
  859. if (frames == 0)
  860. return false;
  861. if (kData->audioIn.count > 0)
  862. {
  863. CARLA_ASSERT(inBuffer != nullptr);
  864. if (inBuffer == nullptr)
  865. return false;
  866. }
  867. if (kData->audioOut.count > 0)
  868. {
  869. CARLA_ASSERT(outBuffer != nullptr);
  870. if (outBuffer == nullptr)
  871. return false;
  872. }
  873. uint32_t i, k;
  874. // --------------------------------------------------------------------------------------------------------
  875. // Try lock, silence otherwise
  876. if (kData->engine->isOffline())
  877. {
  878. kData->singleMutex.lock();
  879. }
  880. else if (! kData->singleMutex.tryLock())
  881. {
  882. for (i=0; i < kData->audioOut.count; ++i)
  883. {
  884. for (k=0; k < frames; ++k)
  885. outBuffer[i][k+timeOffset] = 0.0f;
  886. }
  887. return false;
  888. }
  889. // --------------------------------------------------------------------------------------------------------
  890. // Reset audio buffers
  891. for (i=0; i < kData->audioIn.count; ++i)
  892. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  893. for (i=0; i < kData->audioOut.count; ++i)
  894. carla_zeroFloat(fAudioOutBuffers[i], frames);
  895. // --------------------------------------------------------------------------------------------------------
  896. // Run plugin
  897. fDescriptor->run(fHandle, frames);
  898. if (fHandle2 != nullptr)
  899. fDescriptor->run(fHandle2, frames);
  900. #ifndef BUILD_BRIDGE
  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. #else
  967. for (i=0; i < kData->audioOut.count; ++i)
  968. {
  969. for (k=0; k < frames; ++k)
  970. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  971. }
  972. #endif
  973. // --------------------------------------------------------------------------------------------------------
  974. kData->singleMutex.unlock();
  975. return true;
  976. }
  977. void bufferSizeChanged(const uint32_t newBufferSize) override
  978. {
  979. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  980. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  981. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  982. {
  983. if (fAudioInBuffers[i] != nullptr)
  984. delete[] fAudioInBuffers[i];
  985. fAudioInBuffers[i] = new float[newBufferSize];
  986. }
  987. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  988. {
  989. if (fAudioOutBuffers[i] != nullptr)
  990. delete[] fAudioOutBuffers[i];
  991. fAudioOutBuffers[i] = new float[newBufferSize];
  992. }
  993. if (fHandle2 == nullptr)
  994. {
  995. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  996. {
  997. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  998. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  999. }
  1000. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1001. {
  1002. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1003. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1004. }
  1005. }
  1006. else
  1007. {
  1008. if (kData->audioIn.count > 0)
  1009. {
  1010. CARLA_ASSERT(kData->audioIn.count == 2);
  1011. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1012. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1013. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1014. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1015. }
  1016. if (kData->audioOut.count > 0)
  1017. {
  1018. CARLA_ASSERT(kData->audioOut.count == 2);
  1019. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1020. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1021. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1022. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1023. }
  1024. }
  1025. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1026. }
  1027. void sampleRateChanged(const double newSampleRate) override
  1028. {
  1029. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1030. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1031. // TODO
  1032. (void)newSampleRate;
  1033. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1034. }
  1035. // -------------------------------------------------------------------
  1036. // Plugin buffers
  1037. void clearBuffers() override
  1038. {
  1039. carla_debug("LadspaPlugin::clearBuffers() - start");
  1040. if (fAudioInBuffers != nullptr)
  1041. {
  1042. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1043. {
  1044. if (fAudioInBuffers[i] != nullptr)
  1045. {
  1046. delete[] fAudioInBuffers[i];
  1047. fAudioInBuffers[i] = nullptr;
  1048. }
  1049. }
  1050. delete[] fAudioInBuffers;
  1051. fAudioInBuffers = nullptr;
  1052. }
  1053. if (fAudioOutBuffers != nullptr)
  1054. {
  1055. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1056. {
  1057. if (fAudioOutBuffers[i] != nullptr)
  1058. {
  1059. delete[] fAudioOutBuffers[i];
  1060. fAudioOutBuffers[i] = nullptr;
  1061. }
  1062. }
  1063. delete[] fAudioOutBuffers;
  1064. fAudioOutBuffers = nullptr;
  1065. }
  1066. if (fParamBuffers != nullptr)
  1067. {
  1068. delete[] fParamBuffers;
  1069. fParamBuffers = nullptr;
  1070. }
  1071. CarlaPlugin::clearBuffers();
  1072. carla_debug("LadspaPlugin::clearBuffers() - end");
  1073. }
  1074. // -------------------------------------------------------------------
  1075. const void* getExtraStuff() override
  1076. {
  1077. return fRdfDescriptor;
  1078. }
  1079. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1080. {
  1081. CARLA_ASSERT(kData->engine != nullptr);
  1082. CARLA_ASSERT(kData->client == nullptr);
  1083. CARLA_ASSERT(filename != nullptr);
  1084. CARLA_ASSERT(label != nullptr);
  1085. // ---------------------------------------------------------------
  1086. // first checks
  1087. if (kData->engine == nullptr)
  1088. {
  1089. return false;
  1090. }
  1091. if (kData->client != nullptr)
  1092. {
  1093. kData->engine->setLastError("Plugin client is already registered");
  1094. return false;
  1095. }
  1096. if (filename == nullptr)
  1097. {
  1098. kData->engine->setLastError("null filename");
  1099. return false;
  1100. }
  1101. if (label == nullptr)
  1102. {
  1103. kData->engine->setLastError("null label");
  1104. return false;
  1105. }
  1106. // ---------------------------------------------------------------
  1107. // open DLL
  1108. if (! kData->libOpen(filename))
  1109. {
  1110. kData->engine->setLastError(kData->libError(filename));
  1111. return false;
  1112. }
  1113. // ---------------------------------------------------------------
  1114. // get DLL main entry
  1115. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)kData->libSymbol("ladspa_descriptor");
  1116. if (descFn == nullptr)
  1117. {
  1118. kData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1119. return false;
  1120. }
  1121. // ---------------------------------------------------------------
  1122. // get descriptor that matches label
  1123. unsigned long i = 0;
  1124. while ((fDescriptor = descFn(i++)) != nullptr)
  1125. {
  1126. if (fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1127. break;
  1128. }
  1129. if (fDescriptor == nullptr)
  1130. {
  1131. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1132. return false;
  1133. }
  1134. // ---------------------------------------------------------------
  1135. // get info
  1136. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1137. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1138. if (name != nullptr)
  1139. fName = kData->engine->getUniquePluginName(name);
  1140. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  1141. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1142. else if (fDescriptor->Name != nullptr)
  1143. fName = kData->engine->getUniquePluginName(fDescriptor->Name);
  1144. else
  1145. fName = kData->engine->getUniquePluginName(fDescriptor->Label);
  1146. fFilename = filename;
  1147. // ---------------------------------------------------------------
  1148. // register client
  1149. kData->client = kData->engine->addClient(this);
  1150. if (kData->client == nullptr || ! kData->client->isOk())
  1151. {
  1152. kData->engine->setLastError("Failed to register plugin client");
  1153. return false;
  1154. }
  1155. // ---------------------------------------------------------------
  1156. // initialize plugin
  1157. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)kData->engine->getSampleRate());
  1158. if (fHandle == nullptr)
  1159. {
  1160. kData->engine->setLastError("Plugin failed to initialize");
  1161. return false;
  1162. }
  1163. // ---------------------------------------------------------------
  1164. // load plugin settings
  1165. {
  1166. #ifdef __USE_GNU
  1167. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  1168. #else
  1169. const bool isDssiVst = fFilename.contains("dssi-vst");
  1170. #endif
  1171. // set default options
  1172. fOptions = 0x0;
  1173. if (isDssiVst)
  1174. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1175. if (kData->engine->getOptions().forceStereo)
  1176. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1177. // load settings
  1178. kData->idStr = "LADSPA/";
  1179. kData->idStr += std::strrchr(filename, OS_SEP)+1;
  1180. kData->idStr += "/";
  1181. kData->idStr += CarlaString(uniqueId());
  1182. kData->idStr += "/";
  1183. kData->idStr += label;
  1184. fOptions = kData->loadSettings(fOptions, availableOptions());
  1185. // ignore settings, we need this anyway
  1186. if (isDssiVst)
  1187. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1188. }
  1189. return true;
  1190. }
  1191. private:
  1192. LADSPA_Handle fHandle;
  1193. LADSPA_Handle fHandle2;
  1194. const LADSPA_Descriptor* fDescriptor;
  1195. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1196. float** fAudioInBuffers;
  1197. float** fAudioOutBuffers;
  1198. float* fParamBuffers;
  1199. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1200. };
  1201. CARLA_BACKEND_END_NAMESPACE
  1202. #else // WANT_LADSPA
  1203. # warning Building without LADSPA support
  1204. #endif
  1205. CARLA_BACKEND_START_NAMESPACE
  1206. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1207. {
  1208. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  1209. #ifdef WANT_LADSPA
  1210. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1211. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1212. {
  1213. delete plugin;
  1214. return nullptr;
  1215. }
  1216. plugin->reload();
  1217. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1218. {
  1219. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1220. delete plugin;
  1221. return nullptr;
  1222. }
  1223. return plugin;
  1224. #else
  1225. init.engine->setLastError("LADSPA support not available");
  1226. return nullptr;
  1227. #endif
  1228. }
  1229. CARLA_BACKEND_END_NAMESPACE