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.

4721 lines
165KB

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