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.

3921 lines
135KB

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