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.

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