Collection of tools useful for audio production
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4693 lines
164KB

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