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.

1260 lines
42KB

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