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.

1545 lines
52KB

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