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.

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