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.

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