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.

1486 lines
50KB

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