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.

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