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.

1180 lines
39KB

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