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.

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