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.

1492 lines
51KB

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