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
132KB

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