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.

4345 lines
151KB

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