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. #if 0
  21. } /* adjust editor indent */
  22. #endif
  23. /*!
  24. * @defgroup CarlaBackendLadspaPlugin Carla Backend LADSPA Plugin
  25. *
  26. * The Carla Backend LADSPA Plugin.\n
  27. * http://www.ladspa.org/
  28. * @{
  29. */
  30. class LadspaPlugin : public CarlaPlugin
  31. {
  32. public:
  33. LadspaPlugin(CarlaEngine* const engine, const unsigned short id)
  34. : CarlaPlugin(engine, id)
  35. {
  36. qDebug("LadspaPlugin::LadspaPlugin()");
  37. m_type = PLUGIN_LADSPA;
  38. handle = h2 = nullptr;
  39. descriptor = nullptr;
  40. rdf_descriptor = nullptr;
  41. param_buffers = nullptr;
  42. }
  43. ~LadspaPlugin()
  44. {
  45. qDebug("LadspaPlugin::~LadspaPlugin()");
  46. if (descriptor)
  47. {
  48. if (descriptor->deactivate && m_activeBefore)
  49. {
  50. if (handle)
  51. descriptor->deactivate(handle);
  52. if (h2)
  53. descriptor->deactivate(h2);
  54. }
  55. if (descriptor->cleanup)
  56. {
  57. if (handle)
  58. descriptor->cleanup(handle);
  59. if (h2)
  60. descriptor->cleanup(h2);
  61. }
  62. }
  63. if (rdf_descriptor)
  64. ladspa_rdf_free(rdf_descriptor);
  65. }
  66. // -------------------------------------------------------------------
  67. // Information (base)
  68. PluginCategory category()
  69. {
  70. if (rdf_descriptor)
  71. {
  72. const LADSPA_Properties category = rdf_descriptor->Type;
  73. // Specific Types
  74. if (category & (LADSPA_CLASS_DELAY|LADSPA_CLASS_REVERB))
  75. return PLUGIN_CATEGORY_DELAY;
  76. if (category & (LADSPA_CLASS_PHASER|LADSPA_CLASS_FLANGER|LADSPA_CLASS_CHORUS))
  77. return PLUGIN_CATEGORY_MODULATOR;
  78. if (category & (LADSPA_CLASS_AMPLIFIER))
  79. return PLUGIN_CATEGORY_DYNAMICS;
  80. if (category & (LADSPA_CLASS_UTILITY|LADSPA_CLASS_SPECTRAL|LADSPA_CLASS_FREQUENCY_METER))
  81. return PLUGIN_CATEGORY_UTILITY;
  82. // Pre-set LADSPA Types
  83. if (LADSPA_IS_PLUGIN_DYNAMICS(category))
  84. return PLUGIN_CATEGORY_DYNAMICS;
  85. if (LADSPA_IS_PLUGIN_AMPLITUDE(category))
  86. return PLUGIN_CATEGORY_MODULATOR;
  87. if (LADSPA_IS_PLUGIN_EQ(category))
  88. return PLUGIN_CATEGORY_EQ;
  89. if (LADSPA_IS_PLUGIN_FILTER(category))
  90. return PLUGIN_CATEGORY_FILTER;
  91. if (LADSPA_IS_PLUGIN_FREQUENCY(category))
  92. return PLUGIN_CATEGORY_UTILITY;
  93. if (LADSPA_IS_PLUGIN_SIMULATOR(category))
  94. return PLUGIN_CATEGORY_OTHER;
  95. if (LADSPA_IS_PLUGIN_TIME(category))
  96. return PLUGIN_CATEGORY_DELAY;
  97. if (LADSPA_IS_PLUGIN_GENERATOR(category))
  98. return PLUGIN_CATEGORY_SYNTH;
  99. }
  100. return getPluginCategoryFromName(m_name);
  101. }
  102. long uniqueId()
  103. {
  104. Q_ASSERT(descriptor);
  105. return descriptor->UniqueID;
  106. }
  107. // -------------------------------------------------------------------
  108. // Information (count)
  109. uint32_t parameterScalePointCount(const uint32_t parameterId)
  110. {
  111. Q_ASSERT(parameterId < param.count);
  112. int32_t rindex = param.data[parameterId].rindex;
  113. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  114. {
  115. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  116. if (port)
  117. return port->ScalePointCount;
  118. }
  119. return 0;
  120. }
  121. // -------------------------------------------------------------------
  122. // Information (per-plugin data)
  123. double getParameterValue(const uint32_t parameterId)
  124. {
  125. Q_ASSERT(parameterId < param.count);
  126. return param_buffers[parameterId];
  127. }
  128. double getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  129. {
  130. Q_ASSERT(parameterId < param.count);
  131. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  132. int32_t rindex = param.data[parameterId].rindex;
  133. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  134. {
  135. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  136. if (port && scalePointId < port->ScalePointCount)
  137. {
  138. const LADSPA_RDF_ScalePoint* const scalePoint = &port->ScalePoints[scalePointId];
  139. if (scalePoint)
  140. return scalePoint->Value;
  141. }
  142. }
  143. return 0.0;
  144. }
  145. void getLabel(char* const strBuf)
  146. {
  147. Q_ASSERT(descriptor);
  148. if (descriptor && descriptor->Label)
  149. strncpy(strBuf, descriptor->Label, STR_MAX);
  150. else
  151. CarlaPlugin::getLabel(strBuf);
  152. }
  153. void getMaker(char* const strBuf)
  154. {
  155. Q_ASSERT(descriptor);
  156. if (rdf_descriptor && rdf_descriptor->Creator)
  157. strncpy(strBuf, rdf_descriptor->Creator, STR_MAX);
  158. else if (descriptor && descriptor->Maker)
  159. strncpy(strBuf, descriptor->Maker, STR_MAX);
  160. else
  161. CarlaPlugin::getMaker(strBuf);
  162. }
  163. void getCopyright(char* const strBuf)
  164. {
  165. Q_ASSERT(descriptor);
  166. if (descriptor && descriptor->Copyright)
  167. strncpy(strBuf, descriptor->Copyright, STR_MAX);
  168. else
  169. CarlaPlugin::getCopyright(strBuf);
  170. }
  171. void getRealName(char* const strBuf)
  172. {
  173. Q_ASSERT(descriptor);
  174. if (rdf_descriptor && rdf_descriptor->Title)
  175. strncpy(strBuf, rdf_descriptor->Title, STR_MAX);
  176. else if (descriptor && descriptor->Name)
  177. strncpy(strBuf, descriptor->Name, STR_MAX);
  178. else
  179. CarlaPlugin::getRealName(strBuf);
  180. }
  181. void getParameterName(const uint32_t parameterId, char* const strBuf)
  182. {
  183. Q_ASSERT(descriptor);
  184. Q_ASSERT(parameterId < param.count);
  185. int32_t rindex = param.data[parameterId].rindex;
  186. if (descriptor && rindex < (int32_t)descriptor->PortCount)
  187. strncpy(strBuf, descriptor->PortNames[rindex], STR_MAX);
  188. else
  189. CarlaPlugin::getParameterName(parameterId, strBuf);
  190. }
  191. void getParameterSymbol(const uint32_t parameterId, char* const strBuf)
  192. {
  193. Q_ASSERT(parameterId < param.count);
  194. int32_t rindex = param.data[parameterId].rindex;
  195. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  196. {
  197. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  198. if (LADSPA_PORT_HAS_LABEL(port->Hints) && port->Label)
  199. {
  200. strncpy(strBuf, port->Label, STR_MAX);
  201. return;
  202. }
  203. }
  204. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  205. }
  206. void getParameterUnit(const uint32_t parameterId, char* const strBuf)
  207. {
  208. Q_ASSERT(parameterId < param.count);
  209. int32_t rindex = param.data[parameterId].rindex;
  210. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  211. {
  212. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  213. if (LADSPA_PORT_HAS_UNIT(port->Hints))
  214. {
  215. switch (port->Unit)
  216. {
  217. case LADSPA_UNIT_DB:
  218. strncpy(strBuf, "dB", STR_MAX);
  219. return;
  220. case LADSPA_UNIT_COEF:
  221. strncpy(strBuf, "(coef)", STR_MAX);
  222. return;
  223. case LADSPA_UNIT_HZ:
  224. strncpy(strBuf, "Hz", STR_MAX);
  225. return;
  226. case LADSPA_UNIT_S:
  227. strncpy(strBuf, "s", STR_MAX);
  228. return;
  229. case LADSPA_UNIT_MS:
  230. strncpy(strBuf, "ms", STR_MAX);
  231. return;
  232. case LADSPA_UNIT_MIN:
  233. strncpy(strBuf, "min", STR_MAX);
  234. return;
  235. }
  236. }
  237. }
  238. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  239. }
  240. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  241. {
  242. Q_ASSERT(parameterId < param.count);
  243. Q_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  244. int32_t rindex = param.data[parameterId].rindex;
  245. if (rdf_descriptor && rindex < (int32_t)rdf_descriptor->PortCount)
  246. {
  247. const LADSPA_RDF_Port* const port = &rdf_descriptor->Ports[rindex];
  248. if (port && scalePointId < port->ScalePointCount)
  249. {
  250. const LADSPA_RDF_ScalePoint* const scalePoint = &port->ScalePoints[scalePointId];
  251. if (scalePoint && scalePoint->Label)
  252. strncpy(strBuf, scalePoint->Label, STR_MAX);
  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. param_buffers[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. param_buffers = 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, "-2");
  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, "-2");
  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. step = 1.0;
  464. stepSmall = 1.0;
  465. stepLarge = 1.0;
  466. param.data[j].type = PARAMETER_SAMPLE_RATE;
  467. param.data[j].hints = 0;
  468. }
  469. else
  470. {
  471. param.data[j].type = PARAMETER_OUTPUT;
  472. param.data[j].hints |= PARAMETER_IS_ENABLED;
  473. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  474. needsCtrlOut = true;
  475. }
  476. }
  477. else
  478. {
  479. param.data[j].type = PARAMETER_UNKNOWN;
  480. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  481. }
  482. // extra parameter hints
  483. if (LADSPA_IS_HINT_LOGARITHMIC(portHints.HintDescriptor))
  484. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  485. // check for scalepoints, require at least 2 to make it useful
  486. if (hasPortRDF && rdf_descriptor->Ports[i].ScalePointCount > 1)
  487. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  488. param.ranges[j].min = min;
  489. param.ranges[j].max = max;
  490. param.ranges[j].def = def;
  491. param.ranges[j].step = step;
  492. param.ranges[j].stepSmall = stepSmall;
  493. param.ranges[j].stepLarge = stepLarge;
  494. // Start parameters in their default values
  495. param_buffers[j] = def;
  496. descriptor->connect_port(handle, i, &param_buffers[j]);
  497. if (h2) descriptor->connect_port(h2, i, &param_buffers[j]);
  498. }
  499. else
  500. {
  501. // Not Audio or Control
  502. qCritical("ERROR - Got a broken Port (neither Audio or Control)");
  503. descriptor->connect_port(handle, i, nullptr);
  504. if (h2) descriptor->connect_port(h2, i, nullptr);
  505. }
  506. }
  507. if (needsCtrlIn)
  508. {
  509. #ifndef BUILD_BRIDGE
  510. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  511. {
  512. strcpy(portName, m_name);
  513. strcat(portName, ":control-in");
  514. }
  515. else
  516. #endif
  517. strcpy(portName, "control-in");
  518. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  519. }
  520. if (needsCtrlOut)
  521. {
  522. #ifndef BUILD_BRIDGE
  523. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  524. {
  525. strcpy(portName, m_name);
  526. strcat(portName, ":control-out");
  527. }
  528. else
  529. #endif
  530. strcpy(portName, "control-out");
  531. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  532. }
  533. aIn.count = aIns;
  534. aOut.count = aOuts;
  535. param.count = params;
  536. // plugin checks
  537. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE);
  538. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  539. m_hints |= PLUGIN_CAN_DRYWET;
  540. if (aOuts > 0)
  541. m_hints |= PLUGIN_CAN_VOLUME;
  542. if ((aOuts >= 2 && aOuts%2 == 0) || h2)
  543. m_hints |= PLUGIN_CAN_BALANCE;
  544. x_client->activate();
  545. qDebug("LadspaPlugin::reload() - end");
  546. }
  547. // -------------------------------------------------------------------
  548. // Plugin processing
  549. void process(float* const* const inBuffer, float* const* const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  550. {
  551. uint32_t i, k;
  552. double aInsPeak[2] = { 0.0 };
  553. double aOutsPeak[2] = { 0.0 };
  554. CARLA_PROCESS_CONTINUE_CHECK;
  555. // --------------------------------------------------------------------------------------------------------
  556. // Input VU
  557. if (aIn.count > 0)
  558. {
  559. uint32_t count = h2 ? 2 : aIn.count;
  560. if (count == 1)
  561. {
  562. for (k=0; k < frames; k++)
  563. {
  564. if (abs(inBuffer[0][k]) > aInsPeak[0])
  565. aInsPeak[0] = abs(inBuffer[0][k]);
  566. }
  567. }
  568. else if (count > 1)
  569. {
  570. for (k=0; k < frames; k++)
  571. {
  572. if (abs(inBuffer[0][k]) > aInsPeak[0])
  573. aInsPeak[0] = abs(inBuffer[0][k]);
  574. if (abs(inBuffer[1][k]) > aInsPeak[1])
  575. aInsPeak[1] = abs(inBuffer[1][k]);
  576. }
  577. }
  578. }
  579. CARLA_PROCESS_CONTINUE_CHECK;
  580. // --------------------------------------------------------------------------------------------------------
  581. // Parameters Input [Automation]
  582. if (param.portCin && m_active && m_activeBefore)
  583. {
  584. const CarlaEngineControlEvent* cinEvent;
  585. uint32_t time, nEvents = param.portCin->getEventCount();
  586. for (i=0; i < nEvents; i++)
  587. {
  588. cinEvent = param.portCin->getEvent(i);
  589. if (! cinEvent)
  590. continue;
  591. time = cinEvent->time - framesOffset;
  592. if (time >= frames)
  593. continue;
  594. // Control change
  595. switch (cinEvent->type)
  596. {
  597. case CarlaEngineEventNull:
  598. break;
  599. case CarlaEngineEventControlChange:
  600. {
  601. double value;
  602. // Control backend stuff
  603. if (cinEvent->channel == m_ctrlInChannel)
  604. {
  605. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  606. {
  607. value = cinEvent->value;
  608. setDryWet(value, false, false);
  609. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  610. continue;
  611. }
  612. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  613. {
  614. value = cinEvent->value*127/100;
  615. setVolume(value, false, false);
  616. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  617. continue;
  618. }
  619. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  620. {
  621. double left, right;
  622. value = cinEvent->value/0.5 - 1.0;
  623. if (value < 0.0)
  624. {
  625. left = -1.0;
  626. right = (value*2)+1.0;
  627. }
  628. else if (value > 0.0)
  629. {
  630. left = (value*2)-1.0;
  631. right = 1.0;
  632. }
  633. else
  634. {
  635. left = -1.0;
  636. right = 1.0;
  637. }
  638. setBalanceLeft(left, false, false);
  639. setBalanceRight(right, false, false);
  640. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  641. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  642. continue;
  643. }
  644. }
  645. // Control plugin parameters
  646. for (k=0; k < param.count; k++)
  647. {
  648. if (param.data[k].midiChannel != cinEvent->channel)
  649. continue;
  650. if (param.data[k].midiCC != cinEvent->controller)
  651. continue;
  652. if (param.data[k].type != PARAMETER_INPUT)
  653. continue;
  654. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  655. {
  656. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  657. {
  658. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  659. }
  660. else
  661. {
  662. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  663. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  664. value = rint(value);
  665. }
  666. setParameterValue(k, value, false, false, false);
  667. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  668. }
  669. }
  670. break;
  671. }
  672. case CarlaEngineEventMidiBankChange:
  673. case CarlaEngineEventMidiProgramChange:
  674. break;
  675. case CarlaEngineEventAllSoundOff:
  676. if (cinEvent->channel == m_ctrlInChannel)
  677. {
  678. if (descriptor->deactivate)
  679. {
  680. descriptor->deactivate(handle);
  681. if (h2) descriptor->deactivate(h2);
  682. }
  683. if (descriptor->activate)
  684. {
  685. descriptor->activate(handle);
  686. if (h2) descriptor->activate(h2);
  687. }
  688. }
  689. break;
  690. case CarlaEngineEventAllNotesOff:
  691. break;
  692. }
  693. }
  694. } // End of Parameters Input
  695. CARLA_PROCESS_CONTINUE_CHECK;
  696. // --------------------------------------------------------------------------------------------------------
  697. // Special Parameters
  698. #if 0
  699. for (k=0; k < param.count; k++)
  700. {
  701. if (param.data[k].type == PARAMETER_LATENCY)
  702. {
  703. // TODO: ladspa special params
  704. }
  705. }
  706. CARLA_PROCESS_CONTINUE_CHECK;
  707. #endif
  708. // --------------------------------------------------------------------------------------------------------
  709. // Plugin processing
  710. if (m_active)
  711. {
  712. if (! m_activeBefore)
  713. {
  714. if (descriptor->activate)
  715. {
  716. descriptor->activate(handle);
  717. if (h2) descriptor->activate(h2);
  718. }
  719. }
  720. for (i=0; i < aIn.count; i++)
  721. {
  722. if (i == 0 || ! h2) descriptor->connect_port(handle, aIn.rindexes[i], inBuffer[i]);
  723. if (i == 1 && h2) descriptor->connect_port(h2, aIn.rindexes[i], inBuffer[i]);
  724. }
  725. for (i=0; i < aOut.count; i++)
  726. {
  727. if (i == 0 || ! h2) descriptor->connect_port(handle, aOut.rindexes[i], outBuffer[i]);
  728. if (i == 1 && h2) descriptor->connect_port(h2, aOut.rindexes[i], outBuffer[i]);
  729. }
  730. descriptor->run(handle, frames);
  731. if (h2) descriptor->run(h2, frames);
  732. }
  733. else
  734. {
  735. if (m_activeBefore)
  736. {
  737. if (descriptor->deactivate)
  738. {
  739. descriptor->deactivate(handle);
  740. if (h2) descriptor->deactivate(h2);
  741. }
  742. }
  743. }
  744. CARLA_PROCESS_CONTINUE_CHECK;
  745. // --------------------------------------------------------------------------------------------------------
  746. // Post-processing (dry/wet, volume and balance)
  747. if (m_active)
  748. {
  749. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  750. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  751. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  752. double bal_rangeL, bal_rangeR;
  753. float oldBufLeft[do_balance ? frames : 0];
  754. uint32_t count = h2 ? 2 : aOut.count;
  755. for (i=0; i < count; i++)
  756. {
  757. // Dry/Wet
  758. if (do_drywet)
  759. {
  760. for (k=0; k < frames; k++)
  761. {
  762. if (aOut.count == 1 && ! h2)
  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(param_buffers[k], param.ranges[k]);
  824. if (param.data[k].midiCC > 0)
  825. {
  826. value = (param_buffers[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[] param_buffers;
  848. param_buffers = 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. 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* param_buffers;
  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)
  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