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.

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