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.

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