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.

4519 lines
159KB

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