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.

4503 lines
158KB

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