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.

4707 lines
164KB

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