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.

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