Collection of tools useful for audio production
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
41KB

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