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.

1249 lines
43KB

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