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.

1165 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. if (carlaOptions.forceStereo && (aIns == 1 || aOuts == 1) && ! h2)
  299. {
  300. h2 = descriptor->instantiate(descriptor, sampleRate);
  301. if (aIns == 1)
  302. {
  303. aIns = 2;
  304. forcedStereoIn = true;
  305. }
  306. if (aOuts == 1)
  307. {
  308. aOuts = 2;
  309. forcedStereoOut = true;
  310. }
  311. }
  312. if (aIns > 0)
  313. {
  314. aIn.ports = new CarlaEngineAudioPort*[aIns];
  315. aIn.rindexes = new uint32_t[aIns];
  316. }
  317. if (aOuts > 0)
  318. {
  319. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  320. aOut.rindexes = new uint32_t[aOuts];
  321. }
  322. if (params > 0)
  323. {
  324. param.data = new ParameterData[params];
  325. param.ranges = new ParameterRanges[params];
  326. paramBuffers = new float[params];
  327. }
  328. const int portNameSize = CarlaEngine::maxPortNameSize() - 2;
  329. char portName[portNameSize];
  330. bool needsCtrlIn = false;
  331. bool needsCtrlOut = false;
  332. for (unsigned long i=0; i < portCount; i++)
  333. {
  334. const LADSPA_PortDescriptor portType = descriptor->PortDescriptors[i];
  335. const LADSPA_PortRangeHint portHints = descriptor->PortRangeHints[i];
  336. const bool hasPortRDF = (rdf_descriptor && i < rdf_descriptor->PortCount);
  337. if (LADSPA_IS_PORT_AUDIO(portType))
  338. {
  339. #ifndef BUILD_BRIDGE
  340. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  341. {
  342. strcpy(portName, m_name);
  343. strcat(portName, ":");
  344. strncat(portName, descriptor->PortNames[i], portNameSize/2);
  345. }
  346. else
  347. #endif
  348. strncpy(portName, descriptor->PortNames[i], portNameSize);
  349. if (LADSPA_IS_PORT_INPUT(portType))
  350. {
  351. j = aIn.count++;
  352. aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  353. aIn.rindexes[j] = i;
  354. if (forcedStereoIn)
  355. {
  356. strcat(portName, "_");
  357. aIn.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  358. aIn.rindexes[1] = i;
  359. }
  360. }
  361. else if (LADSPA_IS_PORT_OUTPUT(portType))
  362. {
  363. j = aOut.count++;
  364. aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  365. aOut.rindexes[j] = i;
  366. needsCtrlIn = true;
  367. if (forcedStereoOut)
  368. {
  369. strcat(portName, "_");
  370. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  371. aOut.rindexes[1] = i;
  372. }
  373. }
  374. else
  375. qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
  376. }
  377. else if (LADSPA_IS_PORT_CONTROL(portType))
  378. {
  379. j = param.count++;
  380. param.data[j].index = j;
  381. param.data[j].rindex = i;
  382. param.data[j].hints = 0;
  383. param.data[j].midiChannel = 0;
  384. param.data[j].midiCC = -1;
  385. double min, max, def, step, stepSmall, stepLarge;
  386. // min value
  387. if (LADSPA_IS_HINT_BOUNDED_BELOW(portHints.HintDescriptor))
  388. min = portHints.LowerBound;
  389. else
  390. min = 0.0;
  391. // max value
  392. if (LADSPA_IS_HINT_BOUNDED_ABOVE(portHints.HintDescriptor))
  393. max = portHints.UpperBound;
  394. else
  395. max = 1.0;
  396. if (min > max)
  397. max = min;
  398. else if (max < min)
  399. min = max;
  400. if (max - min == 0.0)
  401. {
  402. qWarning("Broken plugin parameter: max - min == 0");
  403. max = min + 0.1;
  404. }
  405. // default value
  406. if (hasPortRDF && LADSPA_PORT_HAS_DEFAULT(rdf_descriptor->Ports[i].Hints))
  407. def = rdf_descriptor->Ports[i].Default;
  408. else
  409. def = get_default_ladspa_port_value(portHints.HintDescriptor, min, max);
  410. if (def < min)
  411. def = min;
  412. else if (def > max)
  413. def = max;
  414. if (LADSPA_IS_HINT_SAMPLE_RATE(portHints.HintDescriptor))
  415. {
  416. min *= sampleRate;
  417. max *= sampleRate;
  418. def *= sampleRate;
  419. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  420. }
  421. if (LADSPA_IS_HINT_TOGGLED(portHints.HintDescriptor))
  422. {
  423. step = max - min;
  424. stepSmall = step;
  425. stepLarge = step;
  426. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  427. }
  428. else if (LADSPA_IS_HINT_INTEGER(portHints.HintDescriptor))
  429. {
  430. step = 1.0;
  431. stepSmall = 1.0;
  432. stepLarge = 10.0;
  433. param.data[j].hints |= PARAMETER_IS_INTEGER;
  434. }
  435. else
  436. {
  437. double range = max - min;
  438. step = range/100.0;
  439. stepSmall = range/1000.0;
  440. stepLarge = range/10.0;
  441. }
  442. if (LADSPA_IS_PORT_INPUT(portType))
  443. {
  444. param.data[j].type = PARAMETER_INPUT;
  445. param.data[j].hints |= PARAMETER_IS_ENABLED;
  446. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  447. needsCtrlIn = true;
  448. }
  449. else if (LADSPA_IS_PORT_OUTPUT(portType))
  450. {
  451. if (strcmp(descriptor->PortNames[i], "latency") == 0 || strcmp(descriptor->PortNames[i], "_latency") == 0)
  452. {
  453. min = 0.0;
  454. max = sampleRate;
  455. def = 0.0;
  456. step = 1.0;
  457. stepSmall = 1.0;
  458. stepLarge = 1.0;
  459. param.data[j].type = PARAMETER_LATENCY;
  460. param.data[j].hints = 0;
  461. }
  462. else if (strcmp(descriptor->PortNames[i], "_sample-rate") == 0)
  463. {
  464. def = sampleRate;
  465. step = 1.0;
  466. stepSmall = 1.0;
  467. stepLarge = 1.0;
  468. param.data[j].type = PARAMETER_SAMPLE_RATE;
  469. param.data[j].hints = 0;
  470. }
  471. else
  472. {
  473. param.data[j].type = PARAMETER_OUTPUT;
  474. param.data[j].hints |= PARAMETER_IS_ENABLED;
  475. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  476. needsCtrlOut = true;
  477. }
  478. }
  479. else
  480. {
  481. param.data[j].type = PARAMETER_UNKNOWN;
  482. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  483. }
  484. // extra parameter hints
  485. if (LADSPA_IS_HINT_LOGARITHMIC(portHints.HintDescriptor))
  486. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  487. // check for scalepoints, require at least 2 to make it useful
  488. if (hasPortRDF && rdf_descriptor->Ports[i].ScalePointCount > 1)
  489. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  490. param.ranges[j].min = min;
  491. param.ranges[j].max = max;
  492. param.ranges[j].def = def;
  493. param.ranges[j].step = step;
  494. param.ranges[j].stepSmall = stepSmall;
  495. param.ranges[j].stepLarge = stepLarge;
  496. // Start parameters in their default values
  497. paramBuffers[j] = def;
  498. descriptor->connect_port(handle, i, &paramBuffers[j]);
  499. if (h2) descriptor->connect_port(h2, i, &paramBuffers[j]);
  500. }
  501. else
  502. {
  503. // Not Audio or Control
  504. qCritical("ERROR - Got a broken Port (neither Audio or Control)");
  505. descriptor->connect_port(handle, i, nullptr);
  506. if (h2) descriptor->connect_port(h2, i, nullptr);
  507. }
  508. }
  509. if (needsCtrlIn)
  510. {
  511. #ifndef BUILD_BRIDGE
  512. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  513. {
  514. strcpy(portName, m_name);
  515. strcat(portName, ":control-in");
  516. }
  517. else
  518. #endif
  519. strcpy(portName, "control-in");
  520. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  521. }
  522. if (needsCtrlOut)
  523. {
  524. #ifndef BUILD_BRIDGE
  525. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  526. {
  527. strcpy(portName, m_name);
  528. strcat(portName, ":control-out");
  529. }
  530. else
  531. #endif
  532. strcpy(portName, "control-out");
  533. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  534. }
  535. aIn.count = aIns;
  536. aOut.count = aOuts;
  537. param.count = params;
  538. // plugin checks
  539. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE);
  540. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  541. m_hints |= PLUGIN_CAN_DRYWET;
  542. if (aOuts > 0)
  543. m_hints |= PLUGIN_CAN_VOLUME;
  544. if (aOuts >= 2 && aOuts%2 == 0)
  545. m_hints |= PLUGIN_CAN_BALANCE;
  546. x_client->activate();
  547. qDebug("LadspaPlugin::reload() - end");
  548. }
  549. // -------------------------------------------------------------------
  550. // Plugin processing
  551. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  552. {
  553. uint32_t i, k;
  554. double aInsPeak[2] = { 0.0 };
  555. double aOutsPeak[2] = { 0.0 };
  556. CARLA_PROCESS_CONTINUE_CHECK;
  557. // --------------------------------------------------------------------------------------------------------
  558. // Input VU
  559. if (aIn.count > 0)
  560. {
  561. if (aIn.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 (aIn.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. for (i=0; i < aOut.count; i++)
  756. {
  757. // Dry/Wet
  758. if (do_drywet)
  759. {
  760. for (k=0; k < frames; k++)
  761. {
  762. if (aOut.count == 1)
  763. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[0][k]*(1.0-x_dryWet));
  764. else
  765. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[i][k]*(1.0-x_dryWet));
  766. }
  767. }
  768. // Balance
  769. if (do_balance)
  770. {
  771. if (i%2 == 0)
  772. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  773. bal_rangeL = (x_balanceLeft+1.0)/2;
  774. bal_rangeR = (x_balanceRight+1.0)/2;
  775. for (k=0; k < frames; k++)
  776. {
  777. if (i%2 == 0)
  778. {
  779. // left output
  780. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  781. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  782. }
  783. else
  784. {
  785. // right
  786. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  787. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  788. }
  789. }
  790. }
  791. // Volume
  792. if (do_volume)
  793. {
  794. for (k=0; k < frames; k++)
  795. outBuffer[i][k] *= x_volume;
  796. }
  797. // Output VU
  798. for (k=0; i < 2 && k < frames; k++)
  799. {
  800. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  801. aOutsPeak[i] = abs(outBuffer[i][k]);
  802. }
  803. }
  804. }
  805. else
  806. {
  807. // disable any output sound if not active
  808. for (i=0; i < aOut.count; i++)
  809. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  810. aOutsPeak[0] = 0.0;
  811. aOutsPeak[1] = 0.0;
  812. } // End of Post-processing
  813. CARLA_PROCESS_CONTINUE_CHECK;
  814. // --------------------------------------------------------------------------------------------------------
  815. // Control Output
  816. if (param.portCout && m_active)
  817. {
  818. double value;
  819. for (k=0; k < param.count; k++)
  820. {
  821. if (param.data[k].type == PARAMETER_OUTPUT)
  822. {
  823. fixParameterValue(paramBuffers[k], param.ranges[k]);
  824. if (param.data[k].midiCC > 0)
  825. {
  826. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  827. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  828. }
  829. }
  830. }
  831. } // End of Control Output
  832. CARLA_PROCESS_CONTINUE_CHECK;
  833. // --------------------------------------------------------------------------------------------------------
  834. // Peak Values
  835. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  836. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  837. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  838. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  839. m_activeBefore = m_active;
  840. }
  841. // -------------------------------------------------------------------
  842. // Cleanup
  843. void deleteBuffers()
  844. {
  845. qDebug("LadspaPlugin::deleteBuffers() - start");
  846. if (param.count > 0)
  847. delete[] paramBuffers;
  848. paramBuffers = nullptr;
  849. qDebug("LadspaPlugin::deleteBuffers() - end");
  850. }
  851. // -------------------------------------------------------------------
  852. bool init(const char* const filename, const char* const name, const char* const label, const LADSPA_RDF_Descriptor* const rdf_descriptor_)
  853. {
  854. // ---------------------------------------------------------------
  855. // open DLL
  856. if (! libOpen(filename))
  857. {
  858. setLastError(libError(filename));
  859. return false;
  860. }
  861. // ---------------------------------------------------------------
  862. // get DLL main entry
  863. const LADSPA_Descriptor_Function descFn = (LADSPA_Descriptor_Function)libSymbol("ladspa_descriptor");
  864. if (! descFn)
  865. {
  866. setLastError("Could not find the LASDPA Descriptor in the plugin library");
  867. return false;
  868. }
  869. // ---------------------------------------------------------------
  870. // get descriptor that matches label
  871. unsigned long i = 0;
  872. while ((descriptor = descFn(i++)))
  873. {
  874. if (strcmp(descriptor->Label, label) == 0)
  875. break;
  876. }
  877. if (! descriptor)
  878. {
  879. setLastError("Could not find the requested plugin Label in the plugin library");
  880. return false;
  881. }
  882. // ---------------------------------------------------------------
  883. // initialize plugin
  884. handle = descriptor->instantiate(descriptor, x_engine->getSampleRate());
  885. if (! handle)
  886. {
  887. setLastError("Plugin failed to initialize");
  888. return false;
  889. }
  890. // ---------------------------------------------------------------
  891. // get info
  892. m_filename = strdup(filename);
  893. if (is_ladspa_rdf_descriptor_valid(rdf_descriptor_, descriptor))
  894. rdf_descriptor = ladspa_rdf_dup(rdf_descriptor_);
  895. if (name)
  896. m_name = x_engine->getUniqueName(name);
  897. else if (rdf_descriptor && rdf_descriptor->Title)
  898. m_name = x_engine->getUniqueName(rdf_descriptor->Title);
  899. else
  900. m_name = x_engine->getUniqueName(descriptor->Name);
  901. // ---------------------------------------------------------------
  902. // register client
  903. x_client = x_engine->addClient(this);
  904. if (! x_client->isOk())
  905. {
  906. setLastError("Failed to register plugin client");
  907. return false;
  908. }
  909. return true;
  910. }
  911. private:
  912. LADSPA_Handle handle, h2;
  913. const LADSPA_Descriptor* descriptor;
  914. const LADSPA_RDF_Descriptor* rdf_descriptor;
  915. float* paramBuffers;
  916. };
  917. CarlaPlugin* CarlaPlugin::newLADSPA(const initializer& init, const void* const extra)
  918. {
  919. qDebug("CarlaPlugin::newLADSPA(%p, \"%s\", \"%s\", \"%s\", %p)", init.engine, init.filename, init.name, init.label, extra);
  920. short id = init.engine->getNewPluginId();
  921. if (id < 0 || id > MAX_PLUGINS)
  922. {
  923. setLastError("Maximum number of plugins reached");
  924. return nullptr;
  925. }
  926. LadspaPlugin* const plugin = new LadspaPlugin(init.engine, id);
  927. if (! plugin->init(init.filename, init.name, init.label, (const LADSPA_RDF_Descriptor*)extra))
  928. {
  929. delete plugin;
  930. return nullptr;
  931. }
  932. plugin->reload();
  933. #ifndef BUILD_BRIDGE
  934. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  935. {
  936. const uint32_t ins = plugin->audioInCount();
  937. const uint32_t outs = plugin->audioOutCount();
  938. if (ins > 2 || outs > 2 || (ins != outs && ins != 0 && outs != 0))
  939. {
  940. setLastError("Carla's rack mode can only work with Mono or Stereo LADSPA plugins, sorry!");
  941. delete plugin;
  942. return nullptr;
  943. }
  944. }
  945. #endif
  946. plugin->registerToOsc();
  947. return plugin;
  948. }
  949. /**@}*/
  950. CARLA_BACKEND_END_NAMESPACE