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.

1361 lines
47KB

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