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.

4500 lines
158KB

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