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.

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