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.

962 lines
33KB

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