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.

1265 lines
41KB

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