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.

1533 lines
53KB

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