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.

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