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.

4294 lines
148KB

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