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.

1208 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. 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 sd(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.createNew(aIns);
  310. aIns = 0;
  311. }
  312. if (aOuts > 0)
  313. {
  314. fData->audioOut.createNew(aOuts);
  315. aOuts = 0;
  316. }
  317. if (params > 0)
  318. {
  319. fData->param.createNew(params);
  320. fParamBuffers = new float[params];
  321. params = 0;
  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 = aIns++;
  345. fData->audioIn.ports[j].port = (CarlaEngineAudioPort*)fData->client->addPort(kEnginePortTypeAudio, portName, true);
  346. fData->audioIn.ports[j].rindex = i;
  347. if (forcedStereoIn)
  348. {
  349. portName += "_2";
  350. fData->audioIn.ports[1].port = (CarlaEngineAudioPort*)fData->client->addPort(kEnginePortTypeAudio, portName, true);
  351. fData->audioIn.ports[1].rindex = i;
  352. }
  353. }
  354. else if (LADSPA_IS_PORT_OUTPUT(portType))
  355. {
  356. j = aOuts++;
  357. fData->audioOut.ports[j].port = (CarlaEngineAudioPort*)fData->client->addPort(kEnginePortTypeAudio, portName, false);
  358. fData->audioOut.ports[j].rindex = i;
  359. needsCtrlIn = true;
  360. if (forcedStereoOut)
  361. {
  362. portName += "_2";
  363. fData->audioOut.ports[1].port = (CarlaEngineAudioPort*)fData->client->addPort(kEnginePortTypeAudio, portName, false);
  364. fData->audioOut.ports[1].rindex = 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 = params++;
  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.0f;
  384. // max value
  385. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portHints.HintDescriptor))
  386. max = portHints.UpperBound;
  387. else
  388. max = 1.0f;
  389. if (min > max)
  390. max = min;
  391. else if (max < min)
  392. min = max;
  393. if (max - min == 0.0f)
  394. {
  395. qWarning("Broken plugin parameter: max - min == 0");
  396. max = min + 0.1f;
  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.0f;
  424. stepSmall = 1.0f;
  425. stepLarge = 10.0f;
  426. fData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  427. }
  428. else
  429. {
  430. float range = max - min;
  431. step = range/100.0f;
  432. stepSmall = range/1000.0f;
  433. stepLarge = range/10.0f;
  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.0f;
  447. max = sampleRate;
  448. def = 0.0f;
  449. step = 1.0f;
  450. stepSmall = 1.0f;
  451. stepLarge = 1.0f;
  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.0f;
  459. stepSmall = 1.0f;
  460. stepLarge = 1.0f;
  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. fData->event.portIn = (CarlaEngineEventPort*)fData->client->addPort(kEnginePortTypeEvent, 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. fData->event.portOut = (CarlaEngineEventPort*)fData->client->addPort(kEnginePortTypeEvent, portName, false);
  526. }
  527. // plugin checks
  528. fData->hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  529. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  530. fData->hints |= PLUGIN_CAN_DRYWET;
  531. if (aOuts > 0)
  532. fData->hints |= PLUGIN_CAN_VOLUME;
  533. if (aOuts >= 2 && aOuts%2 == 0)
  534. fData->hints |= PLUGIN_CAN_BALANCE;
  535. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  536. fData->hints |= PLUGIN_CAN_FORCE_STEREO;
  537. #if 0
  538. // check latency
  539. if (fData->hints & PLUGIN_CAN_DRYWET)
  540. {
  541. bool hasLatency = false;
  542. fData->latency = 0;
  543. for (uint32_t i=0; i < fData->param.count; i++)
  544. {
  545. if (fData->param.data[i].type == PARAMETER_LATENCY)
  546. {
  547. // pre-run so plugin can update latency control-port
  548. float tmpIn[2][aIns];
  549. float tmpOut[2][aOuts];
  550. for (j=0; j < fData->audioIn.count; j++)
  551. {
  552. tmpIn[j][0] = 0.0f;
  553. tmpIn[j][1] = 0.0f;
  554. //if (j == 0 || ! fHandle2)
  555. // fDescriptor->connect_port(fHandle, fData->audioIn.rindexes[j], tmpIn[j]);
  556. }
  557. for (j=0; j < fData->audioOut.count; j++)
  558. {
  559. tmpOut[j][0] = 0.0f;
  560. tmpOut[j][1] = 0.0f;
  561. //if (j == 0 || ! fHandle2)
  562. // fDescriptor->connect_port(fHandle, fData->audioOut.rindexes[j], tmpOut[j]);
  563. }
  564. if (fDescriptor->activate)
  565. fDescriptor->activate(fHandle);
  566. fDescriptor->run(fHandle, 2);
  567. if (fDescriptor->deactivate)
  568. fDescriptor->deactivate(fHandle);
  569. fData->latency = rint(fParamBuffers[i]);
  570. hasLatency = true;
  571. break;
  572. }
  573. }
  574. if (hasLatency)
  575. {
  576. fData->client->setLatency(fData->latency);
  577. recreateLatencyBuffers();
  578. }
  579. }
  580. #endif
  581. fData->client->activate();
  582. qDebug("LadspaPlugin::reload() - end");
  583. }
  584. // -------------------------------------------------------------------
  585. // Plugin processing
  586. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  587. {
  588. uint32_t i, k;
  589. CARLA_PROCESS_CONTINUE_CHECK;
  590. // --------------------------------------------------------------------------------------------------------
  591. // Parameters Input [Automation]
  592. #if 0
  593. //if (param.portCin && m_active && m_activeBefore)
  594. {
  595. const EngineEvent* cinEvent = nullptr;
  596. uint32_t time, nEvents = 0; //param.portCin->getEventCount();
  597. for (i=0; i < nEvents; i++)
  598. {
  599. //cinEvent = param.portCin->getEvent(i);
  600. if (! cinEvent)
  601. continue;
  602. time = cinEvent->time - framesOffset;
  603. if (time >= frames)
  604. continue;
  605. // Control change
  606. switch (cinEvent->type)
  607. {
  608. case kEngineControlEventTypeNull:
  609. break;
  610. case CarlaEngineParameterChangeEvent:
  611. {
  612. double value;
  613. // Control backend stuff
  614. if (cinEvent->channel == m_ctrlInChannel)
  615. {
  616. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  617. {
  618. value = cinEvent->value;
  619. setDryWet(value, false, false);
  620. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  621. continue;
  622. }
  623. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  624. {
  625. value = cinEvent->value*127/100;
  626. setVolume(value, false, false);
  627. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  628. continue;
  629. }
  630. if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  631. {
  632. double left, right;
  633. value = cinEvent->value/0.5 - 1.0;
  634. if (value < 0.0)
  635. {
  636. left = -1.0;
  637. right = (value*2)+1.0;
  638. }
  639. else if (value > 0.0)
  640. {
  641. left = (value*2)-1.0;
  642. right = 1.0;
  643. }
  644. else
  645. {
  646. left = -1.0;
  647. right = 1.0;
  648. }
  649. setBalanceLeft(left, false, false);
  650. setBalanceRight(right, false, false);
  651. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  652. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  653. continue;
  654. }
  655. }
  656. // Control plugin parameters
  657. for (k=0; k < param.count; k++)
  658. {
  659. if (param.data[k].midiChannel != cinEvent->channel)
  660. continue;
  661. if (param.data[k].midiCC != cinEvent->parameter)
  662. continue;
  663. if (param.data[k].type != PARAMETER_INPUT)
  664. continue;
  665. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  666. {
  667. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  668. {
  669. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  670. }
  671. else
  672. {
  673. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  674. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  675. value = rint(value);
  676. }
  677. setParameterValue(k, value, false, false, false);
  678. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  679. }
  680. }
  681. break;
  682. }
  683. case CarlaEngineMidiBankChangeEvent:
  684. case CarlaEngineMidiProgramChangeEvent:
  685. break;
  686. case CarlaEngineAllSoundOffEvent:
  687. if (cinEvent->channel == m_ctrlInChannel)
  688. {
  689. if (fDescriptor->deactivate)
  690. {
  691. descriptor->deactivate(fHandle);
  692. if (fHandle2) descriptor->deactivate(fHandle2);
  693. }
  694. if (fDescriptor->activate)
  695. {
  696. descriptor->activate(fHandle);
  697. if (fHandle2) descriptor->activate(fHandle2);
  698. }
  699. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  700. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  701. }
  702. break;
  703. case CarlaEngineAllNotesOffEvent:
  704. break;
  705. }
  706. }
  707. } // End of Parameters Input
  708. CARLA_PROCESS_CONTINUE_CHECK;
  709. #endif
  710. // --------------------------------------------------------------------------------------------------------
  711. // Special Parameters
  712. #if 0
  713. for (k=0; k < param.count; k++)
  714. {
  715. if (param.data[k].type == PARAMETER_LATENCY)
  716. {
  717. // TODO: ladspa special params
  718. }
  719. }
  720. CARLA_PROCESS_CONTINUE_CHECK;
  721. #endif
  722. // --------------------------------------------------------------------------------------------------------
  723. // Plugin processing
  724. if (fData->active)
  725. {
  726. if (! fData->activeBefore)
  727. {
  728. #if 0
  729. if (fData->latency > 0)
  730. {
  731. for (i=0; i < fData->audioIn.count; i++)
  732. memset(fData->latencyBuffers[i], 0, sizeof(float)*fData->latency);
  733. }
  734. #endif
  735. if (fDescriptor->activate)
  736. {
  737. fDescriptor->activate(fHandle);
  738. if (fHandle2) fDescriptor->activate(fHandle2);
  739. }
  740. }
  741. for (i=0; i < fData->audioIn.count; i++)
  742. {
  743. //if (i == 0 || ! fHandle2)
  744. fDescriptor->connect_port(fHandle, fData->audioIn.ports[i].rindex, inBuffer[i]);
  745. //else if (i == 1)
  746. // fDescriptor->connect_port(fHandle2, fData->audioIn.ports[i].rindex, inBuffer[i]);
  747. }
  748. for (i=0; i < fData->audioOut.count; i++)
  749. {
  750. //if (i == 0 || ! fHandle2)
  751. fDescriptor->connect_port(fHandle, fData->audioOut.ports[i].rindex, outBuffer[i]);
  752. //else if (i == 1)
  753. //fDescriptor->connect_port(fHandle2, fData->audioOut.ports[i].rindex, outBuffer[i]);
  754. }
  755. fDescriptor->run(fHandle, frames);
  756. if (fHandle2) fDescriptor->run(fHandle2, frames);
  757. }
  758. else
  759. {
  760. if (fData->activeBefore)
  761. {
  762. if (fDescriptor->deactivate)
  763. {
  764. fDescriptor->deactivate(fHandle);
  765. if (fHandle2) fDescriptor->deactivate(fHandle2);
  766. }
  767. }
  768. }
  769. CARLA_PROCESS_CONTINUE_CHECK;
  770. // --------------------------------------------------------------------------------------------------------
  771. // Post-processing (dry/wet, volume and balance)
  772. if (fData->active)
  773. {
  774. const bool doDryWet = (fData->hints & PLUGIN_CAN_DRYWET) > 0 && fData->postProc.dryWet != 1.0;
  775. const bool doVolume = (fData->hints & PLUGIN_CAN_VOLUME) > 0 && fData->postProc.volume != 1.0;
  776. const bool doBalance = (fData->hints & PLUGIN_CAN_BALANCE) > 0 && (fData->postProc.balanceLeft != -1.0 || fData->postProc.balanceRight != 1.0);
  777. float balRangeL, balRangeR;
  778. float bufValue, oldBufLeft[doBalance ? frames : 1];
  779. for (i=0; i < fData->audioOut.count; i++)
  780. {
  781. // Dry/Wet
  782. if (doDryWet)
  783. {
  784. for (k=0; k < frames; k++)
  785. {
  786. //if (k < m_latency && m_latency < frames)
  787. // bufValue = (aIn.count == 1) ? fData->latencyBuffers[0][k] : fData->latencyBuffers[i][k];
  788. //else
  789. // bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  790. bufValue = (fData->audioIn.count == 1) ? inBuffer[0][k] : inBuffer[i][k];
  791. outBuffer[i][k] = (outBuffer[i][k] * fData->postProc.dryWet) + (bufValue * (1.0f - fData->postProc.dryWet));
  792. }
  793. }
  794. // Balance
  795. if (doBalance)
  796. {
  797. if (i % 2 == 0)
  798. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  799. balRangeL = (fData->postProc.balanceLeft + 1.0f)/2.0f;
  800. balRangeR = (fData->postProc.balanceRight + 1.0f)/2.0f;
  801. for (k=0; k < frames; k++)
  802. {
  803. if (i % 2 == 0)
  804. {
  805. // left output
  806. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  807. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  808. }
  809. else
  810. {
  811. // right
  812. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  813. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  814. }
  815. }
  816. }
  817. // Volume
  818. if (doVolume)
  819. {
  820. for (k=0; k < frames; k++)
  821. outBuffer[i][k] *= fData->postProc.volume;
  822. }
  823. }
  824. #if 0
  825. // Latency, save values for next callback
  826. if (fData->latency > 0 && fData->latency < frames)
  827. {
  828. for (i=0; i < aIn.count; i++)
  829. memcpy(fData->latencyBuffers[i], inBuffer[i] + (frames - fData->latency), sizeof(float)*fData->latency);
  830. }
  831. #endif
  832. }
  833. else
  834. {
  835. // disable any output sound if not active
  836. for (i=0; i < fData->audioOut.count; i++)
  837. carla_zeroFloat(outBuffer[i], frames);
  838. } // End of Post-processing
  839. CARLA_PROCESS_CONTINUE_CHECK;
  840. // --------------------------------------------------------------------------------------------------------
  841. // Control Output
  842. if (fData->event.portOut && fData->active)
  843. {
  844. float value;
  845. for (k=0; k < fData->param.count; k++)
  846. {
  847. if (fData->param.data[k].type == PARAMETER_OUTPUT)
  848. {
  849. fData->param.ranges[k].fixValue(fParamBuffers[k]);
  850. if (fData->param.data[k].midiCC > 0)
  851. {
  852. value = fData->param.ranges[k].Value(fParamBuffers[k]);
  853. fData->event.portOut->writeControlEvent(framesOffset, fData->param.data[k].midiChannel, kEngineControlEventTypeParameter, fData->param.data[k].midiCC, value);
  854. }
  855. }
  856. }
  857. } // End of Control Output
  858. CARLA_PROCESS_CONTINUE_CHECK;
  859. // --------------------------------------------------------------------------------------------------------
  860. fData->activeBefore = fData->active;
  861. }
  862. // -------------------------------------------------------------------
  863. // Cleanup
  864. void deleteBuffers()
  865. {
  866. qDebug("LadspaPlugin::deleteBuffers() - start");
  867. if (fParamBuffers != nullptr)
  868. {
  869. delete[] fParamBuffers;
  870. fParamBuffers = nullptr;
  871. }
  872. CarlaPlugin::deleteBuffers();
  873. qDebug("LadspaPlugin::deleteBuffers() - end");
  874. }
  875. // -------------------------------------------------------------------
  876. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  877. {
  878. // ---------------------------------------------------------------
  879. // open DLL
  880. if (! libOpen(filename))
  881. {
  882. fData->engine->setLastError(libError(filename));
  883. return false;
  884. }
  885. // ---------------------------------------------------------------
  886. // get DLL main entry
  887. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)libSymbol("ladspa_descriptor");
  888. if (descFn == nullptr)
  889. {
  890. fData->engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  891. return false;
  892. }
  893. // ---------------------------------------------------------------
  894. // get descriptor that matches label
  895. unsigned long i = 0;
  896. while ((fDescriptor = descFn(i++)) != nullptr)
  897. {
  898. if (fDescriptor->Label == nullptr)
  899. continue;
  900. if (std::strcmp(fDescriptor->Label, label) == 0)
  901. break;
  902. }
  903. if (fDescriptor == nullptr)
  904. {
  905. fData->engine->setLastError("Could not find the requested plugin Label in the plugin library");
  906. return false;
  907. }
  908. // ---------------------------------------------------------------
  909. // get info
  910. if (is_ladspa_rdf_descriptor_valid(rdfDescriptor, fDescriptor))
  911. fRdfDescriptor = ladspa_rdf_dup(rdfDescriptor);
  912. if (name != nullptr)
  913. fData->name = fData->engine->getNewUniquePluginName(name);
  914. else if (fRdfDescriptor && fRdfDescriptor->Title)
  915. fData->name = fData->engine->getNewUniquePluginName(fRdfDescriptor->Title);
  916. else
  917. fData->name = fData->engine->getNewUniquePluginName(fDescriptor->Name);
  918. fData->filename = filename;
  919. // ---------------------------------------------------------------
  920. // register client
  921. fData->client = fData->engine->addClient(this);
  922. if (! fData->client->isOk())
  923. {
  924. fData->engine->setLastError("Failed to register plugin client");
  925. return false;
  926. }
  927. // ---------------------------------------------------------------
  928. // initialize plugin
  929. fHandle = fDescriptor->instantiate(fDescriptor, fData->engine->getSampleRate());
  930. if (! fHandle)
  931. {
  932. fData->engine->setLastError("Plugin failed to initialize");
  933. return false;
  934. }
  935. return true;
  936. }
  937. private:
  938. LADSPA_Handle fHandle;
  939. LADSPA_Handle fHandle2;
  940. const LADSPA_Descriptor* fDescriptor;
  941. const LADSPA_RDF_Descriptor* fRdfDescriptor;
  942. float* fParamBuffers;
  943. };
  944. CARLA_BACKEND_END_NAMESPACE
  945. #else // WANT_LADSPA
  946. # warning Building without LADSPA support
  947. #endif
  948. CARLA_BACKEND_START_NAMESPACE
  949. CarlaPlugin* CarlaPlugin::newLADSPA(const Initializer& init, const LADSPA_RDF_Descriptor* const rdfDescriptor)
  950. {
  951. qDebug("CarlaPlugin::newLADSPA({%p, \"%s\", \"%s\", \"%s\"}, %p)", init.engine, init.filename, init.name, init.label, rdfDescriptor);
  952. #ifdef WANT_LADSPA
  953. LadspaPlugin* const plugin = new LadspaPlugin(init.engine, init.id);
  954. if (! plugin->init(init.filename, init.name, init.label, rdfDescriptor))
  955. {
  956. delete plugin;
  957. return nullptr;
  958. }
  959. plugin->reload();
  960. if (init.engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  961. {
  962. if ((plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  963. {
  964. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  965. delete plugin;
  966. return nullptr;
  967. }
  968. }
  969. plugin->registerToOscClient();
  970. return plugin;
  971. #else
  972. init.engine->setLastError("LADSPA support not available");
  973. return nullptr;
  974. #endif
  975. }
  976. CARLA_BACKEND_END_NAMESPACE