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.

4713 lines
165KB

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