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.

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