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.

4338 lines
151KB

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