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