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.

1167 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. /*!
  21. * @defgroup CarlaBackendLadspaPlugin Carla Backend LADSPA Plugin
  22. *
  23. * The Carla Backend LADSPA Plugin.\n
  24. * http://www.ladspa.org/
  25. * @{
  26. */
  27. class LadspaPlugin : public CarlaPlugin
  28. {
  29. public:
  30. LadspaPlugin(CarlaEngine* const engine, const unsigned short id)
  31. : CarlaPlugin(engine, id)
  32. {
  33. qDebug("LadspaPlugin::LadspaPlugin()");
  34. m_type = PLUGIN_LADSPA;
  35. handle = h2 = nullptr;
  36. descriptor = nullptr;
  37. rdf_descriptor = nullptr;
  38. paramBuffers = nullptr;
  39. }
  40. ~LadspaPlugin()
  41. {
  42. qDebug("LadspaPlugin::~LadspaPlugin()");
  43. if (descriptor)
  44. {
  45. if (descriptor->deactivate && m_activeBefore)
  46. {
  47. if (handle)
  48. descriptor->deactivate(handle);
  49. if (h2)
  50. descriptor->deactivate(h2);
  51. }
  52. if (descriptor->cleanup)
  53. {
  54. if (handle)
  55. descriptor->cleanup(handle);
  56. if (h2)
  57. descriptor->cleanup(h2);
  58. }
  59. }
  60. if (rdf_descriptor)
  61. ladspa_rdf_free(rdf_descriptor);
  62. }
  63. // -------------------------------------------------------------------
  64. // Information (base)
  65. PluginCategory category()
  66. {
  67. if (rdf_descriptor)
  68. {
  69. const LADSPA_Properties category = rdf_descriptor->Type;
  70. // Specific Types
  71. if (category & (LADSPA_CLASS_DELAY|LADSPA_CLASS_REVERB))
  72. return PLUGIN_CATEGORY_DELAY;
  73. if (category & (LADSPA_CLASS_PHASER|LADSPA_CLASS_FLANGER|LADSPA_CLASS_CHORUS))
  74. return PLUGIN_CATEGORY_MODULATOR;
  75. if (category & (LADSPA_CLASS_AMPLIFIER))
  76. return PLUGIN_CATEGORY_DYNAMICS;
  77. if (category & (LADSPA_CLASS_UTILITY|LADSPA_CLASS_SPECTRAL|LADSPA_CLASS_FREQUENCY_METER))
  78. return PLUGIN_CATEGORY_UTILITY;
  79. // Pre-set LADSPA Types
  80. if (LADSPA_IS_PLUGIN_DYNAMICS(category))
  81. return PLUGIN_CATEGORY_DYNAMICS;
  82. if (LADSPA_IS_PLUGIN_AMPLITUDE(category))
  83. return PLUGIN_CATEGORY_MODULATOR;
  84. if (LADSPA_IS_PLUGIN_EQ(category))
  85. return PLUGIN_CATEGORY_EQ;
  86. if (LADSPA_IS_PLUGIN_FILTER(category))
  87. return PLUGIN_CATEGORY_FILTER;
  88. if (LADSPA_IS_PLUGIN_FREQUENCY(category))
  89. return PLUGIN_CATEGORY_UTILITY;
  90. if (LADSPA_IS_PLUGIN_SIMULATOR(category))
  91. return PLUGIN_CATEGORY_OTHER;
  92. if (LADSPA_IS_PLUGIN_TIME(category))
  93. return PLUGIN_CATEGORY_DELAY;
  94. if (LADSPA_IS_PLUGIN_GENERATOR(category))
  95. return PLUGIN_CATEGORY_SYNTH;
  96. }
  97. return getPluginCategoryFromName(m_name);
  98. }
  99. long uniqueId()
  100. {
  101. Q_ASSERT(descriptor);
  102. return descriptor->UniqueID;
  103. }
  104. // -------------------------------------------------------------------
  105. // Information (count)
  106. uint32_t parameterScalePointCount(const uint32_t parameterId)
  107. {
  108. Q_ASSERT(parameterId < param.count);
  109. int32_t rindex = param.data[parameterId].rindex;
  110. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  111. {
  112. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  113. if (port)
  114. return port->ScalePointCount;
  115. }
  116. return 0;
  117. }
  118. // -------------------------------------------------------------------
  119. // Information (per-plugin data)
  120. double getParameterValue(const uint32_t parameterId)
  121. {
  122. Q_ASSERT(parameterId < param.count);
  123. return paramBuffers[parameterId];
  124. }
  125. double getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  126. {
  127. Q_ASSERT(parameterId < param.count);
  128. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  129. int32_t rindex = param.data[parameterId].rindex;
  130. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  131. {
  132. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  133. if (port && scalePointId < port->ScalePointCount)
  134. {
  135. const LADSPA_RDF_ScalePoint* const scalePoint = &port->ScalePoints[scalePointId];
  136. if (scalePoint)
  137. return scalePoint->Value;
  138. }
  139. }
  140. return 0.0;
  141. }
  142. void getLabel(char* const strBuf)
  143. {
  144. Q_ASSERT(descriptor);
  145. if (descriptor && descriptor->Label)
  146. strncpy(strBuf, descriptor->Label, STR_MAX);
  147. else
  148. CarlaPlugin::getLabel(strBuf);
  149. }
  150. void getMaker(char* const strBuf)
  151. {
  152. Q_ASSERT(descriptor);
  153. if (rdf_descriptor && rdf_descriptor->Creator)
  154. strncpy(strBuf, rdf_descriptor->Creator, STR_MAX);
  155. else if (descriptor && descriptor->Maker)
  156. strncpy(strBuf, descriptor->Maker, STR_MAX);
  157. else
  158. CarlaPlugin::getMaker(strBuf);
  159. }
  160. void getCopyright(char* const strBuf)
  161. {
  162. Q_ASSERT(descriptor);
  163. if (descriptor && descriptor->Copyright)
  164. strncpy(strBuf, descriptor->Copyright, STR_MAX);
  165. else
  166. CarlaPlugin::getCopyright(strBuf);
  167. }
  168. void getRealName(char* const strBuf)
  169. {
  170. Q_ASSERT(descriptor);
  171. if (rdf_descriptor && rdf_descriptor->Title)
  172. strncpy(strBuf, rdf_descriptor->Title, STR_MAX);
  173. else if (descriptor && descriptor->Name)
  174. strncpy(strBuf, descriptor->Name, STR_MAX);
  175. else
  176. CarlaPlugin::getRealName(strBuf);
  177. }
  178. void getParameterName(const uint32_t parameterId, char* const strBuf)
  179. {
  180. Q_ASSERT(descriptor);
  181. Q_ASSERT(parameterId < param.count);
  182. int32_t rindex = param.data[parameterId].rindex;
  183. if (descriptor && rindex < (int32_t)descriptor->PortCount)
  184. strncpy(strBuf, descriptor->PortNames[rindex], STR_MAX);
  185. else
  186. CarlaPlugin::getParameterName(parameterId, strBuf);
  187. }
  188. void getParameterSymbol(const uint32_t parameterId, char* const strBuf)
  189. {
  190. Q_ASSERT(parameterId < param.count);
  191. int32_t rindex = param.data[parameterId].rindex;
  192. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  193. {
  194. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  195. if (LADSPA_PORT_HAS_LABEL(port->Hints) && port->Label)
  196. {
  197. strncpy(strBuf, port->Label, STR_MAX);
  198. return;
  199. }
  200. }
  201. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  202. }
  203. void getParameterUnit(const uint32_t parameterId, char* const strBuf)
  204. {
  205. Q_ASSERT(parameterId < param.count);
  206. int32_t rindex = param.data[parameterId].rindex;
  207. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  208. {
  209. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  210. if (LADSPA_PORT_HAS_UNIT(port->Hints))
  211. {
  212. switch (port->Unit)
  213. {
  214. case LADSPA_UNIT_DB:
  215. strncpy(strBuf, "dB", STR_MAX);
  216. return;
  217. case LADSPA_UNIT_COEF:
  218. strncpy(strBuf, "(coef)", STR_MAX);
  219. return;
  220. case LADSPA_UNIT_HZ:
  221. strncpy(strBuf, "Hz", STR_MAX);
  222. return;
  223. case LADSPA_UNIT_S:
  224. strncpy(strBuf, "s", STR_MAX);
  225. return;
  226. case LADSPA_UNIT_MS:
  227. strncpy(strBuf, "ms", STR_MAX);
  228. return;
  229. case LADSPA_UNIT_MIN:
  230. strncpy(strBuf, "min", STR_MAX);
  231. return;
  232. }
  233. }
  234. }
  235. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  236. }
  237. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  238. {
  239. Q_ASSERT(parameterId < param.count);
  240. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  241. int32_t rindex = param.data[parameterId].rindex;
  242. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  243. {
  244. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  245. if (port && scalePointId < port->ScalePointCount)
  246. {
  247. const LADSPA_RDF_ScalePoint* const scalePoint = &port->ScalePoints[scalePointId];
  248. if (scalePoint && scalePoint->Label)
  249. {
  250. strncpy(strBuf, scalePoint->Label, STR_MAX);
  251. return;
  252. }
  253. }
  254. }
  255. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  256. }
  257. // -------------------------------------------------------------------
  258. // Set data (plugin-specific stuff)
  259. void setParameterValue(const uint32_t parameterId, double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  260. {
  261. Q_ASSERT(parameterId < param.count);
  262. paramBuffers[parameterId] = fixParameterValue(value, param.ranges[parameterId]);
  263. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  264. }
  265. // -------------------------------------------------------------------
  266. // Plugin state
  267. void reload()
  268. {
  269. qDebug("LadspaPlugin::reload() - start");
  270. Q_ASSERT(descriptor);
  271. // Safely disable plugin for reload
  272. const ScopedDisabler m(this);
  273. if (x_client->isActive())
  274. x_client->deactivate();
  275. // Remove client ports
  276. removeClientPorts();
  277. // Delete old data
  278. deleteBuffers();
  279. uint32_t aIns, aOuts, params, j;
  280. aIns = aOuts = params = 0;
  281. const double sampleRate = x_engine->getSampleRate();
  282. const unsigned long portCount = descriptor->PortCount;
  283. bool forcedStereoIn, forcedStereoOut;
  284. forcedStereoIn = forcedStereoOut = false;
  285. for (unsigned long i=0; i < portCount; i++)
  286. {
  287. const LADSPA_PortDescriptor portType = descriptor->PortDescriptors[i];
  288. if (LADSPA_IS_PORT_AUDIO(portType))
  289. {
  290. if (LADSPA_IS_PORT_INPUT(portType))
  291. aIns += 1;
  292. else if (LADSPA_IS_PORT_OUTPUT(portType))
  293. aOuts += 1;
  294. }
  295. else if (LADSPA_IS_PORT_CONTROL(portType))
  296. params += 1;
  297. }
  298. #ifndef BUILD_BRIDGE
  299. if (carlaOptions.forceStereo && (aIns == 1 || aOuts == 1) && ! h2)
  300. {
  301. h2 = descriptor->instantiate(descriptor, sampleRate);
  302. if (aIns == 1)
  303. {
  304. aIns = 2;
  305. forcedStereoIn = true;
  306. }
  307. if (aOuts == 1)
  308. {
  309. aOuts = 2;
  310. forcedStereoOut = true;
  311. }
  312. }
  313. #endif
  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() - 2;
  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. def = sampleRate;
  467. step = 1.0;
  468. stepSmall = 1.0;
  469. stepLarge = 1.0;
  470. param.data[j].type = PARAMETER_SAMPLE_RATE;
  471. param.data[j].hints = 0;
  472. }
  473. else
  474. {
  475. param.data[j].type = PARAMETER_OUTPUT;
  476. param.data[j].hints |= PARAMETER_IS_ENABLED;
  477. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  478. needsCtrlOut = true;
  479. }
  480. }
  481. else
  482. {
  483. param.data[j].type = PARAMETER_UNKNOWN;
  484. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  485. }
  486. // extra parameter hints
  487. if (LADSPA_IS_HINT_LOGARITHMIC(portHints.HintDescriptor))
  488. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  489. // check for scalepoints, require at least 2 to make it useful
  490. if (hasPortRDF && rdf_descriptor->Ports[i].ScalePointCount > 1)
  491. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  492. param.ranges[j].min = min;
  493. param.ranges[j].max = max;
  494. param.ranges[j].def = def;
  495. param.ranges[j].step = step;
  496. param.ranges[j].stepSmall = stepSmall;
  497. param.ranges[j].stepLarge = stepLarge;
  498. // Start parameters in their default values
  499. paramBuffers[j] = def;
  500. descriptor->connect_port(handle, i, &paramBuffers[j]);
  501. if (h2) descriptor->connect_port(h2, i, &paramBuffers[j]);
  502. }
  503. else
  504. {
  505. // Not Audio or Control
  506. qCritical("ERROR - Got a broken Port (neither Audio or Control)");
  507. descriptor->connect_port(handle, i, nullptr);
  508. if (h2) descriptor->connect_port(h2, i, nullptr);
  509. }
  510. }
  511. if (needsCtrlIn)
  512. {
  513. #ifndef BUILD_BRIDGE
  514. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  515. {
  516. strcpy(portName, m_name);
  517. strcat(portName, ":control-in");
  518. }
  519. else
  520. #endif
  521. strcpy(portName, "control-in");
  522. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  523. }
  524. if (needsCtrlOut)
  525. {
  526. #ifndef BUILD_BRIDGE
  527. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  528. {
  529. strcpy(portName, m_name);
  530. strcat(portName, ":control-out");
  531. }
  532. else
  533. #endif
  534. strcpy(portName, "control-out");
  535. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  536. }
  537. aIn.count = aIns;
  538. aOut.count = aOuts;
  539. param.count = params;
  540. // plugin checks
  541. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE);
  542. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  543. m_hints |= PLUGIN_CAN_DRYWET;
  544. if (aOuts > 0)
  545. m_hints |= PLUGIN_CAN_VOLUME;
  546. if (aOuts >= 2 && aOuts%2 == 0)
  547. m_hints |= PLUGIN_CAN_BALANCE;
  548. x_client->activate();
  549. qDebug("LadspaPlugin::reload() - end");
  550. }
  551. // -------------------------------------------------------------------
  552. // Plugin processing
  553. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  554. {
  555. uint32_t i, k;
  556. double aInsPeak[2] = { 0.0 };
  557. double aOutsPeak[2] = { 0.0 };
  558. CARLA_PROCESS_CONTINUE_CHECK;
  559. // --------------------------------------------------------------------------------------------------------
  560. // Input VU
  561. if (aIn.count > 0)
  562. {
  563. if (aIn.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 (aIn.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. for (i=0; i < aOut.count; i++)
  758. {
  759. // Dry/Wet
  760. if (do_drywet)
  761. {
  762. for (k=0; k < frames; k++)
  763. {
  764. if (aOut.count == 1)
  765. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[0][k]*(1.0-x_dryWet));
  766. else
  767. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[i][k]*(1.0-x_dryWet));
  768. }
  769. }
  770. // Balance
  771. if (do_balance)
  772. {
  773. if (i%2 == 0)
  774. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  775. bal_rangeL = (x_balanceLeft+1.0)/2;
  776. bal_rangeR = (x_balanceRight+1.0)/2;
  777. for (k=0; k < frames; k++)
  778. {
  779. if (i%2 == 0)
  780. {
  781. // left output
  782. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  783. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  784. }
  785. else
  786. {
  787. // right
  788. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  789. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  790. }
  791. }
  792. }
  793. // Volume
  794. if (do_volume)
  795. {
  796. for (k=0; k < frames; k++)
  797. outBuffer[i][k] *= x_volume;
  798. }
  799. // Output VU
  800. for (k=0; i < 2 && k < frames; k++)
  801. {
  802. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  803. aOutsPeak[i] = abs(outBuffer[i][k]);
  804. }
  805. }
  806. }
  807. else
  808. {
  809. // disable any output sound if not active
  810. for (i=0; i < aOut.count; i++)
  811. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  812. aOutsPeak[0] = 0.0;
  813. aOutsPeak[1] = 0.0;
  814. } // End of Post-processing
  815. CARLA_PROCESS_CONTINUE_CHECK;
  816. // --------------------------------------------------------------------------------------------------------
  817. // Control Output
  818. if (param.portCout && m_active)
  819. {
  820. double value;
  821. for (k=0; k < param.count; k++)
  822. {
  823. if (param.data[k].type == PARAMETER_OUTPUT)
  824. {
  825. fixParameterValue(paramBuffers[k], param.ranges[k]);
  826. if (param.data[k].midiCC > 0)
  827. {
  828. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  829. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  830. }
  831. }
  832. }
  833. } // End of Control Output
  834. CARLA_PROCESS_CONTINUE_CHECK;
  835. // --------------------------------------------------------------------------------------------------------
  836. // Peak Values
  837. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  838. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  839. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  840. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  841. m_activeBefore = m_active;
  842. }
  843. // -------------------------------------------------------------------
  844. // Cleanup
  845. void deleteBuffers()
  846. {
  847. qDebug("LadspaPlugin::deleteBuffers() - start");
  848. if (param.count > 0)
  849. delete[] paramBuffers;
  850. paramBuffers = nullptr;
  851. qDebug("LadspaPlugin::deleteBuffers() - end");
  852. }
  853. // -------------------------------------------------------------------
  854. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdf_descriptor_)
  855. {
  856. // ---------------------------------------------------------------
  857. // open DLL
  858. if (! libOpen(filename))
  859. {
  860. setLastError(libError(filename));
  861. return false;
  862. }
  863. // ---------------------------------------------------------------
  864. // get DLL main entry
  865. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)libSymbol("ladspa_descriptor");
  866. if (! descFn)
  867. {
  868. setLastError("Could not find the LASDPA Descriptor in the plugin library");
  869. return false;
  870. }
  871. // ---------------------------------------------------------------
  872. // get descriptor that matches label
  873. unsigned long i = 0;
  874. while ((descriptor = descFn(i++)))
  875. {
  876. if (strcmp(descriptor->Label, label) == 0)
  877. break;
  878. }
  879. if (! descriptor)
  880. {
  881. setLastError("Could not find the requested plugin Label in the plugin library");
  882. return false;
  883. }
  884. // ---------------------------------------------------------------
  885. // initialize plugin
  886. handle = descriptor->instantiate(descriptor, x_engine->getSampleRate());
  887. if (! handle)
  888. {
  889. setLastError("Plugin failed to initialize");
  890. return false;
  891. }
  892. // ---------------------------------------------------------------
  893. // get info
  894. m_filename = strdup(filename);
  895. if (is_ladspa_rdf_descriptor_valid(rdf_descriptor_, descriptor))
  896. rdf_descriptor = ladspa_rdf_dup(rdf_descriptor_);
  897. if (name)
  898. m_name = x_engine->getUniqueName(name);
  899. else if (rdf_descriptor && rdf_descriptor->Title)
  900. m_name = x_engine->getUniqueName(rdf_descriptor->Title);
  901. else
  902. m_name = x_engine->getUniqueName(descriptor->Name);
  903. // ---------------------------------------------------------------
  904. // register client
  905. x_client = x_engine->addClient(this);
  906. if (! x_client->isOk())
  907. {
  908. setLastError("Failed to register plugin client");
  909. return false;
  910. }
  911. return true;
  912. }
  913. private:
  914. LADSPA_Handle handle, h2;
  915. const LADSPA_Descriptor* descriptor;
  916. const LADSPA_RDF_Descriptor* rdf_descriptor;
  917. float* paramBuffers;
  918. };
  919. CarlaPlugin* CarlaPlugin::newLADSPA(const initializer& init, const void* const extra)
  920. {
  921. qDebug("CarlaPlugin::newLADSPA(%p, \"%s\", \"%s\", \"%s\", %p)", init.engine, init.filename, init.name, init.label, extra);
  922. short id = init.engine->getNewPluginId();
  923. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  924. {
  925. setLastError("Maximum number of plugins reached");
  926. return nullptr;
  927. }
  928. LadspaPlugin* const plugin = new LadspaPlugin(init.engine, id);
  929. if (! plugin->init(init.filename, init.name, init.label, (const LADSPA_RDF_Descriptor*)extra))
  930. {
  931. delete plugin;
  932. return nullptr;
  933. }
  934. plugin->reload();
  935. #ifndef BUILD_BRIDGE
  936. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  937. {
  938. const uint32_t ins = plugin->audioInCount();
  939. const uint32_t outs = plugin->audioOutCount();
  940. if (ins > 2 || outs > 2 || (ins != outs && ins != 0 && outs != 0))
  941. {
  942. setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  943. delete plugin;
  944. return nullptr;
  945. }
  946. }
  947. #endif
  948. plugin->registerToOsc();
  949. return plugin;
  950. }
  951. /**@}*/
  952. CARLA_BACKEND_END_NAMESPACE