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.

4339 lines
150KB

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