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.

1515 lines
51KB

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