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.

1060 lines
36KB

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