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.

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