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.

1550 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. #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. break;
  813. case kEngineControlEventTypeAllSoundOff:
  814. #ifndef BUILD_BRIDGE
  815. if (event.channel == kData->ctrlChannel)
  816. {
  817. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0f);
  818. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0f);
  819. }
  820. #endif
  821. break;
  822. case kEngineControlEventTypeAllNotesOff:
  823. break;
  824. }
  825. break;
  826. }
  827. case kEngineEventTypeMidi:
  828. // ignored in LADSPA
  829. break;
  830. }
  831. }
  832. kData->postRtEvents.trySplice();
  833. if (frames > timeOffset)
  834. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  835. } // End of Event Input and Processing
  836. // --------------------------------------------------------------------------------------------------------
  837. // Plugin processing (no events)
  838. else
  839. {
  840. processSingle(inBuffer, outBuffer, frames, 0);
  841. } // End of Plugin processing (no events)
  842. CARLA_PROCESS_CONTINUE_CHECK;
  843. // --------------------------------------------------------------------------------------------------------
  844. // Control Output
  845. if (kData->event.portOut != nullptr)
  846. {
  847. uint8_t channel;
  848. uint16_t param;
  849. float value;
  850. for (k=0; k < kData->param.count; ++k)
  851. {
  852. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  853. continue;
  854. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  855. if (kData->param.data[k].midiCC > 0)
  856. {
  857. channel = kData->param.data[k].midiChannel;
  858. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  859. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  860. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  861. }
  862. }
  863. } // End of Control Output
  864. }
  865. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  866. {
  867. CARLA_ASSERT(frames > 0);
  868. if (frames == 0)
  869. return false;
  870. if (kData->audioIn.count > 0)
  871. {
  872. CARLA_ASSERT(inBuffer != nullptr);
  873. if (inBuffer == nullptr)
  874. return false;
  875. }
  876. if (kData->audioOut.count > 0)
  877. {
  878. CARLA_ASSERT(outBuffer != nullptr);
  879. if (outBuffer == nullptr)
  880. return false;
  881. }
  882. uint32_t i, k;
  883. // --------------------------------------------------------------------------------------------------------
  884. // Try lock, silence otherwise
  885. if (kData->engine->isOffline())
  886. {
  887. kData->singleMutex.lock();
  888. }
  889. else if (! kData->singleMutex.tryLock())
  890. {
  891. for (i=0; i < kData->audioOut.count; ++i)
  892. {
  893. for (k=0; k < frames; ++k)
  894. outBuffer[i][k+timeOffset] = 0.0f;
  895. }
  896. return false;
  897. }
  898. // --------------------------------------------------------------------------------------------------------
  899. // Reset audio buffers
  900. for (i=0; i < kData->audioIn.count; ++i)
  901. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  902. for (i=0; i < kData->audioOut.count; ++i)
  903. carla_zeroFloat(fAudioOutBuffers[i], frames);
  904. // --------------------------------------------------------------------------------------------------------
  905. // Run plugin
  906. fDescriptor->run(fHandle, frames);
  907. if (fHandle2 != nullptr)
  908. fDescriptor->run(fHandle2, frames);
  909. #ifndef BUILD_BRIDGE
  910. // --------------------------------------------------------------------------------------------------------
  911. // Post-processing (dry/wet, volume and balance)
  912. {
  913. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  914. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  915. bool isPair;
  916. float bufValue, oldBufLeft[doBalance ? frames : 1];
  917. for (i=0; i < kData->audioOut.count; ++i)
  918. {
  919. // Dry/Wet
  920. if (doDryWet)
  921. {
  922. for (k=0; k < frames; ++k)
  923. {
  924. // TODO
  925. //if (k < kData->latency && kData->latency < frames)
  926. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  927. //else
  928. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  929. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  930. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  931. }
  932. }
  933. // Balance
  934. if (doBalance)
  935. {
  936. isPair = (i % 2 == 0);
  937. if (isPair)
  938. {
  939. CARLA_ASSERT(i+1 < kData->audioOut.count);
  940. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  941. }
  942. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  943. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  944. for (k=0; k < frames; ++k)
  945. {
  946. if (isPair)
  947. {
  948. // left
  949. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  950. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  951. }
  952. else
  953. {
  954. // right
  955. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  956. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  957. }
  958. }
  959. }
  960. // Volume (and buffer copy)
  961. {
  962. for (k=0; k < frames; ++k)
  963. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  964. }
  965. }
  966. #if 0
  967. // Latency, save values for next callback, TODO
  968. if (kData->latency > 0 && kData->latency < frames)
  969. {
  970. for (i=0; i < kData->audioIn.count; ++i)
  971. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  972. }
  973. #endif
  974. } // End of Post-processing
  975. #else
  976. for (i=0; i < kData->audioOut.count; ++i)
  977. {
  978. for (k=0; k < frames; ++k)
  979. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  980. }
  981. #endif
  982. // --------------------------------------------------------------------------------------------------------
  983. kData->singleMutex.unlock();
  984. return true;
  985. }
  986. void bufferSizeChanged(const uint32_t newBufferSize) override
  987. {
  988. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  989. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  990. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  991. {
  992. if (fAudioInBuffers[i] != nullptr)
  993. delete[] fAudioInBuffers[i];
  994. fAudioInBuffers[i] = new float[newBufferSize];
  995. }
  996. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  997. {
  998. if (fAudioOutBuffers[i] != nullptr)
  999. delete[] fAudioOutBuffers[i];
  1000. fAudioOutBuffers[i] = new float[newBufferSize];
  1001. }
  1002. if (fHandle2 == nullptr)
  1003. {
  1004. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1005. {
  1006. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  1007. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  1008. }
  1009. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1010. {
  1011. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  1012. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  1013. }
  1014. }
  1015. else
  1016. {
  1017. if (kData->audioIn.count > 0)
  1018. {
  1019. CARLA_ASSERT(kData->audioIn.count == 2);
  1020. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1021. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1022. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1023. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1024. }
  1025. if (kData->audioOut.count > 0)
  1026. {
  1027. CARLA_ASSERT(kData->audioOut.count == 2);
  1028. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1029. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1030. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1031. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1032. }
  1033. }
  1034. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1035. }
  1036. void sampleRateChanged(const double newSampleRate) override
  1037. {
  1038. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1039. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1040. // TODO
  1041. (void)newSampleRate;
  1042. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1043. }
  1044. // -------------------------------------------------------------------
  1045. // Plugin buffers
  1046. void clearBuffers() override
  1047. {
  1048. carla_debug("LadspaPlugin::clearBuffers() - start");
  1049. if (fAudioInBuffers != nullptr)
  1050. {
  1051. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1052. {
  1053. if (fAudioInBuffers[i] != nullptr)
  1054. {
  1055. delete[] fAudioInBuffers[i];
  1056. fAudioInBuffers[i] = nullptr;
  1057. }
  1058. }
  1059. delete[] fAudioInBuffers;
  1060. fAudioInBuffers = nullptr;
  1061. }
  1062. if (fAudioOutBuffers != nullptr)
  1063. {
  1064. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1065. {
  1066. if (fAudioOutBuffers[i] != nullptr)
  1067. {
  1068. delete[] fAudioOutBuffers[i];
  1069. fAudioOutBuffers[i] = nullptr;
  1070. }
  1071. }
  1072. delete[] fAudioOutBuffers;
  1073. fAudioOutBuffers = nullptr;
  1074. }
  1075. if (fParamBuffers != nullptr)
  1076. {
  1077. delete[] fParamBuffers;
  1078. fParamBuffers = nullptr;
  1079. }
  1080. CarlaPlugin::clearBuffers();
  1081. carla_debug("LadspaPlugin::clearBuffers() - end");
  1082. }
  1083. // -------------------------------------------------------------------
  1084. const void* getExtraStuff() override
  1085. {
  1086. return fRdfDescriptor;
  1087. }
  1088. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1089. {
  1090. CARLA_ASSERT(kData->engine != nullptr);
  1091. CARLA_ASSERT(kData->client == nullptr);
  1092. CARLA_ASSERT(filename != nullptr);
  1093. CARLA_ASSERT(label != nullptr);
  1094. // ---------------------------------------------------------------
  1095. // first checks
  1096. if (kData->engine == nullptr)
  1097. {
  1098. return false;
  1099. }
  1100. if (kData->client != nullptr)
  1101. {
  1102. kData->engine->setLastError("Plugin client is already registered");
  1103. return false;
  1104. }
  1105. if (filename == nullptr)
  1106. {
  1107. kData->engine->setLastError("null filename");
  1108. return false;
  1109. }
  1110. if (label == nullptr)
  1111. {
  1112. kData->engine->setLastError("null label");
  1113. return false;
  1114. }
  1115. // ---------------------------------------------------------------
  1116. // open DLL
  1117. if (! kData->libOpen(filename))
  1118. {
  1119. kData->engine->setLastError(kData->libError(filename));
  1120. return false;
  1121. }
  1122. // ---------------------------------------------------------------
  1123. // get DLL main entry
  1124. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)kData->libSymbol("ladspa_descriptor");
  1125. if (descFn == nullptr)
  1126. {
  1127. kData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1128. return false;
  1129. }
  1130. // ---------------------------------------------------------------
  1131. // get descriptor that matches label
  1132. unsigned long i = 0;
  1133. while ((fDescriptor = descFn(i++)) != nullptr)
  1134. {
  1135. if (fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1136. break;
  1137. }
  1138. if (fDescriptor == nullptr)
  1139. {
  1140. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1141. return false;
  1142. }
  1143. // ---------------------------------------------------------------
  1144. // get info
  1145. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1146. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1147. if (name != nullptr)
  1148. fName = kData->engine->getUniquePluginName(name);
  1149. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  1150. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1151. else if (fDescriptor->Name != nullptr)
  1152. fName = kData->engine->getUniquePluginName(fDescriptor->Name);
  1153. else
  1154. fName = kData->engine->getUniquePluginName(fDescriptor->Label);
  1155. fFilename = filename;
  1156. // ---------------------------------------------------------------
  1157. // register client
  1158. kData->client = kData->engine->addClient(this);
  1159. if (kData->client == nullptr || ! kData->client->isOk())
  1160. {
  1161. kData->engine->setLastError("Failed to register plugin client");
  1162. return false;
  1163. }
  1164. // ---------------------------------------------------------------
  1165. // initialize plugin
  1166. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)kData->engine->getSampleRate());
  1167. if (fHandle == nullptr)
  1168. {
  1169. kData->engine->setLastError("Plugin failed to initialize");
  1170. return false;
  1171. }
  1172. // ---------------------------------------------------------------
  1173. // load plugin settings
  1174. {
  1175. #ifdef __USE_GNU
  1176. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  1177. #else
  1178. const bool isDssiVst = fFilename.contains("dssi-vst");
  1179. #endif
  1180. // set default options
  1181. fOptions = 0x0;
  1182. if (isDssiVst)
  1183. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1184. if (kData->engine->getOptions().forceStereo)
  1185. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1186. // load settings
  1187. kData->idStr = "LADSPA/";
  1188. kData->idStr += std::strrchr(filename, OS_SEP)+1;
  1189. kData->idStr += "/";
  1190. kData->idStr += CarlaString(uniqueId());
  1191. kData->idStr += "/";
  1192. kData->idStr += label;
  1193. fOptions = kData->loadSettings(fOptions, availableOptions());
  1194. // ignore settings, we need this anyway
  1195. if (isDssiVst)
  1196. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1197. }
  1198. return true;
  1199. }
  1200. private:
  1201. LADSPA_Handle fHandle;
  1202. LADSPA_Handle fHandle2;
  1203. const LADSPA_Descriptor* fDescriptor;
  1204. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1205. float** fAudioInBuffers;
  1206. float** fAudioOutBuffers;
  1207. float* fParamBuffers;
  1208. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1209. };
  1210. CARLA_BACKEND_END_NAMESPACE
  1211. #else // WANT_LADSPA
  1212. # warning Building without LADSPA support
  1213. #endif
  1214. CARLA_BACKEND_START_NAMESPACE
  1215. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1216. {
  1217. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  1218. #ifdef WANT_LADSPA
  1219. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1220. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1221. {
  1222. delete plugin;
  1223. return nullptr;
  1224. }
  1225. plugin->reload();
  1226. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1227. {
  1228. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1229. delete plugin;
  1230. return nullptr;
  1231. }
  1232. return plugin;
  1233. #else
  1234. init.engine->setLastError("LADSPA support not available");
  1235. return nullptr;
  1236. #endif
  1237. }
  1238. CARLA_BACKEND_END_NAMESPACE