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.

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