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.

1518 lines
52KB

  1. /*
  2. * Carla LADSPA Plugin
  3. * Copyright (C) 2011-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_LADSPA
  19. #include "CarlaLadspaUtils.hpp"
  20. #include "CarlaLibUtils.hpp"
  21. CARLA_BACKEND_START_NAMESPACE
  22. #if 0
  23. }
  24. #endif
  25. class LadspaPlugin : public CarlaPlugin
  26. {
  27. public:
  28. LadspaPlugin(CarlaEngine* const engine, const unsigned int id)
  29. : CarlaPlugin(engine, id),
  30. fHandle(nullptr),
  31. fHandle2(nullptr),
  32. fDescriptor(nullptr),
  33. fRdfDescriptor(nullptr),
  34. fAudioInBuffers(nullptr),
  35. fAudioOutBuffers(nullptr),
  36. fParamBuffers(nullptr)
  37. {
  38. carla_debug("LadspaPlugin::LadspaPlugin(%p, %i)", engine, id);
  39. }
  40. ~LadspaPlugin() override
  41. {
  42. carla_debug("LadspaPlugin::~LadspaPlugin()");
  43. pData->singleMutex.lock();
  44. pData->masterMutex.lock();
  45. if (pData->client != nullptr && pData->client->isActive())
  46. pData->client->deactivate();
  47. if (pData->active)
  48. {
  49. deactivate();
  50. pData->active = false;
  51. }
  52. if (fDescriptor != nullptr)
  53. {
  54. if (fDescriptor->cleanup != nullptr)
  55. {
  56. if (fHandle != nullptr)
  57. fDescriptor->cleanup(fHandle);
  58. if (fHandle2 != nullptr)
  59. fDescriptor->cleanup(fHandle2);
  60. }
  61. fHandle = nullptr;
  62. fHandle2 = nullptr;
  63. fDescriptor = nullptr;
  64. }
  65. if (fRdfDescriptor != nullptr)
  66. {
  67. delete fRdfDescriptor;
  68. fRdfDescriptor = nullptr;
  69. }
  70. clearBuffers();
  71. }
  72. // -------------------------------------------------------------------
  73. // Information (base)
  74. PluginType getType() const noexcept override
  75. {
  76. return PLUGIN_LADSPA;
  77. }
  78. PluginCategory getCategory() const override
  79. {
  80. if (fRdfDescriptor != nullptr)
  81. {
  82. const LADSPA_PluginType category(fRdfDescriptor->Type);
  83. // Specific Types
  84. if (category & (LADSPA_PLUGIN_DELAY|LADSPA_PLUGIN_REVERB))
  85. return PLUGIN_CATEGORY_DELAY;
  86. if (category & (LADSPA_PLUGIN_PHASER|LADSPA_PLUGIN_FLANGER|LADSPA_PLUGIN_CHORUS))
  87. return PLUGIN_CATEGORY_MODULATOR;
  88. if (category & (LADSPA_PLUGIN_AMPLIFIER))
  89. return PLUGIN_CATEGORY_DYNAMICS;
  90. if (category & (LADSPA_PLUGIN_UTILITY|LADSPA_PLUGIN_SPECTRAL|LADSPA_PLUGIN_FREQUENCY_METER))
  91. return PLUGIN_CATEGORY_UTILITY;
  92. // Pre-set LADSPA Types
  93. if (LADSPA_IS_PLUGIN_DYNAMICS(category))
  94. return PLUGIN_CATEGORY_DYNAMICS;
  95. if (LADSPA_IS_PLUGIN_AMPLITUDE(category))
  96. return PLUGIN_CATEGORY_MODULATOR;
  97. if (LADSPA_IS_PLUGIN_EQ(category))
  98. return PLUGIN_CATEGORY_EQ;
  99. if (LADSPA_IS_PLUGIN_FILTER(category))
  100. return PLUGIN_CATEGORY_FILTER;
  101. if (LADSPA_IS_PLUGIN_FREQUENCY(category))
  102. return PLUGIN_CATEGORY_UTILITY;
  103. if (LADSPA_IS_PLUGIN_SIMULATOR(category))
  104. return PLUGIN_CATEGORY_OTHER;
  105. if (LADSPA_IS_PLUGIN_TIME(category))
  106. return PLUGIN_CATEGORY_DELAY;
  107. if (LADSPA_IS_PLUGIN_GENERATOR(category))
  108. return PLUGIN_CATEGORY_SYNTH;
  109. }
  110. return getPluginCategoryFromName(fName);
  111. }
  112. long getUniqueId() const override
  113. {
  114. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  115. return fDescriptor->UniqueID;
  116. }
  117. // -------------------------------------------------------------------
  118. // Information (count)
  119. uint32_t getParameterScalePointCount(const uint32_t parameterId) const override
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  122. const int32_t rindex(pData->param.data[parameterId].rindex);
  123. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  124. {
  125. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  126. return port->ScalePointCount;
  127. }
  128. return 0;
  129. }
  130. // -------------------------------------------------------------------
  131. // Information (current data)
  132. // nothing
  133. // -------------------------------------------------------------------
  134. // Information (per-plugin data)
  135. unsigned int getAvailableOptions() const override
  136. {
  137. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  138. unsigned int options = 0x0;
  139. if (! isDssiVst)
  140. {
  141. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  142. if (pData->engine->getProccessMode() != PROCESS_MODE_CONTINUOUS_RACK)
  143. {
  144. if (fOptions & PLUGIN_OPTION_FORCE_STEREO)
  145. options |= PLUGIN_OPTION_FORCE_STEREO;
  146. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  147. options |= PLUGIN_OPTION_FORCE_STEREO;
  148. }
  149. }
  150. return options;
  151. }
  152. float getParameterValue(const uint32_t parameterId) const override
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  155. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  156. return fParamBuffers[parameterId];
  157. }
  158. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const override
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  161. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  162. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  163. const int32_t rindex(pData->param.data[parameterId].rindex);
  164. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  165. {
  166. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  167. if (scalePointId < port->ScalePointCount)
  168. {
  169. const LADSPA_RDF_ScalePoint* const scalePoint(&port->ScalePoints[scalePointId]);
  170. return scalePoint->Value;
  171. }
  172. }
  173. return 0.0f;
  174. }
  175. void getLabel(char* const strBuf) const override
  176. {
  177. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  178. if (fDescriptor->Label != nullptr)
  179. std::strncpy(strBuf, fDescriptor->Label, STR_MAX);
  180. else
  181. CarlaPlugin::getLabel(strBuf);
  182. }
  183. void getMaker(char* const strBuf) const override
  184. {
  185. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  186. if (fRdfDescriptor != nullptr && fRdfDescriptor->Creator != nullptr)
  187. std::strncpy(strBuf, fRdfDescriptor->Creator, STR_MAX);
  188. else if (fDescriptor->Maker != nullptr)
  189. std::strncpy(strBuf, fDescriptor->Maker, STR_MAX);
  190. else
  191. CarlaPlugin::getMaker(strBuf);
  192. }
  193. void getCopyright(char* const strBuf) const override
  194. {
  195. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  196. if (fDescriptor->Copyright != nullptr)
  197. std::strncpy(strBuf, fDescriptor->Copyright, STR_MAX);
  198. else
  199. CarlaPlugin::getCopyright(strBuf);
  200. }
  201. void getRealName(char* const strBuf) const override
  202. {
  203. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  204. if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  205. std::strncpy(strBuf, fRdfDescriptor->Title, STR_MAX);
  206. else if (fDescriptor->Name != nullptr)
  207. std::strncpy(strBuf, fDescriptor->Name, STR_MAX);
  208. else
  209. CarlaPlugin::getRealName(strBuf);
  210. }
  211. void getParameterName(const uint32_t parameterId, char* const strBuf) const override
  212. {
  213. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  214. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  215. const int32_t rindex(pData->param.data[parameterId].rindex);
  216. if (rindex < static_cast<int32_t>(fDescriptor->PortCount))
  217. std::strncpy(strBuf, fDescriptor->PortNames[rindex], STR_MAX);
  218. else
  219. CarlaPlugin::getParameterName(parameterId, strBuf);
  220. }
  221. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const override
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  224. const int32_t rindex(pData->param.data[parameterId].rindex);
  225. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  226. {
  227. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  228. if (LADSPA_PORT_HAS_LABEL(port->Hints) && port->Label != nullptr)
  229. {
  230. std::strncpy(strBuf, port->Label, STR_MAX);
  231. return;
  232. }
  233. }
  234. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  235. }
  236. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const override
  237. {
  238. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  239. const int32_t rindex(pData->param.data[parameterId].rindex);
  240. if (fRdfDescriptor != nullptr && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  241. {
  242. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  243. if (LADSPA_PORT_HAS_UNIT(port->Hints))
  244. {
  245. switch (port->Unit)
  246. {
  247. case LADSPA_UNIT_DB:
  248. std::strncpy(strBuf, "dB", STR_MAX);
  249. return;
  250. case LADSPA_UNIT_COEF:
  251. std::strncpy(strBuf, "(coef)", STR_MAX);
  252. return;
  253. case LADSPA_UNIT_HZ:
  254. std::strncpy(strBuf, "Hz", STR_MAX);
  255. return;
  256. case LADSPA_UNIT_S:
  257. std::strncpy(strBuf, "s", STR_MAX);
  258. return;
  259. case LADSPA_UNIT_MS:
  260. std::strncpy(strBuf, "ms", STR_MAX);
  261. return;
  262. case LADSPA_UNIT_MIN:
  263. std::strncpy(strBuf, "min", STR_MAX);
  264. return;
  265. }
  266. }
  267. }
  268. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  269. }
  270. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const override
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  273. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  274. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  275. const int32_t rindex(pData->param.data[parameterId].rindex);
  276. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  277. {
  278. const LADSPA_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  279. if (scalePointId < port->ScalePointCount)
  280. {
  281. const LADSPA_RDF_ScalePoint* const scalePoint(&port->ScalePoints[scalePointId]);
  282. if (scalePoint->Label != nullptr)
  283. {
  284. std::strncpy(strBuf, scalePoint->Label, STR_MAX);
  285. return;
  286. }
  287. }
  288. }
  289. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  290. }
  291. // -------------------------------------------------------------------
  292. // Set data (state)
  293. // nothing
  294. // -------------------------------------------------------------------
  295. // Set data (internal stuff)
  296. // nothing
  297. // -------------------------------------------------------------------
  298. // Set data (plugin-specific stuff)
  299. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  300. {
  301. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  302. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  303. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  304. fParamBuffers[parameterId] = fixedValue;
  305. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  306. }
  307. // -------------------------------------------------------------------
  308. // Set gui stuff
  309. // nothing
  310. // -------------------------------------------------------------------
  311. // Plugin state
  312. void reload() override
  313. {
  314. carla_debug("LadspaPlugin::reload() - start");
  315. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  316. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  317. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  318. const ProcessMode processMode(pData->engine->getProccessMode());
  319. // Safely disable plugin for reload
  320. const ScopedDisabler sd(this);
  321. if (pData->active)
  322. deactivate();
  323. clearBuffers();
  324. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  325. const uint32_t portCount(static_cast<uint32_t>(fDescriptor->PortCount));
  326. uint32_t aIns, aOuts, params, j;
  327. aIns = aOuts = params = 0;
  328. bool forcedStereoIn, forcedStereoOut;
  329. forcedStereoIn = forcedStereoOut = false;
  330. bool needsCtrlIn, needsCtrlOut;
  331. needsCtrlIn = needsCtrlOut = false;
  332. if (portCount > 0)
  333. {
  334. CARLA_ASSERT(fDescriptor->PortDescriptors != nullptr);
  335. CARLA_ASSERT(fDescriptor->PortRangeHints != nullptr);
  336. CARLA_ASSERT(fDescriptor->PortNames != nullptr);
  337. for (uint32_t i=0; i < portCount; ++i)
  338. {
  339. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  340. if (LADSPA_IS_PORT_AUDIO(portType))
  341. {
  342. if (LADSPA_IS_PORT_INPUT(portType))
  343. aIns += 1;
  344. else if (LADSPA_IS_PORT_OUTPUT(portType))
  345. aOuts += 1;
  346. }
  347. else if (LADSPA_IS_PORT_CONTROL(portType))
  348. params += 1;
  349. }
  350. }
  351. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1))
  352. {
  353. if (fHandle2 == nullptr)
  354. fHandle2 = fDescriptor->instantiate(fDescriptor, (unsigned long)sampleRate);
  355. if (fHandle2 != nullptr)
  356. {
  357. if (aIns == 1)
  358. {
  359. aIns = 2;
  360. forcedStereoIn = true;
  361. }
  362. if (aOuts == 1)
  363. {
  364. aOuts = 2;
  365. forcedStereoOut = true;
  366. }
  367. }
  368. }
  369. if (aIns > 0)
  370. {
  371. pData->audioIn.createNew(aIns);
  372. fAudioInBuffers = new float*[aIns];
  373. for (uint32_t i=0; i < aIns; ++i)
  374. fAudioInBuffers[i] = nullptr;
  375. }
  376. if (aOuts > 0)
  377. {
  378. pData->audioOut.createNew(aOuts);
  379. fAudioOutBuffers = new float*[aOuts];
  380. needsCtrlIn = true;
  381. for (uint32_t i=0; i < aOuts; ++i)
  382. fAudioOutBuffers[i] = nullptr;
  383. }
  384. if (params > 0)
  385. {
  386. pData->param.createNew(params);
  387. fParamBuffers = new float[params];
  388. FloatVectorOperations::clear(fParamBuffers, params);
  389. }
  390. const uint portNameSize(pData->engine->getMaxPortNameSize());
  391. CarlaString portName;
  392. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCtrl=0; i < portCount; ++i)
  393. {
  394. const LADSPA_PortDescriptor portType = fDescriptor->PortDescriptors[i];
  395. const LADSPA_PortRangeHint portRangeHints = fDescriptor->PortRangeHints[i];
  396. const bool hasPortRDF = (fRdfDescriptor != nullptr && i < fRdfDescriptor->PortCount);
  397. CARLA_ASSERT(fDescriptor->PortNames[i] != nullptr);
  398. if (LADSPA_IS_PORT_AUDIO(portType))
  399. {
  400. portName.clear();
  401. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  402. {
  403. portName = fName;
  404. portName += ":";
  405. }
  406. portName += fDescriptor->PortNames[i];
  407. portName.truncate(portNameSize);
  408. if (LADSPA_IS_PORT_INPUT(portType))
  409. {
  410. j = iAudioIn++;
  411. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  412. pData->audioIn.ports[j].rindex = i;
  413. if (forcedStereoIn)
  414. {
  415. portName += "_2";
  416. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  417. pData->audioIn.ports[1].rindex = i;
  418. }
  419. }
  420. else if (LADSPA_IS_PORT_OUTPUT(portType))
  421. {
  422. j = iAudioOut++;
  423. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  424. pData->audioOut.ports[j].rindex = i;
  425. if (forcedStereoOut)
  426. {
  427. portName += "_2";
  428. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  429. pData->audioOut.ports[1].rindex = i;
  430. }
  431. }
  432. else
  433. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  434. }
  435. else if (LADSPA_IS_PORT_CONTROL(portType))
  436. {
  437. j = iCtrl++;
  438. pData->param.data[j].index = j;
  439. pData->param.data[j].rindex = i;
  440. pData->param.data[j].hints = 0x0;
  441. pData->param.data[j].midiChannel = 0;
  442. pData->param.data[j].midiCC = -1;
  443. float min, max, def, step, stepSmall, stepLarge;
  444. // min value
  445. if (LADSPA_IS_HINT_BOUNDED_BELOW(portRangeHints.HintDescriptor))
  446. min = portRangeHints.LowerBound;
  447. else
  448. min = 0.0f;
  449. // max value
  450. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portRangeHints.HintDescriptor))
  451. max = portRangeHints.UpperBound;
  452. else
  453. max = 1.0f;
  454. if (min > max)
  455. max = min;
  456. else if (max < min)
  457. min = max;
  458. if (max - min == 0.0f)
  459. {
  460. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fDescriptor->PortNames[i]);
  461. max = min + 0.1f;
  462. }
  463. // default value
  464. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(fRdfDescriptor->Ports[i].Hints))
  465. def = fRdfDescriptor->Ports[i].Default;
  466. else
  467. def = get_default_ladspa_port_value(portRangeHints.HintDescriptor, min, max);
  468. if (def < min)
  469. def = min;
  470. else if (def > max)
  471. def = max;
  472. if (LADSPA_IS_HINT_SAMPLE_RATE(portRangeHints.HintDescriptor))
  473. {
  474. min *= sampleRate;
  475. max *= sampleRate;
  476. def *= sampleRate;
  477. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  478. }
  479. if (LADSPA_IS_HINT_TOGGLED(portRangeHints.HintDescriptor))
  480. {
  481. step = max - min;
  482. stepSmall = step;
  483. stepLarge = step;
  484. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  485. }
  486. else if (LADSPA_IS_HINT_INTEGER(portRangeHints.HintDescriptor))
  487. {
  488. step = 1.0f;
  489. stepSmall = 1.0f;
  490. stepLarge = 10.0f;
  491. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  492. }
  493. else
  494. {
  495. float range = max - min;
  496. step = range/100.0f;
  497. stepSmall = range/1000.0f;
  498. stepLarge = range/10.0f;
  499. }
  500. if (LADSPA_IS_PORT_INPUT(portType))
  501. {
  502. pData->param.data[j].type = PARAMETER_INPUT;
  503. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  504. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  505. needsCtrlIn = true;
  506. }
  507. else if (LADSPA_IS_PORT_OUTPUT(portType))
  508. {
  509. if (std::strcmp(fDescriptor->PortNames[i], "latency") == 0 || std::strcmp(fDescriptor->PortNames[i], "_latency") == 0)
  510. {
  511. min = 0.0f;
  512. max = sampleRate;
  513. def = 0.0f;
  514. step = 1.0f;
  515. stepSmall = 1.0f;
  516. stepLarge = 1.0f;
  517. pData->param.data[j].type = PARAMETER_LATENCY;
  518. pData->param.data[j].hints = 0;
  519. }
  520. else if (std::strcmp(fDescriptor->PortNames[i], "_sample-rate") == 0)
  521. {
  522. def = sampleRate;
  523. step = 1.0f;
  524. stepSmall = 1.0f;
  525. stepLarge = 1.0f;
  526. pData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  527. pData->param.data[j].hints = 0;
  528. }
  529. else
  530. {
  531. pData->param.data[j].type = PARAMETER_OUTPUT;
  532. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  533. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  534. needsCtrlOut = true;
  535. }
  536. }
  537. else
  538. {
  539. pData->param.data[j].type = PARAMETER_UNKNOWN;
  540. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  541. }
  542. // extra parameter hints
  543. if (LADSPA_IS_HINT_LOGARITHMIC(portRangeHints.HintDescriptor))
  544. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  545. // check for scalepoints, require at least 2 to make it useful
  546. if (hasPortRDF && fRdfDescriptor->Ports[i].ScalePointCount > 1)
  547. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  548. pData->param.ranges[j].min = min;
  549. pData->param.ranges[j].max = max;
  550. pData->param.ranges[j].def = def;
  551. pData->param.ranges[j].step = step;
  552. pData->param.ranges[j].stepSmall = stepSmall;
  553. pData->param.ranges[j].stepLarge = stepLarge;
  554. // Start parameters in their default values
  555. fParamBuffers[j] = def;
  556. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  557. if (fHandle2 != nullptr)
  558. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  559. }
  560. else
  561. {
  562. // Not Audio or Control
  563. carla_stderr2("ERROR - Got a broken Port (neither Audio or Control)");
  564. fDescriptor->connect_port(fHandle, i, nullptr);
  565. if (fHandle2 != nullptr)
  566. fDescriptor->connect_port(fHandle2, i, nullptr);
  567. }
  568. }
  569. if (needsCtrlIn)
  570. {
  571. portName.clear();
  572. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  573. {
  574. portName = fName;
  575. portName += ":";
  576. }
  577. portName += "events-in";
  578. portName.truncate(portNameSize);
  579. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  580. }
  581. if (needsCtrlOut)
  582. {
  583. portName.clear();
  584. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  585. {
  586. portName = fName;
  587. portName += ":";
  588. }
  589. portName += "events-out";
  590. portName.truncate(portNameSize);
  591. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  592. }
  593. if (forcedStereoIn || forcedStereoOut)
  594. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  595. else
  596. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  597. // plugin hints
  598. fHints = 0x0;
  599. if (LADSPA_IS_HARD_RT_CAPABLE(fDescriptor->Properties))
  600. fHints |= PLUGIN_IS_RTSAFE;
  601. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  602. fHints |= PLUGIN_CAN_DRYWET;
  603. if (aOuts > 0)
  604. fHints |= PLUGIN_CAN_VOLUME;
  605. if (aOuts >= 2 && aOuts % 2 == 0)
  606. fHints |= PLUGIN_CAN_BALANCE;
  607. // extra plugin hints
  608. pData->extraHints = 0x0;
  609. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  610. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  611. // check latency
  612. if (fHints & PLUGIN_CAN_DRYWET)
  613. {
  614. for (uint32_t i=0; i < pData->param.count; ++i)
  615. {
  616. if (pData->param.data[i].type != PARAMETER_LATENCY)
  617. continue;
  618. // we need to pre-run the plugin so it can update its latency control-port
  619. float tmpIn[aIns][2];
  620. float tmpOut[aOuts][2];
  621. for (j=0; j < aIns; ++j)
  622. {
  623. tmpIn[j][0] = 0.0f;
  624. tmpIn[j][1] = 0.0f;
  625. fDescriptor->connect_port(fHandle, pData->audioIn.ports[j].rindex, tmpIn[j]);
  626. }
  627. for (j=0; j < aOuts; ++j)
  628. {
  629. tmpOut[j][0] = 0.0f;
  630. tmpOut[j][1] = 0.0f;
  631. fDescriptor->connect_port(fHandle, pData->audioOut.ports[j].rindex, tmpOut[j]);
  632. }
  633. if (fDescriptor->activate != nullptr)
  634. fDescriptor->activate(fHandle);
  635. fDescriptor->run(fHandle, 2);
  636. if (fDescriptor->deactivate != nullptr)
  637. fDescriptor->deactivate(fHandle);
  638. const uint32_t latency = (uint32_t)fParamBuffers[i];
  639. if (pData->latency != latency)
  640. {
  641. pData->latency = latency;
  642. pData->client->setLatency(latency);
  643. pData->recreateLatencyBuffers();
  644. }
  645. break;
  646. }
  647. }
  648. bufferSizeChanged(pData->engine->getBufferSize());
  649. if (pData->active)
  650. activate();
  651. carla_debug("LadspaPlugin::reload() - end");
  652. }
  653. // -------------------------------------------------------------------
  654. // Plugin processing
  655. void activate() override
  656. {
  657. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  658. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  659. if (fDescriptor->activate != nullptr)
  660. {
  661. fDescriptor->activate(fHandle);
  662. if (fHandle2 != nullptr)
  663. fDescriptor->activate(fHandle2);
  664. }
  665. }
  666. void deactivate() override
  667. {
  668. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  669. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  670. if (fDescriptor->deactivate != nullptr)
  671. {
  672. fDescriptor->deactivate(fHandle);
  673. if (fHandle2 != nullptr)
  674. fDescriptor->deactivate(fHandle2);
  675. }
  676. }
  677. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  678. {
  679. // --------------------------------------------------------------------------------------------------------
  680. // Check if active
  681. if (! pData->active)
  682. {
  683. // disable any output sound
  684. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  685. FloatVectorOperations::clear(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 (uint32_t i=0; i < pData->audioIn.count; ++i)
  695. FloatVectorOperations::clear(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 isSampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  706. uint32_t time, numEvents = pData->event.portIn->getEventCount();
  707. uint32_t timeOffset = 0;
  708. for (uint32_t i=0; i < numEvents; ++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 && isSampleAccurate)
  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 (uint32_t 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 (uint32_t 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. FloatVectorOperations::copy(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  880. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  881. FloatVectorOperations::clear(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. FloatVectorOperations::copy(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. FloatVectorOperations::copy(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 || filename[0] == '\0')
  1080. {
  1081. pData->engine->setLastError("null filename");
  1082. return false;
  1083. }
  1084. if (label == nullptr || label[0] == '\0')
  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(lib_error(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. CARLA_ASSERT(fName.isNotEmpty());
  1131. CARLA_ASSERT(fFilename.isNotEmpty());
  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. const bool isDssiVst = fFilename.contains("dssi-vst", true);
  1152. // set default options
  1153. fOptions = 0x0;
  1154. if (isDssiVst)
  1155. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1156. if (pData->engine->getOptions().forceStereo)
  1157. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1158. // load settings
  1159. pData->idStr = "LADSPA/";
  1160. pData->idStr += std::strrchr(filename, OS_SEP)+1;
  1161. pData->idStr += "/";
  1162. pData->idStr += CarlaString(getUniqueId());
  1163. pData->idStr += "/";
  1164. pData->idStr += label;
  1165. fOptions = pData->loadSettings(fOptions, getAvailableOptions());
  1166. // ignore settings, we need this anyway
  1167. if (isDssiVst)
  1168. fOptions |= PLUGIN_OPTION_FIXED_BUFFERS;
  1169. }
  1170. return true;
  1171. }
  1172. private:
  1173. LADSPA_Handle fHandle;
  1174. LADSPA_Handle fHandle2;
  1175. const LADSPA_Descriptor* fDescriptor;
  1176. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  1177. float** fAudioInBuffers;
  1178. float** fAudioOutBuffers;
  1179. float* fParamBuffers;
  1180. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LadspaPlugin)
  1181. };
  1182. CARLA_BACKEND_END_NAMESPACE
  1183. #else // WANT_LADSPA
  1184. # warning Building without LADSPA support
  1185. #endif
  1186. CARLA_BACKEND_START_NAMESPACE
  1187. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  1188. {
  1189. carla_debug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  1190. #ifdef WANT_LADSPA
  1191. LadspaPlugin* const plugin(new LadspaPlugin(init.engine, init.id));
  1192. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  1193. {
  1194. delete plugin;
  1195. return nullptr;
  1196. }
  1197. plugin->reload();
  1198. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1199. {
  1200. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1201. delete plugin;
  1202. return nullptr;
  1203. }
  1204. return plugin;
  1205. #else
  1206. init.engine->setLastError("LADSPA support not available");
  1207. return nullptr;
  1208. #endif
  1209. }
  1210. CARLA_BACKEND_END_NAMESPACE