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.

1488 lines
51KB

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