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.

1275 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->UniqueID;
  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. #ifndef BUILD_BRIDGE
  273. const ProcessMode processMode(x_engine->getOptions().processMode);
  274. #endif
  275. // Safely disable plugin for reload
  276. const ScopedDisabler m(this);
  277. if (x_client->isActive())
  278. x_client->deactivate();
  279. // Remove client ports
  280. removeClientPorts();
  281. // Delete old data
  282. deleteBuffers();
  283. uint32_t aIns, aOuts, params, j;
  284. aIns = aOuts = params = 0;
  285. const double sampleRate = x_engine->getSampleRate();
  286. const unsigned long portCount = descriptor->PortCount;
  287. bool forcedStereoIn, forcedStereoOut;
  288. forcedStereoIn = forcedStereoOut = false;
  289. for (unsigned long i=0; i < portCount; i++)
  290. {
  291. const LADSPA_PortDescriptor portType = descriptor->PortDescriptors[i];
  292. if (LADSPA_IS_PORT_AUDIO(portType))
  293. {
  294. if (LADSPA_IS_PORT_INPUT(portType))
  295. aIns += 1;
  296. else if (LADSPA_IS_PORT_OUTPUT(portType))
  297. aOuts += 1;
  298. }
  299. else if (LADSPA_IS_PORT_CONTROL(portType))
  300. params += 1;
  301. }
  302. #ifndef BUILD_BRIDGE
  303. if (x_engine->getOptions().forceStereo && (aIns == 1 || aOuts == 1) && ! h2)
  304. {
  305. h2 = descriptor->instantiate(descriptor, sampleRate);
  306. if (aIns == 1)
  307. {
  308. aIns = 2;
  309. forcedStereoIn = true;
  310. }
  311. if (aOuts == 1)
  312. {
  313. aOuts = 2;
  314. forcedStereoOut = true;
  315. }
  316. }
  317. #endif
  318. if (aIns > 0)
  319. {
  320. aIn.ports = new CarlaEngineAudioPort*[aIns];
  321. aIn.rindexes = new uint32_t[aIns];
  322. }
  323. if (aOuts > 0)
  324. {
  325. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  326. aOut.rindexes = new uint32_t[aOuts];
  327. }
  328. if (params > 0)
  329. {
  330. param.data = new ParameterData[params];
  331. param.ranges = new ParameterRanges[params];
  332. paramBuffers = new float[params];
  333. }
  334. bool needsCtrlIn = false;
  335. bool needsCtrlOut = false;
  336. const int portNameSize = x_engine->maxPortNameSize();
  337. CarlaString portName;
  338. for (unsigned long i=0; i < portCount; i++)
  339. {
  340. const LADSPA_PortDescriptor portType = descriptor->PortDescriptors[i];
  341. const LADSPA_PortRangeHint portHints = descriptor->PortRangeHints[i];
  342. const bool hasPortRDF = (rdf_descriptor && i < rdf_descriptor->PortCount);
  343. if (LADSPA_IS_PORT_AUDIO(portType))
  344. {
  345. portName.clear();
  346. #ifndef BUILD_BRIDGE
  347. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  348. {
  349. portName = m_name;
  350. portName += ":";
  351. }
  352. #endif
  353. portName += descriptor->PortNames[i];
  354. portName.truncate(portNameSize);
  355. if (LADSPA_IS_PORT_INPUT(portType))
  356. {
  357. j = aIn.count++;
  358. aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  359. aIn.rindexes[j] = i;
  360. if (forcedStereoIn)
  361. {
  362. portName += "_2";
  363. aIn.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  364. aIn.rindexes[1] = i;
  365. }
  366. }
  367. else if (LADSPA_IS_PORT_OUTPUT(portType))
  368. {
  369. j = aOut.count++;
  370. aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  371. aOut.rindexes[j] = i;
  372. needsCtrlIn = true;
  373. if (forcedStereoOut)
  374. {
  375. portName += "_2";
  376. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  377. aOut.rindexes[1] = i;
  378. }
  379. }
  380. else
  381. qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
  382. }
  383. else if (LADSPA_IS_PORT_CONTROL(portType))
  384. {
  385. j = param.count++;
  386. param.data[j].index = j;
  387. param.data[j].rindex = i;
  388. param.data[j].hints = 0;
  389. param.data[j].midiChannel = 0;
  390. param.data[j].midiCC = -1;
  391. double min, max, def, step, stepSmall, stepLarge;
  392. // min value
  393. if (LADSPA_IS_HINT_BOUNDED_BELOW(portHints.HintDescriptor))
  394. min = portHints.LowerBound;
  395. else
  396. min = 0.0;
  397. // max value
  398. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portHints.HintDescriptor))
  399. max = portHints.UpperBound;
  400. else
  401. max = 1.0;
  402. if (min > max)
  403. max = min;
  404. else if (max < min)
  405. min = max;
  406. if (max - min == 0.0)
  407. {
  408. qWarning("Broken plugin parameter: max - min == 0");
  409. max = min + 0.1;
  410. }
  411. // default value
  412. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(rdf_descriptor->Ports[i].Hints))
  413. def = rdf_descriptor->Ports[i].Default;
  414. else
  415. def = get_default_ladspa_port_value(portHints.HintDescriptor, min, max);
  416. if (def < min)
  417. def = min;
  418. else if (def > max)
  419. def = max;
  420. if (LADSPA_IS_HINT_SAMPLE_RATE(portHints.HintDescriptor))
  421. {
  422. min *= sampleRate;
  423. max *= sampleRate;
  424. def *= sampleRate;
  425. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  426. }
  427. if (LADSPA_IS_HINT_TOGGLED(portHints.HintDescriptor))
  428. {
  429. step = max - min;
  430. stepSmall = step;
  431. stepLarge = step;
  432. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  433. }
  434. else if (LADSPA_IS_HINT_INTEGER(portHints.HintDescriptor))
  435. {
  436. step = 1.0;
  437. stepSmall = 1.0;
  438. stepLarge = 10.0;
  439. param.data[j].hints |= PARAMETER_IS_INTEGER;
  440. }
  441. else
  442. {
  443. double range = max - min;
  444. step = range/100.0;
  445. stepSmall = range/1000.0;
  446. stepLarge = range/10.0;
  447. }
  448. if (LADSPA_IS_PORT_INPUT(portType))
  449. {
  450. param.data[j].type = PARAMETER_INPUT;
  451. param.data[j].hints |= PARAMETER_IS_ENABLED;
  452. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  453. needsCtrlIn = true;
  454. }
  455. else if (LADSPA_IS_PORT_OUTPUT(portType))
  456. {
  457. if (strcmp(descriptor->PortNames[i], "latency") == 0 || strcmp(descriptor->PortNames[i], "_latency") == 0)
  458. {
  459. min = 0.0;
  460. max = sampleRate;
  461. def = 0.0;
  462. step = 1.0;
  463. stepSmall = 1.0;
  464. stepLarge = 1.0;
  465. param.data[j].type = PARAMETER_LATENCY;
  466. param.data[j].hints = 0;
  467. }
  468. else if (strcmp(descriptor->PortNames[i], "_sample-rate") == 0)
  469. {
  470. def = sampleRate;
  471. step = 1.0;
  472. stepSmall = 1.0;
  473. stepLarge = 1.0;
  474. param.data[j].type = PARAMETER_SAMPLE_RATE;
  475. param.data[j].hints = 0;
  476. }
  477. else
  478. {
  479. param.data[j].type = PARAMETER_OUTPUT;
  480. param.data[j].hints |= PARAMETER_IS_ENABLED;
  481. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  482. needsCtrlOut = true;
  483. }
  484. }
  485. else
  486. {
  487. param.data[j].type = PARAMETER_UNKNOWN;
  488. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  489. }
  490. // extra parameter hints
  491. if (LADSPA_IS_HINT_LOGARITHMIC(portHints.HintDescriptor))
  492. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  493. // check for scalepoints, require at least 2 to make it useful
  494. if (hasPortRDF && rdf_descriptor->Ports[i].ScalePointCount > 1)
  495. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  496. param.ranges[j].min = min;
  497. param.ranges[j].max = max;
  498. param.ranges[j].def = def;
  499. param.ranges[j].step = step;
  500. param.ranges[j].stepSmall = stepSmall;
  501. param.ranges[j].stepLarge = stepLarge;
  502. // Start parameters in their default values
  503. paramBuffers[j] = def;
  504. descriptor->connect_port(handle, i, &paramBuffers[j]);
  505. if (h2) descriptor->connect_port(h2, i, &paramBuffers[j]);
  506. }
  507. else
  508. {
  509. // Not Audio or Control
  510. qCritical("ERROR - Got a broken Port (neither Audio or Control)");
  511. descriptor->connect_port(handle, i, nullptr);
  512. if (h2) descriptor->connect_port(h2, i, nullptr);
  513. }
  514. }
  515. if (needsCtrlIn)
  516. {
  517. portName.clear();
  518. #ifndef BUILD_BRIDGE
  519. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  520. {
  521. portName = m_name;
  522. portName += ":";
  523. }
  524. #endif
  525. portName += "control-in";
  526. portName.truncate(portNameSize);
  527. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  528. }
  529. if (needsCtrlOut)
  530. {
  531. portName.clear();
  532. #ifndef BUILD_BRIDGE
  533. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  534. {
  535. portName = m_name;
  536. portName += ":";
  537. }
  538. #endif
  539. portName += "control-out";
  540. portName.truncate(portNameSize);
  541. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  542. }
  543. aIn.count = aIns;
  544. aOut.count = aOuts;
  545. param.count = params;
  546. // plugin checks
  547. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  548. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  549. m_hints |= PLUGIN_CAN_DRYWET;
  550. if (aOuts > 0)
  551. m_hints |= PLUGIN_CAN_VOLUME;
  552. if (aOuts >= 2 && aOuts%2 == 0)
  553. m_hints |= PLUGIN_CAN_BALANCE;
  554. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0))
  555. m_hints |= PLUGIN_CAN_FORCE_STEREO;
  556. // check latency
  557. if (m_hints & PLUGIN_CAN_DRYWET)
  558. {
  559. bool hasLatency = false;
  560. m_latency = 0;
  561. for (uint32_t i=0; i < param.count; i++)
  562. {
  563. if (param.data[i].type == PARAMETER_LATENCY)
  564. {
  565. // pre-run so plugin can update latency control-port
  566. float tmpIn[2][aIns];
  567. float tmpOut[2][aOuts];
  568. for (j=0; j < aIn.count; j++)
  569. {
  570. tmpIn[j][0] = 0.0f;
  571. tmpIn[j][1] = 0.0f;
  572. if (j == 0 || ! h2)
  573. descriptor->connect_port(handle, aIn.rindexes[j], tmpIn[j]);
  574. }
  575. for (j=0; j < aOut.count; j++)
  576. {
  577. tmpOut[j][0] = 0.0f;
  578. tmpOut[j][1] = 0.0f;
  579. if (j == 0 || ! h2)
  580. descriptor->connect_port(handle, aOut.rindexes[j], tmpOut[j]);
  581. }
  582. if (descriptor->activate)
  583. descriptor->activate(handle);
  584. descriptor->run(handle, 2);
  585. if (descriptor->deactivate)
  586. descriptor->deactivate(handle);
  587. m_latency = rint(paramBuffers[i]);
  588. hasLatency = true;
  589. break;
  590. }
  591. }
  592. if (hasLatency)
  593. {
  594. x_client->setLatency(m_latency);
  595. recreateLatencyBuffers();
  596. }
  597. }
  598. x_client->activate();
  599. qDebug("LadspaPlugin::reload() - end");
  600. }
  601. // -------------------------------------------------------------------
  602. // Plugin processing
  603. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  604. {
  605. uint32_t i, k;
  606. double aInsPeak[2] = { 0.0 };
  607. double aOutsPeak[2] = { 0.0 };
  608. CARLA_PROCESS_CONTINUE_CHECK;
  609. // --------------------------------------------------------------------------------------------------------
  610. // Input VU
  611. #ifndef BUILD_BRIDGE
  612. if (aIn.count > 0 && x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
  613. #else
  614. if (aIn.count > 0)
  615. #endif
  616. {
  617. if (aIn.count == 1)
  618. {
  619. for (k=0; k < frames; k++)
  620. {
  621. if (std::abs(inBuffer[0][k]) > aInsPeak[0])
  622. aInsPeak[0] = std::abs(inBuffer[0][k]);
  623. }
  624. }
  625. else if (aIn.count > 1)
  626. {
  627. for (k=0; k < frames; k++)
  628. {
  629. if (std::abs(inBuffer[0][k]) > aInsPeak[0])
  630. aInsPeak[0] = std::abs(inBuffer[0][k]);
  631. if (std::abs(inBuffer[1][k]) > aInsPeak[1])
  632. aInsPeak[1] = std::abs(inBuffer[1][k]);
  633. }
  634. }
  635. }
  636. CARLA_PROCESS_CONTINUE_CHECK;
  637. // --------------------------------------------------------------------------------------------------------
  638. // Parameters Input [Automation]
  639. if (param.portCin && m_active && m_activeBefore)
  640. {
  641. const CarlaEngineControlEvent* cinEvent;
  642. uint32_t time, nEvents = param.portCin->getEventCount();
  643. for (i=0; i < nEvents; i++)
  644. {
  645. cinEvent = param.portCin->getEvent(i);
  646. if (! cinEvent)
  647. continue;
  648. time = cinEvent->time - framesOffset;
  649. if (time >= frames)
  650. continue;
  651. // Control change
  652. switch (cinEvent->type)
  653. {
  654. case CarlaEngineNullEvent:
  655. break;
  656. case CarlaEngineParameterChangeEvent:
  657. {
  658. double value;
  659. // Control backend stuff
  660. if (cinEvent->channel == m_ctrlInChannel)
  661. {
  662. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->parameter) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  663. {
  664. value = cinEvent->value;
  665. setDryWet(value, false, false);
  666. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  667. continue;
  668. }
  669. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->parameter) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  670. {
  671. value = cinEvent->value*127/100;
  672. setVolume(value, false, false);
  673. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  674. continue;
  675. }
  676. if (MIDI_IS_CONTROL_BALANCE(cinEvent->parameter) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  677. {
  678. double left, right;
  679. value = cinEvent->value/0.5 - 1.0;
  680. if (value < 0.0)
  681. {
  682. left = -1.0;
  683. right = (value*2)+1.0;
  684. }
  685. else if (value > 0.0)
  686. {
  687. left = (value*2)-1.0;
  688. right = 1.0;
  689. }
  690. else
  691. {
  692. left = -1.0;
  693. right = 1.0;
  694. }
  695. setBalanceLeft(left, false, false);
  696. setBalanceRight(right, false, false);
  697. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  698. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  699. continue;
  700. }
  701. }
  702. // Control plugin parameters
  703. for (k=0; k < param.count; k++)
  704. {
  705. if (param.data[k].midiChannel != cinEvent->channel)
  706. continue;
  707. if (param.data[k].midiCC != cinEvent->parameter)
  708. continue;
  709. if (param.data[k].type != PARAMETER_INPUT)
  710. continue;
  711. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  712. {
  713. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  714. {
  715. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  716. }
  717. else
  718. {
  719. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  720. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  721. value = rint(value);
  722. }
  723. setParameterValue(k, value, false, false, false);
  724. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  725. }
  726. }
  727. break;
  728. }
  729. case CarlaEngineMidiBankChangeEvent:
  730. case CarlaEngineMidiProgramChangeEvent:
  731. break;
  732. case CarlaEngineAllSoundOffEvent:
  733. if (cinEvent->channel == m_ctrlInChannel)
  734. {
  735. if (descriptor->deactivate)
  736. {
  737. descriptor->deactivate(handle);
  738. if (h2) descriptor->deactivate(h2);
  739. }
  740. if (descriptor->activate)
  741. {
  742. descriptor->activate(handle);
  743. if (h2) descriptor->activate(h2);
  744. }
  745. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  746. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  747. }
  748. break;
  749. case CarlaEngineAllNotesOffEvent:
  750. break;
  751. }
  752. }
  753. } // End of Parameters Input
  754. CARLA_PROCESS_CONTINUE_CHECK;
  755. // --------------------------------------------------------------------------------------------------------
  756. // Special Parameters
  757. #if 0
  758. for (k=0; k < param.count; k++)
  759. {
  760. if (param.data[k].type == PARAMETER_LATENCY)
  761. {
  762. // TODO: ladspa special params
  763. }
  764. }
  765. CARLA_PROCESS_CONTINUE_CHECK;
  766. #endif
  767. // --------------------------------------------------------------------------------------------------------
  768. // Plugin processing
  769. if (m_active)
  770. {
  771. if (! m_activeBefore)
  772. {
  773. if (m_latency > 0)
  774. {
  775. for (i=0; i < aIn.count; i++)
  776. memset(m_latencyBuffers[i], 0, sizeof(float)*m_latency);
  777. }
  778. if (descriptor->activate)
  779. {
  780. descriptor->activate(handle);
  781. if (h2) descriptor->activate(h2);
  782. }
  783. }
  784. for (i=0; i < aIn.count; i++)
  785. {
  786. if (i == 0 || ! h2) descriptor->connect_port(handle, aIn.rindexes[i], inBuffer[i]);
  787. else if (i == 1) descriptor->connect_port(h2, aIn.rindexes[i], inBuffer[i]);
  788. }
  789. for (i=0; i < aOut.count; i++)
  790. {
  791. if (i == 0 || ! h2) descriptor->connect_port(handle, aOut.rindexes[i], outBuffer[i]);
  792. else if (i == 1) descriptor->connect_port(h2, aOut.rindexes[i], outBuffer[i]);
  793. }
  794. descriptor->run(handle, frames);
  795. if (h2) descriptor->run(h2, frames);
  796. }
  797. else
  798. {
  799. if (m_activeBefore)
  800. {
  801. if (descriptor->deactivate)
  802. {
  803. descriptor->deactivate(handle);
  804. if (h2) descriptor->deactivate(h2);
  805. }
  806. }
  807. }
  808. CARLA_PROCESS_CONTINUE_CHECK;
  809. // --------------------------------------------------------------------------------------------------------
  810. // Post-processing (dry/wet, volume and balance)
  811. if (m_active)
  812. {
  813. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  814. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  815. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  816. double bal_rangeL, bal_rangeR;
  817. float bufValue, oldBufLeft[do_balance ? frames : 0];
  818. for (i=0; i < aOut.count; i++)
  819. {
  820. // Dry/Wet
  821. if (do_drywet)
  822. {
  823. for (k=0; k < frames; k++)
  824. {
  825. if (k < m_latency && m_latency < frames)
  826. bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
  827. else
  828. bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  829. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  830. }
  831. }
  832. // Balance
  833. if (do_balance)
  834. {
  835. if (i%2 == 0)
  836. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  837. bal_rangeL = (x_balanceLeft+1.0)/2;
  838. bal_rangeR = (x_balanceRight+1.0)/2;
  839. for (k=0; k < frames; k++)
  840. {
  841. if (i%2 == 0)
  842. {
  843. // left output
  844. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  845. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  846. }
  847. else
  848. {
  849. // right
  850. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  851. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  852. }
  853. }
  854. }
  855. // Volume
  856. if (do_volume)
  857. {
  858. for (k=0; k < frames; k++)
  859. outBuffer[i][k] *= x_volume;
  860. }
  861. // Output VU
  862. #ifndef BUILD_BRIDGE
  863. if (x_engine->getOptions().processMode != PROCESS_MODE_CONTINUOUS_RACK)
  864. #endif
  865. {
  866. for (k=0; i < 2 && k < frames; k++)
  867. {
  868. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  869. aOutsPeak[i] = abs(outBuffer[i][k]);
  870. }
  871. }
  872. }
  873. // Latency, save values for next callback
  874. if (m_latency > 0 && m_latency < frames)
  875. {
  876. for (i=0; i < aIn.count; i++)
  877. memcpy(m_latencyBuffers[i], inBuffer[i] + (frames - m_latency), sizeof(float)*m_latency);
  878. }
  879. }
  880. else
  881. {
  882. // disable any output sound if not active
  883. for (i=0; i < aOut.count; i++)
  884. carla_zeroF(outBuffer[i], frames);
  885. aOutsPeak[0] = 0.0;
  886. aOutsPeak[1] = 0.0;
  887. } // End of Post-processing
  888. CARLA_PROCESS_CONTINUE_CHECK;
  889. // --------------------------------------------------------------------------------------------------------
  890. // Control Output
  891. if (param.portCout && m_active)
  892. {
  893. double value;
  894. for (k=0; k < param.count; k++)
  895. {
  896. if (param.data[k].type == PARAMETER_OUTPUT)
  897. {
  898. fixParameterValue(paramBuffers[k], param.ranges[k]);
  899. if (param.data[k].midiCC > 0)
  900. {
  901. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  902. param.portCout->writeEvent(CarlaEngineParameterChangeEvent, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  903. }
  904. }
  905. }
  906. } // End of Control Output
  907. CARLA_PROCESS_CONTINUE_CHECK;
  908. // --------------------------------------------------------------------------------------------------------
  909. // Peak Values
  910. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  911. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  912. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  913. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  914. m_activeBefore = m_active;
  915. }
  916. // -------------------------------------------------------------------
  917. // Cleanup
  918. void deleteBuffers()
  919. {
  920. qDebug("LadspaPlugin::deleteBuffers() - start");
  921. if (param.count > 0)
  922. delete[] paramBuffers;
  923. paramBuffers = nullptr;
  924. CarlaPlugin::deleteBuffers();
  925. qDebug("LadspaPlugin::deleteBuffers() - end");
  926. }
  927. // -------------------------------------------------------------------
  928. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdf_descriptor_)
  929. {
  930. // ---------------------------------------------------------------
  931. // open DLL
  932. if (! libOpen(filename))
  933. {
  934. x_engine->setLastError(libError(filename));
  935. return false;
  936. }
  937. // ---------------------------------------------------------------
  938. // get DLL main entry
  939. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)libSymbol("ladspa_descriptor");
  940. if (! descFn)
  941. {
  942. x_engine->setLastError("Could not find the LASDPA Descriptor in the plugin library");
  943. return false;
  944. }
  945. // ---------------------------------------------------------------
  946. // get descriptor that matches label
  947. unsigned long i = 0;
  948. while ((descriptor = descFn(i++)))
  949. {
  950. if (strcmp(descriptor->Label, label) == 0)
  951. break;
  952. }
  953. if (! descriptor)
  954. {
  955. x_engine->setLastError("Could not find the requested plugin Label in the plugin library");
  956. return false;
  957. }
  958. // ---------------------------------------------------------------
  959. // get info
  960. m_filename = strdup(filename);
  961. if (is_ladspa_rdf_descriptor_valid(rdf_descriptor_, descriptor))
  962. rdf_descriptor = ladspa_rdf_dup(rdf_descriptor_);
  963. if (name)
  964. m_name = x_engine->getUniquePluginName(name);
  965. else if (rdf_descriptor && rdf_descriptor->Title)
  966. m_name = x_engine->getUniquePluginName(rdf_descriptor->Title);
  967. else
  968. m_name = x_engine->getUniquePluginName(descriptor->Name);
  969. // ---------------------------------------------------------------
  970. // register client
  971. x_client = x_engine->addClient(this);
  972. if (! x_client->isOk())
  973. {
  974. x_engine->setLastError("Failed to register plugin client");
  975. return false;
  976. }
  977. // ---------------------------------------------------------------
  978. // initialize plugin
  979. handle = descriptor->instantiate(descriptor, x_engine->getSampleRate());
  980. if (! handle)
  981. {
  982. x_engine->setLastError("Plugin failed to initialize");
  983. return false;
  984. }
  985. return true;
  986. }
  987. private:
  988. LADSPA_Handle handle, h2;
  989. const LADSPA_Descriptor* descriptor;
  990. const LADSPA_RDF_Descriptor* rdf_descriptor;
  991. float* paramBuffers;
  992. };
  993. /**@}*/
  994. CARLA_BACKEND_END_NAMESPACE
  995. #else // WANT_LADSPA
  996. # warning Building without LADSPA support
  997. #endif
  998. CARLA_BACKEND_START_NAMESPACE
  999. CarlaPlugin* CarlaPlugin::newLADSPA(const initializer& init, const void* const extra)
  1000. {
  1001. qDebug("CarlaPlugin::newLADSPA(%p, \"%s\", \"%s\", \"%s\", %p)", init.engine, init.filename, init.name, init.label, extra);
  1002. #ifdef WANT_LADSPA
  1003. short id = init.engine->getNewPluginId();
  1004. if (id < 0 || id > init.engine->maxPluginNumber())
  1005. {
  1006. init.engine->setLastError("Maximum number of plugins reached");
  1007. return nullptr;
  1008. }
  1009. LadspaPlugin* const plugin = new LadspaPlugin(init.engine, id);
  1010. if (! plugin->init(init.filename, init.name, init.label, (const LADSPA_RDF_Descriptor*)extra))
  1011. {
  1012. delete plugin;
  1013. return nullptr;
  1014. }
  1015. plugin->reload();
  1016. # ifndef BUILD_BRIDGE
  1017. if (init.engine->getOptions().processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1018. {
  1019. if (! (plugin->hints() & PLUGIN_CAN_FORCE_STEREO))
  1020. {
  1021. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  1022. delete plugin;
  1023. return nullptr;
  1024. }
  1025. }
  1026. # endif
  1027. plugin->registerToOscControl();
  1028. return plugin;
  1029. #else
  1030. init.engine->setLastError("LADSPA support not available");
  1031. return nullptr;
  1032. #endif
  1033. }
  1034. CARLA_BACKEND_END_NAMESPACE