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.

4593 lines
161KB

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