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.

4529 lines
159KB

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