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.

4730 lines
165KB

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