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.

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