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.

1285 lines
43KB

  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 "dssi/dssi.h"
  19. class DssiPlugin : public CarlaPlugin
  20. {
  21. public:
  22. DssiPlugin() : CarlaPlugin()
  23. {
  24. qDebug("DssiPlugin::DssiPlugin()");
  25. m_type = PLUGIN_DSSI;
  26. handle = nullptr;
  27. descriptor = nullptr;
  28. ldescriptor = nullptr;
  29. ain_rindexes = nullptr;
  30. aout_rindexes = nullptr;
  31. param_buffers = nullptr;
  32. memset(midi_events, 0, sizeof(snd_seq_event_t)*MAX_MIDI_EVENTS);
  33. }
  34. virtual ~DssiPlugin()
  35. {
  36. qDebug("DssiPlugin::~DssiPlugin()");
  37. #ifndef BUILD_BRIDGE
  38. // close UI
  39. if (m_hints & PLUGIN_HAS_GUI)
  40. {
  41. if (osc.data.target)
  42. {
  43. osc_send_hide(&osc.data);
  44. osc_send_quit(&osc.data);
  45. }
  46. if (osc.thread)
  47. {
  48. // FIXME - wait a bit first, then kill
  49. if (osc.thread->isRunning())
  50. {
  51. osc.thread->quit();
  52. if (osc.thread->wait(3000) == false) // 3 sec
  53. qWarning("Failed to properly stop DSSI GUI thread");
  54. }
  55. delete osc.thread;
  56. }
  57. osc_clear_data(&osc.data);
  58. }
  59. #endif
  60. if (handle && ldescriptor->deactivate && m_active_before)
  61. ldescriptor->deactivate(handle);
  62. if (handle && ldescriptor->cleanup)
  63. ldescriptor->cleanup(handle);
  64. handle = nullptr;
  65. descriptor = nullptr;
  66. ldescriptor = nullptr;
  67. }
  68. virtual PluginCategory category()
  69. {
  70. if (midi.port_min && aout.count > 0)
  71. return PLUGIN_CATEGORY_SYNTH;
  72. // TODO - try to get category from label
  73. return PLUGIN_CATEGORY_NONE;
  74. }
  75. virtual long unique_id()
  76. {
  77. return ldescriptor->UniqueID;
  78. }
  79. virtual int32_t chunk_data(void** data_ptr)
  80. {
  81. unsigned long long_data_size = 0;
  82. if (descriptor->get_custom_data(handle, data_ptr, &long_data_size))
  83. return long_data_size;
  84. return 0;
  85. }
  86. virtual double get_parameter_value(uint32_t param_id)
  87. {
  88. return param_buffers[param_id];
  89. }
  90. virtual void get_label(char* buf_str)
  91. {
  92. strncpy(buf_str, ldescriptor->Label, STR_MAX);
  93. }
  94. virtual void get_maker(char* buf_str)
  95. {
  96. strncpy(buf_str, ldescriptor->Maker, STR_MAX);
  97. }
  98. virtual void get_copyright(char* buf_str)
  99. {
  100. strncpy(buf_str, ldescriptor->Copyright, STR_MAX);
  101. }
  102. virtual void get_real_name(char* buf_str)
  103. {
  104. strncpy(buf_str, ldescriptor->Name, STR_MAX);
  105. }
  106. virtual void get_parameter_name(uint32_t param_id, char* buf_str)
  107. {
  108. int32_t rindex = param.data[param_id].rindex;
  109. strncpy(buf_str, ldescriptor->PortNames[rindex], STR_MAX);
  110. }
  111. virtual void get_gui_info(GuiInfo* info)
  112. {
  113. if (m_hints & PLUGIN_HAS_GUI)
  114. info->type = GUI_EXTERNAL_OSC;
  115. else
  116. info->type = GUI_NONE;
  117. }
  118. virtual void set_parameter_value(uint32_t param_id, double value, bool gui_send, bool osc_send, bool callback_send)
  119. {
  120. param_buffers[param_id] = value;
  121. #ifndef BUILD_BRIDGE
  122. if (gui_send)
  123. osc_send_control(&osc.data, param.data[param_id].rindex, value);
  124. #endif
  125. CarlaPlugin::set_parameter_value(param_id, value, gui_send, osc_send, callback_send);
  126. }
  127. virtual void set_custom_data(CustomDataType dtype, const char* key, const char* value, bool gui_send)
  128. {
  129. descriptor->configure(handle, key, value);
  130. #ifndef BUILD_BRIDGE
  131. if (gui_send)
  132. osc_send_configure(&osc.data, key, value);
  133. #endif
  134. if (strcmp(key, "reloadprograms") == 0 || strcmp(key, "load") == 0 || strncmp(key, "patches", 7) == 0)
  135. {
  136. reload_programs(false);
  137. }
  138. else if (strcmp(key, "names") == 0) // Not in the API!
  139. {
  140. if (midiprog.count > 0)
  141. {
  142. //osc_send_set_program_count(&global_osc_data, m_id, midiprog.count);
  143. // FIXME
  144. // Parse names
  145. int j, k, last_str_n = 0;
  146. int str_len = strlen(value);
  147. char name[256];
  148. for (uint32_t i=0; i < prog.count; i++)
  149. {
  150. for (j=0, k=last_str_n; j < 256 && k+j < str_len; j++)
  151. {
  152. name[j] = value[k+j];
  153. if (value[k+j] == ',')
  154. {
  155. name[j] = 0;
  156. last_str_n = k+j+1;
  157. free((void*)midiprog.data[i].name);
  158. midiprog.data[i].name = strdup(name);
  159. break;
  160. }
  161. }
  162. //osc_send_set_program_name(&global_osc_data, m_id, i, midiprog.names[i]);
  163. }
  164. callback_action(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  165. }
  166. }
  167. CarlaPlugin::set_custom_data(dtype, key, value, gui_send);
  168. }
  169. virtual void set_chunk_data(const char* string_data)
  170. {
  171. QByteArray chunk = QByteArray::fromBase64(string_data);
  172. descriptor->set_custom_data(handle, chunk.data(), chunk.size());
  173. }
  174. virtual void set_midi_program(int32_t index, bool gui_send, bool osc_send, bool callback_send, bool block)
  175. {
  176. if (! descriptor->select_program)
  177. return;
  178. if (index >= 0)
  179. {
  180. // TODO - go for id -1 so we don't block audio
  181. if (block) carla_proc_lock();
  182. descriptor->select_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  183. if (block) carla_proc_unlock();
  184. #ifndef BUILD_BRIDGE
  185. if (gui_send)
  186. osc_send_program_as_midi(&osc.data, midiprog.data[index].bank, midiprog.data[index].program);
  187. #endif
  188. }
  189. CarlaPlugin::set_midi_program(index, gui_send, osc_send, callback_send, block);
  190. }
  191. #ifndef BUILD_BRIDGE
  192. virtual void show_gui(bool yesno)
  193. {
  194. if (yesno)
  195. {
  196. osc.thread->start();
  197. }
  198. else
  199. {
  200. osc_send_hide(&osc.data);
  201. osc_send_quit(&osc.data);
  202. osc_clear_data(&osc.data);
  203. }
  204. }
  205. #endif
  206. virtual void reload()
  207. {
  208. qDebug("DssiPlugin::reload()");
  209. short _id = m_id;
  210. // Safely disable plugin for reload
  211. carla_proc_lock();
  212. m_id = -1;
  213. carla_proc_unlock();
  214. // Unregister previous jack ports if needed
  215. if (_id >= 0)
  216. remove_from_jack();
  217. // Delete old data
  218. delete_buffers();
  219. uint32_t ains, aouts, mins, params, j;
  220. ains = aouts = mins = params = 0;
  221. const unsigned long PortCount = ldescriptor->PortCount;
  222. for (unsigned long i=0; i<PortCount; i++)
  223. {
  224. const LADSPA_PortDescriptor PortType = ldescriptor->PortDescriptors[i];
  225. if (LADSPA_IS_PORT_AUDIO(PortType))
  226. {
  227. if (LADSPA_IS_PORT_INPUT(PortType))
  228. ains += 1;
  229. else if (LADSPA_IS_PORT_OUTPUT(PortType))
  230. aouts += 1;
  231. }
  232. else if (LADSPA_IS_PORT_CONTROL(PortType))
  233. params += 1;
  234. }
  235. if (descriptor->run_synth || descriptor->run_multiple_synths)
  236. mins = 1;
  237. if (ains > 0)
  238. {
  239. ain.ports = new jack_port_t*[ains];
  240. ain_rindexes = new uint32_t[ains];
  241. }
  242. if (aouts > 0)
  243. {
  244. aout.ports = new jack_port_t*[aouts];
  245. aout_rindexes = new uint32_t[aouts];
  246. }
  247. if (params > 0)
  248. {
  249. param.data = new ParameterData[params];
  250. param.ranges = new ParameterRanges[params];
  251. param_buffers = new float[params];
  252. }
  253. const int port_name_size = jack_port_name_size();
  254. char port_name[port_name_size];
  255. bool needs_cin = false;
  256. bool needs_cout = false;
  257. for (unsigned long i=0; i<PortCount; i++)
  258. {
  259. const LADSPA_PortDescriptor PortType = ldescriptor->PortDescriptors[i];
  260. const LADSPA_PortRangeHint PortHint = ldescriptor->PortRangeHints[i];
  261. if (LADSPA_IS_PORT_AUDIO(PortType))
  262. {
  263. #ifndef BUILD_BRIDGE
  264. if (carla_options.global_jack_client)
  265. {
  266. strcpy(port_name, m_name);
  267. strcat(port_name, ":");
  268. strncat(port_name, ldescriptor->PortNames[i], port_name_size/2);
  269. }
  270. else
  271. #endif
  272. strncpy(port_name, ldescriptor->PortNames[i], port_name_size/2);
  273. if (LADSPA_IS_PORT_INPUT(PortType))
  274. {
  275. j = ain.count++;
  276. ain.ports[j] = jack_port_register(jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  277. ain_rindexes[j] = i;
  278. }
  279. else if (LADSPA_IS_PORT_OUTPUT(PortType))
  280. {
  281. j = aout.count++;
  282. aout.ports[j] = jack_port_register(jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  283. aout_rindexes[j] = i;
  284. needs_cin = true;
  285. }
  286. else
  287. qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
  288. }
  289. else if (LADSPA_IS_PORT_CONTROL(PortType))
  290. {
  291. j = param.count++;
  292. param.data[j].index = j;
  293. param.data[j].rindex = i;
  294. param.data[j].hints = 0;
  295. param.data[j].midi_channel = 0;
  296. param.data[j].midi_cc = -1;
  297. double min, max, def, step, step_small, step_large;
  298. // min value
  299. if (LADSPA_IS_HINT_BOUNDED_BELOW(PortHint.HintDescriptor))
  300. min = PortHint.LowerBound;
  301. else
  302. min = 0.0;
  303. // max value
  304. if (LADSPA_IS_HINT_BOUNDED_ABOVE(PortHint.HintDescriptor))
  305. max = PortHint.UpperBound;
  306. else
  307. max = 1.0;
  308. if (min > max)
  309. max = min;
  310. else if (max < min)
  311. min = max;
  312. // default value
  313. if (LADSPA_IS_HINT_HAS_DEFAULT(PortHint.HintDescriptor))
  314. {
  315. switch (PortHint.HintDescriptor & LADSPA_HINT_DEFAULT_MASK)
  316. {
  317. case LADSPA_HINT_DEFAULT_MINIMUM:
  318. def = min;
  319. break;
  320. case LADSPA_HINT_DEFAULT_MAXIMUM:
  321. def = max;
  322. break;
  323. case LADSPA_HINT_DEFAULT_0:
  324. def = 0.0;
  325. break;
  326. case LADSPA_HINT_DEFAULT_1:
  327. def = 1.0;
  328. break;
  329. case LADSPA_HINT_DEFAULT_100:
  330. def = 100.0;
  331. break;
  332. case LADSPA_HINT_DEFAULT_440:
  333. def = 440.0;
  334. break;
  335. case LADSPA_HINT_DEFAULT_LOW:
  336. if (LADSPA_IS_HINT_LOGARITHMIC(PortHint.HintDescriptor))
  337. def = exp((log(min)*0.75) + (log(max)*0.25));
  338. else
  339. def = (min*0.75) + (max*0.25);
  340. break;
  341. case LADSPA_HINT_DEFAULT_MIDDLE:
  342. if (LADSPA_IS_HINT_LOGARITHMIC(PortHint.HintDescriptor))
  343. def = sqrt(min*max);
  344. else
  345. def = (min+max)/2;
  346. break;
  347. case LADSPA_HINT_DEFAULT_HIGH:
  348. if (LADSPA_IS_HINT_LOGARITHMIC(PortHint.HintDescriptor))
  349. def = exp((log(min)*0.25) + (log(max)*0.75));
  350. else
  351. def = (min*0.25) + (max*0.75);
  352. break;
  353. default:
  354. if (min < 0.0 && max > 0.0)
  355. def = 0.0;
  356. else
  357. def = min;
  358. break;
  359. }
  360. }
  361. else
  362. {
  363. // no default value
  364. if (min < 0.0 && max > 0.0)
  365. def = 0.0;
  366. else
  367. def = min;
  368. }
  369. if (def < min)
  370. def = min;
  371. else if (def > max)
  372. def = max;
  373. if (max - min <= 0.0)
  374. {
  375. qWarning("Broken plugin parameter -> max - min <= 0");
  376. max = min + 0.1;
  377. }
  378. if (LADSPA_IS_HINT_SAMPLE_RATE(PortHint.HintDescriptor))
  379. {
  380. double sample_rate = get_sample_rate();
  381. min *= sample_rate;
  382. max *= sample_rate;
  383. def *= sample_rate;
  384. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  385. }
  386. if (LADSPA_IS_HINT_INTEGER(PortHint.HintDescriptor))
  387. {
  388. step = 1.0;
  389. step_small = 1.0;
  390. step_large = 10.0;
  391. }
  392. else if (LADSPA_IS_HINT_TOGGLED(PortHint.HintDescriptor))
  393. {
  394. step = max - min;
  395. step_small = step;
  396. step_large = step;
  397. }
  398. else
  399. {
  400. double range = max - min;
  401. step = range/100.0;
  402. step_small = range/1000.0;
  403. step_large = range/10.0;
  404. }
  405. if (LADSPA_IS_PORT_INPUT(PortType))
  406. {
  407. param.data[j].type = PARAMETER_INPUT;
  408. param.data[j].hints |= PARAMETER_IS_ENABLED;
  409. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  410. needs_cin = true;
  411. // MIDI CC value
  412. if (descriptor->get_midi_controller_for_port)
  413. {
  414. int controller = descriptor->get_midi_controller_for_port(handle, i);
  415. if (DSSI_CONTROLLER_IS_SET(controller) && DSSI_IS_CC(controller))
  416. {
  417. int16_t cc = DSSI_CC_NUMBER(controller);
  418. if (! MIDI_IS_CONTROL_BANK_SELECT(cc))
  419. param.data[j].midi_cc = cc;
  420. }
  421. }
  422. }
  423. else if (LADSPA_IS_PORT_OUTPUT(PortType))
  424. {
  425. param.data[j].type = PARAMETER_OUTPUT;
  426. param.data[j].hints |= PARAMETER_IS_ENABLED;
  427. if (strcmp(ldescriptor->PortNames[i], "latency") != 0 && strcmp(ldescriptor->PortNames[i], "_latency") != 0)
  428. {
  429. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  430. needs_cout = true;
  431. }
  432. else
  433. {
  434. // latency parameter
  435. min = 0;
  436. max = get_sample_rate();
  437. def = 0;
  438. step = 1;
  439. step_small = 1;
  440. step_large = 1;
  441. }
  442. }
  443. else
  444. {
  445. param.data[j].type = PARAMETER_UNKNOWN;
  446. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  447. }
  448. param.ranges[j].min = min;
  449. param.ranges[j].max = max;
  450. param.ranges[j].def = def;
  451. param.ranges[j].step = step;
  452. param.ranges[j].step_small = step_small;
  453. param.ranges[j].step_large = step_large;
  454. // Start parameters in their default values
  455. param_buffers[j] = def;
  456. ldescriptor->connect_port(handle, i, &param_buffers[j]);
  457. }
  458. else
  459. {
  460. // Not Audio or Control
  461. qCritical("ERROR - Got a broken Port (neither Audio or Control)");
  462. ldescriptor->connect_port(handle, i, nullptr);
  463. }
  464. }
  465. if (needs_cin)
  466. {
  467. #ifndef BUILD_BRIDGE
  468. if (carla_options.global_jack_client)
  469. {
  470. strcpy(port_name, m_name);
  471. strcat(port_name, ":control-in");
  472. }
  473. else
  474. #endif
  475. strcpy(port_name, "control-in");
  476. param.port_cin = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  477. }
  478. if (needs_cout)
  479. {
  480. #ifndef BUILD_BRIDGE
  481. if (carla_options.global_jack_client)
  482. {
  483. strcpy(port_name, m_name);
  484. strcat(port_name, ":control-out");
  485. }
  486. else
  487. #endif
  488. strcpy(port_name, "control-out");
  489. param.port_cout = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  490. }
  491. if (mins == 1)
  492. {
  493. #ifndef BUILD_BRIDGE
  494. if (carla_options.global_jack_client)
  495. {
  496. strcpy(port_name, m_name);
  497. strcat(port_name, ":midi-in");
  498. }
  499. else
  500. #endif
  501. strcpy(port_name, "midi-in");
  502. midi.port_min = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  503. }
  504. ain.count = ains;
  505. aout.count = aouts;
  506. param.count = params;
  507. reload_programs(true);
  508. // plugin checks
  509. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE);
  510. if (midi.port_min > 0 && aout.count > 0)
  511. m_hints |= PLUGIN_IS_SYNTH;
  512. #ifndef BUILD_BRIDGE
  513. if (carla_options.use_dssi_chunks && QString(m_filename).endsWith("dssi-vst.so", Qt::CaseInsensitive))
  514. {
  515. if (descriptor->get_custom_data && descriptor->set_custom_data)
  516. m_hints |= PLUGIN_USES_CHUNKS;
  517. }
  518. #endif
  519. if (aouts > 0 && (ains == aouts || ains == 1))
  520. m_hints |= PLUGIN_CAN_DRYWET;
  521. if (aouts > 0)
  522. m_hints |= PLUGIN_CAN_VOLUME;
  523. if (aouts >= 2 && aouts%2 == 0)
  524. m_hints |= PLUGIN_CAN_BALANCE;
  525. carla_proc_lock();
  526. m_id = _id;
  527. carla_proc_unlock();
  528. if (carla_options.global_jack_client == false)
  529. jack_activate(jack_client);
  530. }
  531. virtual void reload_programs(bool init)
  532. {
  533. qDebug("DssiPlugin::reload_programs(%s)", bool2str(init));
  534. uint32_t i, old_count = midiprog.count;
  535. // Delete old programs
  536. if (midiprog.count > 0)
  537. {
  538. for (uint32_t i=0; i < midiprog.count; i++)
  539. free((void*)midiprog.data[i].name);
  540. delete[] midiprog.data;
  541. }
  542. midiprog.count = 0;
  543. midiprog.data = nullptr;
  544. // Query new programs
  545. if (descriptor->get_program && descriptor->select_program)
  546. {
  547. while (descriptor->get_program(handle, midiprog.count))
  548. midiprog.count += 1;
  549. }
  550. if (midiprog.count > 0)
  551. midiprog.data = new midi_program_t [midiprog.count];
  552. // Update data
  553. for (i=0; i < midiprog.count; i++)
  554. {
  555. const DSSI_Program_Descriptor* pdesc = descriptor->get_program(handle, i);
  556. if (pdesc)
  557. {
  558. midiprog.data[i].bank = pdesc->Bank;
  559. midiprog.data[i].program = pdesc->Program;
  560. midiprog.data[i].name = strdup(pdesc->Name);
  561. }
  562. else
  563. {
  564. midiprog.data[i].bank = 0;
  565. midiprog.data[i].program = 0;
  566. midiprog.data[i].name = strdup("(error)");
  567. }
  568. }
  569. // Update OSC Names
  570. //osc_send_set_midi_program_count(&global_osc_data, m_id, midiprog.count);
  571. //for (i=0; i < midiprog.count; i++)
  572. // osc_send_set_midi_program_data(&global_osc_data, m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.names[i]);
  573. callback_action(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  574. if (init)
  575. {
  576. if (midiprog.count > 0)
  577. set_midi_program(0, false, false, false, true);
  578. }
  579. else
  580. {
  581. callback_action(CALLBACK_UPDATE, m_id, 0, 0, 0.0);
  582. // Check if current program is invalid
  583. bool program_changed = false;
  584. if (midiprog.count == old_count+1)
  585. {
  586. // one midi program added, probably created by user
  587. midiprog.current = old_count;
  588. program_changed = true;
  589. }
  590. else if (midiprog.current >= (int32_t)midiprog.count)
  591. {
  592. // current midi program > count
  593. midiprog.current = 0;
  594. program_changed = true;
  595. }
  596. else if (midiprog.current < 0 && midiprog.count > 0)
  597. {
  598. // programs exist now, but not before
  599. midiprog.current = 0;
  600. program_changed = true;
  601. }
  602. else if (midiprog.current >= 0 && midiprog.count == 0)
  603. {
  604. // programs existed before, but not anymore
  605. midiprog.current = -1;
  606. program_changed = true;
  607. }
  608. if (program_changed)
  609. set_midi_program(midiprog.current, true, true, true, true);
  610. }
  611. }
  612. virtual void process(jack_nframes_t nframes)
  613. {
  614. uint32_t i, k;
  615. unsigned short plugin_id = m_id;
  616. unsigned long midi_event_count = 0;
  617. double ains_peak_tmp[2] = { 0.0 };
  618. double aouts_peak_tmp[2] = { 0.0 };
  619. jack_default_audio_sample_t* ains_buffer[ain.count];
  620. jack_default_audio_sample_t* aouts_buffer[aout.count];
  621. void* min_buffer = nullptr;
  622. for (i=0; i < ain.count; i++)
  623. ains_buffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(ain.ports[i], nframes);
  624. for (i=0; i < aout.count; i++)
  625. aouts_buffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(aout.ports[i], nframes);
  626. if (midi.port_min > 0)
  627. min_buffer = jack_port_get_buffer(midi.port_min, nframes);
  628. // --------------------------------------------------------------------------------------------------------
  629. // Input VU
  630. if (ain.count > 0)
  631. {
  632. short j2 = (ain.count == 1) ? 0 : 1;
  633. for (k=0; k<nframes; k++)
  634. {
  635. if (abs_d(ains_buffer[0][k]) > ains_peak_tmp[0])
  636. ains_peak_tmp[0] = abs_d(ains_buffer[0][k]);
  637. if (abs_d(ains_buffer[j2][k]) > ains_peak_tmp[1])
  638. ains_peak_tmp[1] = abs_d(ains_buffer[j2][k]);
  639. }
  640. }
  641. CARLA_PROCESS_CONTINUE_CHECK;
  642. // --------------------------------------------------------------------------------------------------------
  643. // Parameters Input [Automation]
  644. if (param.port_cin)
  645. {
  646. void* pin_buffer = jack_port_get_buffer(param.port_cin, nframes);
  647. jack_midi_event_t pin_event;
  648. uint32_t n_pin_events = jack_midi_get_event_count(pin_buffer);
  649. unsigned char next_bank_id = 0;
  650. if (midiprog.current > 0 && midiprog.count > 0)
  651. next_bank_id = midiprog.data[midiprog.current].bank;
  652. for (i=0; i < n_pin_events; i++)
  653. {
  654. if (jack_midi_event_get(&pin_event, pin_buffer, i) != 0)
  655. break;
  656. jack_midi_data_t status = pin_event.buffer[0];
  657. unsigned char channel = status & 0x0F;
  658. // Control change
  659. if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
  660. {
  661. jack_midi_data_t control = pin_event.buffer[1];
  662. jack_midi_data_t c_value = pin_event.buffer[2];
  663. // Bank Select
  664. if (MIDI_IS_CONTROL_BANK_SELECT(control))
  665. {
  666. next_bank_id = c_value;
  667. continue;
  668. }
  669. double value;
  670. // Control GUI stuff (channel 0 only)
  671. if (channel == 0)
  672. {
  673. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(control) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  674. {
  675. value = double(c_value)/127;
  676. set_drywet(value, false, false);
  677. postpone_event(PostEventParameterChange, PARAMETER_DRYWET, value);
  678. continue;
  679. }
  680. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(control) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  681. {
  682. value = double(c_value)/100;
  683. set_volume(value, false, false);
  684. postpone_event(PostEventParameterChange, PARAMETER_VOLUME, value);
  685. continue;
  686. }
  687. else if (MIDI_IS_CONTROL_BALANCE(control) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  688. {
  689. double left, right;
  690. value = (double(c_value)-63.5)/63.5;
  691. if (value < 0)
  692. {
  693. left = -1.0;
  694. right = (value*2)+1.0;
  695. }
  696. else if (value > 0)
  697. {
  698. left = (value*2)-1.0;
  699. right = 1.0;
  700. }
  701. else
  702. {
  703. left = -1.0;
  704. right = 1.0;
  705. }
  706. set_balance_left(left, false, false);
  707. set_balance_right(right, false, false);
  708. postpone_event(PostEventParameterChange, PARAMETER_BALANCE_LEFT, left);
  709. postpone_event(PostEventParameterChange, PARAMETER_BALANCE_RIGHT, right);
  710. continue;
  711. }
  712. else if (control == MIDI_CONTROL_ALL_SOUND_OFF)
  713. {
  714. if (midi.port_min)
  715. send_midi_all_notes_off();
  716. if (m_active && m_active_before)
  717. {
  718. if (ldescriptor->deactivate)
  719. ldescriptor->deactivate(handle);
  720. m_active_before = false;
  721. }
  722. continue;
  723. }
  724. else if (control == MIDI_CONTROL_ALL_NOTES_OFF)
  725. {
  726. if (midi.port_min)
  727. send_midi_all_notes_off();
  728. continue;
  729. }
  730. }
  731. // Control plugin parameters
  732. for (k=0; k < param.count; k++)
  733. {
  734. if (param.data[k].type == PARAMETER_INPUT && (param.data[k].hints & PARAMETER_IS_AUTOMABLE) > 0 && param.data[k].midi_channel == channel && param.data[k].midi_cc == control)
  735. {
  736. value = (double(c_value) / 127 * (param.ranges[k].max - param.ranges[k].min)) + param.ranges[k].min;
  737. set_parameter_value(k, value, false, false, false);
  738. postpone_event(PostEventParameterChange, k, value);
  739. }
  740. }
  741. }
  742. // Program change
  743. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(status))
  744. {
  745. uint32_t mbank_id = next_bank_id;
  746. uint32_t mprog_id = pin_event.buffer[1]; // & 0x7F;
  747. for (k=0; k < midiprog.count; k++)
  748. {
  749. if (midiprog.data[k].bank == mbank_id && midiprog.data[k].program == mprog_id)
  750. {
  751. set_midi_program(k, false, false, false, false);
  752. postpone_event(PostEventMidiProgramChange, k, 0.0);
  753. break;
  754. }
  755. }
  756. }
  757. }
  758. } // End of Parameters Input
  759. CARLA_PROCESS_CONTINUE_CHECK;
  760. // --------------------------------------------------------------------------------------------------------
  761. // MIDI Input (External)
  762. if (midi.port_min)
  763. {
  764. carla_midi_lock();
  765. for (i=0; i < MAX_MIDI_EVENTS && midi_event_count < MAX_MIDI_EVENTS; i++)
  766. {
  767. if (ext_midi_notes[i].valid)
  768. {
  769. snd_seq_event_t* midi_event = &midi_events[midi_event_count];
  770. memset(midi_event, 0, sizeof(snd_seq_event_t));
  771. midi_event->type = ext_midi_notes[i].onoff ? SND_SEQ_EVENT_NOTEON : SND_SEQ_EVENT_NOTEOFF;
  772. midi_event->data.note.channel = 0;
  773. midi_event->data.note.note = ext_midi_notes[i].note;
  774. midi_event->data.note.velocity = ext_midi_notes[i].velo;
  775. ext_midi_notes[i].valid = false;
  776. midi_event_count += 1;
  777. }
  778. else
  779. break;
  780. }
  781. carla_midi_unlock();
  782. } // End of MIDI Input (External)
  783. CARLA_PROCESS_CONTINUE_CHECK;
  784. // --------------------------------------------------------------------------------------------------------
  785. // MIDI Input (JACK)
  786. if (midi.port_min)
  787. {
  788. jack_midi_event_t min_event;
  789. uint32_t n_min_events = jack_midi_get_event_count(min_buffer);
  790. for (k=0; k < n_min_events && midi_event_count < MAX_MIDI_EVENTS; k++)
  791. {
  792. if (jack_midi_event_get(&min_event, min_buffer, k) != 0)
  793. break;
  794. jack_midi_data_t status = min_event.buffer[0];
  795. unsigned char channel = status & 0x0F;
  796. // Fix bad note-off
  797. if (MIDI_IS_STATUS_NOTE_ON(status) && min_event.buffer[2] == 0)
  798. {
  799. min_event.buffer[0] -= 0x10;
  800. status = min_event.buffer[0];
  801. }
  802. snd_seq_event_t* midi_event = &midi_events[midi_event_count];
  803. memset(midi_event, 0, sizeof(snd_seq_event_t));
  804. midi_event->time.tick = min_event.time;
  805. if (MIDI_IS_STATUS_NOTE_OFF(status))
  806. {
  807. jack_midi_data_t note = min_event.buffer[1];
  808. midi_event->type = SND_SEQ_EVENT_NOTEOFF;
  809. midi_event->data.note.channel = channel;
  810. midi_event->data.note.note = note;
  811. postpone_event(PostEventNoteOff, note, 0.0);
  812. }
  813. else if (MIDI_IS_STATUS_NOTE_ON(status))
  814. {
  815. jack_midi_data_t note = min_event.buffer[1];
  816. jack_midi_data_t velo = min_event.buffer[2];
  817. midi_event->type = SND_SEQ_EVENT_NOTEON;
  818. midi_event->data.note.channel = channel;
  819. midi_event->data.note.note = note;
  820. midi_event->data.note.velocity = velo;
  821. postpone_event(PostEventNoteOn, note, velo);
  822. }
  823. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  824. {
  825. jack_midi_data_t note = min_event.buffer[1];
  826. jack_midi_data_t pressure = min_event.buffer[2];
  827. midi_event->type = SND_SEQ_EVENT_KEYPRESS;
  828. midi_event->data.note.channel = channel;
  829. midi_event->data.note.note = note;
  830. midi_event->data.note.velocity = pressure;
  831. }
  832. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  833. {
  834. jack_midi_data_t pressure = min_event.buffer[1];
  835. midi_event->type = SND_SEQ_EVENT_CHANPRESS;
  836. midi_event->data.control.channel = channel;
  837. midi_event->data.control.value = pressure;
  838. }
  839. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  840. {
  841. jack_midi_data_t lsb = min_event.buffer[1];
  842. jack_midi_data_t msb = min_event.buffer[2];
  843. midi_event->type = SND_SEQ_EVENT_PITCHBEND;
  844. midi_event->data.control.channel = channel;
  845. midi_event->data.control.value = ((msb << 7) | lsb) - 8192;
  846. }
  847. else
  848. continue;
  849. midi_event_count += 1;
  850. }
  851. } // End of MIDI Input (JACK)
  852. CARLA_PROCESS_CONTINUE_CHECK;
  853. // --------------------------------------------------------------------------------------------------------
  854. // Plugin processing
  855. if (m_active)
  856. {
  857. if (m_active_before == false)
  858. {
  859. if (ldescriptor->activate)
  860. ldescriptor->activate(handle);
  861. }
  862. for (i=0; i < ain.count; i++)
  863. ldescriptor->connect_port(handle, ain_rindexes[i], ains_buffer[i]);
  864. for (i=0; i < aout.count; i++)
  865. ldescriptor->connect_port(handle, aout_rindexes[i], aouts_buffer[i]);
  866. if (descriptor->run_synth)
  867. {
  868. descriptor->run_synth(handle, nframes, midi_events, midi_event_count);
  869. }
  870. else if (descriptor->run_multiple_synths)
  871. {
  872. snd_seq_event_t* dssi_events_ptr[] = { midi_events, nullptr };
  873. descriptor->run_multiple_synths(1, &handle, nframes, dssi_events_ptr, &midi_event_count);
  874. }
  875. else if (ldescriptor->run)
  876. ldescriptor->run(handle, nframes);
  877. }
  878. else
  879. {
  880. if (m_active_before)
  881. {
  882. if (ldescriptor->deactivate)
  883. ldescriptor->deactivate(handle);
  884. }
  885. }
  886. CARLA_PROCESS_CONTINUE_CHECK;
  887. // --------------------------------------------------------------------------------------------------------
  888. // Post-processing (dry/wet, volume and balance)
  889. if (m_active)
  890. {
  891. double bal_rangeL, bal_rangeR;
  892. jack_default_audio_sample_t old_bal_left[nframes];
  893. for (i=0; i < aout.count; i++)
  894. {
  895. // Dry/Wet and Volume
  896. for (k=0; k<nframes; k++)
  897. {
  898. if ((m_hints & PLUGIN_CAN_DRYWET) > 0 && x_drywet != 1.0)
  899. {
  900. if (aout.count == 1)
  901. aouts_buffer[i][k] = (aouts_buffer[i][k]*x_drywet)+(ains_buffer[0][k]*(1.0-x_drywet));
  902. else
  903. aouts_buffer[i][k] = (aouts_buffer[i][k]*x_drywet)+(ains_buffer[i][k]*(1.0-x_drywet));
  904. }
  905. if (m_hints & PLUGIN_CAN_VOLUME)
  906. aouts_buffer[i][k] *= x_vol;
  907. }
  908. // Balance
  909. if (m_hints & PLUGIN_CAN_BALANCE)
  910. {
  911. if (i%2 == 0)
  912. memcpy(&old_bal_left, aouts_buffer[i], sizeof(jack_default_audio_sample_t)*nframes);
  913. bal_rangeL = (x_bal_left+1.0)/2;
  914. bal_rangeR = (x_bal_right+1.0)/2;
  915. for (k=0; k<nframes; k++)
  916. {
  917. if (i%2 == 0)
  918. {
  919. // left output
  920. aouts_buffer[i][k] = old_bal_left[k]*(1.0-bal_rangeL);
  921. aouts_buffer[i][k] += aouts_buffer[i+1][k]*(1.0-bal_rangeR);
  922. }
  923. else
  924. {
  925. // right
  926. aouts_buffer[i][k] = aouts_buffer[i][k]*bal_rangeR;
  927. aouts_buffer[i][k] += old_bal_left[k]*bal_rangeL;
  928. }
  929. }
  930. }
  931. // Output VU
  932. if (i < 2)
  933. {
  934. for (k=0; k<nframes; k++)
  935. {
  936. if (abs_d(aouts_buffer[i][k]) > aouts_peak_tmp[i])
  937. aouts_peak_tmp[i] = abs_d(aouts_buffer[i][k]);
  938. }
  939. }
  940. }
  941. }
  942. else
  943. {
  944. // disable any output sound if not active
  945. for (i=0; i < aout.count; i++)
  946. memset(aouts_buffer[i], 0.0f, sizeof(jack_default_audio_sample_t)*nframes);
  947. aouts_peak_tmp[0] = 0.0;
  948. aouts_peak_tmp[1] = 0.0;
  949. } // End of Post-processing
  950. CARLA_PROCESS_CONTINUE_CHECK;
  951. // --------------------------------------------------------------------------------------------------------
  952. // Control Output
  953. if (param.port_cout)
  954. {
  955. void* cout_buffer = jack_port_get_buffer(param.port_cout, nframes);
  956. jack_midi_clear_buffer(cout_buffer);
  957. double value;
  958. for (k=0; k < param.count; k++)
  959. {
  960. if (param.data[k].type == PARAMETER_OUTPUT && param.data[k].midi_cc > 0)
  961. {
  962. value = (param_buffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min) * 127;
  963. jack_midi_data_t* event_buffer = jack_midi_event_reserve(cout_buffer, 0, 3);
  964. event_buffer[0] = 0xB0 + param.data[k].midi_channel;
  965. event_buffer[1] = param.data[k].midi_cc;
  966. event_buffer[2] = value;
  967. }
  968. }
  969. } // End of Control Output
  970. CARLA_PROCESS_CONTINUE_CHECK;
  971. // --------------------------------------------------------------------------------------------------------
  972. // Peak Values
  973. ains_peak[(plugin_id*2)+0] = ains_peak_tmp[0];
  974. ains_peak[(plugin_id*2)+1] = ains_peak_tmp[1];
  975. aouts_peak[(plugin_id*2)+0] = aouts_peak_tmp[0];
  976. aouts_peak[(plugin_id*2)+1] = aouts_peak_tmp[1];
  977. m_active_before = m_active;
  978. }
  979. virtual void delete_buffers()
  980. {
  981. qDebug("DssiPlugin::delete_buffers() - start");
  982. if (ain.count > 0)
  983. delete[] ain_rindexes;
  984. if (aout.count > 0)
  985. delete[] aout_rindexes;
  986. if (param.count > 0)
  987. delete[] param_buffers;
  988. ain_rindexes = nullptr;
  989. aout_rindexes = nullptr;
  990. param_buffers = nullptr;
  991. qDebug("DssiPlugin::delete_buffers() - end");
  992. }
  993. bool init(const char* filename, const char* label, void* extra_stuff)
  994. {
  995. if (lib_open(filename))
  996. {
  997. DSSI_Descriptor_Function descfn = (DSSI_Descriptor_Function)lib_symbol("dssi_descriptor");
  998. if (descfn)
  999. {
  1000. unsigned long i = 0;
  1001. while ((descriptor = descfn(i++)))
  1002. {
  1003. ldescriptor = descriptor->LADSPA_Plugin;
  1004. if (strcmp(ldescriptor->Label, label) == 0)
  1005. break;
  1006. }
  1007. if (descriptor && ldescriptor)
  1008. {
  1009. handle = ldescriptor->instantiate(ldescriptor, get_sample_rate());
  1010. if (handle)
  1011. {
  1012. m_filename = strdup(filename);
  1013. m_name = get_unique_name(ldescriptor->Name);
  1014. if (carla_jack_register_plugin(this, &jack_client))
  1015. {
  1016. #ifndef BUILD_BRIDGE
  1017. if (extra_stuff)
  1018. {
  1019. // GUI Stuff
  1020. const char* gui_filename = (char*)extra_stuff;
  1021. osc.thread = new CarlaPluginThread(this, CarlaPluginThread::PLUGIN_THREAD_DSSI_GUI);
  1022. osc.thread->setOscData(gui_filename, ldescriptor->Label);
  1023. m_hints |= PLUGIN_HAS_GUI;
  1024. }
  1025. #else
  1026. Q_UNUSED(extra_stuff);
  1027. #endif
  1028. return true;
  1029. }
  1030. else
  1031. set_last_error("Failed to register plugin in JACK");
  1032. }
  1033. else
  1034. set_last_error("Plugin failed to initialize");
  1035. }
  1036. else
  1037. set_last_error("Could not find the requested plugin Label in the plugin library");
  1038. }
  1039. else
  1040. set_last_error("Could not find the LASDPA Descriptor in the plugin library");
  1041. }
  1042. else
  1043. set_last_error(lib_error());
  1044. return false;
  1045. }
  1046. private:
  1047. LADSPA_Handle handle;
  1048. const LADSPA_Descriptor* ldescriptor;
  1049. const DSSI_Descriptor* descriptor;
  1050. snd_seq_event_t midi_events[MAX_MIDI_EVENTS];
  1051. float* param_buffers;
  1052. uint32_t* ain_rindexes;
  1053. uint32_t* aout_rindexes;
  1054. };
  1055. short add_plugin_dssi(const char* filename, const char* label, void* extra_stuff)
  1056. {
  1057. qDebug("add_plugin_dssi(%s, %s, %p)", filename, label, extra_stuff);
  1058. short id = get_new_plugin_id();
  1059. if (id >= 0)
  1060. {
  1061. DssiPlugin* plugin = new DssiPlugin;
  1062. if (plugin->init(filename, label, extra_stuff))
  1063. {
  1064. plugin->reload();
  1065. plugin->set_id(id);
  1066. unique_names[id] = plugin->name();
  1067. CarlaPlugins[id] = plugin;
  1068. #ifndef BUILD_BRIDGE
  1069. //osc_new_plugin(plugin);
  1070. #endif
  1071. }
  1072. else
  1073. {
  1074. delete plugin;
  1075. id = -1;
  1076. }
  1077. }
  1078. else
  1079. set_last_error("Maximum number of plugins reached");
  1080. return id;
  1081. }