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.

1528 lines
53KB

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