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.

3850 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 and Volume
  1827. if (do_drywet || do_volume)
  1828. {
  1829. for (k=0; k < frames; k++)
  1830. {
  1831. if (do_drywet)
  1832. {
  1833. if (aout.count == 1 && ! h2)
  1834. outBuffer[i][k] = (outBuffer[i][k]*x_drywet)+(inBuffer[0][k]*(1.0-x_drywet));
  1835. else
  1836. outBuffer[i][k] = (outBuffer[i][k]*x_drywet)+(inBuffer[i][k]*(1.0-x_drywet));
  1837. }
  1838. if (do_volume)
  1839. outBuffer[i][k] *= x_vol;
  1840. }
  1841. }
  1842. // Balance
  1843. if (do_balance)
  1844. {
  1845. if (i%2 == 0)
  1846. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1847. bal_rangeL = (x_bal_left+1.0)/2;
  1848. bal_rangeR = (x_bal_right+1.0)/2;
  1849. for (k=0; k < frames; k++)
  1850. {
  1851. if (i%2 == 0)
  1852. {
  1853. // left output
  1854. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1855. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1856. }
  1857. else
  1858. {
  1859. // right
  1860. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1861. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1862. }
  1863. }
  1864. }
  1865. // Output VU
  1866. for (k=0; i < 2 && k < frames; k++)
  1867. {
  1868. if (abs(outBuffer[i][k]) > aouts_peak_tmp[i])
  1869. aouts_peak_tmp[i] = abs(outBuffer[i][k]);
  1870. }
  1871. }
  1872. }
  1873. else
  1874. {
  1875. // disable any output sound if not active
  1876. for (i=0; i < aout.count; i++)
  1877. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  1878. aouts_peak_tmp[0] = 0.0;
  1879. aouts_peak_tmp[1] = 0.0;
  1880. } // End of Post-processing
  1881. CARLA_PROCESS_CONTINUE_CHECK;
  1882. // --------------------------------------------------------------------------------------------------------
  1883. // Control Output
  1884. if (param.portCout && m_active)
  1885. {
  1886. double value, rvalue;
  1887. for (k=0; k < param.count; k++)
  1888. {
  1889. if (param.data[k].type == PARAMETER_OUTPUT)
  1890. {
  1891. if (lv2param[k].type == LV2_PARAMETER_TYPE_CONTROL)
  1892. fixParameterValue(lv2param[k].control, param.ranges[k]);
  1893. if (param.data[k].midiCC > 0)
  1894. {
  1895. switch (lv2param[k].type)
  1896. {
  1897. case LV2_PARAMETER_TYPE_CONTROL:
  1898. value = lv2param[k].control;
  1899. break;
  1900. default:
  1901. value = param.ranges[k].min;
  1902. break;
  1903. }
  1904. rvalue = (value - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1905. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, rvalue);
  1906. }
  1907. }
  1908. }
  1909. } // End of Control Output
  1910. CARLA_PROCESS_CONTINUE_CHECK;
  1911. // --------------------------------------------------------------------------------------------------------
  1912. // MIDI Output
  1913. if (evout.count > 0 && m_active)
  1914. {
  1915. for (i=0; i < evout.count; i++)
  1916. {
  1917. if (! evout.data[i].port)
  1918. continue;
  1919. if (evin.data[i].type & CARLA_EVENT_DATA_ATOM)
  1920. {
  1921. // TODO
  1922. }
  1923. else if (evin.data[i].type & CARLA_EVENT_DATA_EVENT)
  1924. {
  1925. LV2_Event* ev;
  1926. LV2_Event_Iterator iter;
  1927. uint8_t* data;
  1928. lv2_event_begin(&iter, evout.data[i].event);
  1929. for (k=0; k < iter.buf->event_count; k++)
  1930. {
  1931. ev = lv2_event_get(&iter, &data);
  1932. if (ev && data)
  1933. evout.data[i].port->writeEvent(ev->frames, data, ev->size);
  1934. lv2_event_increment(&iter);
  1935. }
  1936. }
  1937. else if (evin.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1938. {
  1939. LV2_MIDIState state = { evout.data[i].midi, frames, 0 };
  1940. uint32_t eventSize;
  1941. double eventTime;
  1942. unsigned char* eventData;
  1943. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  1944. {
  1945. evout.data[i].port->writeEvent(eventTime, eventData, eventSize);
  1946. lv2midi_step(&state);
  1947. }
  1948. }
  1949. }
  1950. }// End of MIDI Output
  1951. CARLA_PROCESS_CONTINUE_CHECK;
  1952. // --------------------------------------------------------------------------------------------------------
  1953. // Peak Values
  1954. x_engine->setInputPeak(m_id, 0, ains_peak_tmp[0]);
  1955. x_engine->setInputPeak(m_id, 1, ains_peak_tmp[1]);
  1956. x_engine->setOutputPeak(m_id, 0, aouts_peak_tmp[0]);
  1957. x_engine->setOutputPeak(m_id, 1, aouts_peak_tmp[1]);
  1958. m_activeBefore = m_active;
  1959. }
  1960. // -------------------------------------------------------------------
  1961. // Cleanup
  1962. void removeClientPorts()
  1963. {
  1964. qDebug("Lv2Plugin::removeClientPorts() - start");
  1965. for (uint32_t i=0; i < evin.count; i++)
  1966. {
  1967. if (evin.data[i].port)
  1968. {
  1969. delete evin.data[i].port;
  1970. evin.data[i].port = nullptr;
  1971. }
  1972. }
  1973. for (uint32_t i=0; i < evout.count; i++)
  1974. {
  1975. if (evout.data[i].port)
  1976. {
  1977. delete evout.data[i].port;
  1978. evout.data[i].port = nullptr;
  1979. }
  1980. }
  1981. CarlaPlugin::removeClientPorts();
  1982. qDebug("Lv2Plugin::removeClientPorts() - end");
  1983. }
  1984. void initBuffers()
  1985. {
  1986. uint32_t i;
  1987. for (i=0; i < evin.count; i++)
  1988. {
  1989. if (evin.data[i].port)
  1990. evin.data[i].port->initBuffer(x_engine);
  1991. }
  1992. for (uint32_t i=0; i < evout.count; i++)
  1993. {
  1994. if (evout.data[i].port)
  1995. evout.data[i].port->initBuffer(x_engine);
  1996. }
  1997. CarlaPlugin::initBuffers();
  1998. }
  1999. void deleteBuffers()
  2000. {
  2001. qDebug("Lv2Plugin::deleteBuffers() - start");
  2002. if (param.count > 0)
  2003. delete[] lv2param;
  2004. if (evin.count > 0)
  2005. {
  2006. for (uint32_t i=0; i < evin.count; i++)
  2007. {
  2008. if (evin.data[i].type & CARLA_EVENT_DATA_ATOM)
  2009. {
  2010. free(evin.data[i].atom);
  2011. }
  2012. else if (evin.data[i].type & CARLA_EVENT_DATA_EVENT)
  2013. {
  2014. free(evin.data[i].event);
  2015. }
  2016. else if (evin.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2017. {
  2018. delete[] evin.data[i].midi->data;
  2019. delete evin.data[i].midi;
  2020. }
  2021. }
  2022. delete[] evin.data;
  2023. }
  2024. if (evout.count > 0)
  2025. {
  2026. for (uint32_t i=0; i < evout.count; i++)
  2027. {
  2028. if (evout.data[i].type & CARLA_EVENT_DATA_ATOM)
  2029. {
  2030. free(evout.data[i].atom);
  2031. }
  2032. else if (evout.data[i].type & CARLA_EVENT_DATA_EVENT)
  2033. {
  2034. free(evout.data[i].event);
  2035. }
  2036. else if (evout.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2037. {
  2038. delete[] evout.data[i].midi->data;
  2039. delete evout.data[i].midi;
  2040. }
  2041. }
  2042. delete[] evout.data;
  2043. }
  2044. lv2param = nullptr;
  2045. evin.count = 0;
  2046. evin.data = nullptr;
  2047. evout.count = 0;
  2048. evout.data = nullptr;
  2049. qDebug("Lv2Plugin::deleteBuffers() - end");
  2050. }
  2051. // -------------------------------------------------------------------
  2052. uint32_t getCustomURID(const char* const uri)
  2053. {
  2054. qDebug("Lv2Plugin::getCustomURID(%s)", uri);
  2055. for (size_t i=0; i < customURIDs.size(); i++)
  2056. {
  2057. if (customURIDs[i] && strcmp(customURIDs[i], uri) == 0)
  2058. return i;
  2059. }
  2060. customURIDs.push_back(strdup(uri));
  2061. return customURIDs.size()-1;
  2062. }
  2063. const char* getCustomURIString(LV2_URID urid)
  2064. {
  2065. qDebug("Lv2Plugin::getCustomURIString(%i)", urid);
  2066. if (urid < customURIDs.size())
  2067. return customURIDs[urid];
  2068. return nullptr;
  2069. }
  2070. void handleAtomTransfer()
  2071. {
  2072. // TODO
  2073. }
  2074. void handleEventTransfer(const char* const type, const char* const key, const char* const stringData)
  2075. {
  2076. qDebug("Lv2Plugin::handleEventTransfer(%s, %s, %s)", type, key, stringData);
  2077. assert(type);
  2078. assert(key);
  2079. assert(stringData);
  2080. QByteArray chunk;
  2081. chunk = QByteArray::fromBase64(stringData);
  2082. const LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  2083. const LV2_URID uridAtomBlank = getCustomURID(LV2_ATOM__Blank);
  2084. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  2085. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  2086. if (atom->type != uridAtomBlank)
  2087. {
  2088. qWarning("Lv2Plugin::handleEventTransfer() - Not blank");
  2089. return;
  2090. }
  2091. const LV2_Atom_Object* const obj = (LV2_Atom_Object*)atom;
  2092. if (obj->body.otype != uridPatchSet)
  2093. {
  2094. qWarning("Lv2Plugin::handleEventTransfer() - Not Patch Set");
  2095. return;
  2096. }
  2097. const LV2_Atom_Object* body = nullptr;
  2098. lv2_atom_object_get(obj, uridPatchBody, &body, 0);
  2099. if (! body)
  2100. {
  2101. qWarning("Lv2Plugin::handleEventTransfer() - Has no body");
  2102. return;
  2103. }
  2104. LV2_ATOM_OBJECT_FOREACH(body, iter)
  2105. {
  2106. CustomDataType dtype = CUSTOM_DATA_INVALID;
  2107. const char* const key = getCustomURIString(iter->key);
  2108. const char* value = nullptr;
  2109. if (iter->value.type == CARLA_URI_MAP_ID_ATOM_STRING || iter->value.type == CARLA_URI_MAP_ID_ATOM_PATH)
  2110. {
  2111. value = strdup((const char*)LV2_ATOM_BODY(&iter->value));
  2112. dtype = (iter->value.type == CARLA_URI_MAP_ID_ATOM_STRING) ? CUSTOM_DATA_STRING : CUSTOM_DATA_PATH;
  2113. }
  2114. else if (iter->value.type == CARLA_URI_MAP_ID_ATOM_CHUNK || iter->value.type >= CARLA_URI_MAP_ID_COUNT)
  2115. {
  2116. QByteArray chunk((const char*)LV2_ATOM_BODY(&iter->value), iter->value.size);
  2117. value = strdup(chunk.toBase64().constData());
  2118. dtype = (iter->value.type == CARLA_URI_MAP_ID_ATOM_CHUNK) ? CUSTOM_DATA_CHUNK : CUSTOM_DATA_BINARY; // FIXME - binary should be a custom type
  2119. }
  2120. else
  2121. value = strdup("");
  2122. setCustomData(dtype, key, value, false);
  2123. free((void*)value);
  2124. }
  2125. }
  2126. void handleProgramChanged(int32_t index)
  2127. {
  2128. if (index == -1)
  2129. {
  2130. const CarlaPluginScopedDisabler m(this);
  2131. reloadPrograms(false);
  2132. }
  2133. else
  2134. {
  2135. if (ext.programs && index >= 0 && index < (int32_t)prog.count)
  2136. {
  2137. const char* progName = ext.programs->get_program(handle, index)->name;
  2138. if (prog.names[index])
  2139. free((void*)prog.names[index]);
  2140. prog.names[index] = strdup(progName);
  2141. }
  2142. }
  2143. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  2144. }
  2145. LV2_State_Status handleStateStore(uint32_t key, const void* const value, size_t size, uint32_t type, uint32_t flags)
  2146. {
  2147. assert(key > 0);
  2148. assert(value);
  2149. CustomDataType dtype;
  2150. const char* const uriKey = getCustomURIString(key);
  2151. if (type == CARLA_URI_MAP_ID_ATOM_CHUNK)
  2152. dtype = CUSTOM_DATA_CHUNK;
  2153. else if (type == CARLA_URI_MAP_ID_ATOM_PATH)
  2154. dtype = CUSTOM_DATA_PATH;
  2155. else if (type == CARLA_URI_MAP_ID_ATOM_STRING)
  2156. dtype = CUSTOM_DATA_STRING;
  2157. else if (type >= CARLA_URI_MAP_ID_COUNT)
  2158. dtype = CUSTOM_DATA_BINARY;
  2159. else
  2160. dtype = CUSTOM_DATA_INVALID;
  2161. // do basic checks
  2162. if (! uriKey)
  2163. {
  2164. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - Invalid key", key, value, size, type, flags);
  2165. return LV2_STATE_ERR_NO_PROPERTY;
  2166. }
  2167. if (! flags & LV2_STATE_IS_POD)
  2168. {
  2169. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - Invalid flags", key, value, size, type, flags);
  2170. return LV2_STATE_ERR_BAD_FLAGS;
  2171. }
  2172. if (dtype == CUSTOM_DATA_INVALID)
  2173. {
  2174. qCritical("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - Invalid type '%s'", key, value, size, type, flags, CustomDataType2str(dtype));
  2175. return LV2_STATE_ERR_BAD_TYPE;
  2176. }
  2177. // Check if we already have this key
  2178. for (size_t i=0; i < custom.size(); i++)
  2179. {
  2180. if (strcmp(custom[i].key, uriKey) == 0)
  2181. {
  2182. free((void*)custom[i].value);
  2183. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2184. custom[i].value = strdup((const char*)value);
  2185. else
  2186. custom[i].value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2187. return LV2_STATE_SUCCESS;
  2188. }
  2189. }
  2190. // Add a new one then
  2191. CustomData newData;
  2192. newData.type = dtype;
  2193. newData.key = strdup(uriKey);
  2194. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2195. newData.value = strdup((const char*)value);
  2196. else
  2197. newData.value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2198. custom.push_back(newData);
  2199. return LV2_STATE_SUCCESS;
  2200. }
  2201. const void* handleStateRetrieve(uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  2202. {
  2203. assert(key > 0);
  2204. const char* const uriKey = getCustomURIString(key);
  2205. if (! uriKey)
  2206. {
  2207. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - Failed to find key", key, size, type, flags);
  2208. return nullptr;
  2209. }
  2210. const char* stringData = nullptr;
  2211. CustomDataType dtype = CUSTOM_DATA_INVALID;
  2212. for (size_t i=0; i < custom.size(); i++)
  2213. {
  2214. if (strcmp(custom[i].key, uriKey) == 0)
  2215. {
  2216. dtype = custom[i].type;
  2217. stringData = custom[i].value;
  2218. break;
  2219. }
  2220. }
  2221. if (! stringData)
  2222. {
  2223. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - Invalid key '%s'", key, size, type, flags, uriKey);
  2224. return nullptr;
  2225. }
  2226. *size = 0;
  2227. *type = 0;
  2228. *flags = LV2_STATE_IS_POD;
  2229. if (dtype == CUSTOM_DATA_STRING)
  2230. {
  2231. *size = strlen(stringData);
  2232. *type = CARLA_URI_MAP_ID_ATOM_STRING;
  2233. return stringData;
  2234. }
  2235. if (dtype == CUSTOM_DATA_PATH)
  2236. {
  2237. *size = strlen(stringData);
  2238. *type = CARLA_URI_MAP_ID_ATOM_PATH;
  2239. return stringData;
  2240. }
  2241. if (dtype == CUSTOM_DATA_CHUNK || dtype == CUSTOM_DATA_BINARY)
  2242. {
  2243. static QByteArray chunk;
  2244. chunk = QByteArray::fromBase64(stringData);
  2245. *size = chunk.size();
  2246. *type = (dtype == CUSTOM_DATA_CHUNK) ? CARLA_URI_MAP_ID_ATOM_CHUNK : key;
  2247. return chunk.constData();
  2248. }
  2249. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - Invalid key type '%s'", key, size, type, flags, CustomDataType2str(dtype));
  2250. return nullptr;
  2251. }
  2252. LV2_Worker_Status handleWorkerSchedule(uint32_t /*size*/, const void* const /*data*/)
  2253. {
  2254. return LV2_WORKER_SUCCESS;
  2255. }
  2256. LV2_Worker_Status handleWorkerRespond(uint32_t /*size*/, const void* const /*data*/)
  2257. {
  2258. if (ext.worker)
  2259. return LV2_WORKER_SUCCESS;
  2260. return LV2_WORKER_ERR_UNKNOWN;
  2261. }
  2262. void handleExternalUiClosed()
  2263. {
  2264. if (ui.handle && ui.descriptor && ui.descriptor->cleanup)
  2265. ui.descriptor->cleanup(ui.handle);
  2266. ui.handle = nullptr;
  2267. x_engine->callback(CALLBACK_SHOW_GUI, m_id, 0, 0, 0.0);
  2268. }
  2269. uint32_t handleUiPortMap(const char* const symbol)
  2270. {
  2271. assert(symbol);
  2272. for (uint32_t i=0; i < rdf_descriptor->PortCount; i++)
  2273. {
  2274. if (strcmp(rdf_descriptor->Ports[i].Symbol, symbol) == 0)
  2275. return i;
  2276. }
  2277. return LV2UI_INVALID_PORT_INDEX;
  2278. }
  2279. int handleUiResize(int width, int height)
  2280. {
  2281. assert(width > 0);
  2282. assert(height > 0);
  2283. gui.width = width;
  2284. gui.height = height;
  2285. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 0.0);
  2286. return 0;
  2287. }
  2288. void handleUiWrite(uint32_t rindex, uint32_t bufferSize, uint32_t format, const void* buffer)
  2289. {
  2290. if (format == 0)
  2291. {
  2292. assert(bufferSize == sizeof(float));
  2293. float value = *(float*)buffer;
  2294. for (uint32_t i=0; i < param.count; i++)
  2295. {
  2296. if (param.data[i].rindex == (int32_t)rindex)
  2297. return setParameterValue(i, value, false, true, true);
  2298. }
  2299. }
  2300. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2301. {
  2302. // TODO
  2303. //LV2_Atom* atom = (LV2_Atom*)buffer;
  2304. //QByteArray chunk((const char*)buffer, buffer_size);
  2305. }
  2306. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2307. {
  2308. const LV2_Atom* const atom = (LV2_Atom*)buffer;
  2309. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2310. ui.descriptor->port_event(ui.handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  2311. }
  2312. }
  2313. // -------------------------------------------------------------------
  2314. #ifndef BUILD_BRIDGE
  2315. bool isUiBridgeable(uint32_t uiId)
  2316. {
  2317. const LV2_RDF_UI* const rdf_ui = &rdf_descriptor->UIs[uiId];
  2318. for (uint32_t i=0; i < rdf_ui->FeatureCount; i++)
  2319. {
  2320. if (strcmp(rdf_ui->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0 || strcmp(rdf_ui->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  2321. return false;
  2322. }
  2323. return true;
  2324. }
  2325. #endif
  2326. bool isUiResizable()
  2327. {
  2328. if (ui.rdf_descriptor)
  2329. {
  2330. for (uint32_t i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  2331. {
  2332. if (strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  2333. return false;
  2334. }
  2335. return true;
  2336. }
  2337. return false;
  2338. }
  2339. void initExternalUi()
  2340. {
  2341. qDebug("Lv2Plugin::initExternalUi()");
  2342. ui.widget = nullptr;
  2343. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  2344. if (ui.handle && ui.widget)
  2345. {
  2346. updateUi();
  2347. }
  2348. else
  2349. {
  2350. ui.handle = nullptr;
  2351. ui.widget = nullptr;
  2352. x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0);
  2353. }
  2354. }
  2355. void updateUi()
  2356. {
  2357. qDebug("Lv2Plugin::updateUi()");
  2358. ext.uiprograms = nullptr;
  2359. if (ui.handle && ui.descriptor)
  2360. {
  2361. if (ui.descriptor->extension_data)
  2362. {
  2363. ext.uiprograms = (LV2_Programs_UI_Interface*)ui.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  2364. if (ext.uiprograms && midiprog.count > 0 && midiprog.current >= 0)
  2365. ext.uiprograms->select_program(ui.handle, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  2366. }
  2367. if (ui.descriptor->port_event)
  2368. {
  2369. // update state (custom data)
  2370. for (size_t i=0; i < custom.size(); i++)
  2371. {
  2372. if (custom[i].type == CUSTOM_DATA_INVALID)
  2373. continue;
  2374. LV2_URID_Map* const URID_Map = (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  2375. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  2376. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  2377. Sratom* sratom = sratom_new(URID_Map);
  2378. SerdChunk chunk = { nullptr, 0 };
  2379. LV2_Atom_Forge forge;
  2380. lv2_atom_forge_init(&forge, URID_Map);
  2381. lv2_atom_forge_set_sink(&forge, sratom_forge_sink, sratom_forge_deref, &chunk);
  2382. LV2_Atom_Forge_Frame refFrame, bodyFrame;
  2383. LV2_Atom_Forge_Ref ref = lv2_atom_forge_blank(&forge, &refFrame, 1, uridPatchSet);
  2384. lv2_atom_forge_property_head(&forge, uridPatchBody, CARLA_URI_MAP_ID_NULL);
  2385. lv2_atom_forge_blank(&forge, &bodyFrame, 2, CARLA_URI_MAP_ID_NULL);
  2386. lv2_atom_forge_property_head(&forge, getCustomURID(custom[i].key), CARLA_URI_MAP_ID_NULL);
  2387. if (custom[i].type == CUSTOM_DATA_STRING)
  2388. lv2_atom_forge_string(&forge, custom[i].value, strlen(custom[i].value));
  2389. else if (custom[i].type == CUSTOM_DATA_PATH)
  2390. lv2_atom_forge_path(&forge, custom[i].value, strlen(custom[i].value));
  2391. else if (custom[i].type == CUSTOM_DATA_CHUNK)
  2392. lv2_atom_forge_literal(&forge, custom[i].value, strlen(custom[i].value), CARLA_URI_MAP_ID_ATOM_CHUNK, CARLA_URI_MAP_ID_NULL);
  2393. else
  2394. lv2_atom_forge_literal(&forge, custom[i].value, strlen(custom[i].value), getCustomURID(custom[i].key), CARLA_URI_MAP_ID_NULL);
  2395. lv2_atom_forge_pop(&forge, &bodyFrame);
  2396. lv2_atom_forge_pop(&forge, &refFrame);
  2397. const LV2_Atom* const atom = lv2_atom_forge_deref(&forge, ref);
  2398. ui.descriptor->port_event(ui.handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  2399. free((void*)chunk.buf);
  2400. sratom_free(sratom);
  2401. }
  2402. // update control ports
  2403. float value;
  2404. for (uint32_t i=0; i < param.count; i++)
  2405. {
  2406. value = getParameterValue(i);
  2407. ui.descriptor->port_event(ui.handle, param.data[i].rindex, sizeof(float), 0, &value);
  2408. }
  2409. }
  2410. }
  2411. }
  2412. // ----------------- DynParam Feature ------------------------------------------------
  2413. // TODO
  2414. // ----------------- Event Feature ---------------------------------------------------
  2415. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  2416. {
  2417. qDebug("Lv2Plugin::carla_lv2_event_ref(%p, %p)", callback_data, event);
  2418. assert(callback_data);
  2419. assert(event);
  2420. return 0;
  2421. }
  2422. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  2423. {
  2424. qDebug("Lv2Plugin::carla_lv2_event_unref(%p, %p)", callback_data, event);
  2425. assert(callback_data);
  2426. assert(event);
  2427. return 0;
  2428. }
  2429. // ----------------- Logs Feature ----------------------------------------------------
  2430. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  2431. {
  2432. qDebug("Lv2Plugin::carla_lv2_log_printf(%p, %i, %s, ...)", handle, type, fmt);
  2433. assert(handle);
  2434. assert(type > 0);
  2435. #ifndef DEBUG
  2436. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2437. return 0;
  2438. #endif
  2439. va_list args;
  2440. va_start(args, fmt);
  2441. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  2442. va_end(args);
  2443. return ret;
  2444. }
  2445. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  2446. {
  2447. qDebug("Lv2Plugin::carla_lv2_log_vprintf(%p, %i, %s, ...)", handle, type, fmt);
  2448. assert(handle);
  2449. assert(type > 0);
  2450. #ifndef DEBUG
  2451. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2452. return 0;
  2453. #endif
  2454. char buf[8196];
  2455. vsprintf(buf, fmt, ap);
  2456. if (*buf == 0)
  2457. return 0;
  2458. switch (type)
  2459. {
  2460. case CARLA_URI_MAP_ID_LOG_ERROR:
  2461. qCritical("%s", buf);
  2462. break;
  2463. case CARLA_URI_MAP_ID_LOG_NOTE:
  2464. printf("%s\n", buf);
  2465. break;
  2466. case CARLA_URI_MAP_ID_LOG_TRACE:
  2467. qDebug("%s", buf);
  2468. break;
  2469. case CARLA_URI_MAP_ID_LOG_WARNING:
  2470. qWarning("%s", buf);
  2471. break;
  2472. default:
  2473. break;
  2474. }
  2475. return strlen(buf);
  2476. }
  2477. // ----------------- Programs Feature ------------------------------------------------
  2478. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  2479. {
  2480. qDebug("Lv2Plugin::carla_lv2_program_changed(%p, %i)", handle, index);
  2481. assert(handle);
  2482. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2483. plugin->handleProgramChanged(index);
  2484. }
  2485. // ----------------- State Feature ---------------------------------------------------
  2486. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  2487. {
  2488. qDebug("Lv2Plugin::carla_lv2_state_make_path(%p, %p)", handle, path);
  2489. assert(handle);
  2490. assert(path);
  2491. QDir dir;
  2492. dir.mkpath(path);
  2493. return strdup(path);
  2494. }
  2495. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  2496. {
  2497. qDebug("Lv2Plugin::carla_lv2_state_map_abstract_path(%p, %p)", handle, absolute_path);
  2498. assert(handle);
  2499. assert(absolute_path);
  2500. QDir dir(absolute_path);
  2501. return strdup(dir.canonicalPath().toUtf8().constData());
  2502. }
  2503. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  2504. {
  2505. qDebug("Lv2Plugin::carla_lv2_state_map_absolute_path(%p, %p)", handle, abstract_path);
  2506. assert(handle);
  2507. assert(abstract_path);
  2508. QDir dir(abstract_path);
  2509. return strdup(dir.absolutePath().toUtf8().constData());
  2510. }
  2511. 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)
  2512. {
  2513. qDebug("Lv2Plugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  2514. assert(handle);
  2515. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2516. return plugin->handleStateStore(key, value, size, type, flags);
  2517. }
  2518. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  2519. {
  2520. qDebug("Lv2Plugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  2521. assert(handle);
  2522. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2523. return plugin->handleStateRetrieve(key, size, type, flags);
  2524. }
  2525. // ----------------- URI-Map Feature -------------------------------------------------
  2526. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  2527. {
  2528. qDebug("Lv2Plugin::carla_lv2_uri_to_id(%p, %s, %s)", data, map, uri);
  2529. return carla_lv2_urid_map(data, uri);
  2530. }
  2531. // ----------------- URID Feature ----------------------------------------------------
  2532. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  2533. {
  2534. qDebug("Lv2Plugin::carla_lv2_urid_map(%p, %s)", handle, uri);
  2535. assert(handle);
  2536. assert(uri);
  2537. // Atom types
  2538. if (strcmp(uri, LV2_ATOM__Chunk) == 0)
  2539. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  2540. if (strcmp(uri, LV2_ATOM__Path) == 0)
  2541. return CARLA_URI_MAP_ID_ATOM_PATH;
  2542. if (strcmp(uri, LV2_ATOM__Sequence) == 0)
  2543. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  2544. if (strcmp(uri, LV2_ATOM__String) == 0)
  2545. return CARLA_URI_MAP_ID_ATOM_STRING;
  2546. if (strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  2547. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  2548. if (strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  2549. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  2550. // Log types
  2551. if (strcmp(uri, LV2_LOG__Error) == 0)
  2552. return CARLA_URI_MAP_ID_LOG_ERROR;
  2553. if (strcmp(uri, LV2_LOG__Note) == 0)
  2554. return CARLA_URI_MAP_ID_LOG_NOTE;
  2555. if (strcmp(uri, LV2_LOG__Trace) == 0)
  2556. return CARLA_URI_MAP_ID_LOG_TRACE;
  2557. if (strcmp(uri, LV2_LOG__Warning) == 0)
  2558. return CARLA_URI_MAP_ID_LOG_WARNING;
  2559. // Others
  2560. if (strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  2561. return CARLA_URI_MAP_ID_MIDI_EVENT;
  2562. // Custom types
  2563. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2564. return plugin->getCustomURID(uri);
  2565. }
  2566. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  2567. {
  2568. qDebug("Lv2Plugin::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  2569. assert(handle);
  2570. assert(urid > 0);
  2571. // Atom types
  2572. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  2573. return LV2_ATOM__Chunk;
  2574. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  2575. return LV2_ATOM__Path;
  2576. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  2577. return LV2_ATOM__Sequence;
  2578. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  2579. return LV2_ATOM__String;
  2580. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2581. return LV2_ATOM__atomTransfer;
  2582. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2583. return LV2_ATOM__eventTransfer;
  2584. // Log types
  2585. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  2586. return LV2_LOG__Error;
  2587. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  2588. return LV2_LOG__Note;
  2589. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  2590. return LV2_LOG__Trace;
  2591. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  2592. return LV2_LOG__Warning;
  2593. // Others
  2594. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  2595. return LV2_MIDI__MidiEvent;
  2596. // Custom types
  2597. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2598. return plugin->getCustomURIString(urid);
  2599. }
  2600. // ----------------- Worker Feature --------------------------------------------------
  2601. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  2602. {
  2603. qDebug("Lv2Plugin::carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  2604. assert(handle);
  2605. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2606. return plugin->handleWorkerSchedule(size, data);
  2607. }
  2608. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  2609. {
  2610. qDebug("Lv2Plugin::carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  2611. assert(handle);
  2612. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2613. return plugin->handleWorkerRespond(size, data);
  2614. }
  2615. // ----------------- UI Port-Map Feature ---------------------------------------------
  2616. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  2617. {
  2618. qDebug("Lv2Plugin::carla_lv2_ui_port_map(%p, %s)", handle, symbol);
  2619. assert(handle);
  2620. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2621. return plugin->handleUiPortMap(symbol);
  2622. }
  2623. // ----------------- UI Resize Feature -----------------------------------------------
  2624. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  2625. {
  2626. qDebug("Lv2Plugin::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  2627. assert(handle);
  2628. Lv2Plugin* plugin = (Lv2Plugin*)handle;
  2629. return plugin->handleUiResize(width, height);
  2630. }
  2631. // ----------------- External UI Feature ---------------------------------------------
  2632. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  2633. {
  2634. qDebug("Lv2Plugin::carla_lv2_external_ui_closed(%p)", controller);
  2635. assert(controller);
  2636. Lv2Plugin* plugin = (Lv2Plugin*)controller;
  2637. plugin->handleExternalUiClosed();
  2638. }
  2639. // ----------------- UI Extension ----------------------------------------------------
  2640. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  2641. {
  2642. qDebug("Lv2Plugin::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  2643. assert(controller);
  2644. Lv2Plugin* plugin = (Lv2Plugin*)controller;
  2645. plugin->handleUiWrite(port_index, buffer_size, format, buffer);
  2646. }
  2647. // -------------------------------------------------------------------
  2648. bool uiLibOpen(const char* filename)
  2649. {
  2650. ui.lib = lib_open(filename);
  2651. return bool(ui.lib);
  2652. }
  2653. bool uiLibClose()
  2654. {
  2655. if (ui.lib)
  2656. return lib_close(ui.lib);
  2657. return false;
  2658. }
  2659. void* uiLibSymbol(const char* symbol)
  2660. {
  2661. if (ui.lib)
  2662. return lib_symbol(ui.lib, symbol);
  2663. return nullptr;
  2664. }
  2665. // -------------------------------------------------------------------
  2666. bool init(const char* bundle, const char* const name, const char* URI)
  2667. {
  2668. // ---------------------------------------------------------------
  2669. // get plugin from lv2_rdf (lilv)
  2670. rdf_descriptor = lv2_rdf_new(URI);
  2671. if (! rdf_descriptor)
  2672. {
  2673. setLastError("Failed to find the requested plugin in the LV2 Bundle");
  2674. return false;
  2675. }
  2676. // ---------------------------------------------------------------
  2677. // open DLL
  2678. if (! libOpen(rdf_descriptor->Binary))
  2679. {
  2680. setLastError(libError(rdf_descriptor->Binary));
  2681. return false;
  2682. }
  2683. // ---------------------------------------------------------------
  2684. // get DLL main entry
  2685. LV2_Descriptor_Function descfn = (LV2_Descriptor_Function)libSymbol("lv2_descriptor");
  2686. if (! descfn)
  2687. {
  2688. setLastError("Could not find the LV2 Descriptor in the plugin library");
  2689. return false;
  2690. }
  2691. // ---------------------------------------------------------------
  2692. // get descriptor that matches URI
  2693. uint32_t i = 0;
  2694. while ((descriptor = descfn(i++)))
  2695. {
  2696. if (strcmp(descriptor->URI, URI) == 0)
  2697. break;
  2698. }
  2699. if (! descriptor)
  2700. {
  2701. setLastError("Could not find the requested plugin URI in the plugin library");
  2702. return false;
  2703. }
  2704. // ---------------------------------------------------------------
  2705. // check supported port-types and features
  2706. bool canContinue = true;
  2707. // Check supported ports
  2708. for (i=0; i < rdf_descriptor->PortCount; i++)
  2709. {
  2710. LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  2711. 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)
  2712. {
  2713. qCritical("Got unsupported port -> %i", PortType);
  2714. if (! LV2_IS_PORT_OPTIONAL(rdf_descriptor->Ports[i].Properties))
  2715. {
  2716. setLastError("Plugin requires a port that is not currently supported");
  2717. canContinue = false;
  2718. break;
  2719. }
  2720. }
  2721. }
  2722. // Check supported features
  2723. for (i=0; i < rdf_descriptor->FeatureCount; i++)
  2724. {
  2725. if (LV2_IS_FEATURE_REQUIRED(rdf_descriptor->Features[i].Type) && is_lv2_feature_supported(rdf_descriptor->Features[i].URI) == false)
  2726. {
  2727. QString msg = QString("Plugin requires a feature that is not supported:\n%1").arg(rdf_descriptor->Features[i].URI);
  2728. setLastError(msg.toUtf8().constData());
  2729. canContinue = false;
  2730. break;
  2731. }
  2732. }
  2733. // Check extensions
  2734. for (i=0; i < rdf_descriptor->ExtensionCount; i++)
  2735. {
  2736. if (strcmp(rdf_descriptor->Extensions[i], LV2DYNPARAM_URI) == 0)
  2737. m_hints |= PLUGIN_HAS_EXTENSION_DYNPARAM;
  2738. else if (strcmp(rdf_descriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  2739. m_hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  2740. else if (strcmp(rdf_descriptor->Extensions[i], LV2_STATE__interface) == 0)
  2741. m_hints |= PLUGIN_HAS_EXTENSION_STATE;
  2742. else if (strcmp(rdf_descriptor->Extensions[i], LV2_WORKER__interface) == 0)
  2743. m_hints |= PLUGIN_HAS_EXTENSION_WORKER;
  2744. else
  2745. qDebug("Plugin has non-supported extension: '%s'", rdf_descriptor->Extensions[i]);
  2746. }
  2747. if (! canContinue)
  2748. {
  2749. // error already set
  2750. return false;
  2751. }
  2752. // ---------------------------------------------------------------
  2753. // initialize features
  2754. LV2_Event_Feature* Event_Feature = new LV2_Event_Feature;
  2755. Event_Feature->callback_data = this;
  2756. Event_Feature->lv2_event_ref = carla_lv2_event_ref;
  2757. Event_Feature->lv2_event_unref = carla_lv2_event_unref;
  2758. LV2_Log_Log* Log_Feature = new LV2_Log_Log;
  2759. Log_Feature->handle = this;
  2760. Log_Feature->printf = carla_lv2_log_printf;
  2761. Log_Feature->vprintf = carla_lv2_log_vprintf;
  2762. LV2_Programs_Host* Programs_Feature = new LV2_Programs_Host;
  2763. Programs_Feature->handle = this;
  2764. Programs_Feature->program_changed = carla_lv2_program_changed;
  2765. LV2_State_Make_Path* State_MakePath_Feature = new LV2_State_Make_Path;
  2766. State_MakePath_Feature->handle = this;
  2767. State_MakePath_Feature->path = carla_lv2_state_make_path;
  2768. LV2_State_Map_Path* State_MapPath_Feature = new LV2_State_Map_Path;
  2769. State_MapPath_Feature->handle = this;
  2770. State_MapPath_Feature->abstract_path = carla_lv2_state_map_abstract_path;
  2771. State_MapPath_Feature->absolute_path = carla_lv2_state_map_absolute_path;
  2772. LV2_URI_Map_Feature* URI_Map_Feature = new LV2_URI_Map_Feature;
  2773. URI_Map_Feature->callback_data = this;
  2774. URI_Map_Feature->uri_to_id = carla_lv2_uri_to_id;
  2775. LV2_URID_Map* URID_Map_Feature = new LV2_URID_Map;
  2776. URID_Map_Feature->handle = this;
  2777. URID_Map_Feature->map = carla_lv2_urid_map;
  2778. LV2_URID_Unmap* URID_Unmap_Feature = new LV2_URID_Unmap;
  2779. URID_Unmap_Feature->handle = this;
  2780. URID_Unmap_Feature->unmap = carla_lv2_urid_unmap;
  2781. LV2_Worker_Schedule* Worker_Feature = new LV2_Worker_Schedule;
  2782. Worker_Feature->handle = this;
  2783. Worker_Feature->schedule_work = carla_lv2_worker_schedule;
  2784. lv2_rtsafe_memory_pool_provider* RT_MemPool_Feature = new lv2_rtsafe_memory_pool_provider;
  2785. rtmempool_allocator_init(RT_MemPool_Feature);
  2786. features[lv2_feature_id_event] = new LV2_Feature;
  2787. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  2788. features[lv2_feature_id_event]->data = Event_Feature;
  2789. features[lv2_feature_id_logs] = new LV2_Feature;
  2790. features[lv2_feature_id_logs]->URI = LV2_LOG__log;
  2791. features[lv2_feature_id_logs]->data = Log_Feature;
  2792. features[lv2_feature_id_programs] = new LV2_Feature;
  2793. features[lv2_feature_id_programs]->URI = LV2_PROGRAMS__Host;
  2794. features[lv2_feature_id_programs]->data = Programs_Feature;
  2795. features[lv2_feature_id_rtmempool] = new LV2_Feature;
  2796. features[lv2_feature_id_rtmempool]->URI = LV2_RTSAFE_MEMORY_POOL_URI;
  2797. features[lv2_feature_id_rtmempool]->data = RT_MemPool_Feature;
  2798. features[lv2_feature_id_state_make_path] = new LV2_Feature;
  2799. features[lv2_feature_id_state_make_path]->URI = LV2_STATE__makePath;
  2800. features[lv2_feature_id_state_make_path]->data = State_MakePath_Feature;
  2801. features[lv2_feature_id_state_map_path] = new LV2_Feature;
  2802. features[lv2_feature_id_state_map_path]->URI = LV2_STATE__mapPath;
  2803. features[lv2_feature_id_state_map_path]->data = State_MapPath_Feature;
  2804. features[lv2_feature_id_strict_bounds] = new LV2_Feature;
  2805. features[lv2_feature_id_strict_bounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  2806. features[lv2_feature_id_strict_bounds]->data = nullptr;
  2807. features[lv2_feature_id_uri_map] = new LV2_Feature;
  2808. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  2809. features[lv2_feature_id_uri_map]->data = URI_Map_Feature;
  2810. features[lv2_feature_id_urid_map] = new LV2_Feature;
  2811. features[lv2_feature_id_urid_map]->URI = LV2_URID__map;
  2812. features[lv2_feature_id_urid_map]->data = URID_Map_Feature;
  2813. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  2814. features[lv2_feature_id_urid_unmap]->URI = LV2_URID__unmap;
  2815. features[lv2_feature_id_urid_unmap]->data = URID_Unmap_Feature;
  2816. features[lv2_feature_id_worker] = new LV2_Feature;
  2817. features[lv2_feature_id_worker]->URI = LV2_WORKER__schedule;
  2818. features[lv2_feature_id_worker]->data = Worker_Feature;
  2819. // ---------------------------------------------------------------
  2820. // initialize plugin
  2821. handle = descriptor->instantiate(descriptor, x_engine->getSampleRate(), rdf_descriptor->Bundle, features);
  2822. if (! handle)
  2823. {
  2824. setLastError("Plugin failed to initialize");
  2825. return false;
  2826. }
  2827. // ---------------------------------------------------------------
  2828. // get info
  2829. m_filename = strdup(bundle);
  2830. if (name)
  2831. m_name = x_engine->getUniqueName(name);
  2832. else
  2833. m_name = x_engine->getUniqueName(rdf_descriptor->Name);
  2834. // ---------------------------------------------------------------
  2835. // register client
  2836. x_client = x_engine->addClient(this);
  2837. if (! x_client->isOk())
  2838. {
  2839. setLastError("Failed to register plugin client");
  2840. return false;
  2841. }
  2842. // ---------------------------------------------------------------
  2843. // gui stuff
  2844. if (rdf_descriptor->UICount > 0)
  2845. {
  2846. qDebug("Has UI");
  2847. // -----------------------------------------------------------
  2848. // find more appropriate ui
  2849. int eQt4, eHWND, eX11, eGtk2, iHWND, iX11, iQt4, iExt, iSuil, iFinal;
  2850. eQt4 = eHWND = eX11 = eGtk2 = iQt4 = iHWND = iX11 = iExt = iSuil = iFinal = -1;
  2851. for (i=0; i < rdf_descriptor->UICount; i++)
  2852. {
  2853. switch (rdf_descriptor->UIs[i].Type)
  2854. {
  2855. case LV2_UI_QT4:
  2856. #ifndef BUILD_BRIDGE
  2857. if (isUiBridgeable(i) && carlaOptions.prefer_ui_bridges)
  2858. eQt4 = i;
  2859. #endif
  2860. iQt4 = i;
  2861. break;
  2862. case LV2_UI_WINDOWS:
  2863. #ifndef BUILD_BRIDGE
  2864. if (isUiBridgeable(i) && carlaOptions.prefer_ui_bridges)
  2865. eHWND = i;
  2866. #endif
  2867. iHWND = i;
  2868. break;
  2869. case LV2_UI_X11:
  2870. #ifndef BUILD_BRIDGE
  2871. if (isUiBridgeable(i) && carlaOptions.prefer_ui_bridges)
  2872. eX11 = i;
  2873. #endif
  2874. iX11 = i;
  2875. break;
  2876. case LV2_UI_GTK2:
  2877. #ifdef BUILD_BRIDGE
  2878. if (false)
  2879. #else
  2880. # ifdef HAVE_SUIL
  2881. if (isUiBridgeable(i) && carlaOptions.prefer_ui_bridges)
  2882. # else
  2883. if (isUiBridgeable(i))
  2884. # endif
  2885. #endif
  2886. eGtk2 = i;
  2887. #ifdef HAVE_SUIL
  2888. iSuil = i;
  2889. #endif
  2890. break;
  2891. case LV2_UI_EXTERNAL:
  2892. case LV2_UI_OLD_EXTERNAL:
  2893. iExt = i;
  2894. break;
  2895. default:
  2896. break;
  2897. }
  2898. }
  2899. if (eQt4 >= 0)
  2900. iFinal = eQt4;
  2901. else if (eHWND >= 0)
  2902. iFinal = eHWND;
  2903. else if (eX11 >= 0)
  2904. iFinal = eX11;
  2905. else if (eGtk2 >= 0)
  2906. iFinal = eGtk2;
  2907. else if (iQt4 >= 0)
  2908. iFinal = iQt4;
  2909. else if (iHWND >= 0)
  2910. iFinal = iHWND;
  2911. else if (iX11 >= 0)
  2912. iFinal = iX11;
  2913. else if (iExt >= 0)
  2914. iFinal = iExt;
  2915. else if (iSuil >= 0)
  2916. iFinal = iSuil;
  2917. #ifndef BUILD_BRIDGE
  2918. bool isBridged = (iFinal == eQt4 || iFinal == eX11 || iFinal == eGtk2);
  2919. #endif
  2920. #ifdef HAVE_SUIL
  2921. bool isSuil = (iFinal == iSuil && !isBridged);
  2922. #endif
  2923. if (iFinal < 0)
  2924. {
  2925. qWarning("Failed to find an appropriate LV2 UI for this plugin");
  2926. return true;
  2927. }
  2928. ui.rdf_descriptor = &rdf_descriptor->UIs[iFinal];
  2929. // -----------------------------------------------------------
  2930. // check supported ui features
  2931. canContinue = true;
  2932. for (i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  2933. {
  2934. if (LV2_IS_FEATURE_REQUIRED(ui.rdf_descriptor->Features[i].Type) && is_lv2_ui_feature_supported(ui.rdf_descriptor->Features[i].URI) == false)
  2935. {
  2936. qCritical("Plugin UI requires a feature that is not supported:\n%s", ui.rdf_descriptor->Features[i].URI);
  2937. canContinue = false;
  2938. break;
  2939. }
  2940. }
  2941. if (! canContinue)
  2942. {
  2943. ui.rdf_descriptor = nullptr;
  2944. return true;
  2945. }
  2946. #ifdef HAVE_SUIL
  2947. if (isSuil)
  2948. {
  2949. // -------------------------------------------------------
  2950. // init suil host
  2951. suil.host = suil_host_new(carla_lv2_ui_write_function, carla_lv2_ui_port_map, nullptr, nullptr);
  2952. }
  2953. else
  2954. #endif
  2955. {
  2956. // -------------------------------------------------------
  2957. // open DLL
  2958. if (! uiLibOpen(ui.rdf_descriptor->Binary))
  2959. {
  2960. qCritical("Could not load UI library, error was:\n%s", libError(ui.rdf_descriptor->Binary));
  2961. ui.rdf_descriptor = nullptr;
  2962. return true;
  2963. }
  2964. // -------------------------------------------------------
  2965. // get DLL main entry
  2966. LV2UI_DescriptorFunction ui_descfn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor");
  2967. if (! ui_descfn)
  2968. {
  2969. qCritical("Could not find the LV2UI Descriptor in the UI library");
  2970. uiLibClose();
  2971. ui.lib = nullptr;
  2972. ui.rdf_descriptor = nullptr;
  2973. return true;
  2974. }
  2975. // -------------------------------------------------------
  2976. // get descriptor that matches URI
  2977. i = 0;
  2978. while ((ui.descriptor = ui_descfn(i++)))
  2979. {
  2980. if (strcmp(ui.descriptor->URI, ui.rdf_descriptor->URI) == 0)
  2981. break;
  2982. }
  2983. if (! ui.descriptor)
  2984. {
  2985. qCritical("Could not find the requested GUI in the plugin UI library");
  2986. uiLibClose();
  2987. ui.lib = nullptr;
  2988. ui.rdf_descriptor = nullptr;
  2989. return true;
  2990. }
  2991. }
  2992. // -----------------------------------------------------------
  2993. // initialize ui according to type
  2994. LV2_Property UiType = ui.rdf_descriptor->Type;
  2995. #ifndef BUILD_BRIDGE
  2996. if (isBridged)
  2997. {
  2998. // -------------------------------------------------------
  2999. // initialize ui bridge
  3000. const char* const oscBinary = lv2bridge2str(UiType);
  3001. qDebug("Has UI - is bridge, uitype = %i : %s", UiType, oscBinary);
  3002. if (oscBinary)
  3003. {
  3004. qDebug("Has UI - has binary");
  3005. gui.type = GUI_EXTERNAL_OSC;
  3006. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  3007. osc.thread->setOscData(oscBinary, descriptor->URI, ui.descriptor->URI);
  3008. }
  3009. else
  3010. qDebug("Has UI - NOT binary");
  3011. }
  3012. else
  3013. #endif
  3014. {
  3015. // -------------------------------------------------------
  3016. // initialize ui features
  3017. QString guiTitle = QString("%1 (GUI)").arg(m_name);
  3018. LV2_Extension_Data_Feature* UI_Data_Feature = new LV2_Extension_Data_Feature;
  3019. UI_Data_Feature->data_access = descriptor->extension_data;
  3020. LV2UI_Port_Map* UI_PortMap_Feature = new LV2UI_Port_Map;
  3021. UI_PortMap_Feature->handle = this;
  3022. UI_PortMap_Feature->port_index = carla_lv2_ui_port_map;
  3023. LV2UI_Resize* UI_Resize_Feature = new LV2UI_Resize;
  3024. UI_Resize_Feature->handle = this;
  3025. UI_Resize_Feature->ui_resize = carla_lv2_ui_resize;
  3026. lv2_external_ui_host* External_UI_Feature = new lv2_external_ui_host;
  3027. External_UI_Feature->ui_closed = carla_lv2_external_ui_closed;
  3028. External_UI_Feature->plugin_human_id = strdup(guiTitle.toUtf8().constData());
  3029. features[lv2_feature_id_data_access] = new LV2_Feature;
  3030. features[lv2_feature_id_data_access]->URI = LV2_DATA_ACCESS_URI;
  3031. features[lv2_feature_id_data_access]->data = UI_Data_Feature;
  3032. features[lv2_feature_id_instance_access] = new LV2_Feature;
  3033. features[lv2_feature_id_instance_access]->URI = LV2_INSTANCE_ACCESS_URI;
  3034. features[lv2_feature_id_instance_access]->data = handle;
  3035. features[lv2_feature_id_ui_parent] = new LV2_Feature;
  3036. features[lv2_feature_id_ui_parent]->URI = LV2_UI__parent;
  3037. features[lv2_feature_id_ui_parent]->data = nullptr;
  3038. features[lv2_feature_id_ui_port_map] = new LV2_Feature;
  3039. features[lv2_feature_id_ui_port_map]->URI = LV2_UI__portMap;
  3040. features[lv2_feature_id_ui_port_map]->data = UI_PortMap_Feature;
  3041. features[lv2_feature_id_ui_resize] = new LV2_Feature;
  3042. features[lv2_feature_id_ui_resize]->URI = LV2_UI__resize;
  3043. features[lv2_feature_id_ui_resize]->data = UI_Resize_Feature;
  3044. features[lv2_feature_id_external_ui] = new LV2_Feature;
  3045. features[lv2_feature_id_external_ui]->URI = LV2_EXTERNAL_UI_URI;
  3046. features[lv2_feature_id_external_ui]->data = External_UI_Feature;
  3047. features[lv2_feature_id_external_ui_old] = new LV2_Feature;
  3048. features[lv2_feature_id_external_ui_old]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3049. features[lv2_feature_id_external_ui_old]->data = External_UI_Feature;
  3050. // -------------------------------------------------------
  3051. // initialize ui
  3052. switch (UiType)
  3053. {
  3054. case LV2_UI_QT4:
  3055. qDebug("Will use LV2 Qt4 UI");
  3056. gui.type = GUI_INTERNAL_QT4;
  3057. gui.resizable = isUiResizable();
  3058. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  3059. updateUi();
  3060. break;
  3061. case LV2_UI_COCOA:
  3062. qDebug("Will use LV2 Cocoa UI");
  3063. gui.type = GUI_INTERNAL_COCOA;
  3064. gui.resizable = isUiResizable();
  3065. break;
  3066. case LV2_UI_WINDOWS:
  3067. qDebug("Will use LV2 Windows UI");
  3068. gui.type = GUI_INTERNAL_HWND;
  3069. gui.resizable = isUiResizable();
  3070. break;
  3071. case LV2_UI_X11:
  3072. qDebug("Will use LV2 X11 UI");
  3073. gui.type = GUI_INTERNAL_X11;
  3074. gui.resizable = isUiResizable();
  3075. break;
  3076. case LV2_UI_GTK2:
  3077. #ifdef HAVE_SUIL
  3078. qDebug("Will use LV2 Gtk2 UI (suil)");
  3079. gui.type = GUI_EXTERNAL_SUIL;
  3080. gui.resizable = isUiResizable();
  3081. 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);
  3082. if (suil.handle)
  3083. {
  3084. ui.handle = ((SuilInstanceImpl*)suil.handle)->handle;
  3085. ui.descriptor = ((SuilInstanceImpl*)suil.handle)->descriptor;
  3086. ui.widget = suil_instance_get_widget(suil.handle);
  3087. if (ui.widget)
  3088. {
  3089. QWidget* widget = (QWidget*)ui.widget;
  3090. widget->setWindowTitle(guiTitle);
  3091. // FIXME - need a proper way for this
  3092. if (strcmp(ui.rdf_descriptor->URI, "http://factorial.hu/plugins/lv2/ir/gui") == 0)
  3093. widget->resize(930, 460);
  3094. }
  3095. }
  3096. #else
  3097. qDebug("Will use LV2 Gtk2 UI, NOT!");
  3098. #endif
  3099. break;
  3100. case LV2_UI_EXTERNAL:
  3101. case LV2_UI_OLD_EXTERNAL:
  3102. qDebug("Will use LV2 External UI");
  3103. gui.type = GUI_EXTERNAL_LV2;
  3104. break;
  3105. default:
  3106. break;
  3107. }
  3108. }
  3109. if (gui.type != GUI_NONE)
  3110. m_hints |= PLUGIN_HAS_GUI;
  3111. } // End of GUI Stuff
  3112. return true;
  3113. }
  3114. private:
  3115. LV2_Handle handle, h2;
  3116. const LV2_Descriptor* descriptor;
  3117. const LV2_RDF_Descriptor* rdf_descriptor;
  3118. LV2_Feature* features[lv2_feature_count+1];
  3119. struct {
  3120. lv2dynparam_plugin_callbacks* dynparam;
  3121. LV2_State_Interface* state;
  3122. LV2_Worker_Interface* worker;
  3123. LV2_Programs_Interface* programs;
  3124. LV2_Programs_UI_Interface* uiprograms;
  3125. } ext;
  3126. struct {
  3127. void* lib;
  3128. LV2UI_Handle handle;
  3129. LV2UI_Widget widget;
  3130. const LV2UI_Descriptor* descriptor;
  3131. const LV2_RDF_UI* rdf_descriptor;
  3132. } ui;
  3133. struct {
  3134. GuiType type;
  3135. bool resizable;
  3136. int width;
  3137. int height;
  3138. } gui;
  3139. #ifdef HAVE_SUIL
  3140. struct {
  3141. SuilHost* host;
  3142. SuilInstance* handle;
  3143. QByteArray pos;
  3144. } suil;
  3145. #endif
  3146. PluginEventData evin;
  3147. PluginEventData evout;
  3148. Lv2ParameterData* lv2param;
  3149. std::vector<const char*> customURIDs;
  3150. };
  3151. CarlaPlugin* CarlaPlugin::newLV2(const initializer& init)
  3152. {
  3153. qDebug("CarlaPlugin::newLV2(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  3154. short id = init.engine->getNewPluginId();
  3155. if (id < 0)
  3156. {
  3157. setLastError("Maximum number of plugins reached");
  3158. return nullptr;
  3159. }
  3160. Lv2Plugin* const plugin = new Lv2Plugin(init.engine, id);
  3161. if (! plugin->init(init.filename, init.name, init.label))
  3162. {
  3163. delete plugin;
  3164. return nullptr;
  3165. }
  3166. plugin->reload();
  3167. #ifndef BUILD_BRIDGE
  3168. if (carlaOptions.process_mode == PROCESS_MODE_CONTINUOUS_RACK)
  3169. {
  3170. const uint32_t ins = plugin->audioInCount();
  3171. const uint32_t outs = plugin->audioOutCount();
  3172. if (ins > 2 || outs > 2 || (ins != outs && ins != 0 && outs != 0))
  3173. {
  3174. setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  3175. delete plugin;
  3176. return nullptr;
  3177. }
  3178. }
  3179. #endif
  3180. plugin->registerToOsc();
  3181. return plugin;
  3182. }
  3183. /**@}*/
  3184. // -------------------------------------------------------------------------------------------------------------------
  3185. int CarlaOsc::handle_lv2_atom_transfer(CARLA_OSC_HANDLE_ARGS2)
  3186. {
  3187. qDebug("CarlaOsc::handle_lv2_atom_transfer()");
  3188. //CARLA_OSC_CHECK_OSC_TYPES(2, "ii");
  3189. CarlaBackend::Lv2Plugin* lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3190. lv2plugin->handleAtomTransfer();
  3191. return 0;
  3192. Q_UNUSED(argc);
  3193. Q_UNUSED(argv);
  3194. Q_UNUSED(types);
  3195. }
  3196. int CarlaOsc::handle_lv2_event_transfer(CARLA_OSC_HANDLE_ARGS2)
  3197. {
  3198. qDebug("CarlaOsc::handle_lv2_event_transfer()");
  3199. CARLA_OSC_CHECK_OSC_TYPES(3, "sss");
  3200. CarlaBackend::Lv2Plugin* lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3201. const char* type = (const char*)&argv[0]->s;
  3202. const char* key = (const char*)&argv[1]->s;
  3203. const char* value = (const char*)&argv[2]->s;
  3204. lv2plugin->handleEventTransfer(type, key, value);
  3205. return 0;
  3206. }
  3207. CARLA_BACKEND_END_NAMESPACE