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.

3849 lines
133KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the COPYING file
  16. */
  17. #include "carla_plugin.h"
  18. #include "carla_lv2.h"
  19. extern "C" {
  20. #include "lv2-rtmempool/rtmempool.h"
  21. }
  22. #include <QtCore/QDir>
  23. #ifndef __WINE__
  24. #include <QtGui/QLayout>
  25. #endif
  26. #ifdef HAVE_SUIL
  27. #include <suil/suil.h>
  28. struct SuilInstanceImpl {
  29. void* lib_handle;
  30. const LV2UI_Descriptor* descriptor;
  31. LV2UI_Handle handle;
  32. // ...
  33. };
  34. #endif
  35. CARLA_BACKEND_START_NAMESPACE
  36. #if 0
  37. } /* adjust editor indent */
  38. #endif
  39. /*!
  40. * @defgroup CarlaBackendLv2Plugin Carla Backend LV2 Plugin
  41. *
  42. * The Carla Backend LV2 Plugin.\n
  43. * http://lv2plug.in/
  44. * @{
  45. */
  46. // static max values
  47. const unsigned int MAX_EVENT_BUFFER = 8192; // 0x2000
  48. //const unsigned int MAX_EVENT_BUFFER = (sizeof(LV2_Atom_Event) + 4) * MAX_MIDI_EVENTS;
  49. /*!
  50. * @defgroup PluginHints Plugin Hints
  51. * @{
  52. */
  53. const unsigned int PLUGIN_HAS_EXTENSION_DYNPARAM = 0x100; //!< LV2 Plugin has DynParam extension
  54. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x200; //!< LV2 Plugin has Programs extension
  55. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x400; //!< LV2 Plugin has State extension
  56. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x800; //!< LV2 Plugin has Worker extension
  57. /**@}*/
  58. /*!
  59. * @defgroup ParameterHints Parameter Hints
  60. * @{
  61. */
  62. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000; //!< LV2 Parameter needs strict bounds
  63. const unsigned int PARAMETER_IS_TRIGGER = 0x2000; //!< LV2 Parameter is trigger (current value should be changed to its default after run())
  64. /**@}*/
  65. /*!
  66. * @defgroup Lv2FeatureIds LV2 Feature Ids
  67. *
  68. * Static index list of the internal LV2 Feature Ids.
  69. * \see CarlaPlugin::hints
  70. * @{
  71. */
  72. const uint32_t lv2_feature_id_event = 0;
  73. const uint32_t lv2_feature_id_logs = 1;
  74. const uint32_t lv2_feature_id_programs = 2;
  75. const uint32_t lv2_feature_id_rtmempool = 3;
  76. const uint32_t lv2_feature_id_state_make_path = 4;
  77. const uint32_t lv2_feature_id_state_map_path = 5;
  78. const uint32_t lv2_feature_id_strict_bounds = 6;
  79. const uint32_t lv2_feature_id_uri_map = 7;
  80. const uint32_t lv2_feature_id_urid_map = 8;
  81. const uint32_t lv2_feature_id_urid_unmap = 9;
  82. const uint32_t lv2_feature_id_worker = 10;
  83. const uint32_t lv2_feature_id_data_access = 11;
  84. const uint32_t lv2_feature_id_instance_access = 12;
  85. const uint32_t lv2_feature_id_ui_parent = 13;
  86. const uint32_t lv2_feature_id_ui_port_map = 14;
  87. const uint32_t lv2_feature_id_ui_resize = 15;
  88. const uint32_t lv2_feature_id_external_ui = 16;
  89. const uint32_t lv2_feature_id_external_ui_old = 17;
  90. const uint32_t lv2_feature_count = 18;
  91. /**@}*/
  92. /*!
  93. * @defgroup Lv2EventTypes LV2 Event Data/Types
  94. *
  95. * Data and buffer types for LV2 EventData ports.
  96. * \see CarlaPlugin::hints
  97. * @{
  98. */
  99. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  100. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  101. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  102. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10;
  103. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  104. /**@}*/
  105. /*!
  106. * @defgroup Lv2UriMapIds LV2 URI Map Ids
  107. *
  108. * Static index list of the internal LV2 URI Map Ids.
  109. * \see CarlaPlugin::hints
  110. * @{
  111. */
  112. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  113. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 1;
  114. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 2;
  115. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 3;
  116. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 4;
  117. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 5;
  118. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 6;
  119. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 7;
  120. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 8;
  121. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 9;
  122. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 10;
  123. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 11;
  124. const uint32_t CARLA_URI_MAP_ID_COUNT = 12;
  125. /**@}*/
  126. enum Lv2ParameterDataType {
  127. LV2_PARAMETER_TYPE_NULL,
  128. LV2_PARAMETER_TYPE_CONTROL
  129. };
  130. struct EventData {
  131. unsigned int type;
  132. CarlaEngineMidiPort* port;
  133. union {
  134. LV2_Atom_Sequence* atom;
  135. LV2_Event_Buffer* event;
  136. LV2_MIDI* midi;
  137. };
  138. EventData()
  139. : type(0),
  140. port(nullptr) {}
  141. };
  142. struct Lv2ParameterData {
  143. Lv2ParameterDataType type;
  144. union {
  145. float control;
  146. };
  147. Lv2ParameterData()
  148. : type(LV2_PARAMETER_TYPE_NULL) {}
  149. };
  150. struct PluginEventData {
  151. uint32_t count;
  152. EventData* data;
  153. PluginEventData()
  154. : count(0),
  155. data(nullptr) {}
  156. };
  157. const char* lv2bridge2str(const LV2_Property type)
  158. {
  159. switch (type)
  160. {
  161. #ifndef BUILD_BRIDGE
  162. case LV2_UI_GTK2:
  163. return carlaOptions.bridge_lv2gtk2;
  164. case LV2_UI_QT4:
  165. return carlaOptions.bridge_lv2qt4;
  166. case LV2_UI_COCOA:
  167. return nullptr; //carlaOptions.bridge_lv2cocoa;
  168. case LV2_UI_WINDOWS:
  169. return nullptr; //carlaOptions.bridge_lv2hwnd;
  170. case LV2_UI_X11:
  171. return carlaOptions.bridge_lv2x11;
  172. #endif
  173. default:
  174. return nullptr;
  175. }
  176. }
  177. class Lv2Plugin : public CarlaPlugin
  178. {
  179. public:
  180. Lv2Plugin(CarlaEngine* const engine, unsigned short id) : CarlaPlugin(engine, id)
  181. {
  182. qDebug("Lv2Plugin::Lv2Plugin()");
  183. m_type = PLUGIN_LV2;
  184. handle = h2 = nullptr;
  185. descriptor = nullptr;
  186. rdf_descriptor = nullptr;
  187. ext.dynparam = nullptr;
  188. ext.state = nullptr;
  189. ext.worker = nullptr;
  190. ext.programs = nullptr;
  191. ext.uiprograms = nullptr;
  192. ui.lib = nullptr;
  193. ui.handle = nullptr;
  194. ui.descriptor = nullptr;
  195. ui.rdf_descriptor = nullptr;
  196. evin.count = 0;
  197. evin.data = nullptr;
  198. evout.count = 0;
  199. evout.data = nullptr;
  200. lv2param = nullptr;
  201. gui.type = GUI_NONE;
  202. gui.resizable = false;
  203. gui.width = 0;
  204. gui.height = 0;
  205. #ifdef HAVE_SUIL
  206. suil.host = nullptr;
  207. suil.handle = nullptr;
  208. #endif
  209. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; i++)
  210. customURIDs.push_back(nullptr);
  211. for (uint32_t i=0; i < lv2_feature_count+1; i++)
  212. features[i] = nullptr;
  213. Lv2World.init();
  214. }
  215. ~Lv2Plugin()
  216. {
  217. qDebug("Lv2Plugin::~Lv2Plugin()");
  218. // close UI
  219. if (m_hints & PLUGIN_HAS_GUI)
  220. {
  221. switch(gui.type)
  222. {
  223. case GUI_INTERNAL_QT4:
  224. case GUI_INTERNAL_HWND:
  225. case GUI_INTERNAL_X11:
  226. break;
  227. case GUI_EXTERNAL_LV2:
  228. //if (ui.widget)
  229. // LV2_EXTERNAL_UI_HIDE((lv2_external_ui*)ui.widget);
  230. break;
  231. #ifndef __WINE__
  232. case GUI_EXTERNAL_SUIL:
  233. if (ui.widget)
  234. ((QWidget*)ui.widget)->close();
  235. break;
  236. #endif
  237. #ifndef BUILD_BRIDGE
  238. case GUI_EXTERNAL_OSC:
  239. if (osc.data.target)
  240. {
  241. osc_send_hide(&osc.data);
  242. osc_send_quit(&osc.data);
  243. }
  244. if (osc.thread)
  245. {
  246. // Wait a bit first, try safe quit else force kill
  247. if (osc.thread->isRunning())
  248. {
  249. if (! osc.thread->wait(2000))
  250. osc.thread->quit();
  251. if (osc.thread->isRunning() && ! osc.thread->wait(1000))
  252. {
  253. qWarning("Failed to properly stop LV2 OSC-GUI thread");
  254. osc.thread->terminate();
  255. }
  256. }
  257. delete osc.thread;
  258. }
  259. osc_clear_data(&osc.data);
  260. break;
  261. #endif
  262. default:
  263. break;
  264. }
  265. #ifdef HAVE_SUIL
  266. if (suil.handle)
  267. suil_instance_free(suil.handle);
  268. if (suil.host)
  269. suil_host_free(suil.host);
  270. ui.handle = nullptr;
  271. ui.descriptor = nullptr;
  272. #endif
  273. if (ui.handle && ui.descriptor && ui.descriptor->cleanup)
  274. ui.descriptor->cleanup(ui.handle);
  275. if (features[lv2_feature_id_data_access] && features[lv2_feature_id_data_access]->data)
  276. delete (LV2_Extension_Data_Feature*)features[lv2_feature_id_data_access]->data;
  277. if (features[lv2_feature_id_ui_port_map] && features[lv2_feature_id_ui_port_map]->data)
  278. delete (LV2UI_Port_Map*)features[lv2_feature_id_ui_port_map]->data;
  279. if (features[lv2_feature_id_ui_resize] && features[lv2_feature_id_ui_resize]->data)
  280. delete (LV2UI_Resize*)features[lv2_feature_id_ui_resize]->data;
  281. if (features[lv2_feature_id_external_ui] && features[lv2_feature_id_external_ui]->data)
  282. {
  283. free((void*)((lv2_external_ui_host*)features[lv2_feature_id_external_ui]->data)->plugin_human_id);
  284. delete (lv2_external_ui_host*)features[lv2_feature_id_external_ui]->data;
  285. }
  286. uiLibClose();
  287. }
  288. if (descriptor)
  289. {
  290. if (descriptor->deactivate && m_activeBefore)
  291. {
  292. if (handle)
  293. descriptor->deactivate(handle);
  294. if (h2)
  295. descriptor->deactivate(handle);
  296. }
  297. if (descriptor->cleanup)
  298. {
  299. if (handle)
  300. descriptor->cleanup(handle);
  301. if (h2)
  302. descriptor->cleanup(h2);
  303. }
  304. }
  305. if (rdf_descriptor)
  306. lv2_rdf_free(rdf_descriptor);
  307. if (features[lv2_feature_id_event] && features[lv2_feature_id_event]->data)
  308. delete (LV2_Event_Feature*)features[lv2_feature_id_event]->data;
  309. if (features[lv2_feature_id_logs] && features[lv2_feature_id_logs]->data)
  310. delete (LV2_Log_Log*)features[lv2_feature_id_logs]->data;
  311. if (features[lv2_feature_id_programs] && features[lv2_feature_id_programs]->data)
  312. delete (LV2_Programs_Host*)features[lv2_feature_id_programs]->data;
  313. if (features[lv2_feature_id_rtmempool] && features[lv2_feature_id_rtmempool]->data)
  314. delete (lv2_rtsafe_memory_pool_provider*)features[lv2_feature_id_rtmempool]->data;
  315. if (features[lv2_feature_id_state_make_path] && features[lv2_feature_id_state_make_path]->data)
  316. delete (LV2_State_Make_Path*)features[lv2_feature_id_state_make_path]->data;
  317. if (features[lv2_feature_id_state_map_path] && features[lv2_feature_id_state_map_path]->data)
  318. delete (LV2_State_Map_Path*)features[lv2_feature_id_state_map_path]->data;
  319. if (features[lv2_feature_id_uri_map] && features[lv2_feature_id_uri_map]->data)
  320. delete (LV2_URI_Map_Feature*)features[lv2_feature_id_uri_map]->data;
  321. if (features[lv2_feature_id_urid_map] && features[lv2_feature_id_urid_map]->data)
  322. delete (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  323. if (features[lv2_feature_id_urid_unmap] && features[lv2_feature_id_urid_unmap]->data)
  324. delete (LV2_URID_Unmap*)features[lv2_feature_id_urid_unmap]->data;
  325. if (features[lv2_feature_id_worker] && features[lv2_feature_id_worker]->data)
  326. delete (LV2_Worker_Schedule*)features[lv2_feature_id_worker]->data;
  327. for (uint32_t i=0; i < lv2_feature_count; i++)
  328. {
  329. if (features[i])
  330. delete features[i];
  331. }
  332. for (size_t i=0; i < customURIDs.size(); i++)
  333. {
  334. if (customURIDs[i])
  335. free((void*)customURIDs[i]);
  336. }
  337. customURIDs.clear();
  338. }
  339. // -------------------------------------------------------------------
  340. // Information (base)
  341. PluginCategory category()
  342. {
  343. LV2_Property Category = rdf_descriptor->Type;
  344. if (LV2_IS_DELAY(Category))
  345. return PLUGIN_CATEGORY_DELAY;
  346. if (LV2_IS_DISTORTION(Category))
  347. return PLUGIN_CATEGORY_OTHER;
  348. if (LV2_IS_DYNAMICS(Category))
  349. return PLUGIN_CATEGORY_DYNAMICS;
  350. if (LV2_IS_EQ(Category))
  351. return PLUGIN_CATEGORY_EQ;
  352. if (LV2_IS_FILTER(Category))
  353. return PLUGIN_CATEGORY_FILTER;
  354. if (LV2_IS_GENERATOR(Category))
  355. return PLUGIN_CATEGORY_SYNTH;
  356. if (LV2_IS_MODULATOR(Category))
  357. return PLUGIN_CATEGORY_MODULATOR;
  358. if (LV2_IS_REVERB(Category))
  359. return PLUGIN_CATEGORY_DELAY;
  360. if (LV2_IS_SIMULATOR(Category))
  361. return PLUGIN_CATEGORY_OTHER;
  362. if (LV2_IS_SPATIAL(Category))
  363. return PLUGIN_CATEGORY_OTHER;
  364. if (LV2_IS_SPECTRAL(Category))
  365. return PLUGIN_CATEGORY_UTILITY;
  366. if (LV2_IS_UTILITY(Category))
  367. return PLUGIN_CATEGORY_UTILITY;
  368. return getPluginCategoryFromName(m_name);
  369. }
  370. long uniqueId()
  371. {
  372. return rdf_descriptor->UniqueID;
  373. }
  374. // -------------------------------------------------------------------
  375. // Information (count)
  376. uint32_t midiInCount()
  377. {
  378. uint32_t i, count = 0;
  379. for (i=0; i < evin.count; i++)
  380. {
  381. if (evin.data[i].type & CARLA_EVENT_TYPE_MIDI)
  382. count += 1;
  383. }
  384. return count;
  385. }
  386. uint32_t midiOutCount()
  387. {
  388. uint32_t i, count = 0;
  389. for (i=0; i < evout.count; i++)
  390. {
  391. if (evout.data[i].type & CARLA_EVENT_TYPE_MIDI)
  392. count += 1;
  393. }
  394. return count;
  395. }
  396. uint32_t parameterScalePointCount(uint32_t parameterId)
  397. {
  398. assert(parameterId < param.count);
  399. int32_t rindex = param.data[parameterId].rindex;
  400. return rdf_descriptor->Ports[rindex].ScalePointCount;
  401. }
  402. // -------------------------------------------------------------------
  403. // Information (per-plugin data)
  404. double getParameterValue(uint32_t parameterId)
  405. {
  406. assert(parameterId < param.count);
  407. switch (lv2param[parameterId].type)
  408. {
  409. case LV2_PARAMETER_TYPE_CONTROL:
  410. return lv2param[parameterId].control;
  411. default:
  412. return 0.0;
  413. }
  414. }
  415. double getParameterScalePointValue(uint32_t parameterId, uint32_t scalePointId)
  416. {
  417. assert(parameterId < param.count);
  418. assert(scalePointId < parameterScalePointCount(parameterId));
  419. int32_t rindex = param.data[parameterId].rindex;
  420. return rdf_descriptor->Ports[rindex].ScalePoints[scalePointId].Value;
  421. }
  422. void getLabel(char* const strBuf)
  423. {
  424. strncpy(strBuf, rdf_descriptor->URI, STR_MAX);
  425. }
  426. void getMaker(char* const strBuf)
  427. {
  428. if (rdf_descriptor->Author)
  429. strncpy(strBuf, rdf_descriptor->Author, STR_MAX);
  430. else
  431. CarlaPlugin::getMaker(strBuf);
  432. }
  433. void getCopyright(char* const strBuf)
  434. {
  435. if (rdf_descriptor->License)
  436. strncpy(strBuf, rdf_descriptor->License, STR_MAX);
  437. else
  438. CarlaPlugin::getCopyright(strBuf);
  439. }
  440. void getRealName(char* const strBuf)
  441. {
  442. if (rdf_descriptor->Name)
  443. strncpy(strBuf, rdf_descriptor->Name, STR_MAX);
  444. else
  445. CarlaPlugin::getRealName(strBuf);
  446. }
  447. void getParameterName(uint32_t parameterId, char* const strBuf)
  448. {
  449. assert(parameterId < param.count);
  450. int32_t rindex = param.data[parameterId].rindex;
  451. strncpy(strBuf, rdf_descriptor->Ports[rindex].Name, STR_MAX);
  452. }
  453. void getParameterSymbol(uint32_t parameterId, char* const strBuf)
  454. {
  455. assert(parameterId < param.count);
  456. int32_t rindex = param.data[parameterId].rindex;
  457. strncpy(strBuf, rdf_descriptor->Ports[rindex].Symbol, STR_MAX);
  458. }
  459. void getParameterUnit(uint32_t parameterId, char* const strBuf)
  460. {
  461. assert(parameterId < param.count);
  462. int32_t rindex = param.data[parameterId].rindex;
  463. const LV2_RDF_Port* const Port = &rdf_descriptor->Ports[rindex];
  464. if (LV2_HAVE_UNIT_SYMBOL(Port->Unit.Hints))
  465. strncpy(strBuf, Port->Unit.Symbol, STR_MAX);
  466. else if (LV2_HAVE_UNIT(Port->Unit.Hints))
  467. {
  468. switch (Port->Unit.Type)
  469. {
  470. case LV2_UNIT_BAR:
  471. strncpy(strBuf, "bars", STR_MAX);
  472. return;
  473. case LV2_UNIT_BEAT:
  474. strncpy(strBuf, "beats", STR_MAX);
  475. return;
  476. case LV2_UNIT_BPM:
  477. strncpy(strBuf, "BPM", STR_MAX);
  478. return;
  479. case LV2_UNIT_CENT:
  480. strncpy(strBuf, "ct", STR_MAX);
  481. return;
  482. case LV2_UNIT_CM:
  483. strncpy(strBuf, "cm", STR_MAX);
  484. return;
  485. case LV2_UNIT_COEF:
  486. strncpy(strBuf, "(coef)", STR_MAX);
  487. return;
  488. case LV2_UNIT_DB:
  489. strncpy(strBuf, "dB", STR_MAX);
  490. return;
  491. case LV2_UNIT_DEGREE:
  492. strncpy(strBuf, "deg", STR_MAX);
  493. return;
  494. case LV2_UNIT_FRAME:
  495. strncpy(strBuf, "frames", STR_MAX);
  496. return;
  497. case LV2_UNIT_HZ:
  498. strncpy(strBuf, "Hz", STR_MAX);
  499. return;
  500. case LV2_UNIT_INCH:
  501. strncpy(strBuf, "in", STR_MAX);
  502. return;
  503. case LV2_UNIT_KHZ:
  504. strncpy(strBuf, "kHz", STR_MAX);
  505. return;
  506. case LV2_UNIT_KM:
  507. strncpy(strBuf, "km", STR_MAX);
  508. return;
  509. case LV2_UNIT_M:
  510. strncpy(strBuf, "m", STR_MAX);
  511. return;
  512. case LV2_UNIT_MHZ:
  513. strncpy(strBuf, "MHz", STR_MAX);
  514. return;
  515. case LV2_UNIT_MIDINOTE:
  516. strncpy(strBuf, "note", STR_MAX);
  517. return;
  518. case LV2_UNIT_MILE:
  519. strncpy(strBuf, "mi", STR_MAX);
  520. return;
  521. case LV2_UNIT_MIN:
  522. strncpy(strBuf, "min", STR_MAX);
  523. return;
  524. case LV2_UNIT_MM:
  525. strncpy(strBuf, "mm", STR_MAX);
  526. return;
  527. case LV2_UNIT_MS:
  528. strncpy(strBuf, "ms", STR_MAX);
  529. return;
  530. case LV2_UNIT_OCT:
  531. strncpy(strBuf, "oct", STR_MAX);
  532. return;
  533. case LV2_UNIT_PC:
  534. strncpy(strBuf, "%", STR_MAX);
  535. return;
  536. case LV2_UNIT_S:
  537. strncpy(strBuf, "s", STR_MAX);
  538. return;
  539. case LV2_UNIT_SEMITONE:
  540. strncpy(strBuf, "semi", STR_MAX);
  541. return;
  542. }
  543. }
  544. *strBuf = 0;
  545. }
  546. void getParameterScalePointLabel(uint32_t parameterId, uint32_t scalePointId, char* const strBuf)
  547. {
  548. assert(parameterId < param.count);
  549. assert(scalePointId < parameterScalePointCount(parameterId));
  550. int32_t rindex = param.data[parameterId].rindex;
  551. strncpy(strBuf, rdf_descriptor->Ports[rindex].ScalePoints[scalePointId].Label, STR_MAX);
  552. }
  553. void getGuiInfo(GuiType* type, bool* resizable)
  554. {
  555. *type = gui.type;
  556. *resizable = gui.resizable;
  557. }
  558. // -------------------------------------------------------------------
  559. // Set data (plugin-specific stuff)
  560. void setParameterValue(uint32_t parameterId, double value, bool sendGui, bool sendOsc, bool sendCallback)
  561. {
  562. assert(parameterId < param.count);
  563. switch (lv2param[parameterId].type)
  564. {
  565. case LV2_PARAMETER_TYPE_CONTROL:
  566. lv2param[parameterId].control = fixParameterValue(value, param.ranges[parameterId]);
  567. break;
  568. default:
  569. break;
  570. }
  571. if (sendGui)
  572. {
  573. switch (gui.type)
  574. {
  575. case GUI_INTERNAL_QT4:
  576. case GUI_INTERNAL_HWND:
  577. case GUI_INTERNAL_X11:
  578. case GUI_EXTERNAL_LV2:
  579. case GUI_EXTERNAL_SUIL:
  580. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  581. {
  582. float fvalue = value;
  583. ui.descriptor->port_event(ui.handle, param.data[parameterId].rindex, sizeof(float), 0, &fvalue);
  584. }
  585. break;
  586. #ifndef BUILD_BRIDGE
  587. case GUI_EXTERNAL_OSC:
  588. //osc_send_control(&osc.data, param.data[parameterId].rindex, value);
  589. break;
  590. #endif
  591. default:
  592. break;
  593. }
  594. }
  595. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  596. }
  597. void setCustomData(CustomDataType type, const char* key, const char* value, bool sendGui)
  598. {
  599. CarlaPlugin::setCustomData(type, key, value, sendGui);
  600. if (ext.state)
  601. {
  602. const char* const stype = getCustomDataTypeString(type);
  603. LV2_State_Status status;
  604. if (x_engine->isOffline())
  605. {
  606. engineProcessLock();
  607. status = ext.state->restore(handle, carla_lv2_state_retrieve, this, 0, features);
  608. engineProcessUnlock();
  609. }
  610. else
  611. {
  612. const CarlaPluginScopedDisabler m(this);
  613. status = ext.state->restore(handle, carla_lv2_state_retrieve, this, 0, features);
  614. }
  615. switch (status)
  616. {
  617. case LV2_STATE_SUCCESS:
  618. qDebug("Lv2Plugin::setCustomData(%s, %s, <value>, %s) - success", stype, key, bool2str(sendGui));
  619. break;
  620. case LV2_STATE_ERR_UNKNOWN:
  621. qWarning("Lv2Plugin::setCustomData(%s, %s, <value>, %s) - unknown error", stype, key, bool2str(sendGui));
  622. break;
  623. case LV2_STATE_ERR_BAD_TYPE:
  624. qWarning("Lv2Plugin::setCustomData(%s, %s, <value>, %s) - error, bad type", stype, key, bool2str(sendGui));
  625. break;
  626. case LV2_STATE_ERR_BAD_FLAGS:
  627. qWarning("Lv2Plugin::setCustomData(%s, %s, <value>, %s) - error, bad flags", stype, key, bool2str(sendGui));
  628. break;
  629. case LV2_STATE_ERR_NO_FEATURE:
  630. qWarning("Lv2Plugin::setCustomData(%s, %s, <value>, %s) - error, missing feature", stype, key, bool2str(sendGui));
  631. break;
  632. case LV2_STATE_ERR_NO_PROPERTY:
  633. qWarning("Lv2Plugin::setCustomData(%s, %s, <value>, %s) - error, missing property", stype, key, bool2str(sendGui));
  634. break;
  635. }
  636. }
  637. }
  638. void setMidiProgram(int32_t index, bool sendGui, bool sendOsc, bool sendCallback, bool block)
  639. {
  640. assert(index < (int32_t)midiprog.count);
  641. if (ext.programs && index >= 0)
  642. {
  643. if (x_engine->isOffline())
  644. {
  645. if (block) engineProcessLock();
  646. ext.programs->select_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  647. if (block) engineProcessUnlock();
  648. }
  649. else
  650. {
  651. const CarlaPluginScopedDisabler m(this, block);
  652. ext.programs->select_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  653. }
  654. if (sendGui)
  655. {
  656. #ifndef BUILD_BRIDGE
  657. //if (gui.type == GUI_EXTERNAL_OSC)
  658. // osc_send_midi_program(&osc.data, midiprog.data[index].bank, midiprog.data[index].program, false);
  659. //else
  660. #endif
  661. if (ext.uiprograms)
  662. ext.uiprograms->select_program(ui.handle, midiprog.data[index].bank, midiprog.data[index].program);
  663. }
  664. }
  665. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, block);
  666. }
  667. // -------------------------------------------------------------------
  668. // Set gui stuff
  669. void setGuiData(int, GuiDataHandle handle)
  670. {
  671. switch(gui.type)
  672. {
  673. #ifndef __WINE__
  674. case GUI_INTERNAL_QT4:
  675. if (ui.widget)
  676. {
  677. QDialog* dialog = handle;
  678. QWidget* widget = (QWidget*)ui.widget;
  679. dialog->layout()->addWidget(widget);
  680. widget->adjustSize();
  681. widget->setParent(dialog);
  682. widget->show();
  683. }
  684. break;
  685. #endif
  686. case GUI_INTERNAL_HWND:
  687. case GUI_INTERNAL_X11:
  688. if (ui.descriptor)
  689. {
  690. #ifdef __WINE__
  691. features[lv2_feature_id_ui_parent]->data = (void*)handle;
  692. #else
  693. features[lv2_feature_id_ui_parent]->data = (void*)handle->winId();
  694. #endif
  695. ui.handle = ui.descriptor->instantiate(ui.descriptor,
  696. descriptor->URI, ui.rdf_descriptor->Bundle,
  697. carla_lv2_ui_write_function, this, &ui.widget,features);
  698. updateUi();
  699. }
  700. break;
  701. default:
  702. break;
  703. }
  704. }
  705. void showGui(bool yesNo)
  706. {
  707. switch(gui.type)
  708. {
  709. case GUI_INTERNAL_QT4:
  710. break;
  711. case GUI_INTERNAL_COCOA:
  712. case GUI_INTERNAL_HWND:
  713. case GUI_INTERNAL_X11:
  714. if (yesNo && gui.width > 0 && gui.height > 0)
  715. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, gui.width, gui.height, 0.0);
  716. break;
  717. case GUI_EXTERNAL_LV2:
  718. if (! ui.handle)
  719. initExternalUi();
  720. if (ui.handle && ui.descriptor && ui.widget)
  721. {
  722. if (yesNo)
  723. {
  724. LV2_EXTERNAL_UI_SHOW((lv2_external_ui*)ui.widget);
  725. }
  726. else
  727. {
  728. LV2_EXTERNAL_UI_HIDE((lv2_external_ui*)ui.widget);
  729. if (rdf_descriptor->Author && strcmp(rdf_descriptor->Author, "linuxDSP") == 0)
  730. {
  731. qWarning("linuxDSP LV2 UI hack (force close instead of hide)");
  732. if (ui.descriptor->cleanup)
  733. ui.descriptor->cleanup(ui.handle);
  734. ui.handle = nullptr;
  735. }
  736. }
  737. }
  738. else
  739. // failed to init UI
  740. x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0);
  741. break;
  742. #ifndef __WINE__
  743. case GUI_EXTERNAL_SUIL:
  744. if (ui.widget)
  745. {
  746. QWidget* widget = (QWidget*)ui.widget;
  747. #ifdef HAVE_SUIL
  748. if (yesNo)
  749. widget->restoreGeometry(suil.pos);
  750. else
  751. suil.pos = widget->saveGeometry();
  752. #endif
  753. widget->setVisible(yesNo);
  754. }
  755. break;
  756. #endif
  757. #ifndef BUILD_BRIDGE
  758. case GUI_EXTERNAL_OSC:
  759. if (yesNo)
  760. {
  761. osc.thread->start();
  762. }
  763. else
  764. {
  765. osc_send_hide(&osc.data);
  766. osc_send_quit(&osc.data);
  767. osc_clear_data(&osc.data);
  768. }
  769. break;
  770. #endif
  771. default:
  772. break;
  773. }
  774. }
  775. void idleGui()
  776. {
  777. if (ui.handle && ui.descriptor && gui.type != GUI_EXTERNAL_OSC)
  778. {
  779. // Update output port values
  780. if (ui.descriptor->port_event)
  781. {
  782. float value;
  783. for (uint32_t i=0; i < param.count; i++)
  784. {
  785. if (param.data[i].type == PARAMETER_OUTPUT)
  786. {
  787. value = getParameterValue(i);
  788. ui.descriptor->port_event(ui.handle, param.data[i].rindex, sizeof(float), 0, &value);
  789. }
  790. }
  791. }
  792. // Update UI
  793. if (ui.widget && gui.type == GUI_EXTERNAL_LV2)
  794. LV2_EXTERNAL_UI_RUN((lv2_external_ui*)ui.widget);
  795. }
  796. }
  797. // -------------------------------------------------------------------
  798. // Plugin state
  799. void reload()
  800. {
  801. qDebug("Lv2Plugin::reload() - start");
  802. // Safely disable plugin for reload
  803. const CarlaPluginScopedDisabler m(this);
  804. if (x_client->isActive())
  805. x_client->deactivate();
  806. // Remove client ports
  807. removeClientPorts();
  808. // Delete old data
  809. deleteBuffers();
  810. uint32_t ains, aouts, cvIns, cvOuts, params, j;
  811. ains = aouts = cvIns = cvOuts = params = 0;
  812. std::vector<unsigned int> evIns, evOuts;
  813. const double sampleRate = x_engine->getSampleRate();
  814. const uint32_t PortCount = rdf_descriptor->PortCount;
  815. for (uint32_t i=0; i < PortCount; i++)
  816. {
  817. const LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  818. if (LV2_IS_PORT_AUDIO(PortType))
  819. {
  820. if (LV2_IS_PORT_INPUT(PortType))
  821. ains += 1;
  822. else if (LV2_IS_PORT_OUTPUT(PortType))
  823. aouts += 1;
  824. }
  825. else if (LV2_IS_PORT_CV(PortType))
  826. {
  827. if (LV2_IS_PORT_INPUT(PortType))
  828. cvIns += 1;
  829. else if (LV2_IS_PORT_OUTPUT(PortType))
  830. cvOuts += 1;
  831. }
  832. else if (LV2_IS_PORT_ATOM_SEQUENCE(PortType))
  833. {
  834. if (LV2_IS_PORT_INPUT(PortType))
  835. evIns.push_back(CARLA_EVENT_DATA_ATOM);
  836. else if (LV2_IS_PORT_OUTPUT(PortType))
  837. evOuts.push_back(CARLA_EVENT_DATA_ATOM);
  838. }
  839. else if (LV2_IS_PORT_EVENT(PortType))
  840. {
  841. if (LV2_IS_PORT_INPUT(PortType))
  842. evIns.push_back(CARLA_EVENT_DATA_EVENT);
  843. else if (LV2_IS_PORT_OUTPUT(PortType))
  844. evOuts.push_back(CARLA_EVENT_DATA_EVENT);
  845. }
  846. else if (LV2_IS_PORT_MIDI_LL(PortType))
  847. {
  848. if (LV2_IS_PORT_INPUT(PortType))
  849. evIns.push_back(CARLA_EVENT_DATA_MIDI_LL);
  850. else if (LV2_IS_PORT_OUTPUT(PortType))
  851. evOuts.push_back(CARLA_EVENT_DATA_MIDI_LL);
  852. }
  853. else if (LV2_IS_PORT_CONTROL(PortType))
  854. params += 1;
  855. }
  856. bool forcedStereoIn, forcedStereoOut;
  857. forcedStereoIn = forcedStereoOut = true;
  858. if (carlaOptions.force_stereo && (ains == 1 || aouts == 1) && ! h2)
  859. {
  860. h2 = descriptor->instantiate(descriptor, sampleRate, rdf_descriptor->Bundle, features);
  861. if (ains == 1)
  862. {
  863. ains = 2;
  864. forcedStereoIn = true;
  865. }
  866. if (aouts == 1)
  867. {
  868. aouts = 2;
  869. forcedStereoOut = true;
  870. }
  871. }
  872. if (ains > 0)
  873. {
  874. ain.ports = new CarlaEngineAudioPort*[ains];
  875. ain.rindexes = new uint32_t[ains];
  876. }
  877. if (aouts > 0)
  878. {
  879. aout.ports = new CarlaEngineAudioPort*[aouts];
  880. aout.rindexes = new uint32_t[aouts];
  881. }
  882. if (evIns.size() > 0)
  883. {
  884. const size_t size = evIns.size();
  885. evin.data = new EventData[size];
  886. for (j=0; j < size; j++)
  887. {
  888. evin.data[j].port = nullptr;
  889. evin.data[j].type = 0;
  890. if (evIns[j] == CARLA_EVENT_DATA_ATOM)
  891. {
  892. evin.data[j].type = CARLA_EVENT_DATA_ATOM;
  893. evin.data[j].atom = (LV2_Atom_Sequence*)malloc(sizeof(LV2_Atom_Sequence) + MAX_EVENT_BUFFER);
  894. evin.data[j].atom->atom.size = sizeof(LV2_Atom_Sequence_Body);
  895. evin.data[j].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  896. evin.data[j].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  897. evin.data[j].atom->body.pad = 0;
  898. }
  899. else if (evIns[j] == CARLA_EVENT_DATA_EVENT)
  900. {
  901. evin.data[j].type = CARLA_EVENT_DATA_EVENT;
  902. evin.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  903. }
  904. else if (evIns[j] == CARLA_EVENT_DATA_MIDI_LL)
  905. {
  906. evin.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  907. evin.data[j].midi = new LV2_MIDI;
  908. evin.data[j].midi->capacity = MAX_EVENT_BUFFER;
  909. evin.data[j].midi->data = new unsigned char [MAX_EVENT_BUFFER];
  910. }
  911. }
  912. }
  913. if (evOuts.size() > 0)
  914. {
  915. const size_t size = evOuts.size();
  916. evout.data = new EventData[size];
  917. for (j=0; j < size; j++)
  918. {
  919. evout.data[j].port = nullptr;
  920. evout.data[j].type = 0;
  921. if (evOuts[j] == CARLA_EVENT_DATA_ATOM)
  922. {
  923. evout.data[j].type = CARLA_EVENT_DATA_ATOM;
  924. evout.data[j].atom = (LV2_Atom_Sequence*)malloc(sizeof(LV2_Atom_Sequence) + MAX_EVENT_BUFFER);
  925. evout.data[j].atom->atom.size = sizeof(LV2_Atom_Sequence_Body);
  926. evout.data[j].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  927. evout.data[j].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  928. evout.data[j].atom->body.pad = 0;
  929. }
  930. else if (evOuts[j] == CARLA_EVENT_DATA_EVENT)
  931. {
  932. evout.data[j].type = CARLA_EVENT_DATA_EVENT;
  933. evout.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  934. }
  935. else if (evOuts[j] == CARLA_EVENT_DATA_MIDI_LL)
  936. {
  937. evout.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  938. evout.data[j].midi = new LV2_MIDI;
  939. evout.data[j].midi->capacity = MAX_EVENT_BUFFER;
  940. evout.data[j].midi->data = new unsigned char [MAX_EVENT_BUFFER];
  941. }
  942. }
  943. }
  944. if (params > 0)
  945. {
  946. param.data = new ParameterData[params];
  947. param.ranges = new ParameterRanges[params];
  948. lv2param = new Lv2ParameterData[params];
  949. }
  950. const int portNameSize = CarlaEngine::maxPortNameSize() - 1;
  951. char portName[portNameSize];
  952. bool needsCin = false;
  953. bool needsCout = false;
  954. for (uint32_t i=0; i < PortCount; i++)
  955. {
  956. const LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  957. if (LV2_IS_PORT_AUDIO(PortType) || LV2_IS_PORT_ATOM_SEQUENCE(PortType) || LV2_IS_PORT_CV(PortType) || LV2_IS_PORT_EVENT(PortType) || LV2_IS_PORT_MIDI_LL(PortType))
  958. {
  959. #ifndef BUILD_BRIDGE
  960. if (carlaOptions.process_mode != PROCESS_MODE_MULTIPLE_CLIENTS)
  961. {
  962. strcpy(portName, m_name);
  963. strcat(portName, ":");
  964. strncat(portName, rdf_descriptor->Ports[i].Name, portNameSize/2);
  965. }
  966. else
  967. #endif
  968. strncpy(portName, rdf_descriptor->Ports[i].Name, portNameSize);
  969. }
  970. if (LV2_IS_PORT_AUDIO(PortType))
  971. {
  972. if (LV2_IS_PORT_INPUT(PortType))
  973. {
  974. j = ain.count++;
  975. ain.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  976. ain.rindexes[j] = i;
  977. if (forcedStereoIn)
  978. {
  979. strcat(portName, "2");
  980. ain.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  981. ain.rindexes[1] = i;
  982. }
  983. }
  984. else if (LV2_IS_PORT_OUTPUT(PortType))
  985. {
  986. j = aout.count++;
  987. aout.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  988. aout.rindexes[j] = i;
  989. needsCin = true;
  990. if (forcedStereoOut)
  991. {
  992. strcat(portName, "2");
  993. aout.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  994. aout.rindexes[1] = i;
  995. }
  996. }
  997. else
  998. qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
  999. }
  1000. else if (LV2_IS_PORT_CV(PortType))
  1001. {
  1002. if (LV2_IS_PORT_INPUT(PortType))
  1003. {
  1004. qWarning("WARNING - CV Ports are not supported yet");
  1005. }
  1006. else if (LV2_IS_PORT_OUTPUT(PortType))
  1007. {
  1008. qWarning("WARNING - CV Ports are not supported yet");
  1009. }
  1010. else
  1011. qWarning("WARNING - Got a broken Port (CV, but not input or output)");
  1012. descriptor->connect_port(handle, i, nullptr);
  1013. if (h2) descriptor->connect_port(h2, i, nullptr);
  1014. }
  1015. else if (LV2_IS_PORT_ATOM_SEQUENCE(PortType))
  1016. {
  1017. if (LV2_IS_PORT_INPUT(PortType))
  1018. {
  1019. j = evin.count++;
  1020. descriptor->connect_port(handle, i, evin.data[j].atom);
  1021. if (h2) descriptor->connect_port(h2, i, evin.data[j].atom);
  1022. if (PortType & LV2_PORT_SUPPORTS_MIDI_EVENT)
  1023. {
  1024. evin.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1025. evin.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  1026. }
  1027. if (PortType & LV2_PORT_SUPPORTS_PATCH_MESSAGE)
  1028. {
  1029. evin.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1030. }
  1031. }
  1032. else if (LV2_IS_PORT_OUTPUT(PortType))
  1033. {
  1034. j = evout.count++;
  1035. descriptor->connect_port(handle, i, evout.data[j].atom);
  1036. if (h2) descriptor->connect_port(h2, i, evout.data[j].atom);
  1037. if (PortType & LV2_PORT_SUPPORTS_MIDI_EVENT)
  1038. {
  1039. evout.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1040. evout.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  1041. }
  1042. if (PortType & LV2_PORT_SUPPORTS_PATCH_MESSAGE)
  1043. {
  1044. evout.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1045. }
  1046. }
  1047. else
  1048. qWarning("WARNING - Got a broken Port (Atom Sequence, but not input or output)");
  1049. }
  1050. else if (LV2_IS_PORT_EVENT(PortType))
  1051. {
  1052. if (LV2_IS_PORT_INPUT(PortType))
  1053. {
  1054. j = evin.count++;
  1055. descriptor->connect_port(handle, i, evin.data[j].event);
  1056. if (h2) descriptor->connect_port(h2, i, evin.data[j].event);
  1057. if (PortType & LV2_PORT_SUPPORTS_MIDI_EVENT)
  1058. {
  1059. evin.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1060. evin.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  1061. }
  1062. }
  1063. else if (LV2_IS_PORT_OUTPUT(PortType))
  1064. {
  1065. j = evout.count++;
  1066. descriptor->connect_port(handle, i, evout.data[j].event);
  1067. if (h2) descriptor->connect_port(h2, i, evout.data[j].event);
  1068. if (PortType & LV2_PORT_SUPPORTS_MIDI_EVENT)
  1069. {
  1070. evout.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1071. evout.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  1072. }
  1073. }
  1074. else
  1075. qWarning("WARNING - Got a broken Port (Event, but not input or output)");
  1076. }
  1077. else if (LV2_IS_PORT_MIDI_LL(PortType))
  1078. {
  1079. if (LV2_IS_PORT_INPUT(PortType))
  1080. {
  1081. j = evin.count++;
  1082. descriptor->connect_port(handle, i, evin.data[j].midi);
  1083. if (h2) descriptor->connect_port(h2, i, evin.data[j].midi);
  1084. evin.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1085. evin.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  1086. }
  1087. else if (LV2_IS_PORT_OUTPUT(PortType))
  1088. {
  1089. j = evout.count++;
  1090. descriptor->connect_port(handle, i, evout.data[j].midi);
  1091. if (h2) descriptor->connect_port(h2, i, evout.data[j].midi);
  1092. evout.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1093. evout.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  1094. }
  1095. else
  1096. qWarning("WARNING - Got a broken Port (Midi, but not input or output)");
  1097. }
  1098. else if (LV2_IS_PORT_CONTROL(PortType))
  1099. {
  1100. const LV2_Property PortProps = rdf_descriptor->Ports[i].Properties;
  1101. const LV2_Property PortDesignation = rdf_descriptor->Ports[i].Designation;
  1102. const LV2_RDF_PortPoints PortPoints = rdf_descriptor->Ports[i].Points;
  1103. j = param.count++;
  1104. param.data[j].index = j;
  1105. param.data[j].rindex = i;
  1106. param.data[j].hints = 0;
  1107. param.data[j].midiChannel = 0;
  1108. param.data[j].midiCC = -1;
  1109. double min, max, def, step, step_small, step_large;
  1110. // min value
  1111. if (LV2_HAVE_MINIMUM_PORT_POINT(PortPoints.Hints))
  1112. min = PortPoints.Minimum;
  1113. else
  1114. min = 0.0;
  1115. // max value
  1116. if (LV2_HAVE_MAXIMUM_PORT_POINT(PortPoints.Hints))
  1117. max = PortPoints.Maximum;
  1118. else
  1119. max = 1.0;
  1120. if (min > max)
  1121. max = min;
  1122. else if (max < min)
  1123. min = max;
  1124. // stupid hack for ir.lv2 (broken plugin)
  1125. if (strcmp(rdf_descriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && strncmp(rdf_descriptor->Ports[i].Name, "FileHash", 8) == 0)
  1126. {
  1127. min = 0.0;
  1128. max = 16777215.0; // 0xffffff
  1129. }
  1130. if (max - min == 0.0)
  1131. {
  1132. qWarning("Broken plugin parameter: max - min == 0");
  1133. max = min + 0.1;
  1134. }
  1135. // default value
  1136. if (LV2_HAVE_DEFAULT_PORT_POINT(PortPoints.Hints))
  1137. {
  1138. def = PortPoints.Default;
  1139. }
  1140. else
  1141. {
  1142. // no default value
  1143. if (min < 0.0 && max > 0.0)
  1144. def = 0.0;
  1145. else
  1146. def = min;
  1147. }
  1148. if (def < min)
  1149. def = min;
  1150. else if (def > max)
  1151. def = max;
  1152. if (LV2_IS_PORT_SAMPLE_RATE(PortProps))
  1153. {
  1154. min *= sampleRate;
  1155. max *= sampleRate;
  1156. def *= sampleRate;
  1157. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1158. }
  1159. if (LV2_IS_PORT_TOGGLED(PortProps))
  1160. {
  1161. step = max - min;
  1162. step_small = step;
  1163. step_large = step;
  1164. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1165. }
  1166. else if (LV2_IS_PORT_INTEGER(PortProps))
  1167. {
  1168. step = 1.0;
  1169. step_small = 1.0;
  1170. step_large = 10.0;
  1171. param.data[j].hints |= PARAMETER_IS_INTEGER;
  1172. }
  1173. else
  1174. {
  1175. double range = max - min;
  1176. step = range/100.0;
  1177. step_small = range/1000.0;
  1178. step_large = range/10.0;
  1179. }
  1180. if (LV2_IS_PORT_INPUT(PortType))
  1181. {
  1182. param.data[j].type = PARAMETER_INPUT;
  1183. if (rdf_descriptor->Ports[i].Designation == 0)
  1184. {
  1185. param.data[j].hints |= PARAMETER_IS_ENABLED;
  1186. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1187. needsCin = true;
  1188. }
  1189. // MIDI CC value
  1190. LV2_RDF_PortMidiMap* PortMidiMap = &rdf_descriptor->Ports[i].MidiMap;
  1191. if (LV2_IS_PORT_MIDI_MAP_CC(PortMidiMap->Type))
  1192. {
  1193. if (! MIDI_IS_CONTROL_BANK_SELECT(PortMidiMap->Number))
  1194. param.data[j].midiCC = PortMidiMap->Number;
  1195. }
  1196. }
  1197. else if (LV2_IS_PORT_OUTPUT(PortType))
  1198. {
  1199. if (LV2_IS_PORT_LATENCY(PortDesignation))
  1200. {
  1201. min = 0.0;
  1202. max = sampleRate;
  1203. def = 0.0;
  1204. step = 1.0;
  1205. step_small = 1.0;
  1206. step_large = 1.0;
  1207. param.data[j].type = PARAMETER_LATENCY;
  1208. param.data[j].hints = 0;
  1209. }
  1210. else
  1211. {
  1212. param.data[j].type = PARAMETER_OUTPUT;
  1213. param.data[j].hints |= PARAMETER_IS_ENABLED;
  1214. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1215. needsCout = true;
  1216. }
  1217. }
  1218. else
  1219. {
  1220. param.data[j].type = PARAMETER_UNKNOWN;
  1221. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  1222. }
  1223. // extra parameter hints
  1224. if (LV2_IS_PORT_ENUMERATION(PortProps))
  1225. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1226. if (LV2_IS_PORT_LOGARITHMIC(PortProps))
  1227. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1228. if (LV2_IS_PORT_TRIGGER(PortProps))
  1229. param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1230. if (LV2_IS_PORT_STRICT_BOUNDS(PortProps))
  1231. param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1232. // check if parameter is not enabled or automable
  1233. if (LV2_IS_PORT_NOT_ON_GUI(PortProps))
  1234. param.data[j].hints &= ~PARAMETER_IS_ENABLED;
  1235. if (LV2_IS_PORT_CAUSES_ARTIFACTS(PortProps) || LV2_IS_PORT_EXPENSIVE(PortProps) || LV2_IS_PORT_NOT_AUTOMATIC(PortProps))
  1236. param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1237. param.ranges[j].min = min;
  1238. param.ranges[j].max = max;
  1239. param.ranges[j].def = def;
  1240. param.ranges[j].step = step;
  1241. param.ranges[j].stepSmall = step_small;
  1242. param.ranges[j].stepLarge = step_large;
  1243. // Set LV2 params as needed
  1244. lv2param[j].type = LV2_PARAMETER_TYPE_CONTROL;
  1245. lv2param[j].control = def;
  1246. descriptor->connect_port(handle, i, &lv2param[j].control);
  1247. if (h2) descriptor->connect_port(h2, i, &lv2param[j].control);
  1248. }
  1249. else
  1250. {
  1251. // Port Type not supported, but it's optional anyway
  1252. descriptor->connect_port(handle, i, nullptr);
  1253. if (h2) descriptor->connect_port(h2, i, nullptr);
  1254. }
  1255. }
  1256. if (needsCin)
  1257. {
  1258. #ifndef BUILD_BRIDGE
  1259. if (carlaOptions.process_mode != PROCESS_MODE_MULTIPLE_CLIENTS)
  1260. {
  1261. strcpy(portName, m_name);
  1262. strcat(portName, ":control-in");
  1263. }
  1264. else
  1265. #endif
  1266. strcpy(portName, "control-in");
  1267. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  1268. }
  1269. if (needsCout)
  1270. {
  1271. #ifndef BUILD_BRIDGE
  1272. if (carlaOptions.process_mode != PROCESS_MODE_MULTIPLE_CLIENTS)
  1273. {
  1274. strcpy(portName, m_name);
  1275. strcat(portName, ":control-out");
  1276. }
  1277. else
  1278. #endif
  1279. strcpy(portName, "control-out");
  1280. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  1281. }
  1282. ain.count = ains;
  1283. aout.count = aouts;
  1284. evin.count = evIns.size();
  1285. evout.count = evOuts.size();
  1286. param.count = params;
  1287. // plugin checks
  1288. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE);
  1289. if (LV2_IS_GENERATOR(rdf_descriptor->Type))
  1290. m_hints |= PLUGIN_IS_SYNTH;
  1291. if (aouts > 0 && (ains == aouts || ains == 1))
  1292. m_hints |= PLUGIN_CAN_DRYWET;
  1293. if (aouts > 0)
  1294. m_hints |= PLUGIN_CAN_VOLUME;
  1295. if (aouts >= 2 && aouts%2 == 0)
  1296. m_hints |= PLUGIN_CAN_BALANCE;
  1297. // check extensions
  1298. ext.dynparam = nullptr;
  1299. ext.state = nullptr;
  1300. ext.worker = nullptr;
  1301. ext.programs = nullptr;
  1302. if (descriptor->extension_data)
  1303. {
  1304. if (m_hints & PLUGIN_HAS_EXTENSION_DYNPARAM)
  1305. ext.dynparam = (lv2dynparam_plugin_callbacks*)descriptor->extension_data(LV2DYNPARAM_URI);
  1306. if (m_hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  1307. ext.programs = (LV2_Programs_Interface*)descriptor->extension_data(LV2_PROGRAMS__Interface);
  1308. if (m_hints & PLUGIN_HAS_EXTENSION_STATE)
  1309. ext.state = (LV2_State_Interface*)descriptor->extension_data(LV2_STATE__interface);
  1310. if (m_hints & PLUGIN_HAS_EXTENSION_WORKER)
  1311. ext.worker = (LV2_Worker_Interface*)descriptor->extension_data(LV2_WORKER__interface);
  1312. }
  1313. reloadPrograms(true);
  1314. //if (ext.dynparam)
  1315. // ext.dynparam->host_attach(handle, &dynparam_host, this);
  1316. x_client->activate();
  1317. qDebug("Lv2Plugin::reload() - end");
  1318. }
  1319. void reloadPrograms(bool init)
  1320. {
  1321. qDebug("Lv2Plugin::reloadPrograms(%s)", bool2str(init));
  1322. uint32_t i, oldCount = midiprog.count;
  1323. // Delete old programs
  1324. if (midiprog.count > 0)
  1325. {
  1326. for (uint32_t i=0; i < midiprog.count; i++)
  1327. free((void*)midiprog.data[i].name);
  1328. delete[] midiprog.data;
  1329. }
  1330. midiprog.count = 0;
  1331. midiprog.data = nullptr;
  1332. // Query new programs
  1333. if (ext.programs)
  1334. {
  1335. while (ext.programs->get_program(handle, midiprog.count))
  1336. midiprog.count += 1;
  1337. }
  1338. if (midiprog.count > 0)
  1339. midiprog.data = new midi_program_t[midiprog.count];
  1340. // Update data
  1341. for (i=0; i < midiprog.count; i++)
  1342. {
  1343. const LV2_Program_Descriptor* const pdesc = ext.programs->get_program(handle, i);
  1344. assert(pdesc);
  1345. midiprog.data[i].bank = pdesc->bank;
  1346. midiprog.data[i].program = pdesc->program;
  1347. midiprog.data[i].name = strdup(pdesc->name);
  1348. }
  1349. #ifndef BUILD_BRIDGE
  1350. // Update OSC Names
  1351. //osc_global_send_set_midi_program_count(m_id, midiprog.count);
  1352. //for (i=0; i < midiprog.count; i++)
  1353. // osc_global_send_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1354. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  1355. #endif
  1356. if (init)
  1357. {
  1358. if (midiprog.count > 0)
  1359. setMidiProgram(0, false, false, false, true);
  1360. }
  1361. else
  1362. {
  1363. x_engine->callback(CALLBACK_UPDATE, m_id, 0, 0, 0.0);
  1364. // Check if current program is invalid
  1365. bool programChanged = false;
  1366. if (midiprog.count == oldCount+1)
  1367. {
  1368. // one midi program added, probably created by user
  1369. midiprog.current = oldCount;
  1370. programChanged = true;
  1371. }
  1372. else if (midiprog.current >= (int32_t)midiprog.count)
  1373. {
  1374. // current midi program > count
  1375. midiprog.current = 0;
  1376. programChanged = true;
  1377. }
  1378. else if (midiprog.current < 0 && midiprog.count > 0)
  1379. {
  1380. // programs exist now, but not before
  1381. midiprog.current = 0;
  1382. programChanged = true;
  1383. }
  1384. else if (midiprog.current >= 0 && midiprog.count == 0)
  1385. {
  1386. // programs existed before, but not anymore
  1387. midiprog.current = -1;
  1388. programChanged = true;
  1389. }
  1390. if (programChanged)
  1391. setMidiProgram(midiprog.current, true, true, true, true);
  1392. }
  1393. }
  1394. void prepareForSave()
  1395. {
  1396. if (ext.state)
  1397. ext.state->save(handle, carla_lv2_state_store, this, LV2_STATE_IS_POD, features);
  1398. }
  1399. // -------------------------------------------------------------------
  1400. // Plugin processing
  1401. void process(float** inBuffer, float** outBuffer, uint32_t frames, uint32_t framesOffset)
  1402. {
  1403. uint32_t i, k;
  1404. uint32_t midiEventCount = 0;
  1405. double ains_peak_tmp[2] = { 0.0 };
  1406. double aouts_peak_tmp[2] = { 0.0 };
  1407. // handle midi from different APIs
  1408. uint32_t evinAtomOffsets[evin.count];
  1409. LV2_Event_Iterator evinEventIters[evin.count];
  1410. LV2_MIDIState evinMidiStates[evin.count];
  1411. for (i=0; i < evin.count; i++)
  1412. {
  1413. if (evin.data[i].type & CARLA_EVENT_DATA_ATOM)
  1414. {
  1415. evinAtomOffsets[i] = 0;
  1416. evin.data[i].atom->atom.size = 0;
  1417. evin.data[i].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1418. evin.data[i].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1419. evin.data[i].atom->body.pad = 0;
  1420. }
  1421. else if (evin.data[i].type & CARLA_EVENT_DATA_EVENT)
  1422. {
  1423. lv2_event_buffer_reset(evin.data[i].event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(evin.data[i].event + 1));
  1424. lv2_event_begin(&evinEventIters[i], evin.data[i].event);
  1425. }
  1426. else if (evin.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1427. {
  1428. evinMidiStates[i].midi = evin.data[i].midi;
  1429. evinMidiStates[i].frame_count = frames;
  1430. evinMidiStates[i].position = 0;
  1431. evinMidiStates[i].midi->event_count = 0;
  1432. evinMidiStates[i].midi->size = 0;
  1433. }
  1434. }
  1435. for (i=0; i < evout.count; i++)
  1436. {
  1437. if (evout.data[i].type & CARLA_EVENT_DATA_ATOM)
  1438. {
  1439. evout.data[i].atom->atom.size = 0;
  1440. evout.data[i].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1441. evout.data[i].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1442. evout.data[i].atom->body.pad = 0;
  1443. }
  1444. else if (evout.data[i].type & CARLA_EVENT_DATA_EVENT)
  1445. {
  1446. lv2_event_buffer_reset(evout.data[i].event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(evout.data[i].event + 1));
  1447. }
  1448. else if (evout.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1449. {
  1450. // not needed
  1451. }
  1452. }
  1453. CARLA_PROCESS_CONTINUE_CHECK;
  1454. // --------------------------------------------------------------------------------------------------------
  1455. // Input VU
  1456. if (ain.count > 0)
  1457. {
  1458. uint32_t count = h2 ? 2 : ain.count;
  1459. if (count == 1)
  1460. {
  1461. for (k=0; k < frames; k++)
  1462. {
  1463. if (abs(inBuffer[0][k]) > ains_peak_tmp[0])
  1464. ains_peak_tmp[0] = abs(inBuffer[0][k]);
  1465. }
  1466. }
  1467. else if (count > 1)
  1468. {
  1469. for (k=0; k < frames; k++)
  1470. {
  1471. if (abs(inBuffer[0][k]) > ains_peak_tmp[0])
  1472. ains_peak_tmp[0] = abs(inBuffer[0][k]);
  1473. if (abs(inBuffer[1][k]) > ains_peak_tmp[1])
  1474. ains_peak_tmp[1] = abs(inBuffer[1][k]);
  1475. }
  1476. }
  1477. }
  1478. CARLA_PROCESS_CONTINUE_CHECK;
  1479. // --------------------------------------------------------------------------------------------------------
  1480. // Parameters Input [Automation]
  1481. if (param.portCin && m_active && m_activeBefore)
  1482. {
  1483. bool allNotesOffSent = false;
  1484. const CarlaEngineControlEvent* cinEvent;
  1485. uint32_t time, nEvents = param.portCin->getEventCount();
  1486. uint32_t nextBankId = 0;
  1487. if (midiprog.current >= 0 && midiprog.count > 0)
  1488. nextBankId = midiprog.data[midiprog.current].bank;
  1489. for (i=0; i < nEvents; i++)
  1490. {
  1491. cinEvent = param.portCin->getEvent(i);
  1492. if (! cinEvent)
  1493. continue;
  1494. time = cinEvent->time - framesOffset;
  1495. if (time >= frames)
  1496. continue;
  1497. // Control change
  1498. switch (cinEvent->type)
  1499. {
  1500. case CarlaEngineEventNull:
  1501. break;
  1502. case CarlaEngineEventControlChange:
  1503. {
  1504. double value;
  1505. // Control backend stuff
  1506. if (cinEvent->channel == cin_channel)
  1507. {
  1508. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  1509. {
  1510. value = cinEvent->value;
  1511. setDryWet(value, false, false);
  1512. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  1513. continue;
  1514. }
  1515. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  1516. {
  1517. value = cinEvent->value*127/100;
  1518. setVolume(value, false, false);
  1519. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  1520. continue;
  1521. }
  1522. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  1523. {
  1524. double left, right;
  1525. value = cinEvent->value/0.5 - 1.0;
  1526. if (value < 0)
  1527. {
  1528. left = -1.0;
  1529. right = (value*2)+1.0;
  1530. }
  1531. else if (value > 0)
  1532. {
  1533. left = (value*2)-1.0;
  1534. right = 1.0;
  1535. }
  1536. else
  1537. {
  1538. left = -1.0;
  1539. right = 1.0;
  1540. }
  1541. setBalanceLeft(left, false, false);
  1542. setBalanceRight(right, false, false);
  1543. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1544. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1545. continue;
  1546. }
  1547. }
  1548. // Control plugin parameters
  1549. for (k=0; k < param.count; k++)
  1550. {
  1551. if (param.data[k].midiChannel != cinEvent->channel)
  1552. continue;
  1553. if (param.data[k].midiCC != cinEvent->controller)
  1554. continue;
  1555. if (param.data[k].type != PARAMETER_INPUT)
  1556. continue;
  1557. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  1558. {
  1559. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1560. {
  1561. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  1562. }
  1563. else
  1564. {
  1565. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  1566. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  1567. value = rint(value);
  1568. }
  1569. setParameterValue(k, value, false, false, false);
  1570. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  1571. }
  1572. }
  1573. break;
  1574. }
  1575. case CarlaEngineEventMidiBankChange:
  1576. if (cinEvent->channel == cin_channel)
  1577. nextBankId = rint(cinEvent->value);
  1578. break;
  1579. case CarlaEngineEventMidiProgramChange:
  1580. if (cinEvent->channel == cin_channel)
  1581. {
  1582. uint32_t nextProgramId = rint(cinEvent->value);
  1583. for (k=0; k < midiprog.count; k++)
  1584. {
  1585. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  1586. {
  1587. setMidiProgram(k, false, false, false, false);
  1588. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  1589. break;
  1590. }
  1591. }
  1592. }
  1593. break;
  1594. case CarlaEngineEventAllSoundOff:
  1595. if (cinEvent->channel == cin_channel)
  1596. {
  1597. if (midi.portMin && ! allNotesOffSent)
  1598. sendMidiAllNotesOff();
  1599. if (descriptor->deactivate)
  1600. {
  1601. descriptor->deactivate(handle);
  1602. if (h2) descriptor->deactivate(h2);
  1603. }
  1604. if (descriptor->activate)
  1605. {
  1606. descriptor->activate(handle);
  1607. if (h2) descriptor->activate(h2);
  1608. }
  1609. allNotesOffSent = true;
  1610. }
  1611. break;
  1612. case CarlaEngineEventAllNotesOff:
  1613. if (cinEvent->channel == cin_channel)
  1614. {
  1615. if (midi.portMin && ! allNotesOffSent)
  1616. sendMidiAllNotesOff();
  1617. allNotesOffSent = true;
  1618. }
  1619. break;
  1620. }
  1621. }
  1622. } // End of Parameters Input
  1623. CARLA_PROCESS_CONTINUE_CHECK;
  1624. // --------------------------------------------------------------------------------------------------------
  1625. // MIDI Input (External)
  1626. if (evin.count > 0 && cin_channel >= 0 && cin_channel < 16 && m_active && m_activeBefore)
  1627. {
  1628. engineMidiLock();
  1629. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  1630. {
  1631. if (extMidiNotes[i].channel < 0)
  1632. break;
  1633. uint8_t midiEvent[4] = { 0 };
  1634. midiEvent[0] = cin_channel + extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1635. midiEvent[1] = extMidiNotes[i].note;
  1636. midiEvent[2] = extMidiNotes[i].velo;
  1637. // send to all midi inputs
  1638. for (k=0; k < evin.count; k++)
  1639. {
  1640. if (evin.data[k].type & CARLA_EVENT_TYPE_MIDI)
  1641. {
  1642. if (evin.data[k].type & CARLA_EVENT_DATA_ATOM)
  1643. {
  1644. LV2_Atom_Event* const aev = (LV2_Atom_Event*)((char*)LV2_ATOM_CONTENTS(LV2_Atom_Sequence, evin.data[k].atom) + evinAtomOffsets[k]);
  1645. aev->time.frames = 0;
  1646. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1647. aev->body.size = 3;
  1648. memcpy(LV2_ATOM_BODY(&aev->body), midiEvent, 3);
  1649. const uint32_t padSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 3);
  1650. evinAtomOffsets[k] += padSize;
  1651. evin.data[k].atom->atom.size += padSize;
  1652. }
  1653. else if (evin.data[k].type & CARLA_EVENT_DATA_EVENT)
  1654. lv2_event_write(&evinEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  1655. else if (evin.data[k].type & CARLA_EVENT_DATA_MIDI_LL)
  1656. lv2midi_put_event(&evinMidiStates[k], 0, 3, midiEvent);
  1657. }
  1658. }
  1659. extMidiNotes[i].channel = -1;
  1660. midiEventCount += 1;
  1661. }
  1662. engineMidiUnlock();
  1663. } // End of MIDI Input (External)
  1664. CARLA_PROCESS_CONTINUE_CHECK;
  1665. // --------------------------------------------------------------------------------------------------------
  1666. // MIDI Input (System)
  1667. if (evin.count > 0 && m_active && m_activeBefore)
  1668. {
  1669. for (i=0; i < evin.count; i++)
  1670. {
  1671. if (! evin.data[i].port)
  1672. continue;
  1673. const CarlaEngineMidiEvent* minEvent;
  1674. uint32_t time, nEvents = evin.data[i].port->getEventCount();
  1675. for (k=0; k < nEvents && midiEventCount < MAX_MIDI_EVENTS; k++)
  1676. {
  1677. minEvent = evin.data[i].port->getEvent(k);
  1678. if (! minEvent)
  1679. continue;
  1680. time = minEvent->time - framesOffset;
  1681. if (time >= frames)
  1682. continue;
  1683. uint8_t status = minEvent->data[0];
  1684. uint8_t channel = status & 0x0F;
  1685. // Fix bad note-off
  1686. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  1687. status -= 0x10;
  1688. // write supported status types
  1689. 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))
  1690. {
  1691. if (evin.data[i].type & CARLA_EVENT_DATA_ATOM)
  1692. {
  1693. LV2_Atom_Event* aev = (LV2_Atom_Event*)((char*)LV2_ATOM_CONTENTS(LV2_Atom_Sequence, evin.data[i].atom) + evinAtomOffsets[i]);
  1694. aev->time.frames = time;
  1695. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1696. aev->body.size = minEvent->size;
  1697. memcpy(LV2_ATOM_BODY(&aev->body), minEvent->data, minEvent->size);
  1698. const uint32_t padSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + minEvent->size);
  1699. evinAtomOffsets[i] += padSize;
  1700. evin.data[i].atom->atom.size += padSize;
  1701. }
  1702. else if (evin.data[i].type & CARLA_EVENT_DATA_EVENT)
  1703. lv2_event_write(&evinEventIters[i], time, 0, CARLA_URI_MAP_ID_MIDI_EVENT, minEvent->size, minEvent->data);
  1704. else if (evin.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1705. lv2midi_put_event(&evinMidiStates[i], time, minEvent->size, minEvent->data);
  1706. if (channel == cin_channel)
  1707. {
  1708. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1709. postponeEvent(PluginPostEventNoteOff, channel, minEvent->data[1], 0.0);
  1710. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1711. postponeEvent(PluginPostEventNoteOn, channel, minEvent->data[1], minEvent->data[2]);
  1712. }
  1713. }
  1714. midiEventCount += 1;
  1715. }
  1716. }
  1717. }// End of MIDI Input (System)
  1718. CARLA_PROCESS_CONTINUE_CHECK;
  1719. // --------------------------------------------------------------------------------------------------------
  1720. // Special Parameters
  1721. int32_t rindex;
  1722. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  1723. for (k=0; k < param.count; k++)
  1724. {
  1725. if (param.data[k].type == PARAMETER_LATENCY)
  1726. {
  1727. // TODO
  1728. continue;
  1729. }
  1730. rindex = param.data[k].rindex;
  1731. switch (rdf_descriptor->Ports[rindex].Designation)
  1732. {
  1733. case LV2_PORT_TIME_BAR:
  1734. setParameterValue(k, timeInfo->bbt.bar, false, false, false);
  1735. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->bbt.bar);
  1736. break;
  1737. case LV2_PORT_TIME_BAR_BEAT:
  1738. setParameterValue(k, timeInfo->bbt.tick, false, false, false);
  1739. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->bbt.tick);
  1740. break;
  1741. case LV2_PORT_TIME_BEAT:
  1742. setParameterValue(k, timeInfo->bbt.beat, false, false, false);
  1743. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->bbt.beat);
  1744. break;
  1745. case LV2_PORT_TIME_BEAT_UNIT:
  1746. setParameterValue(k, timeInfo->bbt.beat_type, false, false, false);
  1747. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->bbt.beat_type);
  1748. break;
  1749. case LV2_PORT_TIME_BEATS_PER_BAR:
  1750. setParameterValue(k, timeInfo->bbt.beats_per_bar, false, false, false);
  1751. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->bbt.beats_per_bar);
  1752. break;
  1753. case LV2_PORT_TIME_BEATS_PER_MINUTE:
  1754. setParameterValue(k, timeInfo->bbt.beats_per_minute, false, false, false);
  1755. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->bbt.beats_per_minute);
  1756. break;
  1757. case LV2_PORT_TIME_FRAME:
  1758. setParameterValue(k, timeInfo->frame, false, false, false);
  1759. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->frame);
  1760. break;
  1761. case LV2_PORT_TIME_FRAMES_PER_SECOND:
  1762. break;
  1763. case LV2_PORT_TIME_POSITION:
  1764. setParameterValue(k, timeInfo->time, false, false, false);
  1765. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->time);
  1766. break;
  1767. case LV2_PORT_TIME_SPEED:
  1768. setParameterValue(k, timeInfo->playing ? 1.0 : 0.0, false, false, false);
  1769. postponeEvent(PluginPostEventParameterChange, k, 0, timeInfo->playing ? 1.0 : 0.0);
  1770. break;
  1771. }
  1772. }
  1773. CARLA_PROCESS_CONTINUE_CHECK;
  1774. // --------------------------------------------------------------------------------------------------------
  1775. // Plugin processing
  1776. if (m_active)
  1777. {
  1778. if (! m_activeBefore)
  1779. {
  1780. // TODO - send sound-off notes-off events here
  1781. if (descriptor->activate)
  1782. {
  1783. descriptor->activate(handle);
  1784. if (h2) descriptor->activate(h2);
  1785. }
  1786. }
  1787. for (i=0; i < ain.count; i++)
  1788. {
  1789. if (i == 0 || ! h2) descriptor->connect_port(handle, ain.rindexes[i], inBuffer[i]);
  1790. if (h2 && i == 1) descriptor->connect_port(h2, ain.rindexes[i], inBuffer[1]);
  1791. }
  1792. for (i=0; i < aout.count; i++)
  1793. {
  1794. if (i == 0 || ! h2) descriptor->connect_port(handle, aout.rindexes[i], outBuffer[i]);
  1795. if (h2 && i == 1) descriptor->connect_port(h2, aout.rindexes[i], outBuffer[1]);
  1796. }
  1797. descriptor->run(handle, frames);
  1798. if (h2) descriptor->run(h2, frames);
  1799. }
  1800. else
  1801. {
  1802. if (m_activeBefore)
  1803. {
  1804. if (descriptor->deactivate)
  1805. {
  1806. descriptor->deactivate(handle);
  1807. if (h2) descriptor->deactivate(h2);
  1808. }
  1809. }
  1810. }
  1811. CARLA_PROCESS_CONTINUE_CHECK;
  1812. // --------------------------------------------------------------------------------------------------------
  1813. // Post-processing (dry/wet, volume and balance)
  1814. if (m_active)
  1815. {
  1816. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_drywet != 1.0;
  1817. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_vol != 1.0;
  1818. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_bal_left != -1.0 || x_bal_right != 1.0);
  1819. double bal_rangeL, bal_rangeR;
  1820. float oldBufLeft[do_balance ? frames : 0];
  1821. uint32_t count = h2 ? 2 : aout.count;
  1822. for (i=0; i < count; i++)
  1823. {
  1824. // Dry/Wet and Volume
  1825. if (do_drywet || do_volume)
  1826. {
  1827. for (k=0; k < frames; k++)
  1828. {
  1829. if (do_drywet)
  1830. {
  1831. if (aout.count == 1 && ! h2)
  1832. outBuffer[i][k] = (outBuffer[i][k]*x_drywet)+(inBuffer[0][k]*(1.0-x_drywet));
  1833. else
  1834. outBuffer[i][k] = (outBuffer[i][k]*x_drywet)+(inBuffer[i][k]*(1.0-x_drywet));
  1835. }
  1836. if (do_volume)
  1837. outBuffer[i][k] *= x_vol;
  1838. }
  1839. }
  1840. // Balance
  1841. if (do_balance)
  1842. {
  1843. if (i%2 == 0)
  1844. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1845. bal_rangeL = (x_bal_left+1.0)/2;
  1846. bal_rangeR = (x_bal_right+1.0)/2;
  1847. for (k=0; k < frames; k++)
  1848. {
  1849. if (i%2 == 0)
  1850. {
  1851. // left output
  1852. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1853. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1854. }
  1855. else
  1856. {
  1857. // right
  1858. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1859. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1860. }
  1861. }
  1862. }
  1863. // Output VU
  1864. for (k=0; i < 2 && k < frames; k++)
  1865. {
  1866. if (abs(outBuffer[i][k]) > aouts_peak_tmp[i])
  1867. aouts_peak_tmp[i] = abs(outBuffer[i][k]);
  1868. }
  1869. }
  1870. }
  1871. else
  1872. {
  1873. // disable any output sound if not active
  1874. for (i=0; i < aout.count; i++)
  1875. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  1876. aouts_peak_tmp[0] = 0.0;
  1877. aouts_peak_tmp[1] = 0.0;
  1878. } // End of Post-processing
  1879. CARLA_PROCESS_CONTINUE_CHECK;
  1880. // --------------------------------------------------------------------------------------------------------
  1881. // Control Output
  1882. if (param.portCout && m_active)
  1883. {
  1884. double value, rvalue;
  1885. for (k=0; k < param.count; k++)
  1886. {
  1887. if (param.data[k].type == PARAMETER_OUTPUT)
  1888. {
  1889. if (lv2param[k].type == LV2_PARAMETER_TYPE_CONTROL)
  1890. fixParameterValue(lv2param[k].control, param.ranges[k]);
  1891. if (param.data[k].midiCC > 0)
  1892. {
  1893. switch (lv2param[k].type)
  1894. {
  1895. case LV2_PARAMETER_TYPE_CONTROL:
  1896. value = lv2param[k].control;
  1897. break;
  1898. default:
  1899. value = param.ranges[k].min;
  1900. break;
  1901. }
  1902. rvalue = (value - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1903. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, rvalue);
  1904. }
  1905. }
  1906. }
  1907. } // End of Control Output
  1908. CARLA_PROCESS_CONTINUE_CHECK;
  1909. // --------------------------------------------------------------------------------------------------------
  1910. // MIDI Output
  1911. if (evout.count > 0 && m_active)
  1912. {
  1913. for (i=0; i < evout.count; i++)
  1914. {
  1915. if (! evout.data[i].port)
  1916. continue;
  1917. if (evin.data[i].type & CARLA_EVENT_DATA_ATOM)
  1918. {
  1919. // TODO
  1920. }
  1921. else if (evin.data[i].type & CARLA_EVENT_DATA_EVENT)
  1922. {
  1923. LV2_Event* ev;
  1924. LV2_Event_Iterator iter;
  1925. uint8_t* data;
  1926. lv2_event_begin(&iter, evout.data[i].event);
  1927. for (k=0; k < iter.buf->event_count; k++)
  1928. {
  1929. ev = lv2_event_get(&iter, &data);
  1930. if (ev && data)
  1931. evout.data[i].port->writeEvent(ev->frames, data, ev->size);
  1932. lv2_event_increment(&iter);
  1933. }
  1934. }
  1935. else if (evin.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1936. {
  1937. LV2_MIDIState state = { evout.data[i].midi, frames, 0 };
  1938. uint32_t eventSize;
  1939. double eventTime;
  1940. unsigned char* eventData;
  1941. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  1942. {
  1943. evout.data[i].port->writeEvent(eventTime, eventData, eventSize);
  1944. lv2midi_step(&state);
  1945. }
  1946. }
  1947. }
  1948. }// End of MIDI Output
  1949. CARLA_PROCESS_CONTINUE_CHECK;
  1950. // --------------------------------------------------------------------------------------------------------
  1951. // Peak Values
  1952. x_engine->setInputPeak(m_id, 0, ains_peak_tmp[0]);
  1953. x_engine->setInputPeak(m_id, 1, ains_peak_tmp[1]);
  1954. x_engine->setOutputPeak(m_id, 0, aouts_peak_tmp[0]);
  1955. x_engine->setOutputPeak(m_id, 1, aouts_peak_tmp[1]);
  1956. m_activeBefore = m_active;
  1957. }
  1958. // -------------------------------------------------------------------
  1959. // Cleanup
  1960. void removeClientPorts()
  1961. {
  1962. qDebug("Lv2Plugin::removeClientPorts() - start");
  1963. for (uint32_t i=0; i < evin.count; i++)
  1964. {
  1965. if (evin.data[i].port)
  1966. {
  1967. delete evin.data[i].port;
  1968. evin.data[i].port = nullptr;
  1969. }
  1970. }
  1971. for (uint32_t i=0; i < evout.count; i++)
  1972. {
  1973. if (evout.data[i].port)
  1974. {
  1975. delete evout.data[i].port;
  1976. evout.data[i].port = nullptr;
  1977. }
  1978. }
  1979. CarlaPlugin::removeClientPorts();
  1980. qDebug("Lv2Plugin::removeClientPorts() - end");
  1981. }
  1982. void initBuffers()
  1983. {
  1984. uint32_t i;
  1985. for (i=0; i < evin.count; i++)
  1986. {
  1987. if (evin.data[i].port)
  1988. evin.data[i].port->initBuffer(x_engine);
  1989. }
  1990. for (uint32_t i=0; i < evout.count; i++)
  1991. {
  1992. if (evout.data[i].port)
  1993. evout.data[i].port->initBuffer(x_engine);
  1994. }
  1995. CarlaPlugin::initBuffers();
  1996. }
  1997. void deleteBuffers()
  1998. {
  1999. qDebug("Lv2Plugin::deleteBuffers() - start");
  2000. if (param.count > 0)
  2001. delete[] lv2param;
  2002. if (evin.count > 0)
  2003. {
  2004. for (uint32_t i=0; i < evin.count; i++)
  2005. {
  2006. if (evin.data[i].type & CARLA_EVENT_DATA_ATOM)
  2007. {
  2008. free(evin.data[i].atom);
  2009. }
  2010. else if (evin.data[i].type & CARLA_EVENT_DATA_EVENT)
  2011. {
  2012. free(evin.data[i].event);
  2013. }
  2014. else if (evin.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2015. {
  2016. delete[] evin.data[i].midi->data;
  2017. delete evin.data[i].midi;
  2018. }
  2019. }
  2020. delete[] evin.data;
  2021. }
  2022. if (evout.count > 0)
  2023. {
  2024. for (uint32_t i=0; i < evout.count; i++)
  2025. {
  2026. if (evout.data[i].type & CARLA_EVENT_DATA_ATOM)
  2027. {
  2028. free(evout.data[i].atom);
  2029. }
  2030. else if (evout.data[i].type & CARLA_EVENT_DATA_EVENT)
  2031. {
  2032. free(evout.data[i].event);
  2033. }
  2034. else if (evout.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2035. {
  2036. delete[] evout.data[i].midi->data;
  2037. delete evout.data[i].midi;
  2038. }
  2039. }
  2040. delete[] evout.data;
  2041. }
  2042. lv2param = nullptr;
  2043. evin.count = 0;
  2044. evin.data = nullptr;
  2045. evout.count = 0;
  2046. evout.data = nullptr;
  2047. qDebug("Lv2Plugin::deleteBuffers() - end");
  2048. }
  2049. // -------------------------------------------------------------------
  2050. uint32_t getCustomURID(const char* const uri)
  2051. {
  2052. qDebug("Lv2Plugin::getCustomURID(%s)", uri);
  2053. for (size_t i=0; i < customURIDs.size(); i++)
  2054. {
  2055. if (customURIDs[i] && strcmp(customURIDs[i], uri) == 0)
  2056. return i;
  2057. }
  2058. customURIDs.push_back(strdup(uri));
  2059. return customURIDs.size()-1;
  2060. }
  2061. const char* getCustomURIString(LV2_URID urid)
  2062. {
  2063. qDebug("Lv2Plugin::getCustomURIString(%i)", urid);
  2064. if (urid < customURIDs.size())
  2065. return customURIDs[urid];
  2066. return nullptr;
  2067. }
  2068. void handleAtomTransfer()
  2069. {
  2070. // TODO
  2071. }
  2072. void handleEventTransfer(const char* const type, const char* const key, const char* const stringData)
  2073. {
  2074. qDebug("Lv2Plugin::handleEventTransfer(%s, %s, %s)", type, key, stringData);
  2075. assert(type);
  2076. assert(key);
  2077. assert(stringData);
  2078. QByteArray chunk;
  2079. chunk = QByteArray::fromBase64(stringData);
  2080. const LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  2081. const LV2_URID uridAtomBlank = getCustomURID(LV2_ATOM__Blank);
  2082. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  2083. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  2084. if (atom->type != uridAtomBlank)
  2085. {
  2086. qWarning("Lv2Plugin::handleEventTransfer() - Not blank");
  2087. return;
  2088. }
  2089. const LV2_Atom_Object* const obj = (LV2_Atom_Object*)atom;
  2090. if (obj->body.otype != uridPatchSet)
  2091. {
  2092. qWarning("Lv2Plugin::handleEventTransfer() - Not Patch Set");
  2093. return;
  2094. }
  2095. const LV2_Atom_Object* body = nullptr;
  2096. lv2_atom_object_get(obj, uridPatchBody, &body, 0);
  2097. if (! body)
  2098. {
  2099. qWarning("Lv2Plugin::handleEventTransfer() - Has no body");
  2100. return;
  2101. }
  2102. LV2_ATOM_OBJECT_FOREACH(body, iter)
  2103. {
  2104. CustomDataType dtype = CUSTOM_DATA_INVALID;
  2105. const char* const key = getCustomURIString(iter->key);
  2106. const char* value = nullptr;
  2107. if (iter->value.type == CARLA_URI_MAP_ID_ATOM_STRING || iter->value.type == CARLA_URI_MAP_ID_ATOM_PATH)
  2108. {
  2109. value = strdup((const char*)LV2_ATOM_BODY(&iter->value));
  2110. dtype = (iter->value.type == CARLA_URI_MAP_ID_ATOM_STRING) ? CUSTOM_DATA_STRING : CUSTOM_DATA_PATH;
  2111. }
  2112. else if (iter->value.type == CARLA_URI_MAP_ID_ATOM_CHUNK || iter->value.type >= CARLA_URI_MAP_ID_COUNT)
  2113. {
  2114. QByteArray chunk((const char*)LV2_ATOM_BODY(&iter->value), iter->value.size);
  2115. value = strdup(chunk.toBase64().constData());
  2116. dtype = (iter->value.type == CARLA_URI_MAP_ID_ATOM_CHUNK) ? CUSTOM_DATA_CHUNK : CUSTOM_DATA_BINARY; // FIXME - binary should be a custom type
  2117. }
  2118. else
  2119. value = strdup("");
  2120. setCustomData(dtype, key, value, false);
  2121. free((void*)value);
  2122. }
  2123. }
  2124. void handleProgramChanged(int32_t index)
  2125. {
  2126. if (index == -1)
  2127. {
  2128. const CarlaPluginScopedDisabler m(this);
  2129. reloadPrograms(false);
  2130. }
  2131. else
  2132. {
  2133. if (ext.programs && index >= 0 && index < (int32_t)prog.count)
  2134. {
  2135. const char* progName = ext.programs->get_program(handle, index)->name;
  2136. if (prog.names[index])
  2137. free((void*)prog.names[index]);
  2138. prog.names[index] = strdup(progName);
  2139. }
  2140. }
  2141. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  2142. }
  2143. LV2_State_Status handleStateStore(uint32_t key, const void* const value, size_t size, uint32_t type, uint32_t flags)
  2144. {
  2145. assert(key > 0);
  2146. assert(value);
  2147. CustomDataType dtype;
  2148. const char* const uriKey = getCustomURIString(key);
  2149. if (type == CARLA_URI_MAP_ID_ATOM_CHUNK)
  2150. dtype = CUSTOM_DATA_CHUNK;
  2151. else if (type == CARLA_URI_MAP_ID_ATOM_PATH)
  2152. dtype = CUSTOM_DATA_PATH;
  2153. else if (type == CARLA_URI_MAP_ID_ATOM_STRING)
  2154. dtype = CUSTOM_DATA_STRING;
  2155. else if (type >= CARLA_URI_MAP_ID_COUNT)
  2156. dtype = CUSTOM_DATA_BINARY;
  2157. else
  2158. dtype = CUSTOM_DATA_INVALID;
  2159. // do basic checks
  2160. if (! uriKey)
  2161. {
  2162. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - Invalid key", key, value, size, type, flags);
  2163. return LV2_STATE_ERR_NO_PROPERTY;
  2164. }
  2165. if (! flags & LV2_STATE_IS_POD)
  2166. {
  2167. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - Invalid flags", key, value, size, type, flags);
  2168. return LV2_STATE_ERR_BAD_FLAGS;
  2169. }
  2170. if (dtype == CUSTOM_DATA_INVALID)
  2171. {
  2172. qCritical("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - Invalid type '%s'", key, value, size, type, flags, CustomDataType2str(dtype));
  2173. return LV2_STATE_ERR_BAD_TYPE;
  2174. }
  2175. // Check if we already have this key
  2176. for (size_t i=0; i < custom.size(); i++)
  2177. {
  2178. if (strcmp(custom[i].key, uriKey) == 0)
  2179. {
  2180. free((void*)custom[i].value);
  2181. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2182. custom[i].value = strdup((const char*)value);
  2183. else
  2184. custom[i].value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2185. return LV2_STATE_SUCCESS;
  2186. }
  2187. }
  2188. // Add a new one then
  2189. CustomData newData;
  2190. newData.type = dtype;
  2191. newData.key = strdup(uriKey);
  2192. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2193. newData.value = strdup((const char*)value);
  2194. else
  2195. newData.value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2196. custom.push_back(newData);
  2197. return LV2_STATE_SUCCESS;
  2198. }
  2199. const void* handleStateRetrieve(uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  2200. {
  2201. assert(key > 0);
  2202. const char* const uriKey = getCustomURIString(key);
  2203. if (! uriKey)
  2204. {
  2205. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - Failed to find key", key, size, type, flags);
  2206. return nullptr;
  2207. }
  2208. const char* stringData = nullptr;
  2209. CustomDataType dtype = CUSTOM_DATA_INVALID;
  2210. for (size_t i=0; i < custom.size(); i++)
  2211. {
  2212. if (strcmp(custom[i].key, uriKey) == 0)
  2213. {
  2214. dtype = custom[i].type;
  2215. stringData = custom[i].value;
  2216. break;
  2217. }
  2218. }
  2219. if (! stringData)
  2220. {
  2221. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - Invalid key '%s'", key, size, type, flags, uriKey);
  2222. return nullptr;
  2223. }
  2224. *size = 0;
  2225. *type = 0;
  2226. *flags = LV2_STATE_IS_POD;
  2227. if (dtype == CUSTOM_DATA_STRING)
  2228. {
  2229. *size = strlen(stringData);
  2230. *type = CARLA_URI_MAP_ID_ATOM_STRING;
  2231. return stringData;
  2232. }
  2233. if (dtype == CUSTOM_DATA_PATH)
  2234. {
  2235. *size = strlen(stringData);
  2236. *type = CARLA_URI_MAP_ID_ATOM_PATH;
  2237. return stringData;
  2238. }
  2239. if (dtype == CUSTOM_DATA_CHUNK || dtype == CUSTOM_DATA_BINARY)
  2240. {
  2241. static QByteArray chunk;
  2242. chunk = QByteArray::fromBase64(stringData);
  2243. *size = chunk.size();
  2244. *type = (dtype == CUSTOM_DATA_CHUNK) ? CARLA_URI_MAP_ID_ATOM_CHUNK : key;
  2245. return chunk.constData();
  2246. }
  2247. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - Invalid key type '%s'", key, size, type, flags, CustomDataType2str(dtype));
  2248. return nullptr;
  2249. }
  2250. LV2_Worker_Status handleWorkerSchedule(uint32_t /*size*/, const void* const /*data*/)
  2251. {
  2252. return LV2_WORKER_SUCCESS;
  2253. }
  2254. LV2_Worker_Status handleWorkerRespond(uint32_t /*size*/, const void* const /*data*/)
  2255. {
  2256. if (ext.worker)
  2257. return LV2_WORKER_SUCCESS;
  2258. return LV2_WORKER_ERR_UNKNOWN;
  2259. }
  2260. void handleExternalUiClosed()
  2261. {
  2262. if (ui.handle && ui.descriptor && ui.descriptor->cleanup)
  2263. ui.descriptor->cleanup(ui.handle);
  2264. ui.handle = nullptr;
  2265. x_engine->callback(CALLBACK_SHOW_GUI, m_id, 0, 0, 0.0);
  2266. }
  2267. uint32_t handleUiPortMap(const char* const symbol)
  2268. {
  2269. assert(symbol);
  2270. for (uint32_t i=0; i < rdf_descriptor->PortCount; i++)
  2271. {
  2272. if (strcmp(rdf_descriptor->Ports[i].Symbol, symbol) == 0)
  2273. return i;
  2274. }
  2275. return LV2UI_INVALID_PORT_INDEX;
  2276. }
  2277. int handleUiResize(int width, int height)
  2278. {
  2279. assert(width > 0);
  2280. assert(height > 0);
  2281. gui.width = width;
  2282. gui.height = height;
  2283. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 0.0);
  2284. return 0;
  2285. }
  2286. void handleUiWrite(uint32_t rindex, uint32_t bufferSize, uint32_t format, const void* buffer)
  2287. {
  2288. if (format == 0)
  2289. {
  2290. assert(bufferSize == sizeof(float));
  2291. float value = *(float*)buffer;
  2292. for (uint32_t i=0; i < param.count; i++)
  2293. {
  2294. if (param.data[i].rindex == (int32_t)rindex)
  2295. return setParameterValue(i, value, false, true, true);
  2296. }
  2297. }
  2298. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2299. {
  2300. // TODO
  2301. //LV2_Atom* atom = (LV2_Atom*)buffer;
  2302. //QByteArray chunk((const char*)buffer, buffer_size);
  2303. }
  2304. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2305. {
  2306. const LV2_Atom* const atom = (LV2_Atom*)buffer;
  2307. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2308. ui.descriptor->port_event(ui.handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  2309. }
  2310. }
  2311. // -------------------------------------------------------------------
  2312. #ifndef BUILD_BRIDGE
  2313. bool isUiBridgeable(uint32_t uiId)
  2314. {
  2315. const LV2_RDF_UI* const rdf_ui = &rdf_descriptor->UIs[uiId];
  2316. for (uint32_t i=0; i < rdf_ui->FeatureCount; i++)
  2317. {
  2318. if (strcmp(rdf_ui->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0 || strcmp(rdf_ui->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  2319. return false;
  2320. }
  2321. return true;
  2322. }
  2323. #endif
  2324. bool isUiResizable()
  2325. {
  2326. if (ui.rdf_descriptor)
  2327. {
  2328. for (uint32_t i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  2329. {
  2330. if (strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  2331. return false;
  2332. }
  2333. return true;
  2334. }
  2335. return false;
  2336. }
  2337. void initExternalUi()
  2338. {
  2339. qDebug("Lv2Plugin::initExternalUi()");
  2340. ui.widget = nullptr;
  2341. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  2342. if (ui.handle && ui.widget)
  2343. {
  2344. updateUi();
  2345. }
  2346. else
  2347. {
  2348. ui.handle = nullptr;
  2349. ui.widget = nullptr;
  2350. x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0);
  2351. }
  2352. }
  2353. void updateUi()
  2354. {
  2355. qDebug("Lv2Plugin::updateUi()");
  2356. ext.uiprograms = nullptr;
  2357. if (ui.handle && ui.descriptor)
  2358. {
  2359. if (ui.descriptor->extension_data)
  2360. {
  2361. ext.uiprograms = (LV2_Programs_UI_Interface*)ui.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  2362. if (ext.uiprograms && midiprog.count > 0 && midiprog.current >= 0)
  2363. ext.uiprograms->select_program(ui.handle, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  2364. }
  2365. if (ui.descriptor->port_event)
  2366. {
  2367. // update state (custom data)
  2368. for (size_t i=0; i < custom.size(); i++)
  2369. {
  2370. if (custom[i].type == CUSTOM_DATA_INVALID)
  2371. continue;
  2372. LV2_URID_Map* const URID_Map = (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  2373. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  2374. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  2375. Sratom* sratom = sratom_new(URID_Map);
  2376. SerdChunk chunk = { nullptr, 0 };
  2377. LV2_Atom_Forge forge;
  2378. lv2_atom_forge_init(&forge, URID_Map);
  2379. lv2_atom_forge_set_sink(&forge, sratom_forge_sink, sratom_forge_deref, &chunk);
  2380. LV2_Atom_Forge_Frame refFrame, bodyFrame;
  2381. LV2_Atom_Forge_Ref ref = lv2_atom_forge_blank(&forge, &refFrame, 1, uridPatchSet);
  2382. lv2_atom_forge_property_head(&forge, uridPatchBody, CARLA_URI_MAP_ID_NULL);
  2383. lv2_atom_forge_blank(&forge, &bodyFrame, 2, CARLA_URI_MAP_ID_NULL);
  2384. lv2_atom_forge_property_head(&forge, getCustomURID(custom[i].key), CARLA_URI_MAP_ID_NULL);
  2385. if (custom[i].type == CUSTOM_DATA_STRING)
  2386. lv2_atom_forge_string(&forge, custom[i].value, strlen(custom[i].value));
  2387. else if (custom[i].type == CUSTOM_DATA_PATH)
  2388. lv2_atom_forge_path(&forge, custom[i].value, strlen(custom[i].value));
  2389. else if (custom[i].type == CUSTOM_DATA_CHUNK)
  2390. lv2_atom_forge_literal(&forge, custom[i].value, strlen(custom[i].value), CARLA_URI_MAP_ID_ATOM_CHUNK, CARLA_URI_MAP_ID_NULL);
  2391. else
  2392. lv2_atom_forge_literal(&forge, custom[i].value, strlen(custom[i].value), getCustomURID(custom[i].key), CARLA_URI_MAP_ID_NULL);
  2393. lv2_atom_forge_pop(&forge, &bodyFrame);
  2394. lv2_atom_forge_pop(&forge, &refFrame);
  2395. const LV2_Atom* const atom = lv2_atom_forge_deref(&forge, ref);
  2396. ui.descriptor->port_event(ui.handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  2397. free((void*)chunk.buf);
  2398. sratom_free(sratom);
  2399. }
  2400. // update control ports
  2401. float value;
  2402. for (uint32_t i=0; i < param.count; i++)
  2403. {
  2404. value = getParameterValue(i);
  2405. ui.descriptor->port_event(ui.handle, param.data[i].rindex, sizeof(float), 0, &value);
  2406. }
  2407. }
  2408. }
  2409. }
  2410. // ----------------- DynParam Feature ------------------------------------------------
  2411. // TODO
  2412. // ----------------- Event Feature ---------------------------------------------------
  2413. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  2414. {
  2415. qDebug("Lv2Plugin::carla_lv2_event_ref(%p, %p)", callback_data, event);
  2416. assert(callback_data);
  2417. assert(event);
  2418. return 0;
  2419. }
  2420. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  2421. {
  2422. qDebug("Lv2Plugin::carla_lv2_event_unref(%p, %p)", callback_data, event);
  2423. assert(callback_data);
  2424. assert(event);
  2425. return 0;
  2426. }
  2427. // ----------------- Logs Feature ----------------------------------------------------
  2428. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  2429. {
  2430. qDebug("Lv2Plugin::carla_lv2_log_printf(%p, %i, %s, ...)", handle, type, fmt);
  2431. assert(handle);
  2432. assert(type > 0);
  2433. #ifndef DEBUG
  2434. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2435. return 0;
  2436. #endif
  2437. va_list args;
  2438. va_start(args, fmt);
  2439. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  2440. va_end(args);
  2441. return ret;
  2442. }
  2443. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  2444. {
  2445. qDebug("Lv2Plugin::carla_lv2_log_vprintf(%p, %i, %s, ...)", handle, type, fmt);
  2446. assert(handle);
  2447. assert(type > 0);
  2448. #ifndef DEBUG
  2449. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2450. return 0;
  2451. #endif
  2452. char buf[8196];
  2453. vsprintf(buf, fmt, ap);
  2454. if (*buf == 0)
  2455. return 0;
  2456. switch (type)
  2457. {
  2458. case CARLA_URI_MAP_ID_LOG_ERROR:
  2459. qCritical("%s", buf);
  2460. break;
  2461. case CARLA_URI_MAP_ID_LOG_NOTE:
  2462. printf("%s\n", buf);
  2463. break;
  2464. case CARLA_URI_MAP_ID_LOG_TRACE:
  2465. qDebug("%s", buf);
  2466. break;
  2467. case CARLA_URI_MAP_ID_LOG_WARNING:
  2468. qWarning("%s", buf);
  2469. break;
  2470. default:
  2471. break;
  2472. }
  2473. return strlen(buf);
  2474. }
  2475. // ----------------- Programs Feature ------------------------------------------------
  2476. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  2477. {
  2478. qDebug("Lv2Plugin::carla_lv2_program_changed(%p, %i)", handle, index);
  2479. assert(handle);
  2480. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2481. plugin->handleProgramChanged(index);
  2482. }
  2483. // ----------------- State Feature ---------------------------------------------------
  2484. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  2485. {
  2486. qDebug("Lv2Plugin::carla_lv2_state_make_path(%p, %p)", handle, path);
  2487. assert(handle);
  2488. assert(path);
  2489. QDir dir;
  2490. dir.mkpath(path);
  2491. return strdup(path);
  2492. }
  2493. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  2494. {
  2495. qDebug("Lv2Plugin::carla_lv2_state_map_abstract_path(%p, %p)", handle, absolute_path);
  2496. assert(handle);
  2497. assert(absolute_path);
  2498. QDir dir(absolute_path);
  2499. return strdup(dir.canonicalPath().toUtf8().constData());
  2500. }
  2501. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  2502. {
  2503. qDebug("Lv2Plugin::carla_lv2_state_map_absolute_path(%p, %p)", handle, abstract_path);
  2504. assert(handle);
  2505. assert(abstract_path);
  2506. QDir dir(abstract_path);
  2507. return strdup(dir.absolutePath().toUtf8().constData());
  2508. }
  2509. static LV2_State_Status carla_lv2_state_store(LV2_State_Handle handle, uint32_t key, const void* value, size_t size, uint32_t type, uint32_t flags)
  2510. {
  2511. qDebug("Lv2Plugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  2512. assert(handle);
  2513. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2514. return plugin->handleStateStore(key, value, size, type, flags);
  2515. }
  2516. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  2517. {
  2518. qDebug("Lv2Plugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  2519. assert(handle);
  2520. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2521. return plugin->handleStateRetrieve(key, size, type, flags);
  2522. }
  2523. // ----------------- URI-Map Feature -------------------------------------------------
  2524. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  2525. {
  2526. qDebug("Lv2Plugin::carla_lv2_uri_to_id(%p, %s, %s)", data, map, uri);
  2527. return carla_lv2_urid_map(data, uri);
  2528. }
  2529. // ----------------- URID Feature ----------------------------------------------------
  2530. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  2531. {
  2532. qDebug("Lv2Plugin::carla_lv2_urid_map(%p, %s)", handle, uri);
  2533. assert(handle);
  2534. assert(uri);
  2535. // Atom types
  2536. if (strcmp(uri, LV2_ATOM__Chunk) == 0)
  2537. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  2538. if (strcmp(uri, LV2_ATOM__Path) == 0)
  2539. return CARLA_URI_MAP_ID_ATOM_PATH;
  2540. if (strcmp(uri, LV2_ATOM__Sequence) == 0)
  2541. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  2542. if (strcmp(uri, LV2_ATOM__String) == 0)
  2543. return CARLA_URI_MAP_ID_ATOM_STRING;
  2544. if (strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  2545. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  2546. if (strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  2547. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  2548. // Log types
  2549. if (strcmp(uri, LV2_LOG__Error) == 0)
  2550. return CARLA_URI_MAP_ID_LOG_ERROR;
  2551. if (strcmp(uri, LV2_LOG__Note) == 0)
  2552. return CARLA_URI_MAP_ID_LOG_NOTE;
  2553. if (strcmp(uri, LV2_LOG__Trace) == 0)
  2554. return CARLA_URI_MAP_ID_LOG_TRACE;
  2555. if (strcmp(uri, LV2_LOG__Warning) == 0)
  2556. return CARLA_URI_MAP_ID_LOG_WARNING;
  2557. // Others
  2558. if (strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  2559. return CARLA_URI_MAP_ID_MIDI_EVENT;
  2560. // Custom types
  2561. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2562. return plugin->getCustomURID(uri);
  2563. }
  2564. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  2565. {
  2566. qDebug("Lv2Plugin::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  2567. assert(handle);
  2568. assert(urid > 0);
  2569. // Atom types
  2570. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  2571. return LV2_ATOM__Chunk;
  2572. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  2573. return LV2_ATOM__Path;
  2574. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  2575. return LV2_ATOM__Sequence;
  2576. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  2577. return LV2_ATOM__String;
  2578. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2579. return LV2_ATOM__atomTransfer;
  2580. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2581. return LV2_ATOM__eventTransfer;
  2582. // Log types
  2583. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  2584. return LV2_LOG__Error;
  2585. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  2586. return LV2_LOG__Note;
  2587. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  2588. return LV2_LOG__Trace;
  2589. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  2590. return LV2_LOG__Warning;
  2591. // Others
  2592. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  2593. return LV2_MIDI__MidiEvent;
  2594. // Custom types
  2595. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2596. return plugin->getCustomURIString(urid);
  2597. }
  2598. // ----------------- Worker Feature --------------------------------------------------
  2599. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  2600. {
  2601. qDebug("Lv2Plugin::carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  2602. assert(handle);
  2603. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2604. return plugin->handleWorkerSchedule(size, data);
  2605. }
  2606. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  2607. {
  2608. qDebug("Lv2Plugin::carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  2609. assert(handle);
  2610. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2611. return plugin->handleWorkerRespond(size, data);
  2612. }
  2613. // ----------------- UI Port-Map Feature ---------------------------------------------
  2614. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  2615. {
  2616. qDebug("Lv2Plugin::carla_lv2_ui_port_map(%p, %s)", handle, symbol);
  2617. assert(handle);
  2618. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2619. return plugin->handleUiPortMap(symbol);
  2620. }
  2621. // ----------------- UI Resize Feature -----------------------------------------------
  2622. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  2623. {
  2624. qDebug("Lv2Plugin::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  2625. assert(handle);
  2626. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2627. return plugin->handleUiResize(width, height);
  2628. }
  2629. // ----------------- External UI Feature ---------------------------------------------
  2630. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  2631. {
  2632. qDebug("Lv2Plugin::carla_lv2_external_ui_closed(%p)", controller);
  2633. assert(controller);
  2634. Lv2Plugin* plugin = (Lv2Plugin*)controller;
  2635. plugin->handleExternalUiClosed();
  2636. }
  2637. // ----------------- UI Extension ----------------------------------------------------
  2638. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  2639. {
  2640. qDebug("Lv2Plugin::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  2641. assert(controller);
  2642. Lv2Plugin* plugin = (Lv2Plugin*)controller;
  2643. plugin->handleUiWrite(port_index, buffer_size, format, buffer);
  2644. }
  2645. // -------------------------------------------------------------------
  2646. bool uiLibOpen(const char* filename)
  2647. {
  2648. ui.lib = lib_open(filename);
  2649. return bool(ui.lib);
  2650. }
  2651. bool uiLibClose()
  2652. {
  2653. if (ui.lib)
  2654. return lib_close(ui.lib);
  2655. return false;
  2656. }
  2657. void* uiLibSymbol(const char* symbol)
  2658. {
  2659. if (ui.lib)
  2660. return lib_symbol(ui.lib, symbol);
  2661. return nullptr;
  2662. }
  2663. // -------------------------------------------------------------------
  2664. bool init(const char* bundle, const char* const name, const char* URI)
  2665. {
  2666. // ---------------------------------------------------------------
  2667. // get plugin from lv2_rdf (lilv)
  2668. rdf_descriptor = lv2_rdf_new(URI);
  2669. if (! rdf_descriptor)
  2670. {
  2671. setLastError("Failed to find the requested plugin in the LV2 Bundle");
  2672. return false;
  2673. }
  2674. // ---------------------------------------------------------------
  2675. // open DLL
  2676. if (! libOpen(rdf_descriptor->Binary))
  2677. {
  2678. setLastError(libError(rdf_descriptor->Binary));
  2679. return false;
  2680. }
  2681. // ---------------------------------------------------------------
  2682. // get DLL main entry
  2683. LV2_Descriptor_Function descfn = (LV2_Descriptor_Function)libSymbol("lv2_descriptor");
  2684. if (! descfn)
  2685. {
  2686. setLastError("Could not find the LV2 Descriptor in the plugin library");
  2687. return false;
  2688. }
  2689. // ---------------------------------------------------------------
  2690. // get descriptor that matches URI
  2691. uint32_t i = 0;
  2692. while ((descriptor = descfn(i++)))
  2693. {
  2694. if (strcmp(descriptor->URI, URI) == 0)
  2695. break;
  2696. }
  2697. if (! descriptor)
  2698. {
  2699. setLastError("Could not find the requested plugin URI in the plugin library");
  2700. return false;
  2701. }
  2702. // ---------------------------------------------------------------
  2703. // check supported port-types and features
  2704. bool canContinue = true;
  2705. // Check supported ports
  2706. for (i=0; i < rdf_descriptor->PortCount; i++)
  2707. {
  2708. LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  2709. if (bool(LV2_IS_PORT_AUDIO(PortType) || LV2_IS_PORT_CONTROL(PortType) || LV2_IS_PORT_ATOM_SEQUENCE(PortType) || LV2_IS_PORT_EVENT(PortType) || LV2_IS_PORT_MIDI_LL(PortType)) == false)
  2710. {
  2711. qCritical("Got unsupported port -> %i", PortType);
  2712. if (! LV2_IS_PORT_OPTIONAL(rdf_descriptor->Ports[i].Properties))
  2713. {
  2714. setLastError("Plugin requires a port that is not currently supported");
  2715. canContinue = false;
  2716. break;
  2717. }
  2718. }
  2719. }
  2720. // Check supported features
  2721. for (i=0; i < rdf_descriptor->FeatureCount; i++)
  2722. {
  2723. if (LV2_IS_FEATURE_REQUIRED(rdf_descriptor->Features[i].Type) && is_lv2_feature_supported(rdf_descriptor->Features[i].URI) == false)
  2724. {
  2725. QString msg = QString("Plugin requires a feature that is not supported:\n%1").arg(rdf_descriptor->Features[i].URI);
  2726. setLastError(msg.toUtf8().constData());
  2727. canContinue = false;
  2728. break;
  2729. }
  2730. }
  2731. // Check extensions
  2732. for (i=0; i < rdf_descriptor->ExtensionCount; i++)
  2733. {
  2734. if (strcmp(rdf_descriptor->Extensions[i], LV2DYNPARAM_URI) == 0)
  2735. m_hints |= PLUGIN_HAS_EXTENSION_DYNPARAM;
  2736. else if (strcmp(rdf_descriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  2737. m_hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  2738. else if (strcmp(rdf_descriptor->Extensions[i], LV2_STATE__interface) == 0)
  2739. m_hints |= PLUGIN_HAS_EXTENSION_STATE;
  2740. else if (strcmp(rdf_descriptor->Extensions[i], LV2_WORKER__interface) == 0)
  2741. m_hints |= PLUGIN_HAS_EXTENSION_WORKER;
  2742. else
  2743. qDebug("Plugin has non-supported extension: '%s'", rdf_descriptor->Extensions[i]);
  2744. }
  2745. if (! canContinue)
  2746. {
  2747. // error already set
  2748. return false;
  2749. }
  2750. // ---------------------------------------------------------------
  2751. // initialize features
  2752. LV2_Event_Feature* Event_Feature = new LV2_Event_Feature;
  2753. Event_Feature->callback_data = this;
  2754. Event_Feature->lv2_event_ref = carla_lv2_event_ref;
  2755. Event_Feature->lv2_event_unref = carla_lv2_event_unref;
  2756. LV2_Log_Log* Log_Feature = new LV2_Log_Log;
  2757. Log_Feature->handle = this;
  2758. Log_Feature->printf = carla_lv2_log_printf;
  2759. Log_Feature->vprintf = carla_lv2_log_vprintf;
  2760. LV2_Programs_Host* Programs_Feature = new LV2_Programs_Host;
  2761. Programs_Feature->handle = this;
  2762. Programs_Feature->program_changed = carla_lv2_program_changed;
  2763. LV2_State_Make_Path* State_MakePath_Feature = new LV2_State_Make_Path;
  2764. State_MakePath_Feature->handle = this;
  2765. State_MakePath_Feature->path = carla_lv2_state_make_path;
  2766. LV2_State_Map_Path* State_MapPath_Feature = new LV2_State_Map_Path;
  2767. State_MapPath_Feature->handle = this;
  2768. State_MapPath_Feature->abstract_path = carla_lv2_state_map_abstract_path;
  2769. State_MapPath_Feature->absolute_path = carla_lv2_state_map_absolute_path;
  2770. LV2_URI_Map_Feature* URI_Map_Feature = new LV2_URI_Map_Feature;
  2771. URI_Map_Feature->callback_data = this;
  2772. URI_Map_Feature->uri_to_id = carla_lv2_uri_to_id;
  2773. LV2_URID_Map* URID_Map_Feature = new LV2_URID_Map;
  2774. URID_Map_Feature->handle = this;
  2775. URID_Map_Feature->map = carla_lv2_urid_map;
  2776. LV2_URID_Unmap* URID_Unmap_Feature = new LV2_URID_Unmap;
  2777. URID_Unmap_Feature->handle = this;
  2778. URID_Unmap_Feature->unmap = carla_lv2_urid_unmap;
  2779. LV2_Worker_Schedule* Worker_Feature = new LV2_Worker_Schedule;
  2780. Worker_Feature->handle = this;
  2781. Worker_Feature->schedule_work = carla_lv2_worker_schedule;
  2782. lv2_rtsafe_memory_pool_provider* RT_MemPool_Feature = new lv2_rtsafe_memory_pool_provider;
  2783. rtmempool_allocator_init(RT_MemPool_Feature);
  2784. features[lv2_feature_id_event] = new LV2_Feature;
  2785. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  2786. features[lv2_feature_id_event]->data = Event_Feature;
  2787. features[lv2_feature_id_logs] = new LV2_Feature;
  2788. features[lv2_feature_id_logs]->URI = LV2_LOG__log;
  2789. features[lv2_feature_id_logs]->data = Log_Feature;
  2790. features[lv2_feature_id_programs] = new LV2_Feature;
  2791. features[lv2_feature_id_programs]->URI = LV2_PROGRAMS__Host;
  2792. features[lv2_feature_id_programs]->data = Programs_Feature;
  2793. features[lv2_feature_id_rtmempool] = new LV2_Feature;
  2794. features[lv2_feature_id_rtmempool]->URI = LV2_RTSAFE_MEMORY_POOL_URI;
  2795. features[lv2_feature_id_rtmempool]->data = RT_MemPool_Feature;
  2796. features[lv2_feature_id_state_make_path] = new LV2_Feature;
  2797. features[lv2_feature_id_state_make_path]->URI = LV2_STATE__makePath;
  2798. features[lv2_feature_id_state_make_path]->data = State_MakePath_Feature;
  2799. features[lv2_feature_id_state_map_path] = new LV2_Feature;
  2800. features[lv2_feature_id_state_map_path]->URI = LV2_STATE__mapPath;
  2801. features[lv2_feature_id_state_map_path]->data = State_MapPath_Feature;
  2802. features[lv2_feature_id_strict_bounds] = new LV2_Feature;
  2803. features[lv2_feature_id_strict_bounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  2804. features[lv2_feature_id_strict_bounds]->data = nullptr;
  2805. features[lv2_feature_id_uri_map] = new LV2_Feature;
  2806. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  2807. features[lv2_feature_id_uri_map]->data = URI_Map_Feature;
  2808. features[lv2_feature_id_urid_map] = new LV2_Feature;
  2809. features[lv2_feature_id_urid_map]->URI = LV2_URID__map;
  2810. features[lv2_feature_id_urid_map]->data = URID_Map_Feature;
  2811. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  2812. features[lv2_feature_id_urid_unmap]->URI = LV2_URID__unmap;
  2813. features[lv2_feature_id_urid_unmap]->data = URID_Unmap_Feature;
  2814. features[lv2_feature_id_worker] = new LV2_Feature;
  2815. features[lv2_feature_id_worker]->URI = LV2_WORKER__schedule;
  2816. features[lv2_feature_id_worker]->data = Worker_Feature;
  2817. // ---------------------------------------------------------------
  2818. // initialize plugin
  2819. handle = descriptor->instantiate(descriptor, x_engine->getSampleRate(), rdf_descriptor->Bundle, features);
  2820. if (! handle)
  2821. {
  2822. setLastError("Plugin failed to initialize");
  2823. return false;
  2824. }
  2825. // ---------------------------------------------------------------
  2826. // get info
  2827. m_filename = strdup(bundle);
  2828. if (name)
  2829. m_name = x_engine->getUniqueName(name);
  2830. else
  2831. m_name = x_engine->getUniqueName(rdf_descriptor->Name);
  2832. // ---------------------------------------------------------------
  2833. // register client
  2834. x_client = x_engine->addClient(this);
  2835. if (! x_client->isOk())
  2836. {
  2837. setLastError("Failed to register plugin client");
  2838. return false;
  2839. }
  2840. // ---------------------------------------------------------------
  2841. // gui stuff
  2842. if (rdf_descriptor->UICount > 0)
  2843. {
  2844. qDebug("Has UI");
  2845. // -----------------------------------------------------------
  2846. // find more appropriate ui
  2847. int eQt4, eHWND, eX11, eGtk2, iHWND, iX11, iQt4, iExt, iSuil, iFinal;
  2848. eQt4 = eHWND = eX11 = eGtk2 = iQt4 = iHWND = iX11 = iExt = iSuil = iFinal = -1;
  2849. for (i=0; i < rdf_descriptor->UICount; i++)
  2850. {
  2851. switch (rdf_descriptor->UIs[i].Type)
  2852. {
  2853. case LV2_UI_QT4:
  2854. #ifndef BUILD_BRIDGE
  2855. if (isUiBridgeable(i) && carlaOptions.prefer_ui_bridges)
  2856. eQt4 = i;
  2857. #endif
  2858. iQt4 = i;
  2859. break;
  2860. case LV2_UI_WINDOWS:
  2861. #ifndef BUILD_BRIDGE
  2862. if (isUiBridgeable(i) && carlaOptions.prefer_ui_bridges)
  2863. eHWND = i;
  2864. #endif
  2865. iHWND = i;
  2866. break;
  2867. case LV2_UI_X11:
  2868. #ifndef BUILD_BRIDGE
  2869. if (isUiBridgeable(i) && carlaOptions.prefer_ui_bridges)
  2870. eX11 = i;
  2871. #endif
  2872. iX11 = i;
  2873. break;
  2874. case LV2_UI_GTK2:
  2875. #ifdef BUILD_BRIDGE
  2876. if (false)
  2877. #else
  2878. # ifdef HAVE_SUIL
  2879. if (isUiBridgeable(i) && carlaOptions.prefer_ui_bridges)
  2880. # else
  2881. if (isUiBridgeable(i))
  2882. # endif
  2883. #endif
  2884. eGtk2 = i;
  2885. #ifdef HAVE_SUIL
  2886. iSuil = i;
  2887. #endif
  2888. break;
  2889. case LV2_UI_EXTERNAL:
  2890. case LV2_UI_OLD_EXTERNAL:
  2891. iExt = i;
  2892. break;
  2893. default:
  2894. break;
  2895. }
  2896. }
  2897. if (eQt4 >= 0)
  2898. iFinal = eQt4;
  2899. else if (eHWND >= 0)
  2900. iFinal = eHWND;
  2901. else if (eX11 >= 0)
  2902. iFinal = eX11;
  2903. else if (eGtk2 >= 0)
  2904. iFinal = eGtk2;
  2905. else if (iQt4 >= 0)
  2906. iFinal = iQt4;
  2907. else if (iHWND >= 0)
  2908. iFinal = iHWND;
  2909. else if (iX11 >= 0)
  2910. iFinal = iX11;
  2911. else if (iExt >= 0)
  2912. iFinal = iExt;
  2913. else if (iSuil >= 0)
  2914. iFinal = iSuil;
  2915. #ifndef BUILD_BRIDGE
  2916. bool isBridged = (iFinal == eQt4 || iFinal == eX11 || iFinal == eGtk2);
  2917. #endif
  2918. #ifdef HAVE_SUIL
  2919. bool isSuil = (iFinal == iSuil && !isBridged);
  2920. #endif
  2921. if (iFinal < 0)
  2922. {
  2923. qWarning("Failed to find an appropriate LV2 UI for this plugin");
  2924. return true;
  2925. }
  2926. ui.rdf_descriptor = &rdf_descriptor->UIs[iFinal];
  2927. // -----------------------------------------------------------
  2928. // check supported ui features
  2929. canContinue = true;
  2930. for (i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  2931. {
  2932. if (LV2_IS_FEATURE_REQUIRED(ui.rdf_descriptor->Features[i].Type) && is_lv2_ui_feature_supported(ui.rdf_descriptor->Features[i].URI) == false)
  2933. {
  2934. qCritical("Plugin UI requires a feature that is not supported:\n%s", ui.rdf_descriptor->Features[i].URI);
  2935. canContinue = false;
  2936. break;
  2937. }
  2938. }
  2939. if (! canContinue)
  2940. {
  2941. ui.rdf_descriptor = nullptr;
  2942. return true;
  2943. }
  2944. #ifdef HAVE_SUIL
  2945. if (isSuil)
  2946. {
  2947. // -------------------------------------------------------
  2948. // init suil host
  2949. suil.host = suil_host_new(carla_lv2_ui_write_function, carla_lv2_ui_port_map, nullptr, nullptr);
  2950. }
  2951. else
  2952. #endif
  2953. {
  2954. // -------------------------------------------------------
  2955. // open DLL
  2956. if (! uiLibOpen(ui.rdf_descriptor->Binary))
  2957. {
  2958. qCritical("Could not load UI library, error was:\n%s", libError(ui.rdf_descriptor->Binary));
  2959. ui.rdf_descriptor = nullptr;
  2960. return true;
  2961. }
  2962. // -------------------------------------------------------
  2963. // get DLL main entry
  2964. LV2UI_DescriptorFunction ui_descfn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor");
  2965. if (! ui_descfn)
  2966. {
  2967. qCritical("Could not find the LV2UI Descriptor in the UI library");
  2968. uiLibClose();
  2969. ui.lib = nullptr;
  2970. ui.rdf_descriptor = nullptr;
  2971. return true;
  2972. }
  2973. // -------------------------------------------------------
  2974. // get descriptor that matches URI
  2975. i = 0;
  2976. while ((ui.descriptor = ui_descfn(i++)))
  2977. {
  2978. if (strcmp(ui.descriptor->URI, ui.rdf_descriptor->URI) == 0)
  2979. break;
  2980. }
  2981. if (! ui.descriptor)
  2982. {
  2983. qCritical("Could not find the requested GUI in the plugin UI library");
  2984. uiLibClose();
  2985. ui.lib = nullptr;
  2986. ui.rdf_descriptor = nullptr;
  2987. return true;
  2988. }
  2989. }
  2990. // -----------------------------------------------------------
  2991. // initialize ui according to type
  2992. LV2_Property UiType = ui.rdf_descriptor->Type;
  2993. #ifndef BUILD_BRIDGE
  2994. if (isBridged)
  2995. {
  2996. // -------------------------------------------------------
  2997. // initialize ui bridge
  2998. const char* const oscBinary = lv2bridge2str(UiType);
  2999. qDebug("Has UI - is bridge, uitype = %i : %s", UiType, oscBinary);
  3000. if (oscBinary)
  3001. {
  3002. qDebug("Has UI - has binary");
  3003. gui.type = GUI_EXTERNAL_OSC;
  3004. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  3005. osc.thread->setOscData(oscBinary, descriptor->URI, ui.descriptor->URI);
  3006. }
  3007. else
  3008. qDebug("Has UI - NOT binary");
  3009. }
  3010. else
  3011. #endif
  3012. {
  3013. // -------------------------------------------------------
  3014. // initialize ui features
  3015. QString guiTitle = QString("%1 (GUI)").arg(m_name);
  3016. LV2_Extension_Data_Feature* UI_Data_Feature = new LV2_Extension_Data_Feature;
  3017. UI_Data_Feature->data_access = descriptor->extension_data;
  3018. LV2UI_Port_Map* UI_PortMap_Feature = new LV2UI_Port_Map;
  3019. UI_PortMap_Feature->handle = this;
  3020. UI_PortMap_Feature->port_index = carla_lv2_ui_port_map;
  3021. LV2UI_Resize* UI_Resize_Feature = new LV2UI_Resize;
  3022. UI_Resize_Feature->handle = this;
  3023. UI_Resize_Feature->ui_resize = carla_lv2_ui_resize;
  3024. lv2_external_ui_host* External_UI_Feature = new lv2_external_ui_host;
  3025. External_UI_Feature->ui_closed = carla_lv2_external_ui_closed;
  3026. External_UI_Feature->plugin_human_id = strdup(guiTitle.toUtf8().constData());
  3027. features[lv2_feature_id_data_access] = new LV2_Feature;
  3028. features[lv2_feature_id_data_access]->URI = LV2_DATA_ACCESS_URI;
  3029. features[lv2_feature_id_data_access]->data = UI_Data_Feature;
  3030. features[lv2_feature_id_instance_access] = new LV2_Feature;
  3031. features[lv2_feature_id_instance_access]->URI = LV2_INSTANCE_ACCESS_URI;
  3032. features[lv2_feature_id_instance_access]->data = handle;
  3033. features[lv2_feature_id_ui_parent] = new LV2_Feature;
  3034. features[lv2_feature_id_ui_parent]->URI = LV2_UI__parent;
  3035. features[lv2_feature_id_ui_parent]->data = nullptr;
  3036. features[lv2_feature_id_ui_port_map] = new LV2_Feature;
  3037. features[lv2_feature_id_ui_port_map]->URI = LV2_UI__portMap;
  3038. features[lv2_feature_id_ui_port_map]->data = UI_PortMap_Feature;
  3039. features[lv2_feature_id_ui_resize] = new LV2_Feature;
  3040. features[lv2_feature_id_ui_resize]->URI = LV2_UI__resize;
  3041. features[lv2_feature_id_ui_resize]->data = UI_Resize_Feature;
  3042. features[lv2_feature_id_external_ui] = new LV2_Feature;
  3043. features[lv2_feature_id_external_ui]->URI = LV2_EXTERNAL_UI_URI;
  3044. features[lv2_feature_id_external_ui]->data = External_UI_Feature;
  3045. features[lv2_feature_id_external_ui_old] = new LV2_Feature;
  3046. features[lv2_feature_id_external_ui_old]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3047. features[lv2_feature_id_external_ui_old]->data = External_UI_Feature;
  3048. // -------------------------------------------------------
  3049. // initialize ui
  3050. switch (UiType)
  3051. {
  3052. case LV2_UI_QT4:
  3053. qDebug("Will use LV2 Qt4 UI");
  3054. gui.type = GUI_INTERNAL_QT4;
  3055. gui.resizable = isUiResizable();
  3056. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  3057. updateUi();
  3058. break;
  3059. case LV2_UI_COCOA:
  3060. qDebug("Will use LV2 Cocoa UI");
  3061. gui.type = GUI_INTERNAL_COCOA;
  3062. gui.resizable = isUiResizable();
  3063. break;
  3064. case LV2_UI_WINDOWS:
  3065. qDebug("Will use LV2 Windows UI");
  3066. gui.type = GUI_INTERNAL_HWND;
  3067. gui.resizable = isUiResizable();
  3068. break;
  3069. case LV2_UI_X11:
  3070. qDebug("Will use LV2 X11 UI");
  3071. gui.type = GUI_INTERNAL_X11;
  3072. gui.resizable = isUiResizable();
  3073. break;
  3074. case LV2_UI_GTK2:
  3075. #ifdef HAVE_SUIL
  3076. qDebug("Will use LV2 Gtk2 UI (suil)");
  3077. gui.type = GUI_EXTERNAL_SUIL;
  3078. gui.resizable = isUiResizable();
  3079. suil.handle = suil_instance_new(suil.host, this, LV2_UI__Qt4UI, rdf_descriptor->URI, ui.rdf_descriptor->URI, lv2_get_ui_uri(ui.rdf_descriptor->Type), ui.rdf_descriptor->Bundle, ui.rdf_descriptor->Binary, features);
  3080. if (suil.handle)
  3081. {
  3082. ui.handle = ((SuilInstanceImpl*)suil.handle)->handle;
  3083. ui.descriptor = ((SuilInstanceImpl*)suil.handle)->descriptor;
  3084. ui.widget = suil_instance_get_widget(suil.handle);
  3085. if (ui.widget)
  3086. {
  3087. QWidget* widget = (QWidget*)ui.widget;
  3088. widget->setWindowTitle(guiTitle);
  3089. // FIXME - need a proper way for this
  3090. if (strcmp(ui.rdf_descriptor->URI, "http://factorial.hu/plugins/lv2/ir/gui") == 0)
  3091. widget->resize(930, 460);
  3092. }
  3093. }
  3094. #else
  3095. qDebug("Will use LV2 Gtk2 UI, NOT!");
  3096. #endif
  3097. break;
  3098. case LV2_UI_EXTERNAL:
  3099. case LV2_UI_OLD_EXTERNAL:
  3100. qDebug("Will use LV2 External UI");
  3101. gui.type = GUI_EXTERNAL_LV2;
  3102. break;
  3103. default:
  3104. break;
  3105. }
  3106. }
  3107. if (gui.type != GUI_NONE)
  3108. m_hints |= PLUGIN_HAS_GUI;
  3109. } // End of GUI Stuff
  3110. return true;
  3111. }
  3112. private:
  3113. LV2_Handle handle, h2;
  3114. const LV2_Descriptor* descriptor;
  3115. const LV2_RDF_Descriptor* rdf_descriptor;
  3116. LV2_Feature* features[lv2_feature_count+1];
  3117. struct {
  3118. lv2dynparam_plugin_callbacks* dynparam;
  3119. LV2_State_Interface* state;
  3120. LV2_Worker_Interface* worker;
  3121. LV2_Programs_Interface* programs;
  3122. LV2_Programs_UI_Interface* uiprograms;
  3123. } ext;
  3124. struct {
  3125. void* lib;
  3126. LV2UI_Handle handle;
  3127. LV2UI_Widget widget;
  3128. const LV2UI_Descriptor* descriptor;
  3129. const LV2_RDF_UI* rdf_descriptor;
  3130. } ui;
  3131. struct {
  3132. GuiType type;
  3133. bool resizable;
  3134. int width;
  3135. int height;
  3136. } gui;
  3137. #ifdef HAVE_SUIL
  3138. struct {
  3139. SuilHost* host;
  3140. SuilInstance* handle;
  3141. QByteArray pos;
  3142. } suil;
  3143. #endif
  3144. PluginEventData evin;
  3145. PluginEventData evout;
  3146. Lv2ParameterData* lv2param;
  3147. std::vector<const char*> customURIDs;
  3148. };
  3149. CarlaPlugin* CarlaPlugin::newLV2(const initializer& init)
  3150. {
  3151. qDebug("CarlaPlugin::newLV2(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  3152. short id = init.engine->getNewPluginId();
  3153. if (id < 0)
  3154. {
  3155. setLastError("Maximum number of plugins reached");
  3156. return nullptr;
  3157. }
  3158. Lv2Plugin* const plugin = new Lv2Plugin(init.engine, id);
  3159. if (! plugin->init(init.filename, init.name, init.label))
  3160. {
  3161. delete plugin;
  3162. return nullptr;
  3163. }
  3164. plugin->reload();
  3165. #ifndef BUILD_BRIDGE
  3166. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  3167. {
  3168. const uint32_t ins = plugin->audioInCount();
  3169. const uint32_t outs = plugin->audioOutCount();
  3170. if (ins > 2 || outs > 2 || (ins != outs && ins != 0 && outs != 0))
  3171. {
  3172. setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  3173. delete plugin;
  3174. return nullptr;
  3175. }
  3176. }
  3177. #endif
  3178. plugin->registerToOsc();
  3179. return plugin;
  3180. }
  3181. /**@}*/
  3182. CARLA_BACKEND_END_NAMESPACE
  3183. // -------------------------------------------------------------------------------------------------------------------
  3184. int CarlaOsc::handle_lv2_atom_transfer(CARLA_OSC_HANDLE_ARGS2)
  3185. {
  3186. qDebug("CarlaOsc::handle_lv2_atom_transfer()");
  3187. //CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  3188. CarlaBackend::Lv2Plugin* lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3189. lv2plugin->handleAtomTransfer();
  3190. return 0;
  3191. Q_UNUSED(argc);
  3192. Q_UNUSED(argv);
  3193. Q_UNUSED(types);
  3194. }
  3195. int CarlaOsc::handle_lv2_event_transfer(CARLA_OSC_HANDLE_ARGS2)
  3196. {
  3197. qDebug("CarlaOsc::handle_lv2_event_transfer()");
  3198. CARLA_OSC_CHECK_OSC_TYPES(3, "sss");
  3199. CarlaBackend::Lv2Plugin* lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3200. const char* type = (const char*)&argv[0]->s;
  3201. const char* key = (const char*)&argv[1]->s;
  3202. const char* value = (const char*)&argv[2]->s;
  3203. lv2plugin->handleEventTransfer(type, key, value);
  3204. return 0;
  3205. }