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.

1565 lines
53KB

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