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.

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