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.

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