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.

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