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.

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