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.

4686 lines
164KB

  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. #ifndef BUILD_BRIDGE
  1698. if (aIn.count > 0 && carlaOptions.processMode != PROCESS_MODE_CONTINUOUS_RACK)
  1699. #else
  1700. if (aIn.count > 0)
  1701. #endif
  1702. {
  1703. if (aIn.count == 1)
  1704. {
  1705. for (k=0; k < frames; k++)
  1706. {
  1707. if (abs(inBuffer[0][k]) > aInsPeak[0])
  1708. aInsPeak[0] = abs(inBuffer[0][k]);
  1709. }
  1710. }
  1711. else if (aIn.count > 1)
  1712. {
  1713. for (k=0; k < frames; k++)
  1714. {
  1715. if (abs(inBuffer[0][k]) > aInsPeak[0])
  1716. aInsPeak[0] = abs(inBuffer[0][k]);
  1717. if (abs(inBuffer[1][k]) > aInsPeak[1])
  1718. aInsPeak[1] = abs(inBuffer[1][k]);
  1719. }
  1720. }
  1721. }
  1722. CARLA_PROCESS_CONTINUE_CHECK;
  1723. // --------------------------------------------------------------------------------------------------------
  1724. // Parameters Input [Automation]
  1725. if (param.portCin && m_active && m_activeBefore)
  1726. {
  1727. bool allNotesOffSent = false;
  1728. const CarlaEngineControlEvent* cinEvent;
  1729. uint32_t time, nEvents = param.portCin->getEventCount();
  1730. uint32_t nextBankId = 0;
  1731. if (midiprog.current >= 0 && midiprog.count > 0)
  1732. nextBankId = midiprog.data[midiprog.current].bank;
  1733. for (i=0; i < nEvents; i++)
  1734. {
  1735. cinEvent = param.portCin->getEvent(i);
  1736. if (! cinEvent)
  1737. continue;
  1738. time = cinEvent->time - framesOffset;
  1739. if (time >= frames)
  1740. continue;
  1741. // Control change
  1742. switch (cinEvent->type)
  1743. {
  1744. case CarlaEngineEventNull:
  1745. break;
  1746. case CarlaEngineEventControlChange:
  1747. {
  1748. double value;
  1749. // Control backend stuff
  1750. if (cinEvent->channel == m_ctrlInChannel)
  1751. {
  1752. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  1753. {
  1754. value = cinEvent->value;
  1755. setDryWet(value, false, false);
  1756. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  1757. continue;
  1758. }
  1759. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  1760. {
  1761. value = cinEvent->value*127/100;
  1762. setVolume(value, false, false);
  1763. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  1764. continue;
  1765. }
  1766. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  1767. {
  1768. double left, right;
  1769. value = cinEvent->value/0.5 - 1.0;
  1770. if (value < 0.0)
  1771. {
  1772. left = -1.0;
  1773. right = (value*2)+1.0;
  1774. }
  1775. else if (value > 0.0)
  1776. {
  1777. left = (value*2)-1.0;
  1778. right = 1.0;
  1779. }
  1780. else
  1781. {
  1782. left = -1.0;
  1783. right = 1.0;
  1784. }
  1785. setBalanceLeft(left, false, false);
  1786. setBalanceRight(right, false, false);
  1787. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1788. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1789. continue;
  1790. }
  1791. }
  1792. // Control plugin parameters
  1793. for (k=0; k < param.count; k++)
  1794. {
  1795. if (param.data[k].midiChannel != cinEvent->channel)
  1796. continue;
  1797. if (param.data[k].midiCC != cinEvent->controller)
  1798. continue;
  1799. if (param.data[k].type != PARAMETER_INPUT)
  1800. continue;
  1801. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  1802. {
  1803. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1804. {
  1805. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  1806. }
  1807. else
  1808. {
  1809. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  1810. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  1811. value = rint(value);
  1812. }
  1813. setParameterValue(k, value, false, false, false);
  1814. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  1815. }
  1816. }
  1817. break;
  1818. }
  1819. case CarlaEngineEventMidiBankChange:
  1820. if (cinEvent->channel == m_ctrlInChannel)
  1821. nextBankId = rint(cinEvent->value);
  1822. break;
  1823. case CarlaEngineEventMidiProgramChange:
  1824. if (cinEvent->channel == m_ctrlInChannel)
  1825. {
  1826. uint32_t nextProgramId = rint(cinEvent->value);
  1827. for (k=0; k < midiprog.count; k++)
  1828. {
  1829. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  1830. {
  1831. setMidiProgram(k, false, false, false, false);
  1832. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  1833. break;
  1834. }
  1835. }
  1836. }
  1837. break;
  1838. case CarlaEngineEventAllSoundOff:
  1839. if (cinEvent->channel == m_ctrlInChannel)
  1840. {
  1841. if (evIn.count > 0 && ! allNotesOffSent)
  1842. sendMidiAllNotesOff();
  1843. if (descriptor->deactivate)
  1844. {
  1845. descriptor->deactivate(handle);
  1846. if (h2) descriptor->deactivate(h2);
  1847. }
  1848. if (descriptor->activate)
  1849. {
  1850. descriptor->activate(handle);
  1851. if (h2) descriptor->activate(h2);
  1852. }
  1853. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  1854. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  1855. allNotesOffSent = true;
  1856. }
  1857. break;
  1858. case CarlaEngineEventAllNotesOff:
  1859. if (cinEvent->channel == m_ctrlInChannel)
  1860. {
  1861. if (evIn.count > 0 && ! allNotesOffSent)
  1862. sendMidiAllNotesOff();
  1863. allNotesOffSent = true;
  1864. }
  1865. break;
  1866. }
  1867. }
  1868. } // End of Parameters Input
  1869. CARLA_PROCESS_CONTINUE_CHECK;
  1870. // --------------------------------------------------------------------------------------------------------
  1871. // Event Input
  1872. if (evIn.count > 0 && m_active && m_activeBefore)
  1873. {
  1874. // ----------------------------------------------------------------------------------------------------
  1875. // MIDI Input (External)
  1876. {
  1877. engineMidiLock();
  1878. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  1879. {
  1880. if (extMidiNotes[i].channel < 0)
  1881. break;
  1882. uint8_t midiEvent[3] = { 0 };
  1883. midiEvent[0] = m_ctrlInChannel + extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1884. midiEvent[1] = extMidiNotes[i].note;
  1885. midiEvent[2] = extMidiNotes[i].velo;
  1886. // send to first midi input
  1887. for (k=0; k < evIn.count; k++)
  1888. {
  1889. if (evIn.data[k].type & CARLA_EVENT_TYPE_MIDI)
  1890. {
  1891. if (evIn.data[k].type & CARLA_EVENT_DATA_ATOM)
  1892. {
  1893. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  1894. aev->time.frames = 0;
  1895. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1896. aev->body.size = 3;
  1897. memcpy(LV2_ATOM_BODY(&aev->body), midiEvent, 3);
  1898. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 3);
  1899. evInAtomOffsets[k] += evInPadSize;
  1900. evIn.data[k].atom->atom.size += evInPadSize;
  1901. }
  1902. else if (evIn.data[k].type & CARLA_EVENT_DATA_EVENT)
  1903. {
  1904. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  1905. }
  1906. else if (evIn.data[k].type & CARLA_EVENT_DATA_MIDI_LL)
  1907. {
  1908. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiEvent);
  1909. }
  1910. break;
  1911. }
  1912. }
  1913. extMidiNotes[i].channel = -1; // mark as invalid
  1914. midiEventCount += 1;
  1915. }
  1916. engineMidiUnlock();
  1917. } // End of MIDI Input (External)
  1918. CARLA_PROCESS_CONTINUE_CHECK;
  1919. // ----------------------------------------------------------------------------------------------------
  1920. // MIDI Input (System)
  1921. for (i=0; i < evIn.count; i++)
  1922. {
  1923. if (! evIn.data[i].port)
  1924. continue;
  1925. const CarlaEngineMidiEvent* minEvent;
  1926. uint32_t time, nEvents = evIn.data[i].port->getEventCount();
  1927. for (k=0; k < nEvents && midiEventCount < MAX_MIDI_EVENTS; k++)
  1928. {
  1929. minEvent = evIn.data[i].port->getEvent(k);
  1930. if (! minEvent)
  1931. continue;
  1932. time = minEvent->time - framesOffset;
  1933. if (time >= frames)
  1934. continue;
  1935. uint8_t status = minEvent->data[0];
  1936. uint8_t channel = status & 0x0F;
  1937. // Fix bad note-off
  1938. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  1939. status -= 0x10;
  1940. // only write supported status types
  1941. 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))
  1942. {
  1943. if (evIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1944. {
  1945. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  1946. aev->time.frames = time;
  1947. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1948. aev->body.size = minEvent->size;
  1949. memcpy(LV2_ATOM_BODY(&aev->body), minEvent->data, minEvent->size);
  1950. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + minEvent->size);
  1951. evInAtomOffsets[i] += evInPadSize;
  1952. evIn.data[i].atom->atom.size += evInPadSize;
  1953. }
  1954. else if (evIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1955. {
  1956. lv2_event_write(&evInEventIters[i], time, 0, CARLA_URI_MAP_ID_MIDI_EVENT, minEvent->size, minEvent->data);
  1957. }
  1958. else if (evIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1959. {
  1960. lv2midi_put_event(&evInMidiStates[i], time, minEvent->size, minEvent->data);
  1961. }
  1962. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1963. postponeEvent(PluginPostEventNoteOff, channel, minEvent->data[1], 0.0);
  1964. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1965. postponeEvent(PluginPostEventNoteOn, channel, minEvent->data[1], minEvent->data[2]);
  1966. }
  1967. midiEventCount += 1;
  1968. }
  1969. } // End of MIDI Input (System)
  1970. // ----------------------------------------------------------------------------------------------------
  1971. // Message Input
  1972. {
  1973. for (i=0; i < evIn.count; i++)
  1974. {
  1975. if (! evIn.data[i].type & CARLA_EVENT_TYPE_MESSAGE)
  1976. continue;
  1977. #if 0
  1978. // send transport info if changed
  1979. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  1980. if (timeInfo->playing != lastTimePosPlaying || timeInfo->frame != lastTimePosFrame)
  1981. {
  1982. uint8_t posBuf[256];
  1983. LV2_Atom* const lv2Pos = (LV2_Atom*)posBuf;
  1984. LV2_Atom_Forge tempForge;
  1985. LV2_Atom_Forge* const forge = &tempForge;
  1986. lv2_atom_forge_set_buffer(forge, posBuf, sizeof(uint8_t)*256);
  1987. //LV2_Atom_Forge_Frame frame;
  1988. //lv2_atom_forge_blank(forge, &frame, 1, jalv->urids.time_Position);
  1989. //lv2_atom_forge_property_head(forge, jalv->urids.time_frame, 0);
  1990. lv2_atom_forge_long(forge, timeInfo->frame);
  1991. //lv2_atom_forge_property_head(forge, jalv->urids.time_position, 0);
  1992. lv2_atom_forge_long(forge, timeInfo->time);
  1993. //lv2_atom_forge_property_head(forge, jalv->urids.time_speed, 0);
  1994. lv2_atom_forge_float(forge, timeInfo->playing ? 1.0 : 0.0);
  1995. if (timeInfo->valid & CarlaEngineTimeBBT)
  1996. {
  1997. //lv2_atom_forge_property_head(forge, jalv->urids.time_bar, 0);
  1998. lv2_atom_forge_float(forge, timeInfo->bbt.bar - 1);
  1999. //lv2_atom_forge_property_head(forge, jalv->urids.time_barBeat, 0);
  2000. lv2_atom_forge_float(forge, timeInfo->bbt.beat - 1 + (timeInfo->bbt.tick / timeInfo->bbt.ticks_per_beat));
  2001. //lv2_atom_forge_property_head(forge, jalv->urids.time_beat, 0);
  2002. lv2_atom_forge_float(forge, timeInfo->bbt.beat - 1); // FIXME: -1 ?
  2003. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatUnit, 0);
  2004. lv2_atom_forge_float(forge, timeInfo->bbt.beat_type);
  2005. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatsPerBar, 0);
  2006. lv2_atom_forge_float(forge, timeInfo->bbt.beats_per_bar);
  2007. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatsPerMinute, 0);
  2008. lv2_atom_forge_float(forge, timeInfo->bbt.beats_per_minute);
  2009. }
  2010. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  2011. aev->time.frames = framesOffset;
  2012. aev->body.type = lv2Pos->type;
  2013. aev->body.size = lv2Pos->size;
  2014. memcpy(LV2_ATOM_BODY(&aev->body), LV2_ATOM_BODY(lv2Pos), lv2Pos->size);
  2015. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + lv2Pos->size);
  2016. evInAtomOffsets[i] += evInPadSize;
  2017. evIn.data[i].atom->atom.size += evInPadSize;
  2018. lastTimePosPlaying = timeInfo->playing;
  2019. lastTimePosFrame = timeInfo->playing ? timeInfo->frame + frames : timeInfo->frame;
  2020. }
  2021. #endif
  2022. atomQueueIn.lock();
  2023. if (! atomQueueIn.isEmpty())
  2024. {
  2025. uint32_t portIndex;
  2026. const LV2_Atom* atom;
  2027. while (atomQueueIn.get(&portIndex, &atom, false))
  2028. {
  2029. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  2030. {
  2031. const LV2_Atom_Worker* const atomWorker = (const LV2_Atom_Worker*)atom;
  2032. ext.worker->work_response(handle, atomWorker->body.size, atomWorker->body.data);
  2033. continue;
  2034. }
  2035. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  2036. aev->time.frames = framesOffset;
  2037. aev->body.type = atom->type;
  2038. aev->body.size = atom->size;
  2039. memcpy(LV2_ATOM_BODY(&aev->body), LV2_ATOM_BODY(atom), atom->size);
  2040. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + atom->size);
  2041. evInAtomOffsets[i] += evInPadSize;
  2042. evIn.data[i].atom->atom.size += evInPadSize;
  2043. }
  2044. }
  2045. atomQueueIn.unlock();
  2046. }
  2047. }
  2048. } // End of Event Input
  2049. CARLA_PROCESS_CONTINUE_CHECK;
  2050. // --------------------------------------------------------------------------------------------------------
  2051. // Special Parameters
  2052. {
  2053. int32_t rindex;
  2054. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  2055. for (k=0; k < param.count; k++)
  2056. {
  2057. if (param.data[k].type == PARAMETER_LATENCY)
  2058. {
  2059. // TODO
  2060. }
  2061. else if (param.data[k].type == PARAMETER_LV2_FREEWHEEL)
  2062. {
  2063. setParameterValue(k, x_engine->isOffline() ? 1.0 : 0.0, false, false, false);
  2064. }
  2065. else if (param.data[k].type == PARAMETER_LV2_TIME)
  2066. {
  2067. rindex = param.data[k].rindex;
  2068. CARLA_ASSERT(rindex >= 0 && rindex < (int32_t)rdf_descriptor->PortCount);
  2069. switch (rdf_descriptor->Ports[rindex].Designation)
  2070. {
  2071. // Non-BBT
  2072. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2073. setParameterValue(k, timeInfo->frame, false, false, false);
  2074. break;
  2075. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2076. break;
  2077. case LV2_PORT_DESIGNATION_TIME_POSITION:
  2078. setParameterValue(k, timeInfo->time, false, false, false);
  2079. break;
  2080. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2081. setParameterValue(k, timeInfo->playing ? 1.0 : 0.0, false, false, false);
  2082. break;
  2083. // BBT
  2084. case LV2_PORT_DESIGNATION_TIME_BAR:
  2085. if (timeInfo->valid & CarlaEngineTimeBBT)
  2086. setParameterValue(k, timeInfo->bbt.bar - 1, false, false, false);
  2087. break;
  2088. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2089. if (timeInfo->valid & CarlaEngineTimeBBT)
  2090. setParameterValue(k, float(timeInfo->bbt.beat - 1) + (float(timeInfo->bbt.tick) / timeInfo->bbt.ticks_per_beat), false, false, false);
  2091. break;
  2092. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2093. if (timeInfo->valid & CarlaEngineTimeBBT)
  2094. setParameterValue(k, timeInfo->bbt.beat - 1, false, false, false);
  2095. break;
  2096. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2097. if (timeInfo->valid & CarlaEngineTimeBBT)
  2098. setParameterValue(k, timeInfo->bbt.beat_type, false, false, false);
  2099. break;
  2100. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2101. if (timeInfo->valid & CarlaEngineTimeBBT)
  2102. setParameterValue(k, timeInfo->bbt.beats_per_bar, false, false, false);
  2103. break;
  2104. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2105. if (timeInfo->valid & CarlaEngineTimeBBT)
  2106. setParameterValue(k, timeInfo->bbt.beats_per_minute, false, false, false);
  2107. break;
  2108. }
  2109. }
  2110. }
  2111. }
  2112. CARLA_PROCESS_CONTINUE_CHECK;
  2113. // --------------------------------------------------------------------------------------------------------
  2114. // Plugin processing
  2115. if (m_active)
  2116. {
  2117. if (! m_activeBefore)
  2118. {
  2119. if (evIn.count > 0)
  2120. {
  2121. for (i=0; i < MAX_MIDI_CHANNELS; i++)
  2122. {
  2123. uint8_t midiEvent1[2] = { 0 };
  2124. midiEvent1[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2125. midiEvent1[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2126. uint8_t midiEvent2[2] = { 0 };
  2127. midiEvent2[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2128. midiEvent2[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2129. // send to all midi inputs
  2130. for (k=0; k < evIn.count; k++)
  2131. {
  2132. if (evIn.data[k].type & CARLA_EVENT_TYPE_MIDI)
  2133. {
  2134. if (evIn.data[k].type & CARLA_EVENT_DATA_ATOM)
  2135. {
  2136. const uint32_t padSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 2);
  2137. // all sound off
  2138. LV2_Atom_Event* const aev1 = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  2139. aev1->time.frames = 0;
  2140. aev1->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2141. aev1->body.size = 2;
  2142. memcpy(LV2_ATOM_BODY(&aev1->body), midiEvent1, 2);
  2143. evInAtomOffsets[k] += padSize;
  2144. evIn.data[k].atom->atom.size += padSize;
  2145. // all notes off
  2146. LV2_Atom_Event* const aev2 = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  2147. aev2->time.frames = 0;
  2148. aev2->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2149. aev2->body.size = 2;
  2150. memcpy(LV2_ATOM_BODY(&aev2->body), midiEvent2, 2);
  2151. evInAtomOffsets[k] += padSize;
  2152. evIn.data[k].atom->atom.size += padSize;
  2153. }
  2154. else if (evIn.data[k].type & CARLA_EVENT_DATA_EVENT)
  2155. {
  2156. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiEvent1);
  2157. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiEvent2);
  2158. }
  2159. else if (evIn.data[k].type & CARLA_EVENT_DATA_MIDI_LL)
  2160. {
  2161. lv2midi_put_event(&evInMidiStates[k], 0, 2, midiEvent1);
  2162. lv2midi_put_event(&evInMidiStates[k], 0, 2, midiEvent2);
  2163. }
  2164. }
  2165. }
  2166. }
  2167. midiEventCount = MAX_MIDI_CHANNELS*2;
  2168. }
  2169. if (m_latency > 0)
  2170. {
  2171. for (i=0; i < aIn.count; i++)
  2172. memset(m_latencyBuffers[i], 0, sizeof(float)*m_latency);
  2173. }
  2174. if (descriptor->activate)
  2175. {
  2176. descriptor->activate(handle);
  2177. if (h2) descriptor->activate(h2);
  2178. }
  2179. }
  2180. for (i=0; i < aIn.count; i++)
  2181. {
  2182. if (i == 0 || ! h2) descriptor->connect_port(handle, aIn.rindexes[i], inBuffer[i]);
  2183. else if (i == 1) descriptor->connect_port(h2, aIn.rindexes[i], inBuffer[i]);
  2184. }
  2185. for (i=0; i < aOut.count; i++)
  2186. {
  2187. if (i == 0 || ! h2) descriptor->connect_port(handle, aOut.rindexes[i], outBuffer[i]);
  2188. else if (i == 1) descriptor->connect_port(h2, aOut.rindexes[i], outBuffer[i]);
  2189. }
  2190. descriptor->run(handle, frames);
  2191. if (h2) descriptor->run(h2, frames);
  2192. if (ext.worker && ext.worker->end_run)
  2193. {
  2194. ext.worker->end_run(handle);
  2195. if (h2) ext.worker->end_run(h2);
  2196. }
  2197. }
  2198. else
  2199. {
  2200. if (m_activeBefore)
  2201. {
  2202. if (descriptor->deactivate)
  2203. {
  2204. descriptor->deactivate(handle);
  2205. if (h2) descriptor->deactivate(h2);
  2206. }
  2207. }
  2208. }
  2209. CARLA_PROCESS_CONTINUE_CHECK;
  2210. // --------------------------------------------------------------------------------------------------------
  2211. // Post-processing (dry/wet, volume and balance)
  2212. if (m_active)
  2213. {
  2214. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  2215. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  2216. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  2217. double bal_rangeL, bal_rangeR;
  2218. float bufValue, oldBufLeft[do_balance ? frames : 0];
  2219. for (i=0; i < aOut.count; i++)
  2220. {
  2221. // Dry/Wet
  2222. if (do_drywet)
  2223. {
  2224. for (k=0; k < frames; k++)
  2225. {
  2226. if (k < m_latency && m_latency < frames)
  2227. bufValue = (aIn.count == 1) ? m_latencyBuffers[0][k] : m_latencyBuffers[i][k];
  2228. else
  2229. bufValue = (aIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2230. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  2231. }
  2232. }
  2233. // Balance
  2234. if (do_balance)
  2235. {
  2236. if (i%2 == 0)
  2237. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  2238. bal_rangeL = (x_balanceLeft+1.0)/2;
  2239. bal_rangeR = (x_balanceRight+1.0)/2;
  2240. for (k=0; k < frames; k++)
  2241. {
  2242. if (i%2 == 0)
  2243. {
  2244. // left output
  2245. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  2246. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  2247. }
  2248. else
  2249. {
  2250. // right
  2251. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  2252. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  2253. }
  2254. }
  2255. }
  2256. // Volume
  2257. if (do_volume)
  2258. {
  2259. for (k=0; k < frames; k++)
  2260. outBuffer[i][k] *= x_volume;
  2261. }
  2262. // Output VU
  2263. #ifndef BUILD_BRIDGE
  2264. if (carlaOptions.processMode != PROCESS_MODE_CONTINUOUS_RACK)
  2265. #endif
  2266. {
  2267. for (k=0; i < 2 && k < frames; k++)
  2268. {
  2269. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  2270. aOutsPeak[i] = abs(outBuffer[i][k]);
  2271. }
  2272. }
  2273. }
  2274. // Latency, save values for next callback
  2275. if (m_latency > 0 && m_latency < frames)
  2276. {
  2277. for (i=0; i < aIn.count; i++)
  2278. memcpy(m_latencyBuffers[i], inBuffer[i] + (frames - m_latency), sizeof(float)*m_latency);
  2279. }
  2280. }
  2281. else
  2282. {
  2283. // disable any output sound if not active
  2284. for (i=0; i < aOut.count; i++)
  2285. zeroF(outBuffer[i], frames);
  2286. aOutsPeak[0] = 0.0;
  2287. aOutsPeak[1] = 0.0;
  2288. } // End of Post-processing
  2289. CARLA_PROCESS_CONTINUE_CHECK;
  2290. // --------------------------------------------------------------------------------------------------------
  2291. // Control Output
  2292. if (param.portCout && m_active)
  2293. {
  2294. double value;
  2295. for (k=0; k < param.count; k++)
  2296. {
  2297. if (param.data[k].type == PARAMETER_OUTPUT)
  2298. {
  2299. fixParameterValue(paramBuffers[k], param.ranges[k]);
  2300. if (param.data[k].midiCC > 0)
  2301. {
  2302. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  2303. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  2304. }
  2305. }
  2306. }
  2307. } // End of Control Output
  2308. CARLA_PROCESS_CONTINUE_CHECK;
  2309. // --------------------------------------------------------------------------------------------------------
  2310. // Event Output
  2311. if (evOut.count > 0 && m_active)
  2312. {
  2313. atomQueueOut.lock();
  2314. for (i=0; i < evOut.count; i++)
  2315. {
  2316. // midi events need the midi port to send events to
  2317. if ((evOut.data[i].type & CARLA_EVENT_TYPE_MIDI) > 0 && ! evOut.data[i].port)
  2318. continue;
  2319. if (evOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2320. {
  2321. int32_t size = evOut.data[i].atom->atom.size - sizeof(LV2_Atom_Sequence_Body);
  2322. int32_t offset = 0;
  2323. while (offset < size)
  2324. {
  2325. qDebug("output event??, offset:%i, size:%i", offset, size);
  2326. const LV2_Atom_Event* const aev = (LV2_Atom_Event*)((char*)LV2_ATOM_CONTENTS(LV2_Atom_Sequence, evOut.data[i].atom) + offset);
  2327. if ((! aev) || aev->body.type == CARLA_URI_MAP_ID_NULL)
  2328. break;
  2329. qDebug("output event ------------------------------ YES!");
  2330. if (aev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2331. {
  2332. const unsigned char* const data = (unsigned char*)LV2_ATOM_BODY(&aev->body);
  2333. evOut.data[i].port->writeEvent(aev->time.frames, data, aev->body.size);
  2334. }
  2335. else if (aev->body.type == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2336. {
  2337. if (! atomQueueOut.isFull())
  2338. atomQueueOut.put(evOut.data[i].rindex, &aev->body);
  2339. }
  2340. offset += lv2_atom_pad_size(sizeof(LV2_Atom_Event) + aev->body.size);
  2341. }
  2342. }
  2343. else if (evOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2344. {
  2345. const LV2_Event* ev;
  2346. LV2_Event_Iterator iter;
  2347. uint8_t* data;
  2348. lv2_event_begin(&iter, evOut.data[i].event);
  2349. for (k=0; k < iter.buf->event_count; k++)
  2350. {
  2351. ev = lv2_event_get(&iter, &data);
  2352. if (ev && ev->type == CARLA_URI_MAP_ID_MIDI_EVENT && data)
  2353. evOut.data[i].port->writeEvent(ev->frames, data, ev->size);
  2354. lv2_event_increment(&iter);
  2355. }
  2356. }
  2357. else if (evOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2358. {
  2359. LV2_MIDIState state = { evOut.data[i].midi, frames, 0 };
  2360. uint32_t eventSize;
  2361. double eventTime;
  2362. unsigned char* eventData;
  2363. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  2364. {
  2365. evOut.data[i].port->writeEvent(eventTime, eventData, eventSize);
  2366. lv2midi_step(&state);
  2367. }
  2368. }
  2369. }
  2370. atomQueueOut.unlock();
  2371. } // End of Event Output
  2372. CARLA_PROCESS_CONTINUE_CHECK;
  2373. // --------------------------------------------------------------------------------------------------------
  2374. // Peak Values
  2375. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  2376. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  2377. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  2378. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  2379. m_activeBefore = m_active;
  2380. }
  2381. void bufferSizeChanged(const uint32_t newBufferSize)
  2382. {
  2383. lv2Options.bufferSize = newBufferSize;
  2384. }
  2385. // -------------------------------------------------------------------
  2386. // Post-poned events
  2387. void postEventHandleCustom(const int32_t size, const int32_t, const double, const void* const data)
  2388. {
  2389. qDebug("Lv2Plugin::postEventHandleCustom(%i, %p)", size, data);
  2390. CARLA_ASSERT(ext.worker && ext.worker->work);
  2391. if (ext.worker && ext.worker->work)
  2392. ext.worker->work(handle, carla_lv2_worker_respond, this, size, data);
  2393. }
  2394. void uiParameterChange(const uint32_t index, const double value)
  2395. {
  2396. CARLA_ASSERT(index < param.count);
  2397. if (index >= param.count)
  2398. return;
  2399. #ifndef BUILD_BRIDGE
  2400. if (gui.type == GUI_EXTERNAL_OSC)
  2401. {
  2402. if (osc.data.target)
  2403. osc_send_control(&osc.data, param.data[index].rindex, value);
  2404. }
  2405. else
  2406. #endif
  2407. {
  2408. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2409. {
  2410. float valueF = value;
  2411. ui.descriptor->port_event(ui.handle, param.data[index].rindex, sizeof(float), 0, &valueF);
  2412. }
  2413. }
  2414. }
  2415. void uiMidiProgramChange(const uint32_t index)
  2416. {
  2417. CARLA_ASSERT(index < midiprog.count);
  2418. if (index >= midiprog.count)
  2419. return;
  2420. #ifndef BUILD_BRIDGE
  2421. if (gui.type == GUI_EXTERNAL_OSC)
  2422. {
  2423. if (osc.data.target)
  2424. osc_send_midi_program(&osc.data, midiprog.data[index].bank, midiprog.data[index].program);
  2425. }
  2426. else
  2427. #endif
  2428. {
  2429. if (ext.uiprograms)
  2430. ext.uiprograms->select_program(ui.handle, midiprog.data[index].bank, midiprog.data[index].program);
  2431. }
  2432. }
  2433. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  2434. {
  2435. CARLA_ASSERT(channel < 16);
  2436. CARLA_ASSERT(note < 128);
  2437. CARLA_ASSERT(velo > 0 && velo < 128);
  2438. #ifndef BUILD_BRIDGE
  2439. if (gui.type == GUI_EXTERNAL_OSC)
  2440. {
  2441. if (osc.data.target)
  2442. {
  2443. uint8_t midiData[4] = { 0 };
  2444. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2445. midiData[2] = note;
  2446. midiData[3] = velo;
  2447. osc_send_midi(&osc.data, midiData);
  2448. }
  2449. }
  2450. else
  2451. #endif
  2452. {
  2453. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2454. {
  2455. LV2_Atom_MidiEvent midiEv;
  2456. midiEv.event.time.frames = 0;
  2457. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2458. midiEv.event.body.size = 3;
  2459. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2460. midiEv.data[1] = note;
  2461. midiEv.data[2] = velo;
  2462. ui.descriptor->port_event(ui.handle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2463. }
  2464. }
  2465. }
  2466. void uiNoteOff(const uint8_t channel, const uint8_t note)
  2467. {
  2468. CARLA_ASSERT(channel < 16);
  2469. CARLA_ASSERT(note < 128);
  2470. #ifndef BUILD_BRIDGE
  2471. if (gui.type == GUI_EXTERNAL_OSC)
  2472. {
  2473. if (osc.data.target)
  2474. {
  2475. uint8_t midiData[4] = { 0 };
  2476. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2477. midiData[2] = note;
  2478. osc_send_midi(&osc.data, midiData);
  2479. }
  2480. }
  2481. else
  2482. #endif
  2483. {
  2484. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2485. {
  2486. LV2_Atom_MidiEvent midiEv;
  2487. midiEv.event.time.frames = 0;
  2488. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2489. midiEv.event.body.size = 3;
  2490. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2491. midiEv.data[1] = note;
  2492. midiEv.data[2] = 0;
  2493. ui.descriptor->port_event(ui.handle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2494. }
  2495. }
  2496. }
  2497. // -------------------------------------------------------------------
  2498. // Cleanup
  2499. void removeClientPorts()
  2500. {
  2501. qDebug("Lv2Plugin::removeClientPorts() - start");
  2502. for (uint32_t i=0; i < evIn.count; i++)
  2503. {
  2504. if (evIn.data[i].port)
  2505. {
  2506. delete evIn.data[i].port;
  2507. evIn.data[i].port = nullptr;
  2508. }
  2509. }
  2510. for (uint32_t i=0; i < evOut.count; i++)
  2511. {
  2512. if (evOut.data[i].port)
  2513. {
  2514. delete evOut.data[i].port;
  2515. evOut.data[i].port = nullptr;
  2516. }
  2517. }
  2518. CarlaPlugin::removeClientPorts();
  2519. qDebug("Lv2Plugin::removeClientPorts() - end");
  2520. }
  2521. void initBuffers()
  2522. {
  2523. uint32_t i;
  2524. for (i=0; i < evIn.count; i++)
  2525. {
  2526. if (evIn.data[i].port)
  2527. evIn.data[i].port->initBuffer(x_engine);
  2528. }
  2529. for (uint32_t i=0; i < evOut.count; i++)
  2530. {
  2531. if (evOut.data[i].port)
  2532. evOut.data[i].port->initBuffer(x_engine);
  2533. }
  2534. CarlaPlugin::initBuffers();
  2535. }
  2536. void deleteBuffers()
  2537. {
  2538. qDebug("Lv2Plugin::deleteBuffers() - start");
  2539. if (evIn.count > 0)
  2540. {
  2541. for (uint32_t i=0; i < evIn.count; i++)
  2542. {
  2543. if (evIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  2544. {
  2545. free(evIn.data[i].atom);
  2546. }
  2547. else if (evIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  2548. {
  2549. free(evIn.data[i].event);
  2550. }
  2551. else if (evIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2552. {
  2553. delete[] evIn.data[i].midi->data;
  2554. delete evIn.data[i].midi;
  2555. }
  2556. }
  2557. delete[] evIn.data;
  2558. }
  2559. if (evOut.count > 0)
  2560. {
  2561. for (uint32_t i=0; i < evOut.count; i++)
  2562. {
  2563. if (evOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2564. {
  2565. free(evOut.data[i].atom);
  2566. }
  2567. else if (evOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2568. {
  2569. free(evOut.data[i].event);
  2570. }
  2571. else if (evOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2572. {
  2573. delete[] evOut.data[i].midi->data;
  2574. delete evOut.data[i].midi;
  2575. }
  2576. }
  2577. delete[] evOut.data;
  2578. }
  2579. if (param.count > 0)
  2580. delete[] paramBuffers;
  2581. evIn.count = 0;
  2582. evIn.data = nullptr;
  2583. evOut.count = 0;
  2584. evOut.data = nullptr;
  2585. paramBuffers = nullptr;
  2586. CarlaPlugin::deleteBuffers();
  2587. qDebug("Lv2Plugin::deleteBuffers() - end");
  2588. }
  2589. // -------------------------------------------------------------------
  2590. uint32_t getCustomURID(const char* const uri)
  2591. {
  2592. qDebug("Lv2Plugin::getCustomURID(%s)", uri);
  2593. CARLA_ASSERT(uri);
  2594. if (! uri)
  2595. return CARLA_URI_MAP_ID_NULL;
  2596. for (size_t i=0; i < customURIDs.size(); i++)
  2597. {
  2598. if (customURIDs[i] && strcmp(customURIDs[i], uri) == 0)
  2599. return i;
  2600. }
  2601. customURIDs.push_back(strdup(uri));
  2602. return customURIDs.size()-1;
  2603. }
  2604. const char* getCustomURIString(const LV2_URID urid) const
  2605. {
  2606. qDebug("Lv2Plugin::getCustomURIString(%i)", urid);
  2607. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  2608. if (urid == CARLA_URI_MAP_ID_NULL)
  2609. return nullptr;
  2610. if (urid < customURIDs.size())
  2611. return customURIDs[urid];
  2612. return nullptr;
  2613. }
  2614. // -------------------------------------------------------------------
  2615. void handleTransferAtom(const int32_t portIndex, const LV2_Atom* const atom)
  2616. {
  2617. qDebug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  2618. CARLA_ASSERT(portIndex >= 0);
  2619. CARLA_ASSERT(atom);
  2620. atomQueueIn.put(portIndex, atom);
  2621. }
  2622. void handleTransferEvent(const int32_t portIndex, const LV2_Atom* const atom)
  2623. {
  2624. qDebug("Lv2Plugin::handleTransferEvent(%i, %p)", portIndex, atom);
  2625. CARLA_ASSERT(portIndex >= 0);
  2626. CARLA_ASSERT(atom);
  2627. atomQueueIn.put(portIndex, atom);
  2628. }
  2629. // -------------------------------------------------------------------
  2630. void handleProgramChanged(const int32_t index)
  2631. {
  2632. if (index == -1)
  2633. {
  2634. const CarlaPlugin::ScopedDisabler m(this);
  2635. reloadPrograms(false);
  2636. }
  2637. else
  2638. {
  2639. if (index >= 0 && index < (int32_t)prog.count && ext.programs)
  2640. {
  2641. const char* const progName = ext.programs->get_program(handle, index)->name;
  2642. CARLA_ASSERT(progName);
  2643. if (prog.names[index])
  2644. free((void*)prog.names[index]);
  2645. prog.names[index] = strdup(progName);
  2646. }
  2647. }
  2648. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  2649. }
  2650. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  2651. {
  2652. CARLA_ASSERT(key > 0);
  2653. CARLA_ASSERT(value);
  2654. CustomDataType dtype;
  2655. const char* const uriKey = getCustomURIString(key);
  2656. if (type == CARLA_URI_MAP_ID_ATOM_CHUNK)
  2657. dtype = CUSTOM_DATA_CHUNK;
  2658. else if (type == CARLA_URI_MAP_ID_ATOM_PATH)
  2659. dtype = CUSTOM_DATA_PATH;
  2660. else if (type == CARLA_URI_MAP_ID_ATOM_STRING)
  2661. dtype = CUSTOM_DATA_STRING;
  2662. else if (type >= CARLA_URI_MAP_ID_COUNT)
  2663. dtype = CUSTOM_DATA_BINARY;
  2664. else
  2665. dtype = CUSTOM_DATA_INVALID;
  2666. // do basic checks
  2667. if (! uriKey)
  2668. {
  2669. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  2670. return LV2_STATE_ERR_NO_PROPERTY;
  2671. }
  2672. if (! flags & LV2_STATE_IS_POD)
  2673. {
  2674. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  2675. return LV2_STATE_ERR_BAD_FLAGS;
  2676. }
  2677. if (dtype == CUSTOM_DATA_INVALID)
  2678. {
  2679. qCritical("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type '%s'", key, value, size, type, flags, CustomDataType2str(dtype));
  2680. return LV2_STATE_ERR_BAD_TYPE;
  2681. }
  2682. // Check if we already have this key
  2683. for (size_t i=0; i < custom.size(); i++)
  2684. {
  2685. if (strcmp(custom[i].key, uriKey) == 0)
  2686. {
  2687. free((void*)custom[i].value);
  2688. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2689. custom[i].value = strdup((const char*)value);
  2690. else
  2691. custom[i].value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2692. return LV2_STATE_SUCCESS;
  2693. }
  2694. }
  2695. // Otherwise store it
  2696. CustomData newData;
  2697. newData.type = dtype;
  2698. newData.key = strdup(uriKey);
  2699. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2700. newData.value = strdup((const char*)value);
  2701. else
  2702. newData.value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2703. custom.push_back(newData);
  2704. return LV2_STATE_SUCCESS;
  2705. }
  2706. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  2707. {
  2708. CARLA_ASSERT(key > CARLA_URI_MAP_ID_NULL);
  2709. const char* const uriKey = getCustomURIString(key);
  2710. if (! uriKey)
  2711. {
  2712. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  2713. return nullptr;
  2714. }
  2715. const char* stringData = nullptr;
  2716. CustomDataType dtype = CUSTOM_DATA_INVALID;
  2717. for (size_t i=0; i < custom.size(); i++)
  2718. {
  2719. if (strcmp(custom[i].key, uriKey) == 0)
  2720. {
  2721. dtype = custom[i].type;
  2722. stringData = custom[i].value;
  2723. break;
  2724. }
  2725. }
  2726. if (! stringData)
  2727. {
  2728. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  2729. return nullptr;
  2730. }
  2731. *size = 0;
  2732. *type = 0;
  2733. *flags = LV2_STATE_IS_POD;
  2734. if (dtype == CUSTOM_DATA_STRING)
  2735. {
  2736. *size = strlen(stringData);
  2737. *type = CARLA_URI_MAP_ID_ATOM_STRING;
  2738. return stringData;
  2739. }
  2740. if (dtype == CUSTOM_DATA_PATH)
  2741. {
  2742. *size = strlen(stringData);
  2743. *type = CARLA_URI_MAP_ID_ATOM_PATH;
  2744. return stringData;
  2745. }
  2746. if (dtype == CUSTOM_DATA_CHUNK || dtype == CUSTOM_DATA_BINARY)
  2747. {
  2748. static QByteArray chunk;
  2749. chunk = QByteArray::fromBase64(stringData);
  2750. *size = chunk.size();
  2751. *type = (dtype == CUSTOM_DATA_CHUNK) ? CARLA_URI_MAP_ID_ATOM_CHUNK : key;
  2752. return chunk.constData();
  2753. }
  2754. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key type '%s'", key, size, type, flags, CustomDataType2str(dtype));
  2755. return nullptr;
  2756. }
  2757. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  2758. {
  2759. if (! ext.worker)
  2760. {
  2761. qWarning("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  2762. return LV2_WORKER_ERR_UNKNOWN;
  2763. }
  2764. if (x_engine->isOffline())
  2765. ext.worker->work(handle, carla_lv2_worker_respond, this, size, data);
  2766. else
  2767. postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  2768. return LV2_WORKER_SUCCESS;
  2769. }
  2770. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  2771. {
  2772. LV2_Atom_Worker workerAtom;
  2773. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  2774. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  2775. workerAtom.body.size = size;
  2776. workerAtom.body.data = data;
  2777. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  2778. return LV2_WORKER_SUCCESS;
  2779. }
  2780. void handleExternalUiClosed()
  2781. {
  2782. if (ui.handle && ui.descriptor && ui.descriptor->cleanup)
  2783. ui.descriptor->cleanup(ui.handle);
  2784. ui.handle = nullptr;
  2785. x_engine->callback(CALLBACK_SHOW_GUI, m_id, 0, 0, 0.0);
  2786. }
  2787. uint32_t handleUiPortMap(const char* const symbol)
  2788. {
  2789. CARLA_ASSERT(symbol);
  2790. if (! symbol)
  2791. return LV2UI_INVALID_PORT_INDEX;
  2792. for (uint32_t i=0; i < rdf_descriptor->PortCount; i++)
  2793. {
  2794. if (strcmp(rdf_descriptor->Ports[i].Symbol, symbol) == 0)
  2795. return i;
  2796. }
  2797. return LV2UI_INVALID_PORT_INDEX;
  2798. }
  2799. int handleUiResize(const int width, const int height)
  2800. {
  2801. CARLA_ASSERT(width > 0);
  2802. CARLA_ASSERT(height > 0);
  2803. if (width <= 0 || height <= 0)
  2804. return 1;
  2805. gui.width = width;
  2806. gui.height = height;
  2807. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 0.0);
  2808. return 0;
  2809. }
  2810. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  2811. {
  2812. if (format == 0)
  2813. {
  2814. CARLA_ASSERT(buffer);
  2815. CARLA_ASSERT(bufferSize == sizeof(float));
  2816. if (bufferSize != sizeof(float))
  2817. return;
  2818. float value = *(float*)buffer;
  2819. for (uint32_t i=0; i < param.count; i++)
  2820. {
  2821. if (param.data[i].rindex == (int32_t)rindex)
  2822. return setParameterValue(i, value, false, true, true);
  2823. }
  2824. }
  2825. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2826. {
  2827. CARLA_ASSERT(buffer);
  2828. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2829. handleTransferAtom(rindex, atom);
  2830. }
  2831. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2832. {
  2833. CARLA_ASSERT(buffer);
  2834. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2835. handleTransferEvent(rindex, atom);
  2836. }
  2837. }
  2838. // -------------------------------------------------------------------
  2839. #ifndef BUILD_BRIDGE
  2840. bool isUiBridgeable(const uint32_t uiId)
  2841. {
  2842. CARLA_ASSERT(rdf_descriptor);
  2843. CARLA_ASSERT(uiId < rdf_descriptor->UICount);
  2844. if (uiId >= rdf_descriptor->UICount)
  2845. return false;
  2846. const LV2_RDF_UI* const rdf_ui = &rdf_descriptor->UIs[uiId];
  2847. for (uint32_t i=0; i < rdf_ui->FeatureCount; i++)
  2848. {
  2849. if (strcmp(rdf_ui->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0 || strcmp(rdf_ui->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  2850. return false;
  2851. }
  2852. return true;
  2853. }
  2854. #endif
  2855. bool isUiResizable()
  2856. {
  2857. CARLA_ASSERT(ui.rdf_descriptor);
  2858. if (! ui.rdf_descriptor)
  2859. return false;
  2860. for (uint32_t i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  2861. {
  2862. if (strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  2863. return false;
  2864. }
  2865. return true;
  2866. }
  2867. void initExternalUi()
  2868. {
  2869. qDebug("Lv2Plugin::initExternalUi()");
  2870. ui.widget = nullptr;
  2871. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  2872. if (ui.handle && ui.widget)
  2873. {
  2874. updateUi();
  2875. }
  2876. else
  2877. {
  2878. qWarning("Lv2Plugin::initExternalUi() - failed to instantiate UI");
  2879. ui.handle = nullptr;
  2880. ui.widget = nullptr;
  2881. x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0);
  2882. }
  2883. }
  2884. void uiTransferCustomData(const CustomData* const cdata)
  2885. {
  2886. if (cdata->type == CUSTOM_DATA_INVALID || ! (ui.handle && ui.descriptor && ui.descriptor->port_event))
  2887. return;
  2888. LV2_URID_Map* const URID_Map = (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  2889. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  2890. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  2891. Sratom* const sratom = sratom_new(URID_Map);
  2892. SerdChunk chunk = { nullptr, 0 };
  2893. LV2_Atom_Forge forge;
  2894. lv2_atom_forge_init(&forge, URID_Map);
  2895. lv2_atom_forge_set_sink(&forge, sratom_forge_sink, sratom_forge_deref, &chunk);
  2896. LV2_Atom_Forge_Frame refFrame, bodyFrame;
  2897. LV2_Atom_Forge_Ref ref = lv2_atom_forge_blank(&forge, &refFrame, 1, uridPatchSet);
  2898. lv2_atom_forge_property_head(&forge, uridPatchBody, CARLA_URI_MAP_ID_NULL);
  2899. lv2_atom_forge_blank(&forge, &bodyFrame, 2, CARLA_URI_MAP_ID_NULL);
  2900. lv2_atom_forge_property_head(&forge, getCustomURID(cdata->key), CARLA_URI_MAP_ID_NULL);
  2901. if (cdata->type == CUSTOM_DATA_STRING)
  2902. lv2_atom_forge_string(&forge, cdata->value, strlen(cdata->value));
  2903. else if (cdata->type == CUSTOM_DATA_PATH)
  2904. lv2_atom_forge_path(&forge, cdata->value, strlen(cdata->value));
  2905. else if (cdata->type == CUSTOM_DATA_CHUNK)
  2906. lv2_atom_forge_literal(&forge, cdata->value, strlen(cdata->value), CARLA_URI_MAP_ID_ATOM_CHUNK, CARLA_URI_MAP_ID_NULL);
  2907. else
  2908. lv2_atom_forge_literal(&forge, cdata->value, strlen(cdata->value), getCustomURID(cdata->key), CARLA_URI_MAP_ID_NULL);
  2909. lv2_atom_forge_pop(&forge, &bodyFrame);
  2910. lv2_atom_forge_pop(&forge, &refFrame);
  2911. uint32_t portIndex = evIn.count > 0 ? evIn.data[0].rindex : 0;
  2912. const LV2_Atom* const atom = lv2_atom_forge_deref(&forge, ref);
  2913. ui.descriptor->port_event(ui.handle, portIndex, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  2914. free((void*)chunk.buf);
  2915. sratom_free(sratom);
  2916. }
  2917. void updateUi()
  2918. {
  2919. ext.uiprograms = nullptr;
  2920. if (ui.handle && ui.descriptor)
  2921. {
  2922. if (ui.descriptor->extension_data)
  2923. {
  2924. ext.uiprograms = (const LV2_Programs_UI_Interface*)ui.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  2925. if (ext.uiprograms && ! ext.uiprograms->select_program)
  2926. // invalid
  2927. ext.uiprograms = nullptr;
  2928. if (ext.uiprograms && midiprog.count > 0 && midiprog.current >= 0)
  2929. ext.uiprograms->select_program(ui.handle, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  2930. }
  2931. if (ui.descriptor->port_event)
  2932. {
  2933. // update control ports
  2934. float valueF;
  2935. for (uint32_t i=0; i < param.count; i++)
  2936. {
  2937. valueF = getParameterValue(i);
  2938. ui.descriptor->port_event(ui.handle, param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &valueF);
  2939. }
  2940. }
  2941. }
  2942. }
  2943. // ----------------- Event Feature ---------------------------------------------------
  2944. static uint32_t carla_lv2_event_ref(const LV2_Event_Callback_Data callback_data, LV2_Event* const event)
  2945. {
  2946. qDebug("Lv2Plugin::carla_lv2_event_ref(%p, %p)", callback_data, event);
  2947. CARLA_ASSERT(callback_data);
  2948. CARLA_ASSERT(event);
  2949. return 0;
  2950. }
  2951. static uint32_t carla_lv2_event_unref(const LV2_Event_Callback_Data callback_data, LV2_Event* const event)
  2952. {
  2953. qDebug("Lv2Plugin::carla_lv2_event_unref(%p, %p)", callback_data, event);
  2954. CARLA_ASSERT(callback_data);
  2955. CARLA_ASSERT(event);
  2956. return 0;
  2957. }
  2958. // ----------------- Logs Feature ----------------------------------------------------
  2959. static int carla_lv2_log_printf(const LV2_Log_Handle handle, const LV2_URID type, const char* const fmt, ...)
  2960. {
  2961. qDebug("Lv2Plugin::carla_lv2_log_printf(%p, %i, \"%s\", ...)", handle, type, fmt);
  2962. CARLA_ASSERT(handle);
  2963. CARLA_ASSERT(type > 0);
  2964. #ifndef DEBUG
  2965. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2966. return 0;
  2967. #endif
  2968. va_list args;
  2969. va_start(args, fmt);
  2970. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  2971. va_end(args);
  2972. return ret;
  2973. }
  2974. static int carla_lv2_log_vprintf(const LV2_Log_Handle handle, const LV2_URID type, const char* const fmt, va_list ap)
  2975. {
  2976. qDebug("Lv2Plugin::carla_lv2_log_vprintf(%p, %i, \"%s\", ...)", handle, type, fmt);
  2977. CARLA_ASSERT(handle);
  2978. CARLA_ASSERT(type > 0);
  2979. #ifndef DEBUG
  2980. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2981. return 0;
  2982. #endif
  2983. char buf[8196];
  2984. vsprintf(buf, fmt, ap);
  2985. if (*buf == 0)
  2986. return 0;
  2987. switch (type)
  2988. {
  2989. case CARLA_URI_MAP_ID_LOG_ERROR:
  2990. qCritical("%s", buf);
  2991. break;
  2992. case CARLA_URI_MAP_ID_LOG_NOTE:
  2993. printf("%s\n", buf);
  2994. break;
  2995. case CARLA_URI_MAP_ID_LOG_TRACE:
  2996. qDebug("%s", buf);
  2997. break;
  2998. case CARLA_URI_MAP_ID_LOG_WARNING:
  2999. qWarning("%s", buf);
  3000. break;
  3001. default:
  3002. break;
  3003. }
  3004. return strlen(buf);
  3005. }
  3006. // ----------------- Programs Feature ------------------------------------------------
  3007. static void carla_lv2_program_changed(const LV2_Programs_Handle handle, const int32_t index)
  3008. {
  3009. qDebug("Lv2Plugin::carla_lv2_program_changed(%p, %i)", handle, index);
  3010. CARLA_ASSERT(handle);
  3011. if (! handle)
  3012. return;
  3013. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3014. plugin->handleProgramChanged(index);
  3015. }
  3016. // ----------------- State Feature ---------------------------------------------------
  3017. static char* carla_lv2_state_make_path(const LV2_State_Make_Path_Handle handle, const char* const path)
  3018. {
  3019. qDebug("Lv2Plugin::carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  3020. CARLA_ASSERT(handle);
  3021. CARLA_ASSERT(path);
  3022. if (! path)
  3023. return nullptr;
  3024. QDir dir;
  3025. dir.mkpath(path);
  3026. return strdup(path);
  3027. }
  3028. static char* carla_lv2_state_map_abstract_path(const LV2_State_Map_Path_Handle handle, const char* const absolute_path)
  3029. {
  3030. qDebug("Lv2Plugin::carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  3031. CARLA_ASSERT(handle);
  3032. CARLA_ASSERT(absolute_path);
  3033. if (! absolute_path)
  3034. return nullptr;
  3035. QDir dir(absolute_path);
  3036. return strdup(dir.canonicalPath().toUtf8().constData());
  3037. }
  3038. static char* carla_lv2_state_map_absolute_path(const LV2_State_Map_Path_Handle handle, const char* const abstract_path)
  3039. {
  3040. qDebug("Lv2Plugin::carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  3041. CARLA_ASSERT(handle);
  3042. CARLA_ASSERT(abstract_path);
  3043. if (! abstract_path)
  3044. return nullptr;
  3045. QDir dir(abstract_path);
  3046. return strdup(dir.absolutePath().toUtf8().constData());
  3047. }
  3048. 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)
  3049. {
  3050. qDebug("Lv2Plugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  3051. CARLA_ASSERT(handle);
  3052. if (! handle)
  3053. return LV2_STATE_ERR_UNKNOWN;
  3054. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3055. return plugin->handleStateStore(key, value, size, type, flags);
  3056. }
  3057. 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)
  3058. {
  3059. qDebug("Lv2Plugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  3060. CARLA_ASSERT(handle);
  3061. if (! handle)
  3062. return nullptr;
  3063. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3064. return plugin->handleStateRetrieve(key, size, type, flags);
  3065. }
  3066. // ----------------- URI-Map Feature -------------------------------------------------
  3067. static uint32_t carla_lv2_uri_to_id(const LV2_URI_Map_Callback_Data data, const char* const map, const char* const uri)
  3068. {
  3069. qDebug("Lv2Plugin::carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  3070. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  3071. }
  3072. // ----------------- URID Feature ----------------------------------------------------
  3073. static LV2_URID carla_lv2_urid_map(const LV2_URID_Map_Handle handle, const char* const uri)
  3074. {
  3075. qDebug("Lv2Plugin::carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  3076. CARLA_ASSERT(handle);
  3077. CARLA_ASSERT(uri);
  3078. if (! uri)
  3079. return CARLA_URI_MAP_ID_NULL;
  3080. // Atom types
  3081. if (strcmp(uri, LV2_ATOM__Chunk) == 0)
  3082. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  3083. if (strcmp(uri, LV2_ATOM__Double) == 0)
  3084. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  3085. if (strcmp(uri, LV2_ATOM__Int) == 0)
  3086. return CARLA_URI_MAP_ID_ATOM_INT;
  3087. if (strcmp(uri, LV2_ATOM__Path) == 0)
  3088. return CARLA_URI_MAP_ID_ATOM_PATH;
  3089. if (strcmp(uri, LV2_ATOM__Sequence) == 0)
  3090. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  3091. if (strcmp(uri, LV2_ATOM__String) == 0)
  3092. return CARLA_URI_MAP_ID_ATOM_STRING;
  3093. if (strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  3094. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  3095. if (strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  3096. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  3097. // BufSize types
  3098. if (strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  3099. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  3100. if (strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  3101. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  3102. if (strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  3103. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  3104. // Log types
  3105. if (strcmp(uri, LV2_LOG__Error) == 0)
  3106. return CARLA_URI_MAP_ID_LOG_ERROR;
  3107. if (strcmp(uri, LV2_LOG__Note) == 0)
  3108. return CARLA_URI_MAP_ID_LOG_NOTE;
  3109. if (strcmp(uri, LV2_LOG__Trace) == 0)
  3110. return CARLA_URI_MAP_ID_LOG_TRACE;
  3111. if (strcmp(uri, LV2_LOG__Warning) == 0)
  3112. return CARLA_URI_MAP_ID_LOG_WARNING;
  3113. // Others
  3114. if (strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  3115. return CARLA_URI_MAP_ID_MIDI_EVENT;
  3116. if (strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  3117. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  3118. if (! handle)
  3119. return CARLA_URI_MAP_ID_NULL;
  3120. // Custom types
  3121. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3122. return plugin->getCustomURID(uri);
  3123. }
  3124. static const char* carla_lv2_urid_unmap(const LV2_URID_Map_Handle handle, const LV2_URID urid)
  3125. {
  3126. qDebug("Lv2Plugin::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  3127. CARLA_ASSERT(handle);
  3128. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  3129. if (urid == CARLA_URI_MAP_ID_NULL)
  3130. return nullptr;
  3131. // Atom types
  3132. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  3133. return LV2_ATOM__Chunk;
  3134. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  3135. return LV2_ATOM__Double;
  3136. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  3137. return LV2_ATOM__Int;
  3138. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  3139. return LV2_ATOM__Path;
  3140. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  3141. return LV2_ATOM__Sequence;
  3142. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  3143. return LV2_ATOM__String;
  3144. if (urid == CARLA_URI_MAP_ID_ATOM_WORKER)
  3145. return nullptr; // not a valid URID, only used internally
  3146. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  3147. return LV2_ATOM__atomTransfer;
  3148. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3149. return LV2_ATOM__eventTransfer;
  3150. // BufSize types
  3151. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  3152. return LV2_BUF_SIZE__maxBlockLength;
  3153. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  3154. return LV2_BUF_SIZE__minBlockLength;
  3155. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  3156. return LV2_BUF_SIZE__sequenceSize;
  3157. // Log types
  3158. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  3159. return LV2_LOG__Error;
  3160. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  3161. return LV2_LOG__Note;
  3162. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  3163. return LV2_LOG__Trace;
  3164. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  3165. return LV2_LOG__Warning;
  3166. // Others
  3167. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  3168. return LV2_MIDI__MidiEvent;
  3169. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  3170. return LV2_PARAMETERS__sampleRate;
  3171. if (! handle)
  3172. return nullptr;
  3173. // Custom types
  3174. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3175. return plugin->getCustomURIString(urid);
  3176. }
  3177. // ----------------- Worker Feature --------------------------------------------------
  3178. static LV2_Worker_Status carla_lv2_worker_schedule(const LV2_Worker_Schedule_Handle handle, const uint32_t size, const void* const data)
  3179. {
  3180. qDebug("Lv2Plugin::carla_lv2_worker_schedule(%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->handleWorkerSchedule(size, data);
  3186. }
  3187. static LV2_Worker_Status carla_lv2_worker_respond(const LV2_Worker_Respond_Handle handle, const uint32_t size, const void* const data)
  3188. {
  3189. qDebug("Lv2Plugin::carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  3190. CARLA_ASSERT(handle);
  3191. if (! handle)
  3192. return LV2_WORKER_ERR_UNKNOWN;
  3193. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3194. return plugin->handleWorkerRespond(size, data);
  3195. }
  3196. // ----------------- UI Port-Map Feature ---------------------------------------------
  3197. static uint32_t carla_lv2_ui_port_map(const LV2UI_Feature_Handle handle, const char* const symbol)
  3198. {
  3199. qDebug("Lv2Plugin::carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  3200. CARLA_ASSERT(handle);
  3201. if (! handle)
  3202. return LV2UI_INVALID_PORT_INDEX;
  3203. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3204. return plugin->handleUiPortMap(symbol);
  3205. }
  3206. // ----------------- UI Resize Feature -----------------------------------------------
  3207. static int carla_lv2_ui_resize(const LV2UI_Feature_Handle handle, const int width, const int height)
  3208. {
  3209. qDebug("Lv2Plugin::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  3210. CARLA_ASSERT(handle);
  3211. if (! handle)
  3212. return 1;
  3213. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3214. return plugin->handleUiResize(width, height);
  3215. }
  3216. // ----------------- External UI Feature ---------------------------------------------
  3217. static void carla_lv2_external_ui_closed(const LV2UI_Controller controller)
  3218. {
  3219. qDebug("Lv2Plugin::carla_lv2_external_ui_closed(%p)", controller);
  3220. CARLA_ASSERT(controller);
  3221. if (! controller)
  3222. return;
  3223. Lv2Plugin* const plugin = (Lv2Plugin*)controller;
  3224. plugin->handleExternalUiClosed();
  3225. }
  3226. // ----------------- UI Extension ----------------------------------------------------
  3227. 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)
  3228. {
  3229. qDebug("Lv2Plugin::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  3230. CARLA_ASSERT(controller);
  3231. if (! controller)
  3232. return;
  3233. Lv2Plugin* const plugin = (Lv2Plugin*)controller;
  3234. plugin->handleUiWrite(port_index, buffer_size, format, buffer);
  3235. }
  3236. // -------------------------------------------------------------------
  3237. bool uiLibOpen(const char* const filename)
  3238. {
  3239. ui.lib = lib_open(filename);
  3240. return bool(ui.lib);
  3241. }
  3242. bool uiLibClose()
  3243. {
  3244. if (ui.lib)
  3245. return lib_close(ui.lib);
  3246. return false;
  3247. }
  3248. void* uiLibSymbol(const char* const symbol)
  3249. {
  3250. if (ui.lib)
  3251. return lib_symbol(ui.lib, symbol);
  3252. return nullptr;
  3253. }
  3254. // -------------------------------------------------------------------
  3255. bool init(const char* const bundle, const char* const name, const char* const URI)
  3256. {
  3257. // ---------------------------------------------------------------
  3258. // get plugin from lv2_rdf (lilv)
  3259. rdf_descriptor = lv2_rdf_new(URI);
  3260. if (! rdf_descriptor)
  3261. {
  3262. setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3263. return false;
  3264. }
  3265. // ---------------------------------------------------------------
  3266. // open DLL
  3267. if (! libOpen(rdf_descriptor->Binary))
  3268. {
  3269. setLastError(libError(rdf_descriptor->Binary));
  3270. return false;
  3271. }
  3272. // ---------------------------------------------------------------
  3273. // initialize features (part 1)
  3274. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3275. programsFt->handle = this;
  3276. programsFt->program_changed = carla_lv2_program_changed;
  3277. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3278. uriMapFt->callback_data = this;
  3279. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3280. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3281. uridMapFt->handle = this;
  3282. uridMapFt->map = carla_lv2_urid_map;
  3283. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3284. uridUnmapFt->handle = this;
  3285. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3286. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3287. workerFt->handle = this;
  3288. workerFt->schedule_work = carla_lv2_worker_schedule;
  3289. // ---------------------------------------------------------------
  3290. // initialize features (part 2)
  3291. features[lv2_feature_id_bufsize_bounded] = new LV2_Feature;
  3292. features[lv2_feature_id_bufsize_bounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3293. features[lv2_feature_id_bufsize_bounded]->data = nullptr;
  3294. #ifndef BUILD_BRIDGE
  3295. if (carlaOptions.processHighPrecision)
  3296. {
  3297. features[lv2_feature_id_bufsize_fixed] = new LV2_Feature;
  3298. features[lv2_feature_id_bufsize_fixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3299. features[lv2_feature_id_bufsize_fixed]->data = nullptr;
  3300. features[lv2_feature_id_bufsize_powerof2] = new LV2_Feature;
  3301. features[lv2_feature_id_bufsize_powerof2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3302. features[lv2_feature_id_bufsize_powerof2]->data = nullptr;
  3303. }
  3304. else
  3305. #endif
  3306. {
  3307. // fake, used only to keep a valid features-array
  3308. features[lv2_feature_id_bufsize_fixed] = features[lv2_feature_id_bufsize_bounded];
  3309. features[lv2_feature_id_bufsize_powerof2] = features[lv2_feature_id_bufsize_bounded];
  3310. }
  3311. features[lv2_feature_id_event] = new LV2_Feature;
  3312. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  3313. features[lv2_feature_id_event]->data = ft.event;
  3314. features[lv2_feature_id_logs] = new LV2_Feature;
  3315. features[lv2_feature_id_logs]->URI = LV2_LOG__log;
  3316. features[lv2_feature_id_logs]->data = ft.log;
  3317. features[lv2_feature_id_options] = new LV2_Feature;
  3318. features[lv2_feature_id_options]->URI = LV2_OPTIONS__options;
  3319. features[lv2_feature_id_options]->data = ft.options;
  3320. features[lv2_feature_id_programs] = new LV2_Feature;
  3321. features[lv2_feature_id_programs]->URI = LV2_PROGRAMS__Host;
  3322. features[lv2_feature_id_programs]->data = programsFt;
  3323. features[lv2_feature_id_rtmempool] = new LV2_Feature;
  3324. features[lv2_feature_id_rtmempool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3325. features[lv2_feature_id_rtmempool]->data = ft.rtMemPool;
  3326. features[lv2_feature_id_state_make_path] = new LV2_Feature;
  3327. features[lv2_feature_id_state_make_path]->URI = LV2_STATE__makePath;
  3328. features[lv2_feature_id_state_make_path]->data = ft.stateMakePath;
  3329. features[lv2_feature_id_state_map_path] = new LV2_Feature;
  3330. features[lv2_feature_id_state_map_path]->URI = LV2_STATE__mapPath;
  3331. features[lv2_feature_id_state_map_path]->data = ft.stateMapPath;
  3332. features[lv2_feature_id_strict_bounds] = new LV2_Feature;
  3333. features[lv2_feature_id_strict_bounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3334. features[lv2_feature_id_strict_bounds]->data = nullptr;
  3335. features[lv2_feature_id_uri_map] = new LV2_Feature;
  3336. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  3337. features[lv2_feature_id_uri_map]->data = uriMapFt;
  3338. features[lv2_feature_id_urid_map] = new LV2_Feature;
  3339. features[lv2_feature_id_urid_map]->URI = LV2_URID__map;
  3340. features[lv2_feature_id_urid_map]->data = uridMapFt;
  3341. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  3342. features[lv2_feature_id_urid_unmap]->URI = LV2_URID__unmap;
  3343. features[lv2_feature_id_urid_unmap]->data = uridUnmapFt;
  3344. features[lv2_feature_id_worker] = new LV2_Feature;
  3345. features[lv2_feature_id_worker]->URI = LV2_WORKER__schedule;
  3346. features[lv2_feature_id_worker]->data = workerFt;
  3347. // ---------------------------------------------------------------
  3348. // get DLL main entry
  3349. const LV2_Lib_Descriptor_Function descLibFn = (LV2_Lib_Descriptor_Function)libSymbol("lv2_lib_descriptor");
  3350. if (descLibFn)
  3351. {
  3352. // -----------------------------------------------------------
  3353. // get lib descriptor
  3354. const LV2_Lib_Descriptor* libDesc = descLibFn(rdf_descriptor->Bundle, features);
  3355. if (! libDesc)
  3356. {
  3357. setLastError("Plugin failed to return library descriptor");
  3358. return false;
  3359. }
  3360. // -----------------------------------------------------------
  3361. // get descriptor that matches URI
  3362. uint32_t i = 0;
  3363. while ((descriptor = libDesc->get_plugin(libDesc->handle, i++)))
  3364. {
  3365. if (strcmp(descriptor->URI, URI) == 0)
  3366. break;
  3367. }
  3368. if (libDescs.count(libDesc) > 0)
  3369. {
  3370. if (! descriptor)
  3371. libDesc->cleanup(libDesc->handle);
  3372. }
  3373. else
  3374. libDescs.insert(libDesc);
  3375. }
  3376. else
  3377. {
  3378. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)libSymbol("lv2_descriptor");
  3379. // -----------------------------------------------------------
  3380. // if no descriptor function found, return error
  3381. if (! descFn)
  3382. {
  3383. setLastError("Could not find the LV2 Descriptor in the plugin library");
  3384. return false;
  3385. }
  3386. // -----------------------------------------------------------
  3387. // get descriptor that matches URI
  3388. uint32_t i = 0;
  3389. while ((descriptor = descFn(i++)))
  3390. {
  3391. if (strcmp(descriptor->URI, URI) == 0)
  3392. break;
  3393. }
  3394. }
  3395. if (! descriptor)
  3396. {
  3397. setLastError("Could not find the requested plugin URI in the plugin library");
  3398. return false;
  3399. }
  3400. // ---------------------------------------------------------------
  3401. // check supported port-types and features
  3402. bool canContinue = true;
  3403. // Check supported ports
  3404. for (uint32_t i=0; i < rdf_descriptor->PortCount; i++)
  3405. {
  3406. LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  3407. 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)))
  3408. {
  3409. if (! LV2_IS_PORT_OPTIONAL(rdf_descriptor->Ports[i].Properties))
  3410. {
  3411. setLastError("Plugin requires a port that is not currently supported");
  3412. canContinue = false;
  3413. break;
  3414. }
  3415. }
  3416. }
  3417. // Check supported features
  3418. for (uint32_t i=0; i < rdf_descriptor->FeatureCount && canContinue; i++)
  3419. {
  3420. if (LV2_IS_FEATURE_REQUIRED(rdf_descriptor->Features[i].Type) && ! is_lv2_feature_supported(rdf_descriptor->Features[i].URI))
  3421. {
  3422. QString msg = QString("Plugin requires a feature that is not supported:\n%1").arg(rdf_descriptor->Features[i].URI);
  3423. setLastError(msg.toUtf8().constData());
  3424. canContinue = false;
  3425. break;
  3426. }
  3427. }
  3428. // Check extensions
  3429. for (uint32_t i=0; i < rdf_descriptor->ExtensionCount; i++)
  3430. {
  3431. if (strcmp(rdf_descriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3432. m_hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3433. else if (strcmp(rdf_descriptor->Extensions[i], LV2_STATE__interface) == 0)
  3434. m_hints |= PLUGIN_HAS_EXTENSION_STATE;
  3435. else if (strcmp(rdf_descriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3436. m_hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3437. else
  3438. qDebug("Plugin has non-supported extension: '%s'", rdf_descriptor->Extensions[i]);
  3439. }
  3440. if (! canContinue)
  3441. {
  3442. // error already set
  3443. return false;
  3444. }
  3445. // ---------------------------------------------------------------
  3446. // get info
  3447. m_filename = strdup(bundle);
  3448. if (name)
  3449. m_name = x_engine->getUniquePluginName(name);
  3450. else
  3451. m_name = x_engine->getUniquePluginName(rdf_descriptor->Name);
  3452. // ---------------------------------------------------------------
  3453. // register client
  3454. x_client = x_engine->addClient(this);
  3455. if (! x_client->isOk())
  3456. {
  3457. setLastError("Failed to register plugin client");
  3458. return false;
  3459. }
  3460. // ---------------------------------------------------------------
  3461. // initialize plugin
  3462. handle = descriptor->instantiate(descriptor, x_engine->getSampleRate(), rdf_descriptor->Bundle, features);
  3463. if (! handle)
  3464. {
  3465. setLastError("Plugin failed to initialize");
  3466. return false;
  3467. }
  3468. // ---------------------------------------------------------------
  3469. // gui stuff
  3470. if (rdf_descriptor->UICount == 0)
  3471. return true;
  3472. // -----------------------------------------------------------
  3473. // find more appropriate ui
  3474. int eQt4, eCocoa, eHWND, eX11, eGtk2, eGtk3, iCocoa, iHWND, iX11, iQt4, iExt, iSuil, iFinal;
  3475. eQt4 = eCocoa = eHWND = eX11 = eGtk2 = eGtk3 = iQt4 = iCocoa = iHWND = iX11 = iExt = iSuil = iFinal = -1;
  3476. for (uint32_t i=0; i < rdf_descriptor->UICount; i++)
  3477. {
  3478. switch (rdf_descriptor->UIs[i].Type)
  3479. {
  3480. case LV2_UI_QT4:
  3481. #ifndef BUILD_BRIDGE
  3482. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3483. eQt4 = i;
  3484. #endif
  3485. iQt4 = i;
  3486. break;
  3487. case LV2_UI_COCOA:
  3488. #ifndef BUILD_BRIDGE
  3489. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3490. eCocoa = i;
  3491. #endif
  3492. iCocoa = i;
  3493. break;
  3494. case LV2_UI_WINDOWS:
  3495. #ifndef BUILD_BRIDGE
  3496. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3497. eHWND = i;
  3498. #endif
  3499. iHWND = i;
  3500. break;
  3501. case LV2_UI_X11:
  3502. #ifndef BUILD_BRIDGE
  3503. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3504. eX11 = i;
  3505. #endif
  3506. iX11 = i;
  3507. break;
  3508. case LV2_UI_GTK2:
  3509. #ifdef BUILD_BRIDGE
  3510. if (false)
  3511. #else
  3512. # ifdef HAVE_SUIL
  3513. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3514. # else
  3515. if (isUiBridgeable(i))
  3516. # endif
  3517. #endif
  3518. eGtk2 = i;
  3519. #ifdef HAVE_SUIL
  3520. iSuil = i;
  3521. #endif
  3522. break;
  3523. #ifndef BUILD_BRIDGE
  3524. case LV2_UI_GTK3:
  3525. if (isUiBridgeable(i))
  3526. eGtk3 = i;
  3527. break;
  3528. #endif
  3529. case LV2_UI_EXTERNAL:
  3530. case LV2_UI_OLD_EXTERNAL:
  3531. iExt = i;
  3532. break;
  3533. default:
  3534. break;
  3535. }
  3536. }
  3537. if (eQt4 >= 0)
  3538. iFinal = eQt4;
  3539. else if (eCocoa >= 0)
  3540. iFinal = eCocoa;
  3541. else if (eHWND >= 0)
  3542. iFinal = eHWND;
  3543. else if (eX11 >= 0)
  3544. iFinal = eX11;
  3545. else if (eGtk2 >= 0)
  3546. iFinal = eGtk2;
  3547. else if (eGtk3 >= 0)
  3548. iFinal = eGtk3;
  3549. else if (iQt4 >= 0)
  3550. iFinal = iQt4;
  3551. else if (iCocoa >= 0)
  3552. iFinal = iCocoa;
  3553. else if (iHWND >= 0)
  3554. iFinal = iHWND;
  3555. else if (iX11 >= 0)
  3556. iFinal = iX11;
  3557. else if (iExt >= 0)
  3558. iFinal = iExt;
  3559. else if (iSuil >= 0)
  3560. iFinal = iSuil;
  3561. #ifndef BUILD_BRIDGE
  3562. const bool isBridged = (iFinal == eQt4 || iFinal == eCocoa || iFinal == eHWND || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3);
  3563. #endif
  3564. #ifdef HAVE_SUIL
  3565. const bool isSuil = (iFinal == iSuil && !isBridged);
  3566. #endif
  3567. if (iFinal < 0)
  3568. {
  3569. qWarning("Failed to find an appropriate LV2 UI for this plugin");
  3570. return true;
  3571. }
  3572. ui.rdf_descriptor = &rdf_descriptor->UIs[iFinal];
  3573. // -----------------------------------------------------------
  3574. // check supported ui features
  3575. canContinue = true;
  3576. for (uint32_t i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  3577. {
  3578. if (LV2_IS_FEATURE_REQUIRED(ui.rdf_descriptor->Features[i].Type) && is_lv2_ui_feature_supported(ui.rdf_descriptor->Features[i].URI) == false)
  3579. {
  3580. qCritical("Plugin UI requires a feature that is not supported:\n%s", ui.rdf_descriptor->Features[i].URI);
  3581. canContinue = false;
  3582. break;
  3583. }
  3584. }
  3585. if (! canContinue)
  3586. {
  3587. ui.rdf_descriptor = nullptr;
  3588. return true;
  3589. }
  3590. #ifdef HAVE_SUIL
  3591. if (isSuil)
  3592. {
  3593. // -------------------------------------------------------
  3594. // init suil host
  3595. suil.host = suil_host_new(carla_lv2_ui_write_function, carla_lv2_ui_port_map, nullptr, nullptr);
  3596. }
  3597. else
  3598. #endif
  3599. {
  3600. // -------------------------------------------------------
  3601. // open DLL
  3602. if (! uiLibOpen(ui.rdf_descriptor->Binary))
  3603. {
  3604. qCritical("Could not load UI library, error was:\n%s", libError(ui.rdf_descriptor->Binary));
  3605. ui.rdf_descriptor = nullptr;
  3606. return true;
  3607. }
  3608. // -------------------------------------------------------
  3609. // get DLL main entry
  3610. LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor");
  3611. if (! ui_descFn)
  3612. {
  3613. qCritical("Could not find the LV2UI Descriptor in the UI library");
  3614. uiLibClose();
  3615. ui.lib = nullptr;
  3616. ui.rdf_descriptor = nullptr;
  3617. return true;
  3618. }
  3619. // -------------------------------------------------------
  3620. // get descriptor that matches URI
  3621. uint32_t i = 0;
  3622. while ((ui.descriptor = ui_descFn(i++)))
  3623. {
  3624. if (strcmp(ui.descriptor->URI, ui.rdf_descriptor->URI) == 0)
  3625. break;
  3626. }
  3627. if (! ui.descriptor)
  3628. {
  3629. qCritical("Could not find the requested GUI in the plugin UI library");
  3630. uiLibClose();
  3631. ui.lib = nullptr;
  3632. ui.rdf_descriptor = nullptr;
  3633. return true;
  3634. }
  3635. }
  3636. // -----------------------------------------------------------
  3637. // initialize ui according to type
  3638. const LV2_Property uiType = ui.rdf_descriptor->Type;
  3639. #ifndef BUILD_BRIDGE
  3640. if (isBridged)
  3641. {
  3642. // -------------------------------------------------------
  3643. // initialize ui bridge
  3644. if (const char* const oscBinary = lv2bridge2str(uiType))
  3645. {
  3646. gui.type = GUI_EXTERNAL_OSC;
  3647. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  3648. osc.thread->setOscData(oscBinary, descriptor->URI, ui.descriptor->URI);
  3649. }
  3650. }
  3651. else
  3652. #endif
  3653. {
  3654. // -------------------------------------------------------
  3655. // initialize ui features
  3656. QString guiTitle = QString("%1 (GUI)").arg(m_name);
  3657. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3658. uiDataFt->data_access = descriptor->extension_data;
  3659. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3660. uiPortMapFt->handle = this;
  3661. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3662. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3663. uiResizeFt->handle = this;
  3664. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3665. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3666. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3667. uiExternalHostFt->plugin_human_id = strdup(guiTitle.toUtf8().constData());
  3668. features[lv2_feature_id_data_access] = new LV2_Feature;
  3669. features[lv2_feature_id_data_access]->URI = LV2_DATA_ACCESS_URI;
  3670. features[lv2_feature_id_data_access]->data = uiDataFt;
  3671. features[lv2_feature_id_instance_access] = new LV2_Feature;
  3672. features[lv2_feature_id_instance_access]->URI = LV2_INSTANCE_ACCESS_URI;
  3673. features[lv2_feature_id_instance_access]->data = handle;
  3674. features[lv2_feature_id_ui_parent] = new LV2_Feature;
  3675. features[lv2_feature_id_ui_parent]->URI = LV2_UI__parent;
  3676. features[lv2_feature_id_ui_parent]->data = nullptr;
  3677. features[lv2_feature_id_ui_port_map] = new LV2_Feature;
  3678. features[lv2_feature_id_ui_port_map]->URI = LV2_UI__portMap;
  3679. features[lv2_feature_id_ui_port_map]->data = uiPortMapFt;
  3680. features[lv2_feature_id_ui_resize] = new LV2_Feature;
  3681. features[lv2_feature_id_ui_resize]->URI = LV2_UI__resize;
  3682. features[lv2_feature_id_ui_resize]->data = uiResizeFt;
  3683. features[lv2_feature_id_external_ui] = new LV2_Feature;
  3684. features[lv2_feature_id_external_ui]->URI = LV2_EXTERNAL_UI__Host;
  3685. features[lv2_feature_id_external_ui]->data = uiExternalHostFt;
  3686. features[lv2_feature_id_external_ui_old] = new LV2_Feature;
  3687. features[lv2_feature_id_external_ui_old]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3688. features[lv2_feature_id_external_ui_old]->data = uiExternalHostFt;
  3689. // -------------------------------------------------------
  3690. // initialize ui
  3691. switch (uiType)
  3692. {
  3693. case LV2_UI_QT4:
  3694. qDebug("Will use LV2 Qt4 UI");
  3695. gui.type = GUI_INTERNAL_QT4;
  3696. gui.resizable = isUiResizable();
  3697. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  3698. m_hints |= PLUGIN_USES_SINGLE_THREAD;
  3699. break;
  3700. case LV2_UI_COCOA:
  3701. qDebug("Will use LV2 Cocoa UI");
  3702. gui.type = GUI_INTERNAL_COCOA;
  3703. gui.resizable = isUiResizable();
  3704. break;
  3705. case LV2_UI_WINDOWS:
  3706. qDebug("Will use LV2 Windows UI");
  3707. gui.type = GUI_INTERNAL_HWND;
  3708. gui.resizable = isUiResizable();
  3709. break;
  3710. case LV2_UI_X11:
  3711. qDebug("Will use LV2 X11 UI");
  3712. gui.type = GUI_INTERNAL_X11;
  3713. gui.resizable = isUiResizable();
  3714. break;
  3715. case LV2_UI_GTK2:
  3716. #ifdef HAVE_SUIL
  3717. qDebug("Will use LV2 Gtk2 UI (suil)");
  3718. gui.type = GUI_EXTERNAL_SUIL;
  3719. gui.resizable = isUiResizable();
  3720. 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);
  3721. m_hints |= PLUGIN_USES_SINGLE_THREAD;
  3722. if (suil.handle)
  3723. {
  3724. ui.handle = ((SuilInstanceImpl*)suil.handle)->handle;
  3725. ui.descriptor = ((SuilInstanceImpl*)suil.handle)->descriptor;
  3726. ui.widget = suil_instance_get_widget(suil.handle);
  3727. if (ui.widget)
  3728. {
  3729. QWidget* const widget = (QWidget*)ui.widget;
  3730. widget->setWindowTitle(guiTitle);
  3731. }
  3732. }
  3733. #else
  3734. qDebug("Will use LV2 Gtk2 UI, NOT!");
  3735. #endif
  3736. break;
  3737. case LV2_UI_GTK3:
  3738. qDebug("Will use LV2 Gtk3 UI, NOT!");
  3739. break;
  3740. case LV2_UI_EXTERNAL:
  3741. case LV2_UI_OLD_EXTERNAL:
  3742. qDebug("Will use LV2 External UI");
  3743. gui.type = GUI_EXTERNAL_LV2;
  3744. break;
  3745. }
  3746. }
  3747. if (gui.type != GUI_NONE)
  3748. m_hints |= PLUGIN_HAS_GUI;
  3749. return true;
  3750. }
  3751. private:
  3752. LV2_Handle handle, h2;
  3753. const LV2_Descriptor* descriptor;
  3754. const LV2_RDF_Descriptor* rdf_descriptor;
  3755. LV2_Feature* features[lv2_feature_count+1];
  3756. struct {
  3757. const LV2_State_Interface* state;
  3758. const LV2_Worker_Interface* worker;
  3759. const LV2_Programs_Interface* programs;
  3760. const LV2_Programs_UI_Interface* uiprograms;
  3761. } ext;
  3762. struct {
  3763. void* lib;
  3764. LV2UI_Handle handle;
  3765. LV2UI_Widget widget;
  3766. const LV2UI_Descriptor* descriptor;
  3767. const LV2_RDF_UI* rdf_descriptor;
  3768. } ui;
  3769. struct {
  3770. GuiType type;
  3771. bool resizable;
  3772. int width;
  3773. int height;
  3774. } gui;
  3775. #ifdef HAVE_SUIL
  3776. struct {
  3777. SuilHost* host;
  3778. SuilInstance* handle;
  3779. QByteArray pos;
  3780. } suil;
  3781. #endif
  3782. Lv2AtomQueue atomQueueIn;
  3783. Lv2AtomQueue atomQueueOut;
  3784. Lv2PluginEventData evIn;
  3785. Lv2PluginEventData evOut;
  3786. float* paramBuffers;
  3787. std::vector<const char*> customURIDs;
  3788. bool lastTimePosPlaying;
  3789. uint32_t lastTimePosFrame;
  3790. static unsigned int m_count;
  3791. static std::set<const LV2_Lib_Descriptor*> libDescs;
  3792. static struct Ft {
  3793. LV2_Event_Feature* event;
  3794. LV2_Log_Log* log;
  3795. LV2_Options_Option* options;
  3796. LV2_RtMemPool_Pool* rtMemPool;
  3797. LV2_State_Make_Path* stateMakePath;
  3798. LV2_State_Map_Path* stateMapPath;
  3799. } ft;
  3800. };
  3801. /**@}*/
  3802. // -------------------------------------------------------------------
  3803. // static data
  3804. unsigned int Lv2Plugin::m_count = 0;
  3805. std::set<const LV2_Lib_Descriptor*> Lv2Plugin::libDescs;
  3806. Lv2Plugin::Ft Lv2Plugin::ft = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  3807. // -------------------------------------------------------------------------------------------------------------------
  3808. int CarlaOsc::handleMsgLv2AtomTransfer(CARLA_OSC_HANDLE_ARGS2)
  3809. {
  3810. qDebug("CarlaOsc::handleMsgLv2AtomTransfer()");
  3811. CARLA_OSC_CHECK_OSC_TYPES(3, "iss");
  3812. const int32_t portIndex = argv[0]->i;
  3813. const char* const typeStr = (const char*)&argv[1]->s;
  3814. const char* const atomBuf = (const char*)&argv[2]->s;
  3815. QByteArray chunk;
  3816. chunk = QByteArray::fromBase64(atomBuf);
  3817. LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  3818. CarlaBackend::Lv2Plugin* const lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3819. atom->type = lv2plugin->getCustomURID(typeStr);
  3820. lv2plugin->handleTransferAtom(portIndex, atom);
  3821. return 0;
  3822. }
  3823. int CarlaOsc::handleMsgLv2EventTransfer(CARLA_OSC_HANDLE_ARGS2)
  3824. {
  3825. qDebug("CarlaOsc::handleMsgLv2EventTransfer()");
  3826. CARLA_OSC_CHECK_OSC_TYPES(3, "iss");
  3827. const int32_t portIndex = argv[0]->i;
  3828. const char* const typeStr = (const char*)&argv[1]->s;
  3829. const char* const atomBuf = (const char*)&argv[2]->s;
  3830. QByteArray chunk;
  3831. chunk = QByteArray::fromBase64(atomBuf);
  3832. LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  3833. CarlaBackend::Lv2Plugin* const lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3834. atom->type = lv2plugin->getCustomURID(typeStr);
  3835. lv2plugin->handleTransferEvent(portIndex, atom);
  3836. return 0;
  3837. }
  3838. CARLA_BACKEND_END_NAMESPACE
  3839. #else // WANT_LV2
  3840. # warning Building without LV2 support
  3841. #endif
  3842. CARLA_BACKEND_START_NAMESPACE
  3843. CarlaPlugin* CarlaPlugin::newLV2(const initializer& init)
  3844. {
  3845. qDebug("CarlaPlugin::newLV2(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  3846. #ifdef WANT_LV2
  3847. short id = init.engine->getNewPluginId();
  3848. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  3849. {
  3850. setLastError("Maximum number of plugins reached");
  3851. return nullptr;
  3852. }
  3853. Lv2Plugin* const plugin = new Lv2Plugin(init.engine, id);
  3854. if (! plugin->init(init.filename, init.name, init.label))
  3855. {
  3856. delete plugin;
  3857. return nullptr;
  3858. }
  3859. plugin->reload();
  3860. # ifndef BUILD_BRIDGE
  3861. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  3862. {
  3863. if (! (plugin->hints() & PLUGIN_CAN_FORCE_STEREO))
  3864. {
  3865. setLastError("Carla's rack mode can only work with Mono (simple) or Stereo LV2 plugins, sorry!");
  3866. delete plugin;
  3867. return nullptr;
  3868. }
  3869. }
  3870. # endif
  3871. plugin->registerToOscControl();
  3872. plugin->updateUi();
  3873. return plugin;
  3874. #else
  3875. setLastError("LV2 support not available");
  3876. return nullptr;
  3877. #endif
  3878. }
  3879. CARLA_BACKEND_END_NAMESPACE