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.

1171 lines
38KB

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