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.

1245 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. // --------------------------------------------------------------------------------------------------------
  616. // Parameters Input [Automation]
  617. if (kData->event.portIn != nullptr && kData->activeBefore)
  618. {
  619. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  620. for (i=0; i < nEvents; i++)
  621. {
  622. const EngineEvent& event = kData->event.portIn->getEvent(i);
  623. time = event.time - framesOffset;
  624. if (time >= frames)
  625. continue;
  626. // Control change
  627. switch (event.type)
  628. {
  629. case kEngineEventTypeNull:
  630. break;
  631. case kEngineEventTypeControl:
  632. {
  633. const EngineControlEvent& ctrlEvent = event.ctrl;
  634. switch (ctrlEvent.type)
  635. {
  636. case kEngineControlEventTypeNull:
  637. break;
  638. case kEngineControlEventTypeParameter:
  639. {
  640. // Control backend stuff
  641. if (event.channel == kData->ctrlInChannel)
  642. {
  643. double value;
  644. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  645. {
  646. value = ctrlEvent.value;
  647. setDryWet(value, false, false);
  648. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  649. continue;
  650. }
  651. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  652. {
  653. value = ctrlEvent.value*127/100;
  654. setVolume(value, false, false);
  655. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  656. continue;
  657. }
  658. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  659. {
  660. double left, right;
  661. value = ctrlEvent.value/0.5 - 1.0;
  662. if (value < 0.0)
  663. {
  664. left = -1.0;
  665. right = (value*2)+1.0;
  666. }
  667. else if (value > 0.0)
  668. {
  669. left = (value*2)-1.0;
  670. right = 1.0;
  671. }
  672. else
  673. {
  674. left = -1.0;
  675. right = 1.0;
  676. }
  677. setBalanceLeft(left, false, false);
  678. setBalanceRight(right, false, false);
  679. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  680. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  681. continue;
  682. }
  683. }
  684. // Control plugin parameters
  685. for (k=0; k < kData->param.count; k++)
  686. {
  687. if (kData->param.data[k].midiChannel != event.channel)
  688. continue;
  689. if (kData->param.data[k].midiCC != ctrlEvent.param)
  690. continue;
  691. if (kData->param.data[k].type != PARAMETER_INPUT)
  692. continue;
  693. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  694. continue;
  695. double value;
  696. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  697. {
  698. value = (ctrlEvent.value < 0.5) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  699. }
  700. else
  701. {
  702. // FIXME - ranges call for this
  703. value = ctrlEvent.value * (kData->param.ranges[k].max - kData->param.ranges[k].min) + kData->param.ranges[k].min;
  704. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  705. value = std::rint(value);
  706. }
  707. setParameterValue(k, value, false, false, false);
  708. postponeRtEvent(kPluginPostRtEventParameterChange, k, 0, value);
  709. }
  710. break;
  711. }
  712. case kEngineControlEventTypeMidiBank:
  713. case kEngineControlEventTypeMidiProgram:
  714. break;
  715. case kEngineControlEventTypeAllSoundOff:
  716. if (event.channel == kData->ctrlInChannel)
  717. {
  718. if (fDescriptor->deactivate != nullptr)
  719. {
  720. fDescriptor->deactivate(fHandle);
  721. if (fHandle2 != nullptr)
  722. fDescriptor->deactivate(fHandle2);
  723. }
  724. if (fDescriptor->activate != nullptr)
  725. {
  726. fDescriptor->activate(fHandle);
  727. if (fHandle2 != nullptr)
  728. fDescriptor->activate(fHandle2);
  729. }
  730. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  731. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  732. }
  733. break;
  734. case kEngineControlEventTypeAllNotesOff:
  735. break;
  736. }
  737. break;
  738. }
  739. case kEngineEventTypeMidi:
  740. // ignored in LADSPA
  741. break;
  742. }
  743. }
  744. kData->postRtEvents.trySplice();
  745. } // End of Parameters Input
  746. CARLA_PROCESS_CONTINUE_CHECK;
  747. // --------------------------------------------------------------------------------------------------------
  748. // Plugin processing
  749. {
  750. if (! kData->activeBefore)
  751. {
  752. if (kData->latency > 0)
  753. {
  754. for (i=0; i < kData->audioIn.count; i++)
  755. carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  756. }
  757. if (fDescriptor->activate != nullptr)
  758. {
  759. fDescriptor->activate(fHandle);
  760. if (fHandle2 != nullptr)
  761. fDescriptor->activate(fHandle2);
  762. }
  763. }
  764. if (fHandle2 == nullptr)
  765. {
  766. for (i=0; i < kData->audioIn.count; i++)
  767. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, inBuffer[i]);
  768. for (i=0; i < kData->audioOut.count; i++)
  769. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, outBuffer[i]);
  770. }
  771. else
  772. {
  773. if (kData->audioIn.count > 0)
  774. {
  775. CARLA_ASSERT(kData->audioIn.count == 2);
  776. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, inBuffer[0]);
  777. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, inBuffer[1]);
  778. }
  779. if (kData->audioOut.count > 0)
  780. {
  781. CARLA_ASSERT(kData->audioOut.count == 2);
  782. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, outBuffer[0]);
  783. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, outBuffer[1]);
  784. }
  785. }
  786. fDescriptor->run(fHandle, frames);
  787. if (fHandle2 != nullptr)
  788. fDescriptor->run(fHandle2, frames);
  789. } // End of Plugin processing
  790. CARLA_PROCESS_CONTINUE_CHECK;
  791. // --------------------------------------------------------------------------------------------------------
  792. // Post-processing (dry/wet, volume and balance)
  793. {
  794. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  795. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) > 0 && kData->postProc.volume != 1.0f;
  796. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  797. float bufValue, oldBufLeft[doBalance ? frames : 1];
  798. for (i=0; i < kData->audioOut.count; i++)
  799. {
  800. // Dry/Wet
  801. if (doDryWet)
  802. {
  803. for (k=0; k < frames; k++)
  804. {
  805. // TODO
  806. //if (k < kData->latency && kData->latency < frames)
  807. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  808. //else
  809. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  810. bufValue = inBuffer[ (kData->audioIn.count == 1) ? 0 : i ][k];
  811. outBuffer[i][k] = (outBuffer[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  812. }
  813. }
  814. // Balance
  815. if (doBalance)
  816. {
  817. if (i % 2 == 0)
  818. std::memcpy(oldBufLeft, outBuffer[i], sizeof(float)*frames);
  819. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  820. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  821. for (k=0; k < frames; k++)
  822. {
  823. if (i % 2 == 0)
  824. {
  825. // left
  826. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  827. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  828. }
  829. else
  830. {
  831. // right
  832. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  833. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  834. }
  835. }
  836. }
  837. // Volume
  838. if (doVolume)
  839. {
  840. for (k=0; k < frames; k++)
  841. outBuffer[i][k] *= kData->postProc.volume;
  842. }
  843. }
  844. #if 0
  845. // Latency, save values for next callback, TODO
  846. if (kData->latency > 0 && kData->latency < frames)
  847. {
  848. for (i=0; i < kData->audioIn.count; i++)
  849. std::memcpy(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), sizeof(float)*kData->latency);
  850. }
  851. #endif
  852. } // End of Post-processing
  853. CARLA_PROCESS_CONTINUE_CHECK;
  854. // --------------------------------------------------------------------------------------------------------
  855. // Control Output
  856. if (kData->event.portOut != nullptr)
  857. {
  858. float value;
  859. for (k=0; k < kData->param.count; k++)
  860. {
  861. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  862. continue;
  863. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  864. if (kData->param.data[k].midiCC > 0)
  865. {
  866. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  867. kData->event.portOut->writeControlEvent(framesOffset, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  868. }
  869. }
  870. } // End of Control Output
  871. CARLA_PROCESS_CONTINUE_CHECK;
  872. // --------------------------------------------------------------------------------------------------------
  873. kData->activeBefore = kData->active;
  874. }
  875. // -------------------------------------------------------------------
  876. // Cleanup
  877. void deleteBuffers()
  878. {
  879. qDebug("LadspaPlugin::deleteBuffers() - start");
  880. if (fParamBuffers != nullptr)
  881. {
  882. delete[] fParamBuffers;
  883. fParamBuffers = nullptr;
  884. }
  885. CarlaPlugin::deleteBuffers();
  886. qDebug("LadspaPlugin::deleteBuffers() - end");
  887. }
  888. // -------------------------------------------------------------------
  889. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  890. {
  891. CARLA_ASSERT(kData->engine != nullptr);
  892. CARLA_ASSERT(kData->client == nullptr);
  893. CARLA_ASSERT(filename != nullptr);
  894. CARLA_ASSERT(label != nullptr);
  895. // ---------------------------------------------------------------
  896. // open DLL
  897. if (! libOpen(filename))
  898. {
  899. kData->engine->setLastError(libError(filename));
  900. return false;
  901. }
  902. // ---------------------------------------------------------------
  903. // get DLL main entry
  904. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)libSymbol("ladspa_descriptor");
  905. if (descFn == nullptr)
  906. {
  907. kData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  908. return false;
  909. }
  910. // ---------------------------------------------------------------
  911. // get descriptor that matches label
  912. unsigned long i = 0;
  913. while ((fDescriptor = descFn(i++)) != nullptr)
  914. {
  915. if (fDescriptor->Label != nullptr && std::strcmp(fDescriptor->Label, label) == 0)
  916. break;
  917. }
  918. if (fDescriptor == nullptr)
  919. {
  920. kData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  921. return false;
  922. }
  923. // ---------------------------------------------------------------
  924. // get info
  925. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  926. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  927. if (name != nullptr)
  928. fName = kData->engine->getNewUniquePluginName(name);
  929. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  930. fName = kData->engine->getNewUniquePluginName(fRdfDescriptor->Title);
  931. else if (fDescriptor->Name != nullptr)
  932. fName = kData->engine->getNewUniquePluginName(fDescriptor->Name);
  933. else
  934. fName = kData->engine->getNewUniquePluginName(fDescriptor->Label);
  935. fFilename = filename;
  936. // ---------------------------------------------------------------
  937. // register client
  938. kData->client = kData->engine->addClient(this);
  939. if (kData->client == nullptr || ! kData->client->isOk())
  940. {
  941. kData->engine->setLastError("Failed to register plugin client");
  942. return false;
  943. }
  944. // ---------------------------------------------------------------
  945. // initialize plugin
  946. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate());
  947. if (fHandle == nullptr)
  948. {
  949. kData->engine->setLastError("Plugin failed to initialize");
  950. return false;
  951. }
  952. return true;
  953. }
  954. private:
  955. LADSPA_Handle fHandle;
  956. LADSPA_Handle fHandle2;
  957. const LADSPA_Descriptor* fDescriptor;
  958. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  959. float* fParamBuffers;
  960. };
  961. CARLA_BACKEND_END_NAMESPACE
  962. #else // WANT_LADSPA
  963. # warning Building without LADSPA support
  964. #endif
  965. CARLA_BACKEND_START_NAMESPACE
  966. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  967. {
  968. qDebug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  969. #ifdef WANT_LADSPA
  970. LadspaPlugin* const plugin = new LadspaPlugin(init.engine, init.id);
  971. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  972. {
  973. delete plugin;
  974. return nullptr;
  975. }
  976. plugin->reload();
  977. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  978. {
  979. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  980. delete plugin;
  981. return nullptr;
  982. }
  983. plugin->registerToOscClient();
  984. return plugin;
  985. #else
  986. init.engine->setLastError("LADSPA support not available");
  987. return nullptr;
  988. #endif
  989. }
  990. CARLA_BACKEND_END_NAMESPACE