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.

4161 lines
142KB

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