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.

1213 lines
41KB

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