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.

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