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.

4709 lines
165KB

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