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.

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