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.

1520 lines
52KB

  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 static_cast<long>(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;
  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. uint32_t 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. uint32_t 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. uint32_t j = iCtrl++;
  443. pData->param.data[j].hints = 0x0;
  444. pData->param.data[j].index = static_cast<int32_t>(j);
  445. pData->param.data[j].rindex = static_cast<int32_t>(i);
  446. pData->param.data[j].midiCC = -1;
  447. pData->param.data[j].midiChannel = 0;
  448. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  449. float min, max, def, step, stepSmall, stepLarge;
  450. // min value
  451. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  452. min = portRangeHints.LowerBound;
  453. else
  454. min = 0.0f;
  455. // max value
  456. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  457. max = portRangeHints.UpperBound;
  458. else
  459. max = 1.0f;
  460. if (min > max)
  461. max = min;
  462. else if (max < min)
  463. min = max;
  464. if (max - min == 0.0f)
  465. {
  466. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  467. max = min + 0.1f;
  468. }
  469. // default value
  470. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  471. def = fRdfDescriptor->Ports[i].Default;
  472. else
  473. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  474. if (def < min)
  475. def = min;
  476. else if (def > max)
  477. def = max;
  478. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  479. {
  480. min *= sampleRate;
  481. max *= sampleRate;
  482. def *= sampleRate;
  483. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  484. }
  485. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  486. {
  487. step = max - min;
  488. stepSmall = step;
  489. stepLarge = step;
  490. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  491. }
  492. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  493. {
  494. step = 1.0f;
  495. stepSmall = 1.0f;
  496. stepLarge = 10.0f;
  497. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  498. }
  499. else
  500. {
  501. float range = max - min;
  502. step = range/100.0f;
  503. stepSmall = range/1000.0f;
  504. stepLarge = range/10.0f;
  505. }
  506. if (LADSPA_IS_PORT_INPUT(portType))
  507. {
  508. pData->param.data[j].type = PARAMETER_INPUT;
  509. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  510. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  511. needsCtrlIn = true;
  512. }
  513. else if (LADSPA_IS_PORT_OUTPUT(portType))
  514. {
  515. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  516. {
  517. min = 0.0f;
  518. max = sampleRate;
  519. def = 0.0f;
  520. step = 1.0f;
  521. stepSmall = 1.0f;
  522. stepLarge = 1.0f;
  523. pData->param.data[j].type = PARAMETER_SPECIAL;
  524. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  525. }
  526. else
  527. {
  528. pData->param.data[j].type = PARAMETER_OUTPUT;
  529. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  530. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  531. needsCtrlOut = true;
  532. }
  533. }
  534. else
  535. {
  536. pData->param.data[j].type = PARAMETER_UNKNOWN;
  537. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  538. }
  539. // extra parameter hints
  540. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  541. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  542. // check for scalepoints, require at least 2 to make it useful
  543. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount > 1)
  544. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  545. pData->param.ranges[j].min = min;
  546. pData->param.ranges[j].max = max;
  547. pData->param.ranges[j].def = def;
  548. pData->param.ranges[j].step = step;
  549. pData->param.ranges[j].stepSmall = stepSmall;
  550. pData->param.ranges[j].stepLarge = stepLarge;
  551. // Start parameters in their default values
  552. fParamBuffers[j] = def;
  553. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  554. if (fHandle2 != nullptr)
  555. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  556. }
  557. else
  558. {
  559. // Not Audio or Control
  560. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  561. fDescriptor->connect_port(fHandle, i, nullptr);
  562. if (fHandle2 != nullptr)
  563. fDescriptor->connect_port(fHandle2, i, nullptr);
  564. }
  565. }
  566. if (needsCtrlIn)
  567. {
  568. portName.clear();
  569. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  570. {
  571. portName = pData->name;
  572. portName += ":";
  573. }
  574. portName += "events-in";
  575. portName.truncate(portNameSize);
  576. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  577. }
  578. if (needsCtrlOut)
  579. {
  580. portName.clear();
  581. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  582. {
  583. portName = pData->name;
  584. portName += ":";
  585. }
  586. portName += "events-out";
  587. portName.truncate(portNameSize);
  588. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  589. }
  590. if (forcedStereoIn || forcedStereoOut)
  591. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  592. else
  593. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  594. // plugin hints
  595. pData->hints = 0x0;
  596. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  597. pData->hints |= PLUGIN_IS_RTSAFE;
  598. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  599. pData->hints |= PLUGIN_CAN_DRYWET;
  600. if (aOuts > 0)
  601. pData->hints |= PLUGIN_CAN_VOLUME;
  602. if (aOuts >= 2 && aOuts % 2 == 0)
  603. pData->hints |= PLUGIN_CAN_BALANCE;
  604. // extra plugin hints
  605. pData->extraHints = 0x0;
  606. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  607. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  608. // check latency
  609. if (pData->hints & PLUGIN_CAN_DRYWET)
  610. {
  611. for (uint32_t i=0; i < pData->param.count; ++i)
  612. {
  613. if (pData->param.special[i] != PARAMETER_SPECIAL_LATENCY)
  614. continue;
  615. // we need to pre-run the plugin so it can update its latency control-port
  616. float tmpIn[aIns][2];
  617. float tmpOut[aOuts][2];
  618. for (uint32_t j=0; j < aIns; ++j)
  619. {
  620. tmpIn[j][0] = 0.0f;
  621. tmpIn[j][1] = 0.0f;
  622. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  623. }
  624. for (uint32_t j=0; j < aOuts; ++j)
  625. {
  626. tmpOut[j][0] = 0.0f;
  627. tmpOut[j][1] = 0.0f;
  628. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  629. }
  630. if (fDescriptor->activate != nullptr)
  631. fDescriptor->activate(fHandle);
  632. fDescriptor->run(fHandle, 2);
  633. if (fDescriptor->deactivate != nullptr)
  634. fDescriptor->deactivate(fHandle);
  635. const uint32_t latency = (uint32_t)fParamBuffers[i];
  636. if (pData->latency != latency)
  637. {
  638. pData->latency = latency;
  639. pData->client->setLatency(latency);
  640. pData->recreateLatencyBuffers();
  641. }
  642. break;
  643. }
  644. }
  645. bufferSizeChanged(pData->engine->getBufferSize());
  646. if (pData->active)
  647. activate();
  648. carla_debug("LadspaPlugin::reload() - end");
  649. }
  650. // -------------------------------------------------------------------
  651. // Plugin processing
  652. void activate() noexcept override
  653. {
  654. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  655. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  656. if (fDescriptor->activate != nullptr)
  657. {
  658. try {
  659. fDescriptor->activate(fHandle);
  660. } catch(...) {}
  661. if (fHandle2 != nullptr)
  662. {
  663. try {
  664. fDescriptor->activate(fHandle2);
  665. } catch(...) {}
  666. }
  667. }
  668. }
  669. void deactivate() noexcept override
  670. {
  671. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  672. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  673. if (fDescriptor->deactivate != nullptr)
  674. {
  675. try {
  676. fDescriptor->deactivate(fHandle);
  677. } catch(...) {}
  678. if (fHandle2 != nullptr)
  679. {
  680. try {
  681. fDescriptor->deactivate(fHandle2);
  682. } catch(...) {}
  683. }
  684. }
  685. }
  686. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  687. {
  688. // --------------------------------------------------------------------------------------------------------
  689. // Check if active
  690. if (! pData->active)
  691. {
  692. // disable any output sound
  693. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  694. FLOAT_CLEAR(outBuffer[i], frames);
  695. return;
  696. }
  697. // --------------------------------------------------------------------------------------------------------
  698. // Check if needs reset
  699. if (pData->needsReset)
  700. {
  701. if (pData->latency > 0)
  702. {
  703. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  704. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  705. }
  706. pData->needsReset = false;
  707. }
  708. // --------------------------------------------------------------------------------------------------------
  709. // Event Input and Processing
  710. if (pData->event.portIn != nullptr)
  711. {
  712. // ----------------------------------------------------------------------------------------------------
  713. // Event Input (System)
  714. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  715. uint32_t numEvents = pData->event.portIn->getEventCount();
  716. uint32_t timeOffset = 0;
  717. for (uint32_t i=0; i < numEvents; ++i)
  718. {
  719. const EngineEvent& event(pData->event.portIn->getEvent(i));
  720. if (event.time >= frames)
  721. continue;
  722. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  723. if (isSampleAccurate && event.time > timeOffset)
  724. {
  725. if (processSingle(inBuffer, outBuffer, event.time - timeOffset, timeOffset))
  726. timeOffset = event.time;
  727. }
  728. switch (event.type)
  729. {
  730. case kEngineEventTypeNull:
  731. break;
  732. case kEngineEventTypeControl: {
  733. const EngineControlEvent& ctrlEvent(event.ctrl);
  734. switch (ctrlEvent.type)
  735. {
  736. case kEngineControlEventTypeNull:
  737. break;
  738. case kEngineControlEventTypeParameter: {
  739. #ifndef BUILD_BRIDGE
  740. // Control backend stuff
  741. if (event.channel == pData->ctrlChannel)
  742. {
  743. float value;
  744. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  745. {
  746. value = ctrlEvent.value;
  747. setDryWet(value, false, false);
  748. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  749. break;
  750. }
  751. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  752. {
  753. value = ctrlEvent.value*127.0f/100.0f;
  754. setVolume(value, false, false);
  755. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  756. break;
  757. }
  758. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  759. {
  760. float left, right;
  761. value = ctrlEvent.value/0.5f - 1.0f;
  762. if (value < 0.0f)
  763. {
  764. left = -1.0f;
  765. right = (value*2.0f)+1.0f;
  766. }
  767. else if (value > 0.0f)
  768. {
  769. left = (value*2.0f)-1.0f;
  770. right = 1.0f;
  771. }
  772. else
  773. {
  774. left = -1.0f;
  775. right = 1.0f;
  776. }
  777. setBalanceLeft(left, false, false);
  778. setBalanceRight(right, false, false);
  779. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  780. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  781. break;
  782. }
  783. }
  784. #endif
  785. // Control plugin parameters
  786. for (uint32_t k=0; k < pData->param.count; ++k)
  787. {
  788. if (pData->param.data[k].midiChannel != event.channel)
  789. continue;
  790. if (pData->param.data[k].midiCC != ctrlEvent.param)
  791. continue;
  792. if (pData->param.data[k].type != PARAMETER_INPUT)
  793. continue;
  794. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  795. continue;
  796. float value;
  797. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  798. {
  799. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  800. }
  801. else
  802. {
  803. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  804. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  805. value = std::rint(value);
  806. }
  807. setParameterValue(k, value, false, false, false);
  808. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  809. break;
  810. }
  811. break;
  812. } // case kEngineControlEventTypeParameter
  813. case kEngineControlEventTypeMidiBank:
  814. case kEngineControlEventTypeMidiProgram:
  815. case kEngineControlEventTypeAllSoundOff:
  816. case kEngineControlEventTypeAllNotesOff:
  817. break;
  818. } // switch (ctrlEvent.type)
  819. break;
  820. } // case kEngineEventTypeControl
  821. case kEngineEventTypeMidi:
  822. break;
  823. } // switch (event.type)
  824. }
  825. pData->postRtEvents.trySplice();
  826. if (frames > timeOffset)
  827. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  828. } // End of Event Input and Processing
  829. // --------------------------------------------------------------------------------------------------------
  830. // Plugin processing (no events)
  831. else
  832. {
  833. processSingle(inBuffer, outBuffer, frames, 0);
  834. } // End of Plugin processing (no events)
  835. CARLA_PROCESS_CONTINUE_CHECK;
  836. // --------------------------------------------------------------------------------------------------------
  837. // Control Output
  838. if (pData->event.portOut != nullptr)
  839. {
  840. uint8_t channel;
  841. uint16_t param;
  842. float value;
  843. for (uint32_t k=0; k < pData->param.count; ++k)
  844. {
  845. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  846. continue;
  847. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  848. if (pData->param.data[k].midiCC > 0)
  849. {
  850. channel = pData->param.data[k].midiChannel;
  851. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  852. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  853. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  854. }
  855. }
  856. } // End of Control Output
  857. }
  858. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  859. {
  860. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  861. if (pData->audioIn.count > 0)
  862. {
  863. CARLA_SAFE_ASSERT_RETURN(inBuffer != nullptr, false);
  864. }
  865. if (pData->audioOut.count > 0)
  866. {
  867. CARLA_SAFE_ASSERT_RETURN(outBuffer != nullptr, false);
  868. }
  869. // --------------------------------------------------------------------------------------------------------
  870. // Try lock, silence otherwise
  871. if (pData->engine->isOffline())
  872. {
  873. pData->singleMutex.lock();
  874. }
  875. else if (! pData->singleMutex.tryLock())
  876. {
  877. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  878. {
  879. for (uint32_t k=0; k < frames; ++k)
  880. outBuffer[i][k+timeOffset] = 0.0f;
  881. }
  882. return false;
  883. }
  884. // --------------------------------------------------------------------------------------------------------
  885. // Reset audio buffers
  886. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  887. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  888. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  889. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  890. // --------------------------------------------------------------------------------------------------------
  891. // Run plugin
  892. fDescriptor->run(fHandle, frames);
  893. if (fHandle2 != nullptr)
  894. fDescriptor->run(fHandle2, frames);
  895. #ifndef BUILD_BRIDGE
  896. // --------------------------------------------------------------------------------------------------------
  897. // Post-processing (dry/wet, volume and balance)
  898. {
  899. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  900. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  901. bool isPair;
  902. float bufValue, oldBufLeft[doBalance ? frames : 1];
  903. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  904. {
  905. // Dry/Wet
  906. if (doDryWet)
  907. {
  908. for (uint32_t k=0; k < frames; ++k)
  909. {
  910. // TODO
  911. //if (k < pData->latency && pData->latency < frames)
  912. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  913. //else
  914. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  915. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  916. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  917. }
  918. }
  919. // Balance
  920. if (doBalance)
  921. {
  922. isPair = (i % 2 == 0);
  923. if (isPair)
  924. {
  925. CARLA_ASSERT(i+1 < pData->audioOut.count);
  926. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  927. }
  928. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  929. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  930. for (uint32_t k=0; k < frames; ++k)
  931. {
  932. if (isPair)
  933. {
  934. // left
  935. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  936. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  937. }
  938. else
  939. {
  940. // right
  941. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  942. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  943. }
  944. }
  945. }
  946. // Volume (and buffer copy)
  947. {
  948. for (uint32_t k=0; k < frames; ++k)
  949. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  950. }
  951. }
  952. #if 0
  953. // Latency, save values for next callback, TODO
  954. if (pData->latency > 0 && pData->latency < frames)
  955. {
  956. for (i=0; i < pData->audioIn.count; ++i)
  957. FLOAT_COPY(pData->latencyBuffers[i], inBuffer[i] + (frames - pData->latency), pData->latency);
  958. }
  959. #endif
  960. } // End of Post-processing
  961. #else // BUILD_BRIDGE
  962. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  963. {
  964. for (uint32_t k=0; k < frames; ++k)
  965. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  966. }
  967. #endif
  968. // --------------------------------------------------------------------------------------------------------
  969. pData->singleMutex.unlock();
  970. return true;
  971. }
  972. void bufferSizeChanged(const uint32_t newBufferSize) override
  973. {
  974. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  975. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - start", newBufferSize);
  976. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  977. {
  978. if (fAudioInBuffers[i] != nullptr)
  979. delete[] fAudioInBuffers[i];
  980. fAudioInBuffers[i] = new float[newBufferSize];
  981. }
  982. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  983. {
  984. if (fAudioOutBuffers[i] != nullptr)
  985. delete[] fAudioOutBuffers[i];
  986. fAudioOutBuffers[i] = new float[newBufferSize];
  987. }
  988. if (fHandle2 == nullptr)
  989. {
  990. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  991. {
  992. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  993. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  994. }
  995. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  996. {
  997. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  998. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  999. }
  1000. }
  1001. else
  1002. {
  1003. if (pData->audioIn.count > 0)
  1004. {
  1005. CARLA_ASSERT(pData->audioIn.count == 2);
  1006. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  1007. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  1008. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  1009. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  1010. }
  1011. if (pData->audioOut.count > 0)
  1012. {
  1013. CARLA_ASSERT(pData->audioOut.count == 2);
  1014. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  1015. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  1016. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  1017. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  1018. }
  1019. }
  1020. carla_debug("LadspaPlugin::bufferSizeChanged(%i) - end", newBufferSize);
  1021. }
  1022. void sampleRateChanged(const double newSampleRate) override
  1023. {
  1024. CARLA_ASSERT_INT(newSampleRate > 0.0, (int)newSampleRate);
  1025. carla_debug("LadspaPlugin::sampleRateChanged(%g) - start", newSampleRate);
  1026. // TODO
  1027. (void)newSampleRate;
  1028. carla_debug("LadspaPlugin::sampleRateChanged(%g) - end", newSampleRate);
  1029. }
  1030. // -------------------------------------------------------------------
  1031. // Plugin buffers
  1032. void clearBuffers() override
  1033. {
  1034. carla_debug("LadspaPlugin::clearBuffers() - start");
  1035. if (fAudioInBuffers != nullptr)
  1036. {
  1037. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1038. {
  1039. if (fAudioInBuffers[i] != nullptr)
  1040. {
  1041. delete[] fAudioInBuffers[i];
  1042. fAudioInBuffers[i] = nullptr;
  1043. }
  1044. }
  1045. delete[] fAudioInBuffers;
  1046. fAudioInBuffers = nullptr;
  1047. }
  1048. if (fAudioOutBuffers != nullptr)
  1049. {
  1050. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1051. {
  1052. if (fAudioOutBuffers[i] != nullptr)
  1053. {
  1054. delete[] fAudioOutBuffers[i];
  1055. fAudioOutBuffers[i] = nullptr;
  1056. }
  1057. }
  1058. delete[] fAudioOutBuffers;
  1059. fAudioOutBuffers = nullptr;
  1060. }
  1061. if (fParamBuffers != nullptr)
  1062. {
  1063. delete[] fParamBuffers;
  1064. fParamBuffers = nullptr;
  1065. }
  1066. CarlaPlugin::clearBuffers();
  1067. carla_debug("LadspaPlugin::clearBuffers() - end");
  1068. }
  1069. // -------------------------------------------------------------------
  1070. const void* getExtraStuff() const noexcept override
  1071. {
  1072. return fRdfDescriptor;
  1073. }
  1074. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1075. {
  1076. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1077. // ---------------------------------------------------------------
  1078. // first checks
  1079. if (pData->client != nullptr)
  1080. {
  1081. pData->engine->setLastError("Plugin client is already registered");
  1082. return false;
  1083. }
  1084. if (filename == nullptr || filename[0] == '\0')
  1085. {
  1086. pData->engine->setLastError("null filename");
  1087. return false;
  1088. }
  1089. if (label == nullptr || label[0] == '\0')
  1090. {
  1091. pData->engine->setLastError("null label");
  1092. return false;
  1093. }
  1094. // ---------------------------------------------------------------
  1095. // open DLL
  1096. if (! pData->libOpen(filename))
  1097. {
  1098. pData->engine->setLastError(pData->libError(filename));
  1099. return false;
  1100. }
  1101. // ---------------------------------------------------------------
  1102. // get DLL main entry
  1103. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)pData->libSymbol("ladspa_descriptor");
  1104. if (descFn == nullptr)
  1105. {
  1106. pData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  1107. return false;
  1108. }
  1109. // ---------------------------------------------------------------
  1110. // get descriptor that matches label
  1111. unsigned long i = 0;
  1112. while ((fDescriptor = descFn(i++)) != nullptr)
  1113. {
  1114. if (fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  1115. break;
  1116. }
  1117. if (fDescriptor == nullptr)
  1118. {
  1119. pData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  1120. return false;
  1121. }
  1122. // ---------------------------------------------------------------
  1123. // get info
  1124. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  1125. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  1126. if (name != nullptr && name[0] != '\0')
  1127. pData->name = pData->engine->getUniquePluginName(name);
  1128. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr && fRdfDescriptor->Title[0] != '\0')
  1129. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Title);
  1130. else if (fDescriptor->Name != nullptr && fDescriptor->Name[0] != '\0')
  1131. pData->name = pData->engine->getUniquePluginName(fDescriptor->Name);
  1132. else
  1133. pData->name = pData->engine->getUniquePluginName(fDescriptor->Label);
  1134. pData->filename = carla_strdup(filename);
  1135. // ---------------------------------------------------------------
  1136. // register client
  1137. pData->client = pData->engine->addClient(this);
  1138. if (pData->client == nullptr || ! pData->client->isOk())
  1139. {
  1140. pData->engine->setLastError("Failed to register plugin client");
  1141. return false;
  1142. }
  1143. // ---------------------------------------------------------------
  1144. // initialize plugin
  1145. fHandle = fDescriptor->instantiate(fDescriptor, (unsigned long)pData->engine->getSampleRate());
  1146. if (fHandle == nullptr)
  1147. {
  1148. pData->engine->setLastError("Plugin failed to initialize");
  1149. return false;
  1150. }
  1151. // ---------------------------------------------------------------
  1152. // load plugin settings
  1153. {
  1154. #ifdef __USE_GNU
  1155. const bool isDssiVst(strcasestr(pData->filename, "dssi-vst"));
  1156. #else
  1157. const bool isDssiVst(std::strstr(pData->filename, "dssi-vst"));
  1158. #endif
  1159. // set default options
  1160. pData->options = 0x0;
  1161. if (isDssiVst)
  1162. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1163. if (pData->engine->getOptions().forceStereo)
  1164. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1165. // set identifier string
  1166. CarlaString identifier("LADSPA/");
  1167. identifier += CarlaString(getUniqueId());
  1168. identifier += ",";
  1169. identifier += label;
  1170. pData->identifier = identifier.dup();
  1171. // load settings
  1172. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1173. // ignore settings, we need this anyway
  1174. if (isDssiVst)
  1175. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1176. }
  1177. return true;
  1178. }
  1179. private:
  1180. LADSPA_Handle fHandle;
  1181. LADSPA_Handle fHandle2;
  1182. const LADSPA_Descriptor* fDescriptor;
  1183. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1184. float** fAudioInBuffers;
  1185. float** fAudioOutBuffers;
  1186. float* fParamBuffers;
  1187. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1188. };
  1189. CARLA_BACKEND_END_NAMESPACE
  1190. #endif
  1191. CARLA_BACKEND_START_NAMESPACE
  1192. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1193. {
  1194. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  1195. #ifdef WANT_LADSPA
  1196. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1197. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1198. {
  1199. delete plugin;
  1200. return nullptr;
  1201. }
  1202. plugin->reload();
  1203. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1204. {
  1205. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1206. delete plugin;
  1207. return nullptr;
  1208. }
  1209. return plugin;
  1210. #else
  1211. init.engine->setLastError("LADSPA support not available");
  1212. return nullptr;
  1213. #endif
  1214. }
  1215. CARLA_BACKEND_END_NAMESPACE