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.

1534 lines
53KB

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