Audio plugin host https://kx.studio/carla
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.

4699 lines
165KB

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