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.

4441 lines
156KB

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