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.

4677 lines
163KB

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