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.

1954 lines
67KB

  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. #ifdef BUILD_BRIDGE
  18. #error Cannot use bridge for lv2 plugins
  19. #endif
  20. #include "carla_plugin.h"
  21. #include "lv2/lv2.h"
  22. #include "lv2/atom.h"
  23. #include "lv2/event.h"
  24. #include "lv2/event-helpers.h"
  25. #include "lv2/uri-map.h"
  26. #include "lv2/urid.h"
  27. #include "lv2/ui.h"
  28. #include "lv2_rdf.h"
  29. #include "lv2/lv2-midiport.h"
  30. #include "lv2/lv2-midifunctions.h"
  31. // static max values
  32. const unsigned int MAX_EVENT_BUFFER = 8192; // 0x7FFF; // 32767
  33. // extra plugin hints
  34. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x1000;
  35. const unsigned int PLUGIN_HAS_EXTENSION_DYNPARAM = 0x2000;
  36. // parameter hints
  37. const unsigned int PARAMETER_HAS_STRICT_BOUNDS = 0x100;
  38. // feature ids
  39. const uint32_t lv2_feature_id_uri_map = 0;
  40. const uint32_t lv2_feature_id_urid_map = 1;
  41. const uint32_t lv2_feature_id_urid_unmap = 2;
  42. const uint32_t lv2_feature_id_event = 3;
  43. const uint32_t lv2_feature_id_rtmempool = 4;
  44. //const uint32_t lv2_feature_id_data_access = 5;
  45. //const uint32_t lv2_feature_id_instance_access = 6;
  46. //const uint32_t lv2_feature_id_ui_resize = 7;
  47. //const uint32_t lv2_feature_id_ui_parent = 8;
  48. //const uint32_t lv2_feature_id_external_ui = 9;
  49. //const uint32_t lv2_feature_id_external_ui_old = 10;
  50. const uint32_t lv2_feature_count = 5;
  51. // event data/types
  52. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  53. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  54. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  55. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x10;
  56. const unsigned int CARLA_EVENT_TYPE_TIME = 0x20;
  57. // pre-set uri[d] map ids
  58. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  59. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 1;
  60. const uint32_t CARLA_URI_MAP_ID_EVENT_MIDI = 2;
  61. const uint32_t CARLA_URI_MAP_ID_EVENT_TIME = 3;
  62. const uint32_t CARLA_URI_MAP_ID_COUNT = 4;
  63. enum Lv2ParameterDataType {
  64. LV2_PARAMETER_TYPE_CONTROL,
  65. LV2_PARAMETER_TYPE_SOMETHING_ELSE_HERE
  66. };
  67. struct EventData {
  68. unsigned int types;
  69. jack_port_t* port;
  70. union {
  71. LV2_Event_Buffer* e;
  72. LV2_MIDI* m;
  73. } buffer;
  74. };
  75. struct PluginEventData {
  76. uint32_t count;
  77. EventData* data;
  78. };
  79. struct Lv2ParameterData {
  80. Lv2ParameterDataType type;
  81. union {
  82. float control;
  83. };
  84. };
  85. class Lv2Plugin : public CarlaPlugin
  86. {
  87. public:
  88. Lv2Plugin() :
  89. CarlaPlugin()
  90. {
  91. qDebug("Lv2Plugin::Lv2Plugin()");
  92. m_type = PLUGIN_LV2;
  93. ain_rindexes = nullptr;
  94. aout_rindexes = nullptr;
  95. lv2param = nullptr;
  96. evin.count = 0;
  97. evin.data = nullptr;
  98. evout.count = 0;
  99. evout.data = nullptr;
  100. handle = nullptr;
  101. descriptor = nullptr;
  102. rdf_descriptor = nullptr;
  103. // Fill pre-set URI keys
  104. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; i++)
  105. custom_uri_ids.append(nullptr);
  106. for (uint32_t i=0; i < lv2_feature_count+1; i++)
  107. features[i] = nullptr;
  108. }
  109. virtual ~Lv2Plugin()
  110. {
  111. qDebug("Lv2Plugin::~Lv2Plugin()");
  112. if (handle && descriptor->deactivate && m_active_before)
  113. descriptor->deactivate(handle);
  114. if (handle && descriptor->cleanup)
  115. descriptor->cleanup(handle);
  116. if (rdf_descriptor)
  117. lv2_rdf_free(rdf_descriptor);
  118. handle = nullptr;
  119. descriptor = nullptr;
  120. rdf_descriptor = nullptr;
  121. if (features[lv2_feature_id_uri_map] && features[lv2_feature_id_uri_map]->data)
  122. delete (LV2_URI_Map_Feature*)features[lv2_feature_id_uri_map]->data;
  123. if (features[lv2_feature_id_urid_map] && features[lv2_feature_id_urid_map]->data)
  124. delete (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  125. if (features[lv2_feature_id_urid_unmap] && features[lv2_feature_id_urid_unmap]->data)
  126. delete (LV2_URID_Unmap*)features[lv2_feature_id_urid_unmap]->data;
  127. if (features[lv2_feature_id_event] && features[lv2_feature_id_event]->data)
  128. delete (LV2_Event_Feature*)features[lv2_feature_id_event]->data;
  129. for (uint32_t i=0; i < lv2_feature_count; i++)
  130. {
  131. if (features[i])
  132. delete features[i];
  133. }
  134. for (int i=0; i < custom_uri_ids.count(); i++)
  135. {
  136. if (custom_uri_ids[i])
  137. free((void*)custom_uri_ids[i]);
  138. }
  139. custom_uri_ids.clear();
  140. }
  141. virtual PluginCategory category()
  142. {
  143. LV2_Property Category = rdf_descriptor->Type;
  144. // Specific Types
  145. if (Category & LV2_CLASS_REVERB)
  146. return PLUGIN_CATEGORY_DELAY;
  147. // Pre-set LV2 Types
  148. else if (LV2_IS_GENERATOR(Category))
  149. return PLUGIN_CATEGORY_SYNTH;
  150. else if (LV2_IS_UTILITY(Category))
  151. return PLUGIN_CATEGORY_UTILITY;
  152. else if (LV2_IS_SIMULATOR(Category))
  153. return PLUGIN_CATEGORY_OUTRO;
  154. else if (LV2_IS_DELAY(Category))
  155. return PLUGIN_CATEGORY_DELAY;
  156. else if (LV2_IS_MODULATOR(Category))
  157. return PLUGIN_CATEGORY_MODULATOR;
  158. else if (LV2_IS_FILTER(Category))
  159. return PLUGIN_CATEGORY_FILTER;
  160. else if (LV2_IS_EQUALISER(Category))
  161. return PLUGIN_CATEGORY_EQ;
  162. else if (LV2_IS_SPECTRAL(Category))
  163. return PLUGIN_CATEGORY_UTILITY;
  164. else if (LV2_IS_DISTORTION(Category))
  165. return PLUGIN_CATEGORY_OUTRO;
  166. else if (LV2_IS_DYNAMICS(Category))
  167. return PLUGIN_CATEGORY_DYNAMICS;
  168. // TODO - try to get category from label
  169. return PLUGIN_CATEGORY_NONE;
  170. }
  171. virtual long unique_id()
  172. {
  173. return rdf_descriptor->UniqueID;
  174. }
  175. virtual uint32_t min_count()
  176. {
  177. uint32_t count = 0;
  178. for (uint32_t i=0; i < evin.count; i++)
  179. {
  180. if (evin.data[i].types & CARLA_EVENT_TYPE_MIDI)
  181. count += 1;
  182. }
  183. return count;
  184. }
  185. virtual uint32_t mout_count()
  186. {
  187. uint32_t count = 0;
  188. for (uint32_t i=0; i < evout.count; i++)
  189. {
  190. if (evout.data[i].types & CARLA_EVENT_TYPE_MIDI)
  191. count += 1;
  192. }
  193. return count;
  194. }
  195. virtual uint32_t param_scalepoint_count(uint32_t param_id)
  196. {
  197. int32_t rindex = param.data[param_id].rindex;
  198. return rdf_descriptor->Ports[rindex].ScalePointCount;
  199. }
  200. virtual double get_parameter_value(uint32_t param_id)
  201. {
  202. switch (lv2param[param_id].type)
  203. {
  204. case LV2_PARAMETER_TYPE_CONTROL:
  205. return lv2param[param_id].control;
  206. default:
  207. return 0.0;
  208. }
  209. }
  210. virtual double get_parameter_scalepoint_value(uint32_t param_id, uint32_t scalepoint_id)
  211. {
  212. int32_t param_rindex = param.data[param_id].rindex;
  213. return rdf_descriptor->Ports[param_rindex].ScalePoints[scalepoint_id].Value;
  214. }
  215. virtual void get_label(char* buf_str)
  216. {
  217. strncpy(buf_str, rdf_descriptor->URI, STR_MAX);
  218. }
  219. virtual void get_maker(char* buf_str)
  220. {
  221. strncpy(buf_str, rdf_descriptor->Author, STR_MAX);
  222. }
  223. virtual void get_copyright(char* buf_str)
  224. {
  225. strncpy(buf_str, rdf_descriptor->License, STR_MAX);
  226. }
  227. virtual void get_real_name(char* buf_str)
  228. {
  229. strncpy(buf_str, rdf_descriptor->Name, STR_MAX);
  230. }
  231. virtual void get_parameter_name(uint32_t param_id, char* buf_str)
  232. {
  233. int32_t rindex = param.data[param_id].rindex;
  234. strncpy(buf_str, rdf_descriptor->Ports[rindex].Name, STR_MAX);
  235. }
  236. virtual void get_parameter_symbol(uint32_t param_id, char* buf_str)
  237. {
  238. int32_t rindex = param.data[param_id].rindex;
  239. strncpy(buf_str, rdf_descriptor->Ports[rindex].Symbol, STR_MAX);
  240. }
  241. virtual void get_parameter_label(uint32_t param_id, char* buf_str)
  242. {
  243. int32_t rindex = param.data[param_id].rindex;
  244. LV2_RDF_Port* Port = &rdf_descriptor->Ports[rindex];
  245. if (LV2_HAVE_UNIT_SYMBOL(Port->Unit.Hints))
  246. strncpy(buf_str, Port->Unit.Symbol, STR_MAX);
  247. else if (LV2_HAVE_UNIT(Port->Unit.Hints))
  248. {
  249. switch (Port->Unit.Type)
  250. {
  251. case LV2_UNIT_BAR:
  252. strncpy(buf_str, "bars", STR_MAX);
  253. return;
  254. case LV2_UNIT_BEAT:
  255. strncpy(buf_str, "beats", STR_MAX);
  256. return;
  257. case LV2_UNIT_BPM:
  258. strncpy(buf_str, "BPM", STR_MAX);
  259. return;
  260. case LV2_UNIT_CENT:
  261. strncpy(buf_str, "ct", STR_MAX);
  262. return;
  263. case LV2_UNIT_CM:
  264. strncpy(buf_str, "cm", STR_MAX);
  265. return;
  266. case LV2_UNIT_COEF:
  267. strncpy(buf_str, "(coef)", STR_MAX);
  268. return;
  269. case LV2_UNIT_DB:
  270. strncpy(buf_str, "dB", STR_MAX);
  271. return;
  272. case LV2_UNIT_DEGREE:
  273. strncpy(buf_str, "deg", STR_MAX);
  274. return;
  275. case LV2_UNIT_HZ:
  276. strncpy(buf_str, "Hz", STR_MAX);
  277. return;
  278. case LV2_UNIT_INCH:
  279. strncpy(buf_str, "in", STR_MAX);
  280. return;
  281. case LV2_UNIT_KHZ:
  282. strncpy(buf_str, "kHz", STR_MAX);
  283. return;
  284. case LV2_UNIT_KM:
  285. strncpy(buf_str, "km", STR_MAX);
  286. return;
  287. case LV2_UNIT_M:
  288. strncpy(buf_str, "m", STR_MAX);
  289. return;
  290. case LV2_UNIT_MHZ:
  291. strncpy(buf_str, "MHz", STR_MAX);
  292. return;
  293. case LV2_UNIT_MIDINOTE:
  294. strncpy(buf_str, "note", STR_MAX);
  295. return;
  296. case LV2_UNIT_MILE:
  297. strncpy(buf_str, "mi", STR_MAX);
  298. return;
  299. case LV2_UNIT_MIN:
  300. strncpy(buf_str, "min", STR_MAX);
  301. return;
  302. case LV2_UNIT_MM:
  303. strncpy(buf_str, "mm", STR_MAX);
  304. return;
  305. case LV2_UNIT_MS:
  306. strncpy(buf_str, "ms", STR_MAX);
  307. return;
  308. case LV2_UNIT_OCT:
  309. strncpy(buf_str, "oct", STR_MAX);
  310. return;
  311. case LV2_UNIT_PC:
  312. strncpy(buf_str, "%", STR_MAX);
  313. return;
  314. case LV2_UNIT_S:
  315. strncpy(buf_str, "s", STR_MAX);
  316. return;
  317. case LV2_UNIT_SEMITONE:
  318. strncpy(buf_str, "semi", STR_MAX);
  319. return;
  320. }
  321. }
  322. *buf_str = 0;
  323. }
  324. virtual void get_parameter_scalepoint_label(uint32_t param_id, uint32_t scalepoint_id, char* buf_str)
  325. {
  326. int32_t param_rindex = param.data[param_id].rindex;
  327. strncpy(buf_str, rdf_descriptor->Ports[param_rindex].ScalePoints[scalepoint_id].Label, STR_MAX);
  328. }
  329. virtual void get_gui_info(GuiInfo* info)
  330. {
  331. info->type = GUI_NONE;
  332. }
  333. virtual void set_parameter_value(uint32_t param_id, double value, bool gui_send, bool osc_send, bool callback_send)
  334. {
  335. switch (lv2param[param_id].type)
  336. {
  337. case LV2_PARAMETER_TYPE_CONTROL:
  338. lv2param[param_id].control = value;
  339. break;
  340. default:
  341. break;
  342. }
  343. // if (gui_send && gui.visible)
  344. // {
  345. // switch(gui.type)
  346. // {
  347. // case GUI_INTERNAL_QT4:
  348. // case GUI_INTERNAL_X11:
  349. // case GUI_EXTERNAL_LV2:
  350. // if (ui.descriptor->port_event)
  351. // {
  352. // float fvalue = value;
  353. // ui.descriptor->port_event(ui.handle, param.data[parameter_id].rindex, sizeof(float), 0, &fvalue);
  354. // }
  355. // break;
  356. // case GUI_EXTERNAL_OSC:
  357. // osc_send_control(&osc.data, param.data[parameter_id].rindex, value);
  358. // break;
  359. // default:
  360. // break;
  361. // }
  362. // }
  363. CarlaPlugin::set_parameter_value(param_id, value, gui_send, osc_send, callback_send);
  364. }
  365. virtual void set_custom_data(CustomDataType dtype, const char* key, const char* value, bool gui_send)
  366. {
  367. if ((m_hints & PLUGIN_HAS_EXTENSION_STATE) > 0 && descriptor->extension_data)
  368. {
  369. //LV2_State_Interface* state = (LV2_State_Interface*)descriptor->extension_data(LV2_STATE_INTERFACE_URI);
  370. //if (state)
  371. // state->restore(handle, carla_lv2_state_retrieve, this, 0, features);
  372. }
  373. CarlaPlugin::set_custom_data(dtype, key, value, gui_send);
  374. }
  375. virtual void set_gui_data(int, void* /*ptr*/)
  376. {
  377. // switch(gui.type)
  378. // {
  379. // case GUI_INTERNAL_QT4:
  380. // if (ui.widget)
  381. // {
  382. // QDialog* qtPtr = (QDialog*)ptr;
  383. // QWidget* widget = (QWidget*)ui.widget;
  384. // qtPtr->layout()->addWidget(widget);
  385. // widget->setParent(qtPtr);
  386. // widget->show();
  387. // }
  388. // break;
  389. // case GUI_INTERNAL_X11:
  390. // if (ui.descriptor)
  391. // {
  392. // QDialog* qtPtr = (QDialog*)ptr;
  393. // features[lv2_feature_id_ui_parent]->data = (void*)qtPtr->winId();
  394. // ui.handle = ui.descriptor->instantiate(ui.descriptor,
  395. // descriptor->URI,
  396. // ui.rdf_descriptor->Bundle,
  397. // carla_lv2_ui_write_function,
  398. // this,
  399. // &ui.widget,
  400. // features);
  401. // if (ui.handle && ui.descriptor->port_event)
  402. // update_ui_ports();
  403. // }
  404. // break;
  405. // default:
  406. // break;
  407. // }
  408. }
  409. virtual void show_gui(bool /*yesno*/)
  410. {
  411. // switch(gui.type)
  412. // {
  413. // case GUI_INTERNAL_QT4:
  414. // gui.visible = yesno;
  415. // break;
  416. // case GUI_INTERNAL_X11:
  417. // gui.visible = yesno;
  418. // if (gui.visible && gui.width > 0 && gui.height > 0)
  419. // callback_action(CALLBACK_RESIZE_GUI, id, gui.width, gui.height, 0.0);
  420. // break;
  421. // case GUI_EXTERNAL_OSC:
  422. // if (yesno)
  423. // {
  424. // gui.show_now = true;
  425. // // 40 re-tries, 4 secs
  426. // for (int j=1; j<40 && gui.show_now; j++)
  427. // {
  428. // if (osc.data.target)
  429. // {
  430. // osc_send_show(&osc.data);
  431. // gui.visible = true;
  432. // return;
  433. // }
  434. // else
  435. // // 100 ms
  436. // usleep(100000);
  437. // }
  438. // qDebug("Lv2AudioPlugin::show_gui() - GUI timeout");
  439. // callback_action(CALLBACK_SHOW_GUI, id, 0, 0, 0.0);
  440. // }
  441. // else
  442. // {
  443. // gui.visible = false;
  444. // osc_send_hide(&osc.data);
  445. // osc_send_quit(&osc.data);
  446. // osc_clear_data(&osc.data);
  447. // }
  448. // break;
  449. // case GUI_EXTERNAL_LV2:
  450. // if (!ui.handle)
  451. // reinit_external_ui();
  452. // if (ui.handle && ui.widget)
  453. // {
  454. // if (yesno)
  455. // {
  456. // LV2_EXTERNAL_UI_SHOW((lv2_external_ui*)ui.widget);
  457. // gui.visible = true;
  458. // }
  459. // else
  460. // {
  461. // LV2_EXTERNAL_UI_HIDE((lv2_external_ui*)ui.widget);
  462. // gui.visible = false;
  463. // if (ui.descriptor->cleanup)
  464. // ui.descriptor->cleanup(ui.handle);
  465. // ui.handle = nullptr;
  466. // }
  467. // }
  468. // else
  469. // {
  470. // // failed to init UI
  471. // gui.visible = false;
  472. // callback_action(CALLBACK_SHOW_GUI, id, -1, 0, 0.0);
  473. // }
  474. // break;
  475. // default:
  476. // break;
  477. // }
  478. }
  479. virtual void idle_gui()
  480. {
  481. // switch(gui.type)
  482. // {
  483. // case GUI_INTERNAL_QT4:
  484. // case GUI_INTERNAL_X11:
  485. // case GUI_EXTERNAL_LV2:
  486. // if (ui.descriptor->port_event)
  487. // {
  488. // for (uint32_t i=0; i < param.count; i++)
  489. // {
  490. // if (param.data[i].type == PARAMETER_OUTPUT && (param.data[i].hints & PARAMETER_IS_AUTOMABLE) > 0)
  491. // ui.descriptor->port_event(ui.handle, param.data[i].rindex, sizeof(float), 0, &param.buffers[i]);
  492. // }
  493. // }
  494. // if (gui.type == GUI_EXTERNAL_LV2)
  495. // LV2_EXTERNAL_UI_RUN((lv2_external_ui*)ui.widget);
  496. // break;
  497. // default:
  498. // break;
  499. // }
  500. }
  501. virtual void reload()
  502. {
  503. qDebug("Lv2Plugin::reload()");
  504. short _id = m_id;
  505. // Safely disable plugin for reload
  506. carla_proc_lock();
  507. m_id = -1;
  508. carla_proc_unlock();
  509. // Unregister previous jack ports if needed
  510. if (_id >= 0)
  511. remove_from_jack();
  512. // Delete old data
  513. delete_buffers();
  514. uint32_t ains, aouts, cv_ins, cv_outs, ev_ins, ev_outs, params, j;
  515. ains = aouts = cv_ins = cv_outs = ev_ins = ev_outs = params = 0;
  516. const uint32_t PortCount = rdf_descriptor->PortCount;
  517. unsigned int event_data_type = 0;
  518. for (uint32_t i=0; i<PortCount; i++)
  519. {
  520. const LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  521. if (LV2_IS_PORT_AUDIO(PortType))
  522. {
  523. if (LV2_IS_PORT_INPUT(PortType))
  524. ains += 1;
  525. else if (LV2_IS_PORT_OUTPUT(PortType))
  526. aouts += 1;
  527. }
  528. else if (LV2_IS_PORT_CV(PortType))
  529. {
  530. if (LV2_IS_PORT_INPUT(PortType))
  531. cv_ins += 1;
  532. else if (LV2_IS_PORT_OUTPUT(PortType))
  533. cv_outs += 1;
  534. }
  535. else if (LV2_IS_PORT_EVENT(PortType))
  536. {
  537. if (LV2_IS_PORT_INPUT(PortType))
  538. ev_ins += 1;
  539. else if (LV2_IS_PORT_OUTPUT(PortType))
  540. ev_outs += 1;
  541. event_data_type = CARLA_EVENT_DATA_EVENT;
  542. }
  543. else if (LV2_IS_PORT_MIDI_LL(PortType))
  544. {
  545. if (LV2_IS_PORT_INPUT(PortType))
  546. ev_ins += 1;
  547. else if (LV2_IS_PORT_OUTPUT(PortType))
  548. ev_outs += 1;
  549. event_data_type = CARLA_EVENT_DATA_MIDI_LL;
  550. }
  551. else if (LV2_IS_PORT_CONTROL(PortType))
  552. params += 1;
  553. }
  554. // if (params == 0 && (hints & PLUGIN_HAS_EXTENSION_DYNPARAM) > 0)
  555. // {
  556. // dynparam_plugin = (lv2dynparam_plugin_callbacks*)descriptor->extension_data(LV2DYNPARAM_URI);
  557. // if (dynparam_plugin)
  558. // dynparam_plugin->host_attach(handle, &dynparam_host, this);
  559. // }
  560. if (ains > 0)
  561. {
  562. ain.ports = new jack_port_t*[ains];
  563. ain_rindexes = new uint32_t[ains];
  564. }
  565. if (aouts > 0)
  566. {
  567. aout.ports = new jack_port_t*[aouts];
  568. aout_rindexes = new uint32_t[aouts];
  569. }
  570. if (ev_ins > 0)
  571. {
  572. evin.data = new EventData[ev_ins];
  573. for (j=0; j < ev_ins; j++)
  574. {
  575. evin.data[j].port = nullptr;
  576. if (event_data_type == CARLA_EVENT_DATA_ATOM)
  577. {
  578. evin.data[j].types = CARLA_EVENT_DATA_ATOM;
  579. // TODO
  580. }
  581. else if (event_data_type == CARLA_EVENT_DATA_EVENT)
  582. {
  583. evin.data[j].types = CARLA_EVENT_DATA_EVENT;
  584. evin.data[j].buffer.e = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  585. }
  586. else if (event_data_type == CARLA_EVENT_DATA_MIDI_LL)
  587. {
  588. evin.data[j].types = CARLA_EVENT_DATA_MIDI_LL;
  589. evin.data[j].buffer.m = new LV2_MIDI;
  590. evin.data[j].buffer.m->capacity = MAX_EVENT_BUFFER;
  591. evin.data[j].buffer.m->data = new unsigned char [MAX_EVENT_BUFFER];
  592. }
  593. else
  594. evin.data[j].types = 0;
  595. }
  596. }
  597. if (ev_outs > 0)
  598. {
  599. evout.data = new EventData[ev_outs];
  600. for (j=0; j < ev_outs; j++)
  601. {
  602. evout.data[j].port = nullptr;
  603. if (event_data_type == CARLA_EVENT_DATA_ATOM)
  604. {
  605. evout.data[j].types = CARLA_EVENT_DATA_ATOM;
  606. // TODO
  607. }
  608. else if (event_data_type == CARLA_EVENT_DATA_EVENT)
  609. {
  610. evout.data[j].types = CARLA_EVENT_DATA_EVENT;
  611. evout.data[j].buffer.e = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  612. }
  613. else if (event_data_type == CARLA_EVENT_DATA_MIDI_LL)
  614. {
  615. evout.data[j].types = CARLA_EVENT_DATA_MIDI_LL;
  616. evout.data[j].buffer.m = new LV2_MIDI;
  617. evout.data[j].buffer.m->capacity = MAX_EVENT_BUFFER;
  618. evout.data[j].buffer.m->data = new unsigned char [MAX_EVENT_BUFFER];
  619. }
  620. else
  621. evout.data[j].types = 0;
  622. }
  623. }
  624. if (params > 0)
  625. {
  626. param.data = new ParameterData[params];
  627. param.ranges = new ParameterRanges[params];
  628. lv2param = new Lv2ParameterData[params];
  629. }
  630. const int port_name_size = jack_port_name_size();
  631. char port_name[port_name_size];
  632. bool needs_cin = false;
  633. bool needs_cout = false;
  634. for (uint32_t i=0; i < PortCount; i++)
  635. {
  636. const LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  637. if (LV2_IS_PORT_AUDIO(PortType) || LV2_IS_PORT_CV(PortType) || LV2_IS_PORT_EVENT(PortType) || LV2_IS_PORT_MIDI_LL(PortType))
  638. {
  639. #ifndef BUILD_BRIDGE
  640. if (carla_options.global_jack_client)
  641. {
  642. strcpy(port_name, m_name);
  643. strcat(port_name, ":");
  644. strncat(port_name, rdf_descriptor->Ports[i].Name, port_name_size/2);
  645. }
  646. else
  647. #endif
  648. strncpy(port_name, rdf_descriptor->Ports[i].Name, port_name_size/2);
  649. }
  650. if (LV2_IS_PORT_AUDIO(PortType))
  651. {
  652. if (LV2_IS_PORT_INPUT(PortType))
  653. {
  654. j = ain.count++;
  655. ain.ports[j] = jack_port_register(jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  656. ain_rindexes[j] = i;
  657. }
  658. else if (LV2_IS_PORT_OUTPUT(PortType))
  659. {
  660. j = aout.count++;
  661. aout.ports[j] = jack_port_register(jack_client, port_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  662. aout_rindexes[j] = i;
  663. needs_cin = true;
  664. }
  665. else
  666. qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
  667. }
  668. else if (LV2_IS_PORT_CV(PortType))
  669. {
  670. if (LV2_IS_PORT_INPUT(PortType))
  671. {
  672. qWarning("WARNING - CV Ports are not supported yet");
  673. }
  674. else if (LV2_IS_PORT_OUTPUT(PortType))
  675. {
  676. qWarning("WARNING - CV Ports are not supported yet");
  677. }
  678. else
  679. qWarning("WARNING - Got a broken Port (CV, but not input or output)");
  680. descriptor->connect_port(handle, i, nullptr);
  681. }
  682. else if (LV2_IS_PORT_EVENT(PortType))
  683. {
  684. if (LV2_IS_PORT_INPUT(PortType))
  685. {
  686. j = evin.count++;
  687. descriptor->connect_port(handle, i, evin.data[j].buffer.e);
  688. if (LV2_IS_PORT_EVENT_MIDI(PortType))
  689. {
  690. evin.data[j].types |= CARLA_EVENT_TYPE_MIDI;
  691. evin.data[j].port = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  692. }
  693. if (LV2_IS_PORT_EVENT_TIME(PortType))
  694. {
  695. evin.data[j].types |= CARLA_EVENT_TYPE_TIME;
  696. }
  697. }
  698. else if (LV2_IS_PORT_OUTPUT(PortType))
  699. {
  700. j = evout.count++;
  701. descriptor->connect_port(handle, i, evout.data[j].buffer.e);
  702. if (LV2_IS_PORT_EVENT_MIDI(PortType))
  703. {
  704. evout.data[j].types |= CARLA_EVENT_TYPE_MIDI;
  705. evout.data[j].port = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  706. }
  707. if (LV2_IS_PORT_EVENT_TIME(PortType))
  708. {
  709. evout.data[j].types |= CARLA_EVENT_TYPE_TIME;
  710. }
  711. }
  712. else
  713. qWarning("WARNING - Got a broken Port (Event, but not input or output)");
  714. }
  715. else if (LV2_IS_PORT_MIDI_LL(PortType))
  716. {
  717. if (LV2_IS_PORT_INPUT(PortType))
  718. {
  719. j = evin.count++;
  720. descriptor->connect_port(handle, i, evin.data[j].buffer.m);
  721. evin.data[j].types |= CARLA_EVENT_TYPE_MIDI;
  722. evin.data[j].port = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  723. }
  724. else if (LV2_IS_PORT_OUTPUT(PortType))
  725. {
  726. j = evout.count++;
  727. descriptor->connect_port(handle, i, evout.data[j].buffer.m);
  728. evout.data[j].types |= CARLA_EVENT_TYPE_MIDI;
  729. evout.data[j].port = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  730. }
  731. else
  732. qWarning("WARNING - Got a broken Port (Midi, but not input or output)");
  733. }
  734. else if (LV2_IS_PORT_CONTROL(PortType))
  735. {
  736. const LV2_Property PortProps = rdf_descriptor->Ports[i].Properties;
  737. const LV2_RDF_PortPoints PortPoints = rdf_descriptor->Ports[i].Points;
  738. j = param.count++;
  739. param.data[j].index = j;
  740. param.data[j].rindex = i;
  741. param.data[j].hints = 0;
  742. param.data[j].midi_channel = 0;
  743. param.data[j].midi_cc = -1;
  744. double min, max, def, step, step_small, step_large;
  745. // min value
  746. if (LV2_HAVE_MINIMUM_PORT_POINT(PortPoints.Hints))
  747. min = PortPoints.Minimum;
  748. else
  749. min = 0.0;
  750. // max value
  751. if (LV2_HAVE_MAXIMUM_PORT_POINT(PortPoints.Hints))
  752. max = PortPoints.Maximum;
  753. else
  754. max = 1.0;
  755. if (min > max)
  756. max = min;
  757. else if (max < min)
  758. min = max;
  759. // default value
  760. if (LV2_HAVE_DEFAULT_PORT_POINT(PortPoints.Hints))
  761. def = PortPoints.Default;
  762. else
  763. {
  764. // no default value
  765. if (min < 0.0 && max > 0.0)
  766. def = 0.0;
  767. else
  768. def = min;
  769. }
  770. if (def < min)
  771. def = min;
  772. else if (def > max)
  773. def = max;
  774. if (max - min <= 0.0)
  775. {
  776. qWarning("Broken plugin parameter -> max - min <= 0");
  777. max = min + 0.1;
  778. }
  779. if (LV2_IS_PORT_SAMPLE_RATE(PortProps))
  780. {
  781. double sample_rate = get_sample_rate();
  782. min *= sample_rate;
  783. max *= sample_rate;
  784. def *= sample_rate;
  785. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  786. }
  787. if (LV2_IS_PORT_INTEGER(PortProps))
  788. {
  789. step = 1.0;
  790. step_small = 1.0;
  791. step_large = 10.0;
  792. }
  793. else if (LV2_IS_PORT_TOGGLED(PortProps))
  794. {
  795. step = max - min;
  796. step_small = step;
  797. step_large = step;
  798. }
  799. else
  800. {
  801. double range = max - min;
  802. step = range/100.0;
  803. step_small = range/1000.0;
  804. step_large = range/10.0;
  805. }
  806. if (LV2_IS_PORT_INPUT(PortType))
  807. {
  808. param.data[j].type = PARAMETER_INPUT;
  809. param.data[j].hints |= PARAMETER_IS_ENABLED;
  810. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  811. needs_cin = true;
  812. // MIDI CC value
  813. LV2_RDF_PortMidiMap* PortMidiMap = &rdf_descriptor->Ports[i].MidiMap;
  814. if (LV2_IS_PORT_MIDI_MAP_CC(PortMidiMap->Type))
  815. {
  816. if (! MIDI_IS_CONTROL_BANK_SELECT(PortMidiMap->Number))
  817. param.data[j].midi_cc = PortMidiMap->Number;
  818. }
  819. }
  820. else if (LV2_IS_PORT_OUTPUT(PortType))
  821. {
  822. param.data[j].type = PARAMETER_OUTPUT;
  823. param.data[j].hints |= PARAMETER_IS_ENABLED;
  824. if (LV2_IS_PORT_LATENCY(PortProps) == false)
  825. {
  826. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  827. needs_cout = true;
  828. }
  829. else
  830. {
  831. // latency parameter
  832. min = 0;
  833. max = get_sample_rate();
  834. def = 0;
  835. step = 1;
  836. step_small = 1;
  837. step_large = 1;
  838. }
  839. }
  840. else
  841. {
  842. param.data[j].type = PARAMETER_UNKNOWN;
  843. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  844. }
  845. // extra parameter hints
  846. if (LV2_IS_PORT_ENUMERATION(PortProps))
  847. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  848. if (LV2_IS_PORT_HAS_STRICT_BOUNDS(PortProps) /*|| (force_strict_bounds && LV2_IS_PORT_OUTPUT(PortType))*/)
  849. param.data[j].hints |= PARAMETER_HAS_STRICT_BOUNDS;
  850. // check if parameter is not enabled or automable
  851. if (LV2_IS_PORT_NOT_AUTOMATIC(PortProps) || LV2_IS_PORT_NOT_ON_GUI(PortProps))
  852. param.data[j].hints &= ~PARAMETER_IS_ENABLED;
  853. if (LV2_IS_PORT_CAUSES_ARTIFACTS(PortProps) || LV2_IS_PORT_EXPENSIVE(PortProps))
  854. param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  855. param.ranges[j].min = min;
  856. param.ranges[j].max = max;
  857. param.ranges[j].def = def;
  858. param.ranges[j].step = step;
  859. param.ranges[j].step_small = step_small;
  860. param.ranges[j].step_large = step_large;
  861. // Set LV2 params as needed
  862. lv2param[j].type = LV2_PARAMETER_TYPE_CONTROL;
  863. lv2param[j].control = def;
  864. descriptor->connect_port(handle, i, &lv2param[j].control);
  865. }
  866. else
  867. // Port Type not supported, but it's optional anyway
  868. descriptor->connect_port(handle, i, nullptr);
  869. }
  870. if (needs_cin)
  871. {
  872. #ifndef BUILD_BRIDGE
  873. if (carla_options.global_jack_client)
  874. {
  875. strcpy(port_name, m_name);
  876. strcat(port_name, ":control-in");
  877. }
  878. else
  879. #endif
  880. strcpy(port_name, "control-in");
  881. param.port_cin = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  882. }
  883. if (needs_cout)
  884. {
  885. #ifndef BUILD_BRIDGE
  886. if (carla_options.global_jack_client)
  887. {
  888. strcpy(port_name, m_name);
  889. strcat(port_name, ":control-out");
  890. }
  891. else
  892. #endif
  893. strcpy(port_name, "control-out");
  894. param.port_cout = jack_port_register(jack_client, port_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  895. }
  896. ain.count = ains;
  897. aout.count = aouts;
  898. evin.count = ev_ins;
  899. evout.count = ev_outs;
  900. param.count = params;
  901. reload_programs(true);
  902. // plugin checks
  903. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE);
  904. if (LV2_IS_GENERATOR(rdf_descriptor->Type))
  905. m_hints |= PLUGIN_IS_SYNTH;
  906. if (aouts > 0 && (ains == aouts || ains == 1))
  907. m_hints |= PLUGIN_CAN_DRYWET;
  908. if (aouts > 0)
  909. m_hints |= PLUGIN_CAN_VOLUME;
  910. if (aouts >= 2 && aouts%2 == 0)
  911. m_hints |= PLUGIN_CAN_BALANCE;
  912. carla_proc_lock();
  913. m_id = _id;
  914. carla_proc_unlock();
  915. if (carla_options.global_jack_client == false)
  916. jack_activate(jack_client);
  917. }
  918. virtual void process(jack_nframes_t nframes)
  919. {
  920. uint32_t i, k;
  921. unsigned short plugin_id = m_id;
  922. uint32_t midi_event_count = 0;
  923. double ains_peak_tmp[2] = { 0.0 };
  924. double aouts_peak_tmp[2] = { 0.0 };
  925. jack_default_audio_sample_t* ains_buffer[ain.count];
  926. jack_default_audio_sample_t* aouts_buffer[aout.count];
  927. void* evins_buffer[evin.count];
  928. void* evouts_buffer[evout.count];
  929. // different midi APIs
  930. LV2_MIDIState evin_states[evin.count];
  931. LV2_Event_Iterator evin_iters[evin.count];
  932. for (i=0; i < ain.count; i++)
  933. ains_buffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(ain.ports[i], nframes);
  934. for (i=0; i < aout.count; i++)
  935. aouts_buffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(aout.ports[i], nframes);
  936. for (i=0; i < evin.count; i++)
  937. {
  938. if (evin.data[i].types & CARLA_EVENT_DATA_ATOM)
  939. {
  940. }
  941. else if (evin.data[i].types & CARLA_EVENT_DATA_EVENT)
  942. {
  943. lv2_event_buffer_reset(evin.data[i].buffer.e, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(evin.data[i].buffer.e + 1));
  944. lv2_event_begin(&evin_iters[i], evin.data[i].buffer.e);
  945. }
  946. else if (evin.data[i].types & CARLA_EVENT_DATA_MIDI_LL)
  947. {
  948. evin_states[i].midi = evin.data[i].buffer.m;
  949. evin_states[i].frame_count = nframes;
  950. evin_states[i].position = 0;
  951. evin_states[i].midi->event_count = 0;
  952. evin_states[i].midi->size = 0;
  953. }
  954. if (evin.data[i].port)
  955. evins_buffer[i] = jack_port_get_buffer(evin.data[i].port, nframes);
  956. else
  957. evins_buffer[i] = nullptr;
  958. }
  959. for (i=0; i < evout.count; i++)
  960. {
  961. if (evin.data[i].types & CARLA_EVENT_DATA_ATOM)
  962. {
  963. }
  964. else if (evin.data[i].types & CARLA_EVENT_DATA_EVENT)
  965. {
  966. lv2_event_buffer_reset(evout.data[i].buffer.e, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(evout.data[i].buffer.e + 1));
  967. }
  968. else if (evin.data[i].types & CARLA_EVENT_DATA_MIDI_LL)
  969. {
  970. // not needed
  971. }
  972. if (evout.data[i].port)
  973. evouts_buffer[i] = jack_port_get_buffer(evout.data[i].port, nframes);
  974. else
  975. evouts_buffer[i] = nullptr;
  976. }
  977. // --------------------------------------------------------------------------------------------------------
  978. // Input VU
  979. if (ain.count > 0)
  980. {
  981. short j2 = (ain.count == 1) ? 0 : 1;
  982. for (k=0; k<nframes; k++)
  983. {
  984. if (abs_d(ains_buffer[0][k]) > ains_peak_tmp[0])
  985. ains_peak_tmp[0] = abs_d(ains_buffer[0][k]);
  986. if (abs_d(ains_buffer[j2][k]) > ains_peak_tmp[1])
  987. ains_peak_tmp[1] = abs_d(ains_buffer[j2][k]);
  988. }
  989. }
  990. CARLA_PROCESS_CONTINUE_CHECK;
  991. // --------------------------------------------------------------------------------------------------------
  992. // Parameters Input [Automation]
  993. if (param.port_cin)
  994. {
  995. void* pin_buffer = jack_port_get_buffer(param.port_cin, nframes);
  996. jack_midi_event_t pin_event;
  997. uint32_t n_pin_events = jack_midi_get_event_count(pin_buffer);
  998. unsigned char next_bank_id = 0;
  999. if (midiprog.current > 0 && midiprog.count > 0)
  1000. next_bank_id = midiprog.data[midiprog.current].bank;
  1001. for (i=0; i < n_pin_events; i++)
  1002. {
  1003. if (jack_midi_event_get(&pin_event, pin_buffer, i) != 0)
  1004. break;
  1005. jack_midi_data_t status = pin_event.buffer[0];
  1006. unsigned char channel = status & 0x0F;
  1007. // Control change
  1008. if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
  1009. {
  1010. jack_midi_data_t control = pin_event.buffer[1];
  1011. jack_midi_data_t c_value = pin_event.buffer[2];
  1012. // Bank Select
  1013. if (MIDI_IS_CONTROL_BANK_SELECT(control))
  1014. {
  1015. next_bank_id = c_value;
  1016. continue;
  1017. }
  1018. double value;
  1019. // Control GUI stuff (channel 0 only)
  1020. if (channel == 0)
  1021. {
  1022. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(control) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  1023. {
  1024. value = double(c_value)/127;
  1025. set_drywet(value, false, false);
  1026. postpone_event(PostEventParameterChange, PARAMETER_DRYWET, value);
  1027. continue;
  1028. }
  1029. else if (MIDI_IS_CONTROL_CHANNEL_VOLUME(control) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  1030. {
  1031. value = double(c_value)/100;
  1032. set_volume(value, false, false);
  1033. postpone_event(PostEventParameterChange, PARAMETER_VOLUME, value);
  1034. continue;
  1035. }
  1036. else if (MIDI_IS_CONTROL_BALANCE(control) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  1037. {
  1038. double left, right;
  1039. value = (double(c_value)-63.5)/63.5;
  1040. if (value < 0)
  1041. {
  1042. left = -1.0;
  1043. right = (value*2)+1.0;
  1044. }
  1045. else if (value > 0)
  1046. {
  1047. left = (value*2)-1.0;
  1048. right = 1.0;
  1049. }
  1050. else
  1051. {
  1052. left = -1.0;
  1053. right = 1.0;
  1054. }
  1055. set_balance_left(left, false, false);
  1056. set_balance_right(right, false, false);
  1057. postpone_event(PostEventParameterChange, PARAMETER_BALANCE_LEFT, left);
  1058. postpone_event(PostEventParameterChange, PARAMETER_BALANCE_RIGHT, right);
  1059. continue;
  1060. }
  1061. else if (control == MIDI_CONTROL_ALL_SOUND_OFF)
  1062. {
  1063. if (midi.port_min)
  1064. send_midi_all_notes_off();
  1065. if (m_active && m_active_before)
  1066. {
  1067. if (descriptor->deactivate)
  1068. descriptor->deactivate(handle);
  1069. m_active_before = false;
  1070. }
  1071. continue;
  1072. }
  1073. else if (control == MIDI_CONTROL_ALL_NOTES_OFF)
  1074. {
  1075. if (midi.port_min)
  1076. send_midi_all_notes_off();
  1077. continue;
  1078. }
  1079. }
  1080. // Control plugin parameters
  1081. for (k=0; k < param.count; k++)
  1082. {
  1083. 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)
  1084. {
  1085. value = (double(c_value) / 127 * (param.ranges[k].max - param.ranges[k].min)) + param.ranges[k].min;
  1086. set_parameter_value(k, value, false, false, false);
  1087. postpone_event(PostEventParameterChange, k, value);
  1088. }
  1089. }
  1090. }
  1091. // Program change
  1092. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(status))
  1093. {
  1094. uint32_t mbank_id = next_bank_id;
  1095. uint32_t mprog_id = pin_event.buffer[1]; // & 0x7F;
  1096. // TODO ...
  1097. for (k=0; k < midiprog.count; k++)
  1098. {
  1099. if (midiprog.data[k].bank == mbank_id && midiprog.data[k].program == mprog_id)
  1100. {
  1101. set_midi_program(k, false, false, false, false);
  1102. postpone_event(PostEventMidiProgramChange, k, 0.0);
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. }
  1108. } // End of Parameters Input
  1109. CARLA_PROCESS_CONTINUE_CHECK;
  1110. // --------------------------------------------------------------------------------------------------------
  1111. // MIDI Input (External)
  1112. if (evin.count > 0)
  1113. {
  1114. carla_midi_lock();
  1115. for (i=0; i < MAX_MIDI_EVENTS && midi_event_count < MAX_MIDI_EVENTS; i++)
  1116. {
  1117. if (ext_midi_notes[i].valid)
  1118. {
  1119. uint8_t midi_event[4] = { 0 };
  1120. midi_event[0] = ext_midi_notes[i].onoff ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1121. midi_event[1] = ext_midi_notes[i].note;
  1122. midi_event[2] = ext_midi_notes[i].velo;
  1123. // send to all midi inputs
  1124. for (k=0; k < evin.count; k++)
  1125. {
  1126. if (evin.data[k].types & CARLA_EVENT_TYPE_MIDI)
  1127. {
  1128. if (evin.data[k].types & CARLA_EVENT_DATA_ATOM)
  1129. continue; // TODO
  1130. else if (evin.data[k].types & CARLA_EVENT_DATA_EVENT)
  1131. lv2_event_write(&evin_iters[k], 0, 0, CARLA_URI_MAP_ID_EVENT_MIDI, 3, midi_event);
  1132. else if (evin.data[k].types & CARLA_EVENT_DATA_MIDI_LL)
  1133. lv2midi_put_event(&evin_states[k], 0, 3, midi_event);
  1134. }
  1135. }
  1136. ext_midi_notes[i].valid = false;
  1137. midi_event_count += 1;
  1138. }
  1139. else
  1140. break;
  1141. }
  1142. carla_midi_unlock();
  1143. } // End of MIDI Input (External)
  1144. CARLA_PROCESS_CONTINUE_CHECK;
  1145. // --------------------------------------------------------------------------------------------------------
  1146. // MIDI Input (JACK)
  1147. for (i=0; i < evin.count; i++)
  1148. {
  1149. if (evins_buffer[i] == nullptr)
  1150. continue;
  1151. jack_midi_event_t min_event;
  1152. uint32_t n_min_events = jack_midi_get_event_count(evins_buffer[i]);
  1153. for (k=0; k < n_min_events && midi_event_count < MAX_MIDI_EVENTS; k++)
  1154. {
  1155. if (jack_midi_event_get(&min_event, evins_buffer[i], k) != 0)
  1156. break;
  1157. jack_midi_data_t status = min_event.buffer[0];
  1158. // Fix bad note-off
  1159. if (MIDI_IS_STATUS_NOTE_ON(status) && min_event.buffer[2] == 0)
  1160. {
  1161. min_event.buffer[0] -= 0x10;
  1162. status = min_event.buffer[0];
  1163. }
  1164. // write supported status types
  1165. if (MIDI_IS_STATUS_NOTE_OFF(status) || MIDI_IS_STATUS_NOTE_ON(status) || MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) || MIDI_IS_STATUS_AFTERTOUCH(status) || MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  1166. {
  1167. if (evin.data[i].types & CARLA_EVENT_DATA_ATOM)
  1168. continue; // TODO
  1169. else if (evin.data[i].types & CARLA_EVENT_DATA_EVENT)
  1170. lv2_event_write(&evin_iters[i], min_event.time, 0, CARLA_URI_MAP_ID_EVENT_MIDI, min_event.size, min_event.buffer);
  1171. else if (evin.data[i].types & CARLA_EVENT_DATA_MIDI_LL)
  1172. lv2midi_put_event(&evin_states[i], min_event.time, min_event.size, min_event.buffer);
  1173. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1174. postpone_event(PostEventNoteOff, min_event.buffer[1], 0.0);
  1175. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1176. postpone_event(PostEventNoteOn, min_event.buffer[1], min_event.buffer[2]);
  1177. }
  1178. midi_event_count += 1;
  1179. }
  1180. } // End of MIDI Input (JACK)
  1181. CARLA_PROCESS_CONTINUE_CHECK;
  1182. // --------------------------------------------------------------------------------------------------------
  1183. // Plugin processing
  1184. if (m_active)
  1185. {
  1186. if (m_active_before == false)
  1187. {
  1188. if (descriptor->activate)
  1189. descriptor->activate(handle);
  1190. }
  1191. for (i=0; i < ain.count; i++)
  1192. descriptor->connect_port(handle, ain_rindexes[i], ains_buffer[i]);
  1193. for (i=0; i < aout.count; i++)
  1194. descriptor->connect_port(handle, aout_rindexes[i], aouts_buffer[i]);
  1195. if (descriptor->run)
  1196. descriptor->run(handle, nframes);
  1197. }
  1198. else
  1199. {
  1200. if (m_active_before)
  1201. {
  1202. if (descriptor->deactivate)
  1203. descriptor->deactivate(handle);
  1204. }
  1205. }
  1206. CARLA_PROCESS_CONTINUE_CHECK;
  1207. // --------------------------------------------------------------------------------------------------------
  1208. // Post-processing (dry/wet, volume and balance)
  1209. if (m_active)
  1210. {
  1211. double bal_rangeL, bal_rangeR;
  1212. jack_default_audio_sample_t old_bal_left[nframes];
  1213. for (i=0; i < aout.count; i++)
  1214. {
  1215. // Dry/Wet and Volume
  1216. for (k=0; k<nframes; k++)
  1217. {
  1218. if ((m_hints & PLUGIN_CAN_DRYWET) > 0 && x_drywet != 1.0)
  1219. {
  1220. if (aout.count == 1)
  1221. aouts_buffer[i][k] = (aouts_buffer[i][k]*x_drywet)+(ains_buffer[0][k]*(1.0-x_drywet));
  1222. else
  1223. aouts_buffer[i][k] = (aouts_buffer[i][k]*x_drywet)+(ains_buffer[i][k]*(1.0-x_drywet));
  1224. }
  1225. if (m_hints & PLUGIN_CAN_VOLUME)
  1226. aouts_buffer[i][k] *= x_vol;
  1227. }
  1228. // Balance
  1229. if (m_hints & PLUGIN_CAN_BALANCE)
  1230. {
  1231. if (i%2 == 0)
  1232. memcpy(&old_bal_left, aouts_buffer[i], sizeof(jack_default_audio_sample_t)*nframes);
  1233. bal_rangeL = (x_bal_left+1.0)/2;
  1234. bal_rangeR = (x_bal_right+1.0)/2;
  1235. for (k=0; k<nframes; k++)
  1236. {
  1237. if (i%2 == 0)
  1238. {
  1239. // left output
  1240. aouts_buffer[i][k] = old_bal_left[k]*(1.0-bal_rangeL);
  1241. aouts_buffer[i][k] += aouts_buffer[i+1][k]*(1.0-bal_rangeR);
  1242. }
  1243. else
  1244. {
  1245. // right
  1246. aouts_buffer[i][k] = aouts_buffer[i][k]*bal_rangeR;
  1247. aouts_buffer[i][k] += old_bal_left[k]*bal_rangeL;
  1248. }
  1249. }
  1250. }
  1251. // Output VU
  1252. if (i < 2)
  1253. {
  1254. for (k=0; k<nframes; k++)
  1255. {
  1256. if (abs_d(aouts_buffer[i][k]) > aouts_peak_tmp[i])
  1257. aouts_peak_tmp[i] = abs_d(aouts_buffer[i][k]);
  1258. }
  1259. }
  1260. }
  1261. }
  1262. else
  1263. {
  1264. // disable any output sound if not active
  1265. for (i=0; i < aout.count; i++)
  1266. memset(aouts_buffer[i], 0.0f, sizeof(jack_default_audio_sample_t)*nframes);
  1267. aouts_peak_tmp[0] = 0.0;
  1268. aouts_peak_tmp[1] = 0.0;
  1269. } // End of Post-processing
  1270. CARLA_PROCESS_CONTINUE_CHECK;
  1271. // --------------------------------------------------------------------------------------------------------
  1272. // Control Output
  1273. if (param.port_cout)
  1274. {
  1275. void* cout_buffer = jack_port_get_buffer(param.port_cout, nframes);
  1276. jack_midi_clear_buffer(cout_buffer);
  1277. double value, rvalue;
  1278. for (k=0; k < param.count; k++)
  1279. {
  1280. if (param.data[k].type == PARAMETER_OUTPUT && param.data[k].midi_cc > 0)
  1281. {
  1282. switch (lv2param[k].type)
  1283. {
  1284. case LV2_PARAMETER_TYPE_CONTROL:
  1285. value = lv2param[k].control;
  1286. break;
  1287. default:
  1288. value = param.ranges[k].min;
  1289. break;
  1290. }
  1291. rvalue = (value - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min) * 127;
  1292. jack_midi_data_t* event_buffer = jack_midi_event_reserve(cout_buffer, 0, 3);
  1293. event_buffer[0] = 0xB0 + param.data[k].midi_channel;
  1294. event_buffer[1] = param.data[k].midi_cc;
  1295. event_buffer[2] = rvalue;
  1296. }
  1297. }
  1298. } // End of Control Output
  1299. CARLA_PROCESS_CONTINUE_CHECK;
  1300. // --------------------------------------------------------------------------------------------------------
  1301. // MIDI Output
  1302. for (i=0; i < evout.count; i++)
  1303. {
  1304. if (evouts_buffer[i] == nullptr)
  1305. continue;
  1306. jack_midi_clear_buffer(evouts_buffer[i]);
  1307. if (evin.data[i].types & CARLA_EVENT_DATA_ATOM)
  1308. {
  1309. }
  1310. else if (evin.data[i].types & CARLA_EVENT_DATA_EVENT)
  1311. {
  1312. LV2_Event* ev;
  1313. LV2_Event_Iterator iter;
  1314. uint8_t* data;
  1315. lv2_event_begin(&iter, evout.data[i].buffer.e);
  1316. for (k=0; k < iter.buf->event_count; k++)
  1317. {
  1318. ev = lv2_event_get(&iter, &data);
  1319. if (ev && data)
  1320. jack_midi_event_write(evouts_buffer[i], ev->frames, data, ev->size);
  1321. lv2_event_increment(&iter);
  1322. }
  1323. }
  1324. else if (evin.data[i].types & CARLA_EVENT_DATA_MIDI_LL)
  1325. {
  1326. LV2_MIDIState state = { evout.data[i].buffer.m, nframes, 0 };
  1327. uint32_t event_size;
  1328. double event_timestamp;
  1329. unsigned char* event_data;
  1330. while (lv2midi_get_event(&state, &event_timestamp, &event_size, &event_data) < nframes)
  1331. {
  1332. jack_midi_event_write(evouts_buffer[i], event_timestamp, event_data, event_size);
  1333. lv2midi_step(&state);
  1334. }
  1335. }
  1336. } // End of MIDI Output
  1337. CARLA_PROCESS_CONTINUE_CHECK;
  1338. // --------------------------------------------------------------------------------------------------------
  1339. // Peak Values
  1340. ains_peak[(plugin_id*2)+0] = ains_peak_tmp[0];
  1341. ains_peak[(plugin_id*2)+1] = ains_peak_tmp[1];
  1342. aouts_peak[(plugin_id*2)+0] = aouts_peak_tmp[0];
  1343. aouts_peak[(plugin_id*2)+1] = aouts_peak_tmp[1];
  1344. m_active_before = m_active;
  1345. }
  1346. virtual void delete_buffers()
  1347. {
  1348. qDebug("Lv2Plugin::delete_buffers() - start");
  1349. if (ain.count > 0)
  1350. delete[] ain_rindexes;
  1351. if (aout.count > 0)
  1352. delete[] aout_rindexes;
  1353. if (param.count > 0)
  1354. delete[] lv2param;
  1355. if (evin.count > 0)
  1356. {
  1357. for (uint32_t i=0; i < evin.count; i++)
  1358. {
  1359. if (evin.data[i].types & CARLA_EVENT_DATA_ATOM)
  1360. {
  1361. }
  1362. else if (evin.data[i].types & CARLA_EVENT_DATA_EVENT)
  1363. {
  1364. free(evin.data[i].buffer.e);
  1365. }
  1366. else if (evin.data[i].types & CARLA_EVENT_DATA_MIDI_LL)
  1367. {
  1368. delete[] evin.data[i].buffer.m->data;
  1369. delete evin.data[i].buffer.m;
  1370. }
  1371. }
  1372. delete[] evin.data;
  1373. }
  1374. if (evout.count > 0)
  1375. {
  1376. for (uint32_t i=0; i < evout.count; i++)
  1377. {
  1378. if (evout.data[i].types & CARLA_EVENT_DATA_ATOM)
  1379. {
  1380. }
  1381. else if (evout.data[i].types & CARLA_EVENT_DATA_EVENT)
  1382. {
  1383. free(evout.data[i].buffer.e);
  1384. }
  1385. else if (evout.data[i].types & CARLA_EVENT_DATA_MIDI_LL)
  1386. {
  1387. delete[] evout.data[i].buffer.m->data;
  1388. delete evout.data[i].buffer.m;
  1389. }
  1390. }
  1391. delete[] evout.data;
  1392. }
  1393. lv2param = nullptr;
  1394. evin.count = 0;
  1395. evin.data = nullptr;
  1396. evout.count = 0;
  1397. evout.data = nullptr;
  1398. qDebug("Lv2Plugin::delete_buffers() - end");
  1399. }
  1400. uint32_t get_custom_uri_id(const char* uri)
  1401. {
  1402. qDebug("Lv2Plugin::get_custom_uri_id(%s)", uri);
  1403. for (int i=0; i < custom_uri_ids.count(); i++)
  1404. {
  1405. if (custom_uri_ids[i] && strcmp(custom_uri_ids[i], uri) == 0)
  1406. return i;
  1407. }
  1408. custom_uri_ids.append(strdup(uri));
  1409. return custom_uri_ids.count()-1;
  1410. }
  1411. const char* get_custom_uri_string(int uri_id)
  1412. {
  1413. qDebug("Lv2Plugin::get_custom_uri_string(%i)", uri_id);
  1414. if (uri_id < custom_uri_ids.count())
  1415. return custom_uri_ids.at(uri_id);
  1416. else
  1417. return nullptr;
  1418. }
  1419. #if 0
  1420. // FIXME - resolve jack deactivate
  1421. void lv2_remove_from_jack()
  1422. {
  1423. qDebug("Lv2Plugin::lv2_remove_from_jack() - start");
  1424. for (uint32_t i=0; i < evin.count; i++)
  1425. {
  1426. if (evin.data[i].port)
  1427. jack_port_unregister(jack_client, evin.data[i].port);
  1428. }
  1429. for (uint32_t i=0; i < evout.count; i++)
  1430. {
  1431. if (evout.data[i].port)
  1432. jack_port_unregister(jack_client, evout.data[i].port);
  1433. }
  1434. qDebug("Lv2Plugin::lv2_remove_from_jack() - end");
  1435. }
  1436. #endif
  1437. bool init(const char* filename, const char* URI, void* extra_stuff)
  1438. {
  1439. LV2_RDF_Descriptor* rdf_descriptor_ = (LV2_RDF_Descriptor*)extra_stuff;
  1440. if (rdf_descriptor_)
  1441. {
  1442. rdf_descriptor = lv2_rdf_dup(rdf_descriptor_);
  1443. if (lib_open(rdf_descriptor->Binary))
  1444. {
  1445. LV2_Descriptor_Function descfn = (LV2_Descriptor_Function)lib_symbol("lv2_descriptor");
  1446. if (descfn)
  1447. {
  1448. uint32_t i = 0;
  1449. while ((descriptor = descfn(i++)))
  1450. {
  1451. if (strcmp(descriptor->URI, URI) == 0)
  1452. break;
  1453. }
  1454. if (descriptor)
  1455. {
  1456. bool can_continue = true;
  1457. // Check supported ports
  1458. for (i=0; i < rdf_descriptor->PortCount; i++)
  1459. {
  1460. LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  1461. if (bool(LV2_IS_PORT_AUDIO(PortType) || LV2_IS_PORT_CONTROL(PortType) || LV2_IS_PORT_EVENT(PortType) || LV2_IS_PORT_MIDI_LL(PortType)) == false)
  1462. {
  1463. if (! LV2_IS_PORT_OPTIONAL(rdf_descriptor->Ports[i].Properties))
  1464. {
  1465. set_last_error("Plugin requires a port that is not currently supported");
  1466. can_continue = false;
  1467. break;
  1468. }
  1469. }
  1470. }
  1471. // Check supported features
  1472. for (i=0; i < rdf_descriptor->FeatureCount; i++)
  1473. {
  1474. if (LV2_IS_FEATURE_REQUIRED(rdf_descriptor->Features[i].Type) && is_lv2_feature_supported(rdf_descriptor->Features[i].URI) == false)
  1475. {
  1476. QString msg = QString("Plugin requires a feature that is not supported:\n%1").arg(rdf_descriptor->Features[i].URI);
  1477. set_last_error(msg.toUtf8().constData());
  1478. can_continue = false;
  1479. break;
  1480. }
  1481. else
  1482. qDebug("Plugin wants a feature that is not supported:\n%s", rdf_descriptor->Features[i].URI);
  1483. }
  1484. // Check extensions (...)
  1485. for (i=0; i < rdf_descriptor->ExtensionCount; i++)
  1486. {
  1487. //if (strcmp(rdf_descriptor->Extensions[i], LV2_STATE_INTERFACE_URI) == 0)
  1488. // plugin->hints |= PLUGIN_HAS_EXTENSION_STATE;
  1489. //else if (strcmp(rdf_descriptor->Extensions[i], LV2DYNPARAM_URI) == 0)
  1490. // plugin->hints |= PLUGIN_HAS_EXTENSION_DYNPARAM;
  1491. //else
  1492. qDebug("Plugin has non-supported extension: '%s'", rdf_descriptor->Extensions[i]);
  1493. }
  1494. if (can_continue)
  1495. {
  1496. // Initialize features
  1497. LV2_URI_Map_Feature* URI_Map_Feature = new LV2_URI_Map_Feature;
  1498. URI_Map_Feature->callback_data = this;
  1499. URI_Map_Feature->uri_to_id = carla_lv2_uri_to_id;
  1500. LV2_URID_Map* URID_Map_Feature = new LV2_URID_Map;
  1501. URID_Map_Feature->handle = this;
  1502. URID_Map_Feature->map = carla_lv2_urid_map;
  1503. LV2_URID_Unmap* URID_Unmap_Feature = new LV2_URID_Unmap;
  1504. URID_Unmap_Feature->handle = this;
  1505. URID_Unmap_Feature->unmap = carla_lv2_urid_unmap;
  1506. LV2_Event_Feature* Event_Feature = new LV2_Event_Feature;
  1507. Event_Feature->callback_data = this;
  1508. Event_Feature->lv2_event_ref = nullptr;
  1509. Event_Feature->lv2_event_unref = nullptr;
  1510. features[lv2_feature_id_uri_map] = new LV2_Feature;
  1511. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  1512. features[lv2_feature_id_uri_map]->data = URI_Map_Feature;
  1513. features[lv2_feature_id_urid_map] = new LV2_Feature;
  1514. features[lv2_feature_id_urid_map]->URI = LV2_URID_MAP_URI;
  1515. features[lv2_feature_id_urid_map]->data = URID_Map_Feature;
  1516. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  1517. features[lv2_feature_id_urid_unmap]->URI = LV2_URID_UNMAP_URI;
  1518. features[lv2_feature_id_urid_unmap]->data = URID_Unmap_Feature;
  1519. features[lv2_feature_id_event] = new LV2_Feature;
  1520. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  1521. features[lv2_feature_id_event]->data = Event_Feature;
  1522. handle = descriptor->instantiate(descriptor, get_sample_rate(), rdf_descriptor->Bundle, features);
  1523. if (handle)
  1524. {
  1525. m_filename = strdup(filename);
  1526. m_name = get_unique_name(rdf_descriptor->Name);
  1527. if (carla_jack_register_plugin(this, &jack_client))
  1528. return true;
  1529. else
  1530. set_last_error("Failed to register plugin in JACK");
  1531. }
  1532. else
  1533. set_last_error("Plugin failed to initialize");
  1534. }
  1535. // error already set
  1536. }
  1537. else
  1538. set_last_error("Could not find the requested plugin URI in the plugin library");
  1539. }
  1540. else
  1541. set_last_error("Could not find the LV2 Descriptor in the plugin library");
  1542. }
  1543. else
  1544. set_last_error(lib_error());
  1545. }
  1546. else
  1547. set_last_error("Failed to find the requested plugin in the LV2 Bundle");
  1548. return false;
  1549. }
  1550. // ----------------- URI-Map Feature -------------------------------------------------
  1551. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  1552. {
  1553. qDebug("Lv2AudioPlugin::carla_lv2_uri_to_id(%p, %s, %s)", data, map, uri);
  1554. if (map && strcmp(map, LV2_EVENT_URI) == 0)
  1555. {
  1556. // Event types
  1557. if (strcmp(uri, "http://lv2plug.in/ns/ext/midi#MidiEvent") == 0)
  1558. return CARLA_URI_MAP_ID_EVENT_MIDI;
  1559. else if (strcmp(uri, "http://lv2plug.in/ns/ext/time#Position") == 0)
  1560. return CARLA_URI_MAP_ID_EVENT_TIME;
  1561. }
  1562. else if (strcmp(uri, LV2_ATOM__String) == 0)
  1563. {
  1564. return CARLA_URI_MAP_ID_ATOM_STRING;
  1565. }
  1566. // Custom types
  1567. if (data)
  1568. {
  1569. Lv2Plugin* plugin = (Lv2Plugin*)data;
  1570. return plugin->get_custom_uri_id(uri);
  1571. }
  1572. return 0;
  1573. }
  1574. // ----------------- URID Feature ----------------------------------------------------
  1575. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  1576. {
  1577. qDebug("Lv2AudioPlugin::carla_lv2_urid_map(%p, %s)", handle, uri);
  1578. if (strcmp(uri, "http://lv2plug.in/ns/ext/midi#MidiEvent") == 0)
  1579. return CARLA_URI_MAP_ID_EVENT_MIDI;
  1580. else if (strcmp(uri, "http://lv2plug.in/ns/ext/time#Position") == 0)
  1581. return CARLA_URI_MAP_ID_EVENT_TIME;
  1582. else if (strcmp(uri, LV2_ATOM__String) == 0)
  1583. return CARLA_URI_MAP_ID_ATOM_STRING;
  1584. // Custom types
  1585. if (handle)
  1586. {
  1587. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  1588. return plugin->get_custom_uri_id(uri);
  1589. }
  1590. return 0;
  1591. }
  1592. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  1593. {
  1594. qDebug("Lv2AudioPlugin::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  1595. if (urid == CARLA_URI_MAP_ID_EVENT_MIDI)
  1596. return "http://lv2plug.in/ns/ext/midi#MidiEvent";
  1597. else if (urid == CARLA_URI_MAP_ID_EVENT_TIME)
  1598. return "http://lv2plug.in/ns/ext/time#Position";
  1599. else if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  1600. return LV2_ATOM__String;
  1601. // Custom types
  1602. if (handle)
  1603. {
  1604. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  1605. return plugin->get_custom_uri_string(urid);
  1606. }
  1607. return nullptr;
  1608. }
  1609. private:
  1610. LV2_Handle handle;
  1611. const LV2_Descriptor* descriptor;
  1612. const LV2_RDF_Descriptor* rdf_descriptor;
  1613. LV2_Feature* features[lv2_feature_count+1];
  1614. // struct {
  1615. // void* lib;
  1616. // LV2UI_Handle handle;
  1617. // LV2UI_Widget widget;
  1618. // const LV2UI_Descriptor* descriptor;
  1619. // const LV2_RDF_UI* rdf_descriptor;
  1620. // } ui;
  1621. uint32_t* ain_rindexes;
  1622. uint32_t* aout_rindexes;
  1623. Lv2ParameterData* lv2param;
  1624. PluginEventData evin;
  1625. PluginEventData evout;
  1626. QList<const char*> custom_uri_ids;
  1627. };
  1628. short add_plugin_lv2(const char* filename, const char* label, void* extra_stuff)
  1629. {
  1630. qDebug("add_plugin_lv2(%s, %s, %p)", filename, label, extra_stuff);
  1631. short id = get_new_plugin_id();
  1632. if (id >= 0)
  1633. {
  1634. Lv2Plugin* plugin = new Lv2Plugin;
  1635. if (plugin->init(filename, label, extra_stuff))
  1636. {
  1637. plugin->reload();
  1638. plugin->set_id(id);
  1639. unique_names[id] = plugin->name();
  1640. CarlaPlugins[id] = plugin;
  1641. #ifndef BUILD_BRIDGE
  1642. //osc_new_plugin(plugin);
  1643. #endif
  1644. }
  1645. else
  1646. {
  1647. delete plugin;
  1648. id = -1;
  1649. }
  1650. }
  1651. else
  1652. set_last_error("Maximum number of plugins reached");
  1653. return id;
  1654. }