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.

1432 lines
49KB

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