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.

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