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.

1251 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 && fData->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(fData->name);
  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 < fData->param.count);
  118. const int32_t rindex = fData->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 < fData->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 < fData->param.count);
  137. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  138. const int32_t rindex = fData->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 < fData->param.count);
  190. const int32_t rindex = fData->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 < fData->param.count);
  199. const int32_t rindex = fData->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 < fData->param.count);
  214. const int32_t rindex = fData->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 < fData->param.count);
  249. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  250. const int32_t rindex = fData->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 < fData->param.count);
  271. const float fixedValue = fData->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(fData->engine != nullptr);
  281. CARLA_ASSERT(fDescriptor != nullptr);
  282. CARLA_ASSERT(fHandle != nullptr);
  283. const ProcessMode processMode(fData->engine->getProccessMode());
  284. // Safely disable plugin for reload
  285. const ScopedDisabler sd(this);
  286. if (fData->client->isActive())
  287. fData->client->deactivate();
  288. deleteBuffers();
  289. const double sampleRate = fData->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 ((fData->options & 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. fData->audioIn.createNew(aIns);
  328. }
  329. if (aOuts > 0)
  330. {
  331. fData->audioOut.createNew(aOuts);
  332. }
  333. if (params > 0)
  334. {
  335. fData->param.createNew(params);
  336. fParamBuffers = new float[params];
  337. }
  338. const int portNameSize = fData->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 = fData->name;
  351. portName += ":";
  352. }
  353. portName += fDescriptor->PortNames[i];
  354. portName.truncate(portNameSize);
  355. if (LADSPA_IS_PORT_INPUT(portType))
  356. {
  357. j = iAudioIn++;
  358. fData->audioIn.ports[j].port = (CarlaEngineAudioPort*)fData->client->addPort(kEnginePortTypeAudio, portName, true);
  359. fData->audioIn.ports[j].rindex = i;
  360. if (forcedStereoIn)
  361. {
  362. portName += "_2";
  363. fData->audioIn.ports[1].port = (CarlaEngineAudioPort*)fData->client->addPort(kEnginePortTypeAudio, portName, true);
  364. fData->audioIn.ports[1].rindex = i;
  365. }
  366. }
  367. else if (LADSPA_IS_PORT_OUTPUT(portType))
  368. {
  369. j = iAudioOut++;
  370. fData->audioOut.ports[j].port = (CarlaEngineAudioPort*)fData->client->addPort(kEnginePortTypeAudio, portName, false);
  371. fData->audioOut.ports[j].rindex = i;
  372. needsCtrlIn = true;
  373. if (forcedStereoOut)
  374. {
  375. portName += "_2";
  376. fData->audioOut.ports[1].port = (CarlaEngineAudioPort*)fData->client->addPort(kEnginePortTypeAudio, portName, false);
  377. fData->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. fData->param.data[j].index = j;
  387. fData->param.data[j].rindex = i;
  388. fData->param.data[j].hints = 0x0;
  389. fData->param.data[j].midiChannel = 0;
  390. fData->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. fData->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. fData->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. fData->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. fData->param.data[j].type = PARAMETER_INPUT;
  451. fData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  452. fData->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. fData->param.data[j].type = PARAMETER_LATENCY;
  466. fData->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. fData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  475. fData->param.data[j].hints = 0;
  476. }
  477. else
  478. {
  479. fData->param.data[j].type = PARAMETER_OUTPUT;
  480. fData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  481. fData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  482. needsCtrlOut = true;
  483. }
  484. }
  485. else
  486. {
  487. fData->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. fData->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. fData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  496. fData->param.ranges[j].min = min;
  497. fData->param.ranges[j].max = max;
  498. fData->param.ranges[j].def = def;
  499. fData->param.ranges[j].step = step;
  500. fData->param.ranges[j].stepSmall = stepSmall;
  501. fData->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 = fData->name;
  523. portName += ":";
  524. }
  525. portName += "event-in";
  526. portName.truncate(portNameSize);
  527. fData->event.portIn = (CarlaEngineEventPort*)fData->client->addPort(kEnginePortTypeEvent, portName, true);
  528. }
  529. if (needsCtrlOut)
  530. {
  531. portName.clear();
  532. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  533. {
  534. portName = fData->name;
  535. portName += ":";
  536. }
  537. portName += "event-out";
  538. portName.truncate(portNameSize);
  539. fData->event.portOut = (CarlaEngineEventPort*)fData->client->addPort(kEnginePortTypeEvent, portName, false);
  540. }
  541. // plugin checks
  542. fData->hints &= ~(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. fData->hints |= PLUGIN_CAN_DRYWET;
  545. if (aOuts > 0)
  546. fData->hints |= PLUGIN_CAN_VOLUME;
  547. if (aOuts >= 2 && aOuts % 2 == 0)
  548. fData->hints |= PLUGIN_CAN_BALANCE;
  549. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  550. fData->hints |= PLUGIN_CAN_FORCE_STEREO;
  551. // check latency
  552. if (fData->hints & PLUGIN_CAN_DRYWET)
  553. {
  554. for (uint32_t i=0; i < fData->param.count; i++)
  555. {
  556. if (fData->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, fData->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, fData->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 (fData->latency != latency)
  580. {
  581. fData->latency = latency;
  582. fData->client->setLatency(latency);
  583. recreateLatencyBuffers();
  584. }
  585. break;
  586. }
  587. }
  588. fData->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 (! fData->active)
  599. {
  600. // disable any output sound
  601. for (i=0; i < fData->audioOut.count; i++)
  602. carla_zeroFloat(outBuffer[i], frames);
  603. if (fData->activeBefore)
  604. {
  605. if (fDescriptor->deactivate != nullptr)
  606. {
  607. fDescriptor->deactivate(fHandle);
  608. if (fHandle2 != nullptr)
  609. fDescriptor->deactivate(fHandle2);
  610. }
  611. }
  612. fData->activeBefore = fData->active;
  613. return;
  614. }
  615. CARLA_PROCESS_CONTINUE_CHECK;
  616. // --------------------------------------------------------------------------------------------------------
  617. // Parameters Input [Automation]
  618. if (fData->event.portIn != nullptr && fData->activeBefore)
  619. {
  620. const EngineEvent* event = nullptr;
  621. uint32_t time, nEvents = fData->event.portIn->getEventCount();
  622. for (i=0; i < nEvents; i++)
  623. {
  624. event = fData->event.portIn->getEvent(i);
  625. if (event == nullptr)
  626. continue;
  627. time = event->time - framesOffset;
  628. if (time >= frames)
  629. continue;
  630. // Control change
  631. switch (event->type)
  632. {
  633. case kEngineEventTypeNull:
  634. break;
  635. case kEngineEventTypeControl:
  636. {
  637. const EngineControlEvent& ctrlEvent = event->ctrl;
  638. switch (ctrlEvent.type)
  639. {
  640. case kEngineControlEventTypeNull:
  641. break;
  642. case kEngineControlEventTypeParameter:
  643. {
  644. // Control backend stuff
  645. if (event->channel == fData->ctrlInChannel)
  646. {
  647. double value;
  648. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.parameter) && (fData->hints & PLUGIN_CAN_DRYWET) > 0)
  649. {
  650. value = ctrlEvent.value;
  651. setDryWet(value, false, false);
  652. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  653. continue;
  654. }
  655. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.parameter) && (fData->hints & PLUGIN_CAN_VOLUME) > 0)
  656. {
  657. value = ctrlEvent.value*127/100;
  658. setVolume(value, false, false);
  659. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  660. continue;
  661. }
  662. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.parameter) && (fData->hints & PLUGIN_CAN_BALANCE) > 0)
  663. {
  664. double left, right;
  665. value = ctrlEvent.value/0.5 - 1.0;
  666. if (value < 0.0)
  667. {
  668. left = -1.0;
  669. right = (value*2)+1.0;
  670. }
  671. else if (value > 0.0)
  672. {
  673. left = (value*2)-1.0;
  674. right = 1.0;
  675. }
  676. else
  677. {
  678. left = -1.0;
  679. right = 1.0;
  680. }
  681. setBalanceLeft(left, false, false);
  682. setBalanceRight(right, false, false);
  683. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  684. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  685. continue;
  686. }
  687. }
  688. // Control plugin parameters
  689. for (k=0; k < fData->param.count; k++)
  690. {
  691. if (fData->param.data[k].midiChannel != event->channel)
  692. continue;
  693. if (fData->param.data[k].midiCC != ctrlEvent.parameter)
  694. continue;
  695. if (fData->param.data[k].type != PARAMETER_INPUT)
  696. continue;
  697. if ((fData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  698. continue;
  699. double value;
  700. if (fData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  701. {
  702. value = (ctrlEvent.value < 0.5) ? fData->param.ranges[k].min : fData->param.ranges[k].max;
  703. }
  704. else
  705. {
  706. // FIXME - ranges call for this
  707. value = ctrlEvent.value * (fData->param.ranges[k].max - fData->param.ranges[k].min) + fData->param.ranges[k].min;
  708. if (fData->param.data[k].hints & PARAMETER_IS_INTEGER)
  709. value = std::rint(value);
  710. }
  711. setParameterValue(k, value, false, false, false);
  712. postponeRtEvent(kPluginPostRtEventParameterChange, k, 0, value);
  713. }
  714. break;
  715. }
  716. case kEngineControlEventTypeMidiBank:
  717. case kEngineControlEventTypeMidiProgram:
  718. break;
  719. case kEngineControlEventTypeAllSoundOff:
  720. if (event->channel == fData->ctrlInChannel)
  721. {
  722. if (fDescriptor->deactivate != nullptr)
  723. {
  724. fDescriptor->deactivate(fHandle);
  725. if (fHandle2 != nullptr)
  726. fDescriptor->deactivate(fHandle2);
  727. }
  728. if (fDescriptor->activate != nullptr)
  729. {
  730. fDescriptor->activate(fHandle);
  731. if (fHandle2 != nullptr)
  732. fDescriptor->activate(fHandle2);
  733. }
  734. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  735. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  736. }
  737. break;
  738. case kEngineControlEventTypeAllNotesOff:
  739. break;
  740. }
  741. break;
  742. }
  743. case kEngineEventTypeMidi:
  744. // ignored in LADSPA
  745. break;
  746. }
  747. }
  748. } // End of Parameters Input
  749. CARLA_PROCESS_CONTINUE_CHECK;
  750. // --------------------------------------------------------------------------------------------------------
  751. // Plugin processing
  752. {
  753. if (! fData->activeBefore)
  754. {
  755. if (fData->latency > 0)
  756. {
  757. for (i=0; i < fData->audioIn.count; i++)
  758. carla_zeroFloat(fData->latencyBuffers[i], fData->latency);
  759. }
  760. if (fDescriptor->activate != nullptr)
  761. {
  762. fDescriptor->activate(fHandle);
  763. if (fHandle2 != nullptr)
  764. fDescriptor->activate(fHandle2);
  765. }
  766. }
  767. if (fHandle2 == nullptr)
  768. {
  769. for (i=0; i < fData->audioIn.count; i++)
  770. fDescriptor->connect_port(fHandle, fData->audioIn.ports[i].rindex, inBuffer[i]);
  771. for (i=0; i < fData->audioOut.count; i++)
  772. fDescriptor->connect_port(fHandle, fData->audioOut.ports[i].rindex, outBuffer[i]);
  773. }
  774. else
  775. {
  776. if (fData->audioIn.count > 0)
  777. {
  778. CARLA_ASSERT(fData->audioIn.count == 2);
  779. fDescriptor->connect_port(fHandle, fData->audioIn.ports[0].rindex, inBuffer[0]);
  780. fDescriptor->connect_port(fHandle2, fData->audioIn.ports[1].rindex, inBuffer[1]);
  781. }
  782. if (fData->audioOut.count > 0)
  783. {
  784. CARLA_ASSERT(fData->audioOut.count == 2);
  785. fDescriptor->connect_port(fHandle, fData->audioOut.ports[0].rindex, outBuffer[0]);
  786. fDescriptor->connect_port(fHandle2, fData->audioOut.ports[1].rindex, outBuffer[1]);
  787. }
  788. }
  789. fDescriptor->run(fHandle, frames);
  790. if (fHandle2 != nullptr)
  791. fDescriptor->run(fHandle2, frames);
  792. } // End of Plugin processing
  793. CARLA_PROCESS_CONTINUE_CHECK;
  794. // --------------------------------------------------------------------------------------------------------
  795. // Post-processing (dry/wet, volume and balance)
  796. {
  797. const bool doDryWet = (fData->hints & PLUGIN_CAN_DRYWET) > 0 && fData->postProc.dryWet != 1.0f;
  798. const bool doVolume = (fData->hints & PLUGIN_CAN_VOLUME) > 0 && fData->postProc.volume != 1.0f;
  799. const bool doBalance = (fData->hints & PLUGIN_CAN_BALANCE) > 0 && (fData->postProc.balanceLeft != -1.0f || fData->postProc.balanceRight != 1.0f);
  800. float bufValue, oldBufLeft[doBalance ? frames : 1];
  801. for (i=0; i < fData->audioOut.count; i++)
  802. {
  803. // Dry/Wet
  804. if (doDryWet)
  805. {
  806. for (k=0; k < frames; k++)
  807. {
  808. // TODO
  809. //if (k < fData->latency && fData->latency < frames)
  810. // bufValue = (fData->audioIn.count == 1) ? fData->latencyBuffers[0][k] : fData->latencyBuffers[i][k];
  811. //else
  812. // bufValue = (fData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  813. bufValue = inBuffer[ (fData->audioIn.count == 1) ? 0 : i ][k];
  814. outBuffer[i][k] = (outBuffer[i][k] * fData->postProc.dryWet) + (bufValue * (1.0f - fData->postProc.dryWet));
  815. }
  816. }
  817. // Balance
  818. if (doBalance)
  819. {
  820. if (i % 2 == 0)
  821. std::memcpy(oldBufLeft, outBuffer[i], sizeof(float)*frames);
  822. float balRangeL = (fData->postProc.balanceLeft + 1.0f)/2.0f;
  823. float balRangeR = (fData->postProc.balanceRight + 1.0f)/2.0f;
  824. for (k=0; k < frames; k++)
  825. {
  826. if (i % 2 == 0)
  827. {
  828. // left
  829. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  830. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  831. }
  832. else
  833. {
  834. // right
  835. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  836. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  837. }
  838. }
  839. }
  840. // Volume
  841. if (doVolume)
  842. {
  843. for (k=0; k < frames; k++)
  844. outBuffer[i][k] *= fData->postProc.volume;
  845. }
  846. }
  847. #if 0
  848. // Latency, save values for next callback, TODO
  849. if (fData->latency > 0 && fData->latency < frames)
  850. {
  851. for (i=0; i < fData->audioIn.count; i++)
  852. std::memcpy(fData->latencyBuffers[i], inBuffer[i] + (frames - fData->latency), sizeof(float)*fData->latency);
  853. }
  854. #endif
  855. } // End of Post-processing
  856. CARLA_PROCESS_CONTINUE_CHECK;
  857. // --------------------------------------------------------------------------------------------------------
  858. // Control Output
  859. if (fData->event.portOut != nullptr)
  860. {
  861. float value;
  862. for (k=0; k < fData->param.count; k++)
  863. {
  864. if (fData->param.data[k].type != PARAMETER_OUTPUT)
  865. continue;
  866. fData->param.ranges[k].fixValue(fParamBuffers[k]);
  867. if (fData->param.data[k].midiCC > 0)
  868. {
  869. value = fData->param.ranges[k].Value(fParamBuffers[k]); // FIXME - range 0.0-1.0 new name
  870. fData->event.portOut->writeControlEvent(framesOffset, fData->param.data[k].midiChannel, kEngineControlEventTypeParameter, fData->param.data[k].midiCC, value);
  871. }
  872. }
  873. } // End of Control Output
  874. CARLA_PROCESS_CONTINUE_CHECK;
  875. // --------------------------------------------------------------------------------------------------------
  876. fData->activeBefore = fData->active;
  877. }
  878. // -------------------------------------------------------------------
  879. // Cleanup
  880. void deleteBuffers()
  881. {
  882. qDebug("LadspaPlugin::deleteBuffers() - start");
  883. if (fParamBuffers != nullptr)
  884. {
  885. delete[] fParamBuffers;
  886. fParamBuffers = nullptr;
  887. }
  888. CarlaPlugin::deleteBuffers();
  889. qDebug("LadspaPlugin::deleteBuffers() - end");
  890. }
  891. // -------------------------------------------------------------------
  892. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  893. {
  894. CARLA_ASSERT(fData->engine != nullptr);
  895. CARLA_ASSERT(fData->client == nullptr);
  896. CARLA_ASSERT(filename);
  897. CARLA_ASSERT(label);
  898. // ---------------------------------------------------------------
  899. // open DLL
  900. if (! libOpen(filename))
  901. {
  902. fData->engine->setLastError(libError(filename));
  903. return false;
  904. }
  905. // ---------------------------------------------------------------
  906. // get DLL main entry
  907. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)libSymbol("ladspa_descriptor");
  908. if (descFn == nullptr)
  909. {
  910. fData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  911. return false;
  912. }
  913. // ---------------------------------------------------------------
  914. // get descriptor that matches label
  915. unsigned long i = 0;
  916. while ((fDescriptor = descFn(i++)) != nullptr)
  917. {
  918. if (fDescriptor->Label == nullptr)
  919. continue;
  920. if (std::strcmp(fDescriptor->Label, label) == 0)
  921. break;
  922. }
  923. if (fDescriptor == nullptr)
  924. {
  925. fData->engine->setLastError("Could not find the requested plugin label in the plugin library");
  926. return false;
  927. }
  928. // ---------------------------------------------------------------
  929. // get info
  930. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  931. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  932. if (name != nullptr)
  933. fData->name = fData->engine->getNewUniquePluginName(name);
  934. else if (fRdfDescriptor != nullptr && fRdfDescriptor->Title != nullptr)
  935. fData->name = fData->engine->getNewUniquePluginName(fRdfDescriptor->Title);
  936. else if (fDescriptor->Name != nullptr)
  937. fData->name = fData->engine->getNewUniquePluginName(fDescriptor->Name);
  938. else
  939. fData->name = fData->engine->getNewUniquePluginName(fDescriptor->Label);
  940. fData->filename = filename;
  941. // ---------------------------------------------------------------
  942. // register client
  943. fData->client = fData->engine->addClient(this);
  944. if (fData->client == nullptr || ! fData->client->isOk())
  945. {
  946. fData->engine->setLastError("Failed to register plugin client");
  947. return false;
  948. }
  949. // ---------------------------------------------------------------
  950. // initialize plugin
  951. fHandle = fDescriptor->instantiate(fDescriptor, fData->engine->getSampleRate());
  952. if (fHandle == nullptr)
  953. {
  954. fData->engine->setLastError("Plugin failed to initialize");
  955. return false;
  956. }
  957. return true;
  958. }
  959. private:
  960. LADSPA_Handle fHandle;
  961. LADSPA_Handle fHandle2;
  962. const LADSPA_Descriptor* fDescriptor;
  963. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  964. float* fParamBuffers;
  965. };
  966. CARLA_BACKEND_END_NAMESPACE
  967. #else // WANT_LADSPA
  968. # warning Building without LADSPA support
  969. #endif
  970. CARLA_BACKEND_START_NAMESPACE
  971. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  972. {
  973. qDebug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  974. #ifdef WANT_LADSPA
  975. LadspaPlugin* const plugin = new LadspaPlugin(init.engine, init.id);
  976. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  977. {
  978. delete plugin;
  979. return nullptr;
  980. }
  981. plugin->reload();
  982. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  983. {
  984. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  985. delete plugin;
  986. return nullptr;
  987. }
  988. plugin->registerToOscClient();
  989. return plugin;
  990. #else
  991. init.engine->setLastError("LADSPA support not available");
  992. return nullptr;
  993. #endif
  994. }
  995. CARLA_BACKEND_END_NAMESPACE