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.

4529 lines
159KB

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