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.

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