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.

4679 lines
163KB

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