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.

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