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.

4536 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 MidiProgramData[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. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  1773. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  1774. allNotesOffSent = true;
  1775. }
  1776. break;
  1777. case CarlaEngineEventAllNotesOff:
  1778. if (cinEvent->channel == m_ctrlInChannel)
  1779. {
  1780. if (evIn.count > 0 && ! allNotesOffSent)
  1781. sendMidiAllNotesOff();
  1782. allNotesOffSent = true;
  1783. }
  1784. break;
  1785. }
  1786. }
  1787. } // End of Parameters Input
  1788. CARLA_PROCESS_CONTINUE_CHECK;
  1789. // --------------------------------------------------------------------------------------------------------
  1790. // Event Input
  1791. if (evIn.count > 0 && m_active && m_activeBefore)
  1792. {
  1793. // ----------------------------------------------------------------------------------------------------
  1794. // MIDI Input (External)
  1795. {
  1796. engineMidiLock();
  1797. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  1798. {
  1799. if (extMidiNotes[i].channel < 0)
  1800. break;
  1801. uint8_t midiEvent[3] = { 0 };
  1802. midiEvent[0] = m_ctrlInChannel + extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1803. midiEvent[1] = extMidiNotes[i].note;
  1804. midiEvent[2] = extMidiNotes[i].velo;
  1805. // send to first midi input
  1806. for (k=0; k < evIn.count; k++)
  1807. {
  1808. if (evIn.data[k].type & CARLA_EVENT_TYPE_MIDI)
  1809. {
  1810. if (evIn.data[k].type & CARLA_EVENT_DATA_ATOM)
  1811. {
  1812. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  1813. aev->time.frames = 0;
  1814. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1815. aev->body.size = 3;
  1816. memcpy(LV2_ATOM_BODY(&aev->body), midiEvent, 3);
  1817. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 3);
  1818. evInAtomOffsets[k] += evInPadSize;
  1819. evIn.data[k].atom->atom.size += evInPadSize;
  1820. }
  1821. else if (evIn.data[k].type & CARLA_EVENT_DATA_EVENT)
  1822. {
  1823. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  1824. }
  1825. else if (evIn.data[k].type & CARLA_EVENT_DATA_MIDI_LL)
  1826. {
  1827. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiEvent);
  1828. }
  1829. break;
  1830. }
  1831. }
  1832. extMidiNotes[i].channel = -1; // mark as invalid
  1833. midiEventCount += 1;
  1834. }
  1835. engineMidiUnlock();
  1836. } // End of MIDI Input (External)
  1837. CARLA_PROCESS_CONTINUE_CHECK;
  1838. // ----------------------------------------------------------------------------------------------------
  1839. // MIDI Input (System)
  1840. for (i=0; i < evIn.count; i++)
  1841. {
  1842. if (! evIn.data[i].port)
  1843. continue;
  1844. const CarlaEngineMidiEvent* minEvent;
  1845. uint32_t time, nEvents = evIn.data[i].port->getEventCount();
  1846. for (k=0; k < nEvents && midiEventCount < MAX_MIDI_EVENTS; k++)
  1847. {
  1848. minEvent = evIn.data[i].port->getEvent(k);
  1849. if (! minEvent)
  1850. continue;
  1851. time = minEvent->time - framesOffset;
  1852. if (time >= frames)
  1853. continue;
  1854. uint8_t status = minEvent->data[0];
  1855. uint8_t channel = status & 0x0F;
  1856. // Fix bad note-off
  1857. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  1858. status -= 0x10;
  1859. // only write supported status types
  1860. 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))
  1861. {
  1862. if (evIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1863. {
  1864. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  1865. aev->time.frames = time;
  1866. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1867. aev->body.size = minEvent->size;
  1868. memcpy(LV2_ATOM_BODY(&aev->body), minEvent->data, minEvent->size);
  1869. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + minEvent->size);
  1870. evInAtomOffsets[i] += evInPadSize;
  1871. evIn.data[i].atom->atom.size += evInPadSize;
  1872. }
  1873. else if (evIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1874. {
  1875. lv2_event_write(&evInEventIters[i], time, 0, CARLA_URI_MAP_ID_MIDI_EVENT, minEvent->size, minEvent->data);
  1876. }
  1877. else if (evIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1878. {
  1879. lv2midi_put_event(&evInMidiStates[i], time, minEvent->size, minEvent->data);
  1880. }
  1881. if (MIDI_IS_STATUS_NOTE_OFF(status))
  1882. postponeEvent(PluginPostEventNoteOff, channel, minEvent->data[1], 0.0);
  1883. else if (MIDI_IS_STATUS_NOTE_ON(status))
  1884. postponeEvent(PluginPostEventNoteOn, channel, minEvent->data[1], minEvent->data[2]);
  1885. }
  1886. midiEventCount += 1;
  1887. }
  1888. } // End of MIDI Input (System)
  1889. // ----------------------------------------------------------------------------------------------------
  1890. // Message Input
  1891. {
  1892. for (i=0; i < evIn.count; i++)
  1893. {
  1894. if (! evIn.data[i].type & CARLA_EVENT_TYPE_MESSAGE)
  1895. continue;
  1896. #if 0
  1897. // send transport info if changed
  1898. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  1899. if (timeInfo->playing != lastTimePosPlaying || timeInfo->frame != lastTimePosFrame)
  1900. {
  1901. uint8_t posBuf[256];
  1902. LV2_Atom* const lv2Pos = (LV2_Atom*)posBuf;
  1903. LV2_Atom_Forge tempForge;
  1904. LV2_Atom_Forge* const forge = &tempForge;
  1905. lv2_atom_forge_set_buffer(forge, posBuf, sizeof(uint8_t)*256);
  1906. //LV2_Atom_Forge_Frame frame;
  1907. //lv2_atom_forge_blank(forge, &frame, 1, jalv->urids.time_Position);
  1908. //lv2_atom_forge_property_head(forge, jalv->urids.time_frame, 0);
  1909. lv2_atom_forge_long(forge, timeInfo->frame);
  1910. //lv2_atom_forge_property_head(forge, jalv->urids.time_position, 0);
  1911. lv2_atom_forge_long(forge, timeInfo->time);
  1912. //lv2_atom_forge_property_head(forge, jalv->urids.time_speed, 0);
  1913. lv2_atom_forge_float(forge, timeInfo->playing ? 1.0 : 0.0);
  1914. if (timeInfo->valid & CarlaEngineTimeBBT)
  1915. {
  1916. //lv2_atom_forge_property_head(forge, jalv->urids.time_bar, 0);
  1917. lv2_atom_forge_float(forge, timeInfo->bbt.bar - 1);
  1918. //lv2_atom_forge_property_head(forge, jalv->urids.time_barBeat, 0);
  1919. lv2_atom_forge_float(forge, timeInfo->bbt.beat - 1 + (timeInfo->bbt.tick / timeInfo->bbt.ticks_per_beat));
  1920. //lv2_atom_forge_property_head(forge, jalv->urids.time_beat, 0);
  1921. lv2_atom_forge_float(forge, timeInfo->bbt.beat - 1); // FIXME: -1 ?
  1922. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatUnit, 0);
  1923. lv2_atom_forge_float(forge, timeInfo->bbt.beat_type);
  1924. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatsPerBar, 0);
  1925. lv2_atom_forge_float(forge, timeInfo->bbt.beats_per_bar);
  1926. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatsPerMinute, 0);
  1927. lv2_atom_forge_float(forge, timeInfo->bbt.beats_per_minute);
  1928. }
  1929. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  1930. aev->time.frames = framesOffset;
  1931. aev->body.type = lv2Pos->type;
  1932. aev->body.size = lv2Pos->size;
  1933. memcpy(LV2_ATOM_BODY(&aev->body), LV2_ATOM_BODY(lv2Pos), lv2Pos->size);
  1934. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + lv2Pos->size);
  1935. evInAtomOffsets[i] += evInPadSize;
  1936. evIn.data[i].atom->atom.size += evInPadSize;
  1937. lastTimePosPlaying = timeInfo->playing;
  1938. lastTimePosFrame = timeInfo->playing ? timeInfo->frame + frames : timeInfo->frame;
  1939. }
  1940. #endif
  1941. atomQueueIn.lock();
  1942. if (! atomQueueIn.isEmpty())
  1943. {
  1944. uint32_t portIndex;
  1945. const LV2_Atom* atom;
  1946. while (atomQueueIn.get(&portIndex, &atom, false))
  1947. {
  1948. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  1949. {
  1950. const LV2_Atom_Worker* const atomWorker = (const LV2_Atom_Worker*)atom;
  1951. ext.worker->work_response(handle, atomWorker->body.size, atomWorker->body.data);
  1952. continue;
  1953. }
  1954. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  1955. aev->time.frames = framesOffset;
  1956. aev->body.type = atom->type;
  1957. aev->body.size = atom->size;
  1958. memcpy(LV2_ATOM_BODY(&aev->body), LV2_ATOM_BODY(atom), atom->size);
  1959. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + atom->size);
  1960. evInAtomOffsets[i] += evInPadSize;
  1961. evIn.data[i].atom->atom.size += evInPadSize;
  1962. }
  1963. }
  1964. atomQueueIn.unlock();
  1965. }
  1966. }
  1967. } // End of Event Input
  1968. CARLA_PROCESS_CONTINUE_CHECK;
  1969. // --------------------------------------------------------------------------------------------------------
  1970. // Special Parameters
  1971. {
  1972. int32_t rindex;
  1973. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  1974. for (k=0; k < param.count; k++)
  1975. {
  1976. if (param.data[k].type == PARAMETER_LATENCY)
  1977. {
  1978. // TODO
  1979. }
  1980. else if (param.data[k].type == PARAMETER_LV2_FREEWHEEL)
  1981. {
  1982. setParameterValue(k, x_engine->isOffline() ? 1.0 : 0.0, false, false, false);
  1983. }
  1984. else if (param.data[k].type == PARAMETER_LV2_TIME)
  1985. {
  1986. rindex = param.data[k].rindex;
  1987. CARLA_ASSERT(rindex >= 0 && rindex < (int32_t)rdf_descriptor->PortCount);
  1988. switch (rdf_descriptor->Ports[rindex].Designation)
  1989. {
  1990. // Non-BBT
  1991. case LV2_PORT_DESIGNATION_TIME_FRAME:
  1992. setParameterValue(k, timeInfo->frame, false, false, false);
  1993. break;
  1994. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  1995. break;
  1996. case LV2_PORT_DESIGNATION_TIME_POSITION:
  1997. setParameterValue(k, timeInfo->time, false, false, false);
  1998. break;
  1999. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2000. setParameterValue(k, timeInfo->playing ? 1.0 : 0.0, false, false, false);
  2001. break;
  2002. // BBT
  2003. case LV2_PORT_DESIGNATION_TIME_BAR:
  2004. if (timeInfo->valid & CarlaEngineTimeBBT)
  2005. setParameterValue(k, timeInfo->bbt.bar - 1, false, false, false);
  2006. break;
  2007. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2008. if (timeInfo->valid & CarlaEngineTimeBBT)
  2009. setParameterValue(k, timeInfo->bbt.beat - 1 + (timeInfo->bbt.tick / timeInfo->bbt.ticks_per_beat), false, false, false);
  2010. break;
  2011. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2012. if (timeInfo->valid & CarlaEngineTimeBBT) // FIXME: -1 ?
  2013. setParameterValue(k, timeInfo->bbt.beat - 1, false, false, false);
  2014. break;
  2015. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2016. if (timeInfo->valid & CarlaEngineTimeBBT)
  2017. setParameterValue(k, timeInfo->bbt.beat_type, false, false, false);
  2018. break;
  2019. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2020. if (timeInfo->valid & CarlaEngineTimeBBT)
  2021. setParameterValue(k, timeInfo->bbt.beats_per_bar, false, false, false);
  2022. break;
  2023. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2024. if (timeInfo->valid & CarlaEngineTimeBBT)
  2025. setParameterValue(k, timeInfo->bbt.beats_per_minute, false, false, false);
  2026. break;
  2027. }
  2028. }
  2029. }
  2030. }
  2031. CARLA_PROCESS_CONTINUE_CHECK;
  2032. // --------------------------------------------------------------------------------------------------------
  2033. // Plugin processing
  2034. if (m_active)
  2035. {
  2036. if (! m_activeBefore)
  2037. {
  2038. if (evIn.count > 0)
  2039. {
  2040. for (i=0; i < MAX_MIDI_CHANNELS; i++)
  2041. {
  2042. uint8_t midiEvent1[2] = { 0 };
  2043. midiEvent1[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2044. midiEvent1[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2045. uint8_t midiEvent2[2] = { 0 };
  2046. midiEvent2[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2047. midiEvent2[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2048. // send to all midi inputs
  2049. for (k=0; k < evIn.count; k++)
  2050. {
  2051. if (evIn.data[k].type & CARLA_EVENT_TYPE_MIDI)
  2052. {
  2053. if (evIn.data[k].type & CARLA_EVENT_DATA_ATOM)
  2054. {
  2055. const uint32_t padSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 2);
  2056. // all sound off
  2057. LV2_Atom_Event* const aev1 = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  2058. aev1->time.frames = 0;
  2059. aev1->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2060. aev1->body.size = 2;
  2061. memcpy(LV2_ATOM_BODY(&aev1->body), midiEvent1, 2);
  2062. evInAtomOffsets[k] += padSize;
  2063. evIn.data[k].atom->atom.size += padSize;
  2064. // all notes off
  2065. LV2_Atom_Event* const aev2 = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  2066. aev2->time.frames = 0;
  2067. aev2->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2068. aev2->body.size = 2;
  2069. memcpy(LV2_ATOM_BODY(&aev2->body), midiEvent2, 2);
  2070. evInAtomOffsets[k] += padSize;
  2071. evIn.data[k].atom->atom.size += padSize;
  2072. }
  2073. else if (evIn.data[k].type & CARLA_EVENT_DATA_EVENT)
  2074. {
  2075. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiEvent1);
  2076. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiEvent2);
  2077. }
  2078. else if (evIn.data[k].type & CARLA_EVENT_DATA_MIDI_LL)
  2079. {
  2080. lv2midi_put_event(&evInMidiStates[k], 0, 2, midiEvent1);
  2081. lv2midi_put_event(&evInMidiStates[k], 0, 2, midiEvent2);
  2082. }
  2083. }
  2084. }
  2085. }
  2086. midiEventCount = MAX_MIDI_CHANNELS*2;
  2087. }
  2088. if (descriptor->activate)
  2089. {
  2090. descriptor->activate(handle);
  2091. if (h2) descriptor->activate(h2);
  2092. }
  2093. }
  2094. for (i=0; i < aIn.count; i++)
  2095. {
  2096. if (i == 0 || ! h2) descriptor->connect_port(handle, aIn.rindexes[i], inBuffer[i]);
  2097. if (i == 1 && h2) descriptor->connect_port(h2, aIn.rindexes[i], inBuffer[i]);
  2098. }
  2099. for (i=0; i < aOut.count; i++)
  2100. {
  2101. if (i == 0 || ! h2) descriptor->connect_port(handle, aOut.rindexes[i], outBuffer[i]);
  2102. if (i == 1 && h2) descriptor->connect_port(h2, aOut.rindexes[i], outBuffer[i]);
  2103. }
  2104. descriptor->run(handle, frames);
  2105. if (h2) descriptor->run(h2, frames);
  2106. if (ext.worker && ext.worker->end_run)
  2107. {
  2108. ext.worker->end_run(handle);
  2109. if (h2) ext.worker->end_run(h2);
  2110. }
  2111. }
  2112. else
  2113. {
  2114. if (m_activeBefore)
  2115. {
  2116. if (descriptor->deactivate)
  2117. {
  2118. descriptor->deactivate(handle);
  2119. if (h2) descriptor->deactivate(h2);
  2120. }
  2121. }
  2122. }
  2123. CARLA_PROCESS_CONTINUE_CHECK;
  2124. // --------------------------------------------------------------------------------------------------------
  2125. // Post-processing (dry/wet, volume and balance)
  2126. if (m_active)
  2127. {
  2128. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  2129. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  2130. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  2131. double bal_rangeL, bal_rangeR;
  2132. float oldBufLeft[do_balance ? frames : 0];
  2133. for (i=0; i < aOut.count; i++)
  2134. {
  2135. // Dry/Wet
  2136. if (do_drywet)
  2137. {
  2138. for (k=0; k < frames; k++)
  2139. {
  2140. if (aOut.count == 1)
  2141. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[0][k]*(1.0-x_dryWet));
  2142. else
  2143. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[i][k]*(1.0-x_dryWet));
  2144. }
  2145. }
  2146. // Balance
  2147. if (do_balance)
  2148. {
  2149. if (i%2 == 0)
  2150. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  2151. bal_rangeL = (x_balanceLeft+1.0)/2;
  2152. bal_rangeR = (x_balanceRight+1.0)/2;
  2153. for (k=0; k < frames; k++)
  2154. {
  2155. if (i%2 == 0)
  2156. {
  2157. // left output
  2158. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  2159. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  2160. }
  2161. else
  2162. {
  2163. // right
  2164. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  2165. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  2166. }
  2167. }
  2168. }
  2169. // Volume
  2170. if (do_volume)
  2171. {
  2172. for (k=0; k < frames; k++)
  2173. outBuffer[i][k] *= x_volume;
  2174. }
  2175. // Output VU
  2176. for (k=0; i < 2 && k < frames; k++)
  2177. {
  2178. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  2179. aOutsPeak[i] = abs(outBuffer[i][k]);
  2180. }
  2181. }
  2182. }
  2183. else
  2184. {
  2185. // disable any output sound if not active
  2186. for (i=0; i < aOut.count; i++)
  2187. zeroF(outBuffer[i], frames);
  2188. aOutsPeak[0] = 0.0;
  2189. aOutsPeak[1] = 0.0;
  2190. } // End of Post-processing
  2191. CARLA_PROCESS_CONTINUE_CHECK;
  2192. // --------------------------------------------------------------------------------------------------------
  2193. // Control Output
  2194. if (param.portCout && m_active)
  2195. {
  2196. double value;
  2197. for (k=0; k < param.count; k++)
  2198. {
  2199. if (param.data[k].type == PARAMETER_OUTPUT)
  2200. {
  2201. fixParameterValue(paramBuffers[k], param.ranges[k]);
  2202. if (param.data[k].midiCC > 0)
  2203. {
  2204. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  2205. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  2206. }
  2207. }
  2208. }
  2209. } // End of Control Output
  2210. CARLA_PROCESS_CONTINUE_CHECK;
  2211. // --------------------------------------------------------------------------------------------------------
  2212. // Event Output
  2213. if (evOut.count > 0 && m_active)
  2214. {
  2215. atomQueueOut.lock();
  2216. for (i=0; i < evOut.count; i++)
  2217. {
  2218. // midi events need the midi port to send events to
  2219. if ((evOut.data[i].type & CARLA_EVENT_TYPE_MIDI) > 0 && ! evOut.data[i].port)
  2220. continue;
  2221. if (evOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2222. {
  2223. int32_t size = evOut.data[i].atom->atom.size - sizeof(LV2_Atom_Sequence_Body);
  2224. int32_t offset = 0;
  2225. while (offset < size)
  2226. {
  2227. qDebug("output event??, offset:%i, size:%i", offset, size);
  2228. const LV2_Atom_Event* const aev = (LV2_Atom_Event*)((char*)LV2_ATOM_CONTENTS(LV2_Atom_Sequence, evOut.data[i].atom) + offset);
  2229. if ((! aev) || aev->body.type == CARLA_URI_MAP_ID_NULL)
  2230. break;
  2231. qDebug("output event ------------------------------ YES!");
  2232. if (aev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2233. {
  2234. const unsigned char* const data = (unsigned char*)LV2_ATOM_BODY(&aev->body);
  2235. evOut.data[i].port->writeEvent(aev->time.frames, data, aev->body.size);
  2236. }
  2237. else if (aev->body.type == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2238. {
  2239. if (! atomQueueOut.isFull())
  2240. atomQueueOut.put(evOut.data[i].rindex, &aev->body);
  2241. }
  2242. offset += lv2_atom_pad_size(sizeof(LV2_Atom_Event) + aev->body.size);
  2243. }
  2244. }
  2245. else if (evOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2246. {
  2247. const LV2_Event* ev;
  2248. LV2_Event_Iterator iter;
  2249. uint8_t* data;
  2250. lv2_event_begin(&iter, evOut.data[i].event);
  2251. for (k=0; k < iter.buf->event_count; k++)
  2252. {
  2253. ev = lv2_event_get(&iter, &data);
  2254. if (ev && ev->type == CARLA_URI_MAP_ID_MIDI_EVENT && data)
  2255. evOut.data[i].port->writeEvent(ev->frames, data, ev->size);
  2256. lv2_event_increment(&iter);
  2257. }
  2258. }
  2259. else if (evOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2260. {
  2261. LV2_MIDIState state = { evOut.data[i].midi, frames, 0 };
  2262. uint32_t eventSize;
  2263. double eventTime;
  2264. unsigned char* eventData;
  2265. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  2266. {
  2267. evOut.data[i].port->writeEvent(eventTime, eventData, eventSize);
  2268. lv2midi_step(&state);
  2269. }
  2270. }
  2271. }
  2272. atomQueueOut.unlock();
  2273. } // End of Event Output
  2274. CARLA_PROCESS_CONTINUE_CHECK;
  2275. // --------------------------------------------------------------------------------------------------------
  2276. // Peak Values
  2277. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  2278. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  2279. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  2280. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  2281. m_activeBefore = m_active;
  2282. }
  2283. void bufferSizeChanged(const uint32_t newBufferSize)
  2284. {
  2285. lv2Options.bufferSize = newBufferSize;
  2286. }
  2287. // -------------------------------------------------------------------
  2288. // Post-poned events
  2289. void postEventHandleCustom(const int32_t size, const int32_t, const double, const void* const data)
  2290. {
  2291. qDebug("Lv2Plugin::postEventHandleCustom(%i, %p)", size, data);
  2292. CARLA_ASSERT(ext.worker && ext.worker->work);
  2293. if (ext.worker && ext.worker->work)
  2294. ext.worker->work(handle, carla_lv2_worker_respond, this, size, data);
  2295. }
  2296. void uiParameterChange(const uint32_t index, const double value)
  2297. {
  2298. CARLA_ASSERT(index < param.count);
  2299. if (index >= param.count)
  2300. return;
  2301. #ifndef BUILD_BRIDGE
  2302. if (gui.type == GUI_EXTERNAL_OSC)
  2303. {
  2304. if (osc.data.target)
  2305. osc_send_control(&osc.data, param.data[index].rindex, value);
  2306. }
  2307. else
  2308. #endif
  2309. {
  2310. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2311. {
  2312. float valueF = value;
  2313. ui.descriptor->port_event(ui.handle, param.data[index].rindex, sizeof(float), 0, &valueF);
  2314. }
  2315. }
  2316. }
  2317. void uiMidiProgramChange(const uint32_t index)
  2318. {
  2319. CARLA_ASSERT(index < midiprog.count);
  2320. if (index >= midiprog.count)
  2321. return;
  2322. #ifndef BUILD_BRIDGE
  2323. if (gui.type == GUI_EXTERNAL_OSC)
  2324. {
  2325. if (osc.data.target)
  2326. osc_send_midi_program(&osc.data, midiprog.data[index].bank, midiprog.data[index].program);
  2327. }
  2328. else
  2329. #endif
  2330. {
  2331. if (ext.uiprograms)
  2332. ext.uiprograms->select_program(ui.handle, midiprog.data[index].bank, midiprog.data[index].program);
  2333. }
  2334. }
  2335. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  2336. {
  2337. CARLA_ASSERT(channel < 16);
  2338. CARLA_ASSERT(note < 128);
  2339. CARLA_ASSERT(velo > 0 && velo < 128);
  2340. #ifndef BUILD_BRIDGE
  2341. if (gui.type == GUI_EXTERNAL_OSC)
  2342. {
  2343. if (osc.data.target)
  2344. {
  2345. uint8_t midiData[4] = { 0 };
  2346. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2347. midiData[2] = note;
  2348. midiData[3] = velo;
  2349. osc_send_midi(&osc.data, midiData);
  2350. }
  2351. }
  2352. else
  2353. #endif
  2354. {
  2355. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2356. {
  2357. LV2_Atom_MidiEvent midiEv;
  2358. midiEv.event.time.frames = 0;
  2359. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2360. midiEv.event.body.size = 3;
  2361. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2362. midiEv.data[1] = note;
  2363. midiEv.data[2] = velo;
  2364. ui.descriptor->port_event(ui.handle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2365. }
  2366. }
  2367. }
  2368. void uiNoteOff(const uint8_t channel, const uint8_t note)
  2369. {
  2370. CARLA_ASSERT(channel < 16);
  2371. CARLA_ASSERT(note < 128);
  2372. #ifndef BUILD_BRIDGE
  2373. if (gui.type == GUI_EXTERNAL_OSC)
  2374. {
  2375. if (osc.data.target)
  2376. {
  2377. uint8_t midiData[4] = { 0 };
  2378. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2379. midiData[2] = note;
  2380. osc_send_midi(&osc.data, midiData);
  2381. }
  2382. }
  2383. else
  2384. #endif
  2385. {
  2386. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2387. {
  2388. LV2_Atom_MidiEvent midiEv;
  2389. midiEv.event.time.frames = 0;
  2390. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2391. midiEv.event.body.size = 3;
  2392. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2393. midiEv.data[1] = note;
  2394. midiEv.data[2] = 0;
  2395. ui.descriptor->port_event(ui.handle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2396. }
  2397. }
  2398. }
  2399. // -------------------------------------------------------------------
  2400. // Cleanup
  2401. void removeClientPorts()
  2402. {
  2403. qDebug("Lv2Plugin::removeClientPorts() - start");
  2404. for (uint32_t i=0; i < evIn.count; i++)
  2405. {
  2406. if (evIn.data[i].port)
  2407. {
  2408. delete evIn.data[i].port;
  2409. evIn.data[i].port = nullptr;
  2410. }
  2411. }
  2412. for (uint32_t i=0; i < evOut.count; i++)
  2413. {
  2414. if (evOut.data[i].port)
  2415. {
  2416. delete evOut.data[i].port;
  2417. evOut.data[i].port = nullptr;
  2418. }
  2419. }
  2420. CarlaPlugin::removeClientPorts();
  2421. qDebug("Lv2Plugin::removeClientPorts() - end");
  2422. }
  2423. void initBuffers()
  2424. {
  2425. uint32_t i;
  2426. for (i=0; i < evIn.count; i++)
  2427. {
  2428. if (evIn.data[i].port)
  2429. evIn.data[i].port->initBuffer(x_engine);
  2430. }
  2431. for (uint32_t i=0; i < evOut.count; i++)
  2432. {
  2433. if (evOut.data[i].port)
  2434. evOut.data[i].port->initBuffer(x_engine);
  2435. }
  2436. CarlaPlugin::initBuffers();
  2437. }
  2438. void deleteBuffers()
  2439. {
  2440. qDebug("Lv2Plugin::deleteBuffers() - start");
  2441. if (evIn.count > 0)
  2442. {
  2443. for (uint32_t i=0; i < evIn.count; i++)
  2444. {
  2445. if (evIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  2446. {
  2447. free(evIn.data[i].atom);
  2448. }
  2449. else if (evIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  2450. {
  2451. free(evIn.data[i].event);
  2452. }
  2453. else if (evIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2454. {
  2455. delete[] evIn.data[i].midi->data;
  2456. delete evIn.data[i].midi;
  2457. }
  2458. }
  2459. delete[] evIn.data;
  2460. }
  2461. if (evOut.count > 0)
  2462. {
  2463. for (uint32_t i=0; i < evOut.count; i++)
  2464. {
  2465. if (evOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2466. {
  2467. free(evOut.data[i].atom);
  2468. }
  2469. else if (evOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2470. {
  2471. free(evOut.data[i].event);
  2472. }
  2473. else if (evOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2474. {
  2475. delete[] evOut.data[i].midi->data;
  2476. delete evOut.data[i].midi;
  2477. }
  2478. }
  2479. delete[] evOut.data;
  2480. }
  2481. if (param.count > 0)
  2482. delete[] paramBuffers;
  2483. evIn.count = 0;
  2484. evIn.data = nullptr;
  2485. evOut.count = 0;
  2486. evOut.data = nullptr;
  2487. paramBuffers = nullptr;
  2488. CarlaPlugin::deleteBuffers();
  2489. qDebug("Lv2Plugin::deleteBuffers() - end");
  2490. }
  2491. // -------------------------------------------------------------------
  2492. uint32_t getCustomURID(const char* const uri)
  2493. {
  2494. qDebug("Lv2Plugin::getCustomURID(%s)", uri);
  2495. CARLA_ASSERT(uri);
  2496. if (! uri)
  2497. return CARLA_URI_MAP_ID_NULL;
  2498. for (size_t i=0; i < customURIDs.size(); i++)
  2499. {
  2500. if (customURIDs[i] && strcmp(customURIDs[i], uri) == 0)
  2501. return i;
  2502. }
  2503. customURIDs.push_back(strdup(uri));
  2504. return customURIDs.size()-1;
  2505. }
  2506. const char* getCustomURIString(const LV2_URID urid) const
  2507. {
  2508. qDebug("Lv2Plugin::getCustomURIString(%i)", urid);
  2509. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  2510. if (urid == CARLA_URI_MAP_ID_NULL)
  2511. return nullptr;
  2512. if (urid < customURIDs.size())
  2513. return customURIDs[urid];
  2514. return nullptr;
  2515. }
  2516. // -------------------------------------------------------------------
  2517. void handleTransferAtom(const int32_t portIndex, const LV2_Atom* const atom)
  2518. {
  2519. qDebug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  2520. CARLA_ASSERT(portIndex >= 0);
  2521. CARLA_ASSERT(atom);
  2522. atomQueueIn.put(portIndex, atom);
  2523. }
  2524. void handleTransferEvent(const int32_t portIndex, const LV2_Atom* const atom)
  2525. {
  2526. qDebug("Lv2Plugin::handleTransferEvent(%i, %p)", portIndex, atom);
  2527. CARLA_ASSERT(portIndex >= 0);
  2528. CARLA_ASSERT(atom);
  2529. atomQueueIn.put(portIndex, atom);
  2530. }
  2531. // -------------------------------------------------------------------
  2532. void handleProgramChanged(const int32_t index)
  2533. {
  2534. if (index == -1)
  2535. {
  2536. const CarlaPlugin::ScopedDisabler m(this);
  2537. reloadPrograms(false);
  2538. }
  2539. else
  2540. {
  2541. if (index >= 0 && index < (int32_t)prog.count && ext.programs)
  2542. {
  2543. const char* const progName = ext.programs->get_program(handle, index)->name;
  2544. CARLA_ASSERT(progName);
  2545. if (prog.names[index])
  2546. free((void*)prog.names[index]);
  2547. prog.names[index] = strdup(progName);
  2548. }
  2549. }
  2550. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  2551. }
  2552. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  2553. {
  2554. CARLA_ASSERT(key > 0);
  2555. CARLA_ASSERT(value);
  2556. CustomDataType dtype;
  2557. const char* const uriKey = getCustomURIString(key);
  2558. if (type == CARLA_URI_MAP_ID_ATOM_CHUNK)
  2559. dtype = CUSTOM_DATA_CHUNK;
  2560. else if (type == CARLA_URI_MAP_ID_ATOM_PATH)
  2561. dtype = CUSTOM_DATA_PATH;
  2562. else if (type == CARLA_URI_MAP_ID_ATOM_STRING)
  2563. dtype = CUSTOM_DATA_STRING;
  2564. else if (type >= CARLA_URI_MAP_ID_COUNT)
  2565. dtype = CUSTOM_DATA_BINARY;
  2566. else
  2567. dtype = CUSTOM_DATA_INVALID;
  2568. // do basic checks
  2569. if (! uriKey)
  2570. {
  2571. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  2572. return LV2_STATE_ERR_NO_PROPERTY;
  2573. }
  2574. if (! flags & LV2_STATE_IS_POD)
  2575. {
  2576. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  2577. return LV2_STATE_ERR_BAD_FLAGS;
  2578. }
  2579. if (dtype == CUSTOM_DATA_INVALID)
  2580. {
  2581. qCritical("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type '%s'", key, value, size, type, flags, CustomDataType2str(dtype));
  2582. return LV2_STATE_ERR_BAD_TYPE;
  2583. }
  2584. // Check if we already have this key
  2585. for (size_t i=0; i < custom.size(); i++)
  2586. {
  2587. if (strcmp(custom[i].key, uriKey) == 0)
  2588. {
  2589. free((void*)custom[i].value);
  2590. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2591. custom[i].value = strdup((const char*)value);
  2592. else
  2593. custom[i].value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2594. return LV2_STATE_SUCCESS;
  2595. }
  2596. }
  2597. // Otherwise store it
  2598. CustomData newData;
  2599. newData.type = dtype;
  2600. newData.key = strdup(uriKey);
  2601. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2602. newData.value = strdup((const char*)value);
  2603. else
  2604. newData.value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2605. custom.push_back(newData);
  2606. return LV2_STATE_SUCCESS;
  2607. }
  2608. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  2609. {
  2610. CARLA_ASSERT(key > CARLA_URI_MAP_ID_NULL);
  2611. const char* const uriKey = getCustomURIString(key);
  2612. if (! uriKey)
  2613. {
  2614. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  2615. return nullptr;
  2616. }
  2617. const char* stringData = nullptr;
  2618. CustomDataType dtype = CUSTOM_DATA_INVALID;
  2619. for (size_t i=0; i < custom.size(); i++)
  2620. {
  2621. if (strcmp(custom[i].key, uriKey) == 0)
  2622. {
  2623. dtype = custom[i].type;
  2624. stringData = custom[i].value;
  2625. break;
  2626. }
  2627. }
  2628. if (! stringData)
  2629. {
  2630. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  2631. return nullptr;
  2632. }
  2633. *size = 0;
  2634. *type = 0;
  2635. *flags = LV2_STATE_IS_POD;
  2636. if (dtype == CUSTOM_DATA_STRING)
  2637. {
  2638. *size = strlen(stringData);
  2639. *type = CARLA_URI_MAP_ID_ATOM_STRING;
  2640. return stringData;
  2641. }
  2642. if (dtype == CUSTOM_DATA_PATH)
  2643. {
  2644. *size = strlen(stringData);
  2645. *type = CARLA_URI_MAP_ID_ATOM_PATH;
  2646. return stringData;
  2647. }
  2648. if (dtype == CUSTOM_DATA_CHUNK || dtype == CUSTOM_DATA_BINARY)
  2649. {
  2650. static QByteArray chunk;
  2651. chunk = QByteArray::fromBase64(stringData);
  2652. *size = chunk.size();
  2653. *type = (dtype == CUSTOM_DATA_CHUNK) ? CARLA_URI_MAP_ID_ATOM_CHUNK : key;
  2654. return chunk.constData();
  2655. }
  2656. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key type '%s'", key, size, type, flags, CustomDataType2str(dtype));
  2657. return nullptr;
  2658. }
  2659. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  2660. {
  2661. if (! ext.worker)
  2662. {
  2663. qWarning("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  2664. return LV2_WORKER_ERR_UNKNOWN;
  2665. }
  2666. if (x_engine->isOffline())
  2667. ext.worker->work(handle, carla_lv2_worker_respond, this, size, data);
  2668. else
  2669. postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  2670. return LV2_WORKER_SUCCESS;
  2671. }
  2672. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  2673. {
  2674. LV2_Atom_Worker workerAtom;
  2675. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  2676. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  2677. workerAtom.body.size = size;
  2678. workerAtom.body.data = data;
  2679. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  2680. return LV2_WORKER_SUCCESS;
  2681. }
  2682. void handleExternalUiClosed()
  2683. {
  2684. if (ui.handle && ui.descriptor && ui.descriptor->cleanup)
  2685. ui.descriptor->cleanup(ui.handle);
  2686. ui.handle = nullptr;
  2687. x_engine->callback(CALLBACK_SHOW_GUI, m_id, 0, 0, 0.0);
  2688. }
  2689. uint32_t handleUiPortMap(const char* const symbol)
  2690. {
  2691. CARLA_ASSERT(symbol);
  2692. if (! symbol)
  2693. return LV2UI_INVALID_PORT_INDEX;
  2694. for (uint32_t i=0; i < rdf_descriptor->PortCount; i++)
  2695. {
  2696. if (strcmp(rdf_descriptor->Ports[i].Symbol, symbol) == 0)
  2697. return i;
  2698. }
  2699. return LV2UI_INVALID_PORT_INDEX;
  2700. }
  2701. int handleUiResize(const int width, const int height)
  2702. {
  2703. CARLA_ASSERT(width > 0);
  2704. CARLA_ASSERT(height > 0);
  2705. if (width <= 0 || height <= 0)
  2706. return 1;
  2707. gui.width = width;
  2708. gui.height = height;
  2709. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 0.0);
  2710. return 0;
  2711. }
  2712. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  2713. {
  2714. if (format == 0)
  2715. {
  2716. CARLA_ASSERT(buffer);
  2717. CARLA_ASSERT(bufferSize == sizeof(float));
  2718. if (bufferSize != sizeof(float))
  2719. return;
  2720. float value = *(float*)buffer;
  2721. for (uint32_t i=0; i < param.count; i++)
  2722. {
  2723. if (param.data[i].rindex == (int32_t)rindex)
  2724. return setParameterValue(i, value, false, true, true);
  2725. }
  2726. }
  2727. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2728. {
  2729. CARLA_ASSERT(buffer);
  2730. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2731. handleTransferAtom(rindex, atom);
  2732. }
  2733. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2734. {
  2735. CARLA_ASSERT(buffer);
  2736. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2737. handleTransferEvent(rindex, atom);
  2738. }
  2739. }
  2740. // -------------------------------------------------------------------
  2741. #ifndef BUILD_BRIDGE
  2742. bool isUiBridgeable(const uint32_t uiId)
  2743. {
  2744. CARLA_ASSERT(rdf_descriptor);
  2745. CARLA_ASSERT(uiId < rdf_descriptor->UICount);
  2746. if (uiId >= rdf_descriptor->UICount)
  2747. return false;
  2748. const LV2_RDF_UI* const rdf_ui = &rdf_descriptor->UIs[uiId];
  2749. for (uint32_t i=0; i < rdf_ui->FeatureCount; i++)
  2750. {
  2751. if (strcmp(rdf_ui->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0 || strcmp(rdf_ui->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  2752. return false;
  2753. }
  2754. return true;
  2755. }
  2756. #endif
  2757. bool isUiResizable()
  2758. {
  2759. CARLA_ASSERT(ui.rdf_descriptor);
  2760. if (! ui.rdf_descriptor)
  2761. return false;
  2762. for (uint32_t i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  2763. {
  2764. if (strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  2765. return false;
  2766. }
  2767. return true;
  2768. }
  2769. void initExternalUi()
  2770. {
  2771. qDebug("Lv2Plugin::initExternalUi()");
  2772. ui.widget = nullptr;
  2773. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  2774. if (ui.handle && ui.widget)
  2775. {
  2776. updateUi();
  2777. }
  2778. else
  2779. {
  2780. qWarning("Lv2Plugin::initExternalUi() - failed to instantiate UI");
  2781. ui.handle = nullptr;
  2782. ui.widget = nullptr;
  2783. x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0);
  2784. }
  2785. }
  2786. void uiTransferCustomData(const CustomData* const cdata)
  2787. {
  2788. if (cdata->type == CUSTOM_DATA_INVALID || ! (ui.handle && ui.descriptor && ui.descriptor->port_event))
  2789. return;
  2790. LV2_URID_Map* const URID_Map = (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  2791. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  2792. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  2793. Sratom* const sratom = sratom_new(URID_Map);
  2794. SerdChunk chunk = { nullptr, 0 };
  2795. LV2_Atom_Forge forge;
  2796. lv2_atom_forge_init(&forge, URID_Map);
  2797. lv2_atom_forge_set_sink(&forge, sratom_forge_sink, sratom_forge_deref, &chunk);
  2798. LV2_Atom_Forge_Frame refFrame, bodyFrame;
  2799. LV2_Atom_Forge_Ref ref = lv2_atom_forge_blank(&forge, &refFrame, 1, uridPatchSet);
  2800. lv2_atom_forge_property_head(&forge, uridPatchBody, CARLA_URI_MAP_ID_NULL);
  2801. lv2_atom_forge_blank(&forge, &bodyFrame, 2, CARLA_URI_MAP_ID_NULL);
  2802. lv2_atom_forge_property_head(&forge, getCustomURID(cdata->key), CARLA_URI_MAP_ID_NULL);
  2803. if (cdata->type == CUSTOM_DATA_STRING)
  2804. lv2_atom_forge_string(&forge, cdata->value, strlen(cdata->value));
  2805. else if (cdata->type == CUSTOM_DATA_PATH)
  2806. lv2_atom_forge_path(&forge, cdata->value, strlen(cdata->value));
  2807. else if (cdata->type == CUSTOM_DATA_CHUNK)
  2808. lv2_atom_forge_literal(&forge, cdata->value, strlen(cdata->value), CARLA_URI_MAP_ID_ATOM_CHUNK, CARLA_URI_MAP_ID_NULL);
  2809. else
  2810. lv2_atom_forge_literal(&forge, cdata->value, strlen(cdata->value), getCustomURID(cdata->key), CARLA_URI_MAP_ID_NULL);
  2811. lv2_atom_forge_pop(&forge, &bodyFrame);
  2812. lv2_atom_forge_pop(&forge, &refFrame);
  2813. uint32_t portIndex = evIn.count > 0 ? evIn.data[0].rindex : 0;
  2814. const LV2_Atom* const atom = lv2_atom_forge_deref(&forge, ref);
  2815. ui.descriptor->port_event(ui.handle, portIndex, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  2816. free((void*)chunk.buf);
  2817. sratom_free(sratom);
  2818. }
  2819. void updateUi()
  2820. {
  2821. ext.uiprograms = nullptr;
  2822. if (ui.handle && ui.descriptor)
  2823. {
  2824. if (ui.descriptor->extension_data)
  2825. {
  2826. ext.uiprograms = (const LV2_Programs_UI_Interface*)ui.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  2827. if (ext.uiprograms && ! ext.uiprograms->select_program)
  2828. // invalid
  2829. ext.uiprograms = nullptr;
  2830. if (ext.uiprograms && midiprog.count > 0 && midiprog.current >= 0)
  2831. ext.uiprograms->select_program(ui.handle, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  2832. }
  2833. if (ui.descriptor->port_event)
  2834. {
  2835. // update control ports
  2836. float valueF;
  2837. for (uint32_t i=0; i < param.count; i++)
  2838. {
  2839. valueF = getParameterValue(i);
  2840. ui.descriptor->port_event(ui.handle, param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &valueF);
  2841. }
  2842. }
  2843. }
  2844. }
  2845. // ----------------- Event Feature ---------------------------------------------------
  2846. static uint32_t carla_lv2_event_ref(const LV2_Event_Callback_Data callback_data, LV2_Event* const event)
  2847. {
  2848. qDebug("Lv2Plugin::carla_lv2_event_ref(%p, %p)", callback_data, event);
  2849. CARLA_ASSERT(callback_data);
  2850. CARLA_ASSERT(event);
  2851. return 0;
  2852. }
  2853. static uint32_t carla_lv2_event_unref(const LV2_Event_Callback_Data callback_data, LV2_Event* const event)
  2854. {
  2855. qDebug("Lv2Plugin::carla_lv2_event_unref(%p, %p)", callback_data, event);
  2856. CARLA_ASSERT(callback_data);
  2857. CARLA_ASSERT(event);
  2858. return 0;
  2859. }
  2860. // ----------------- Logs Feature ----------------------------------------------------
  2861. static int carla_lv2_log_printf(const LV2_Log_Handle handle, const LV2_URID type, const char* const fmt, ...)
  2862. {
  2863. qDebug("Lv2Plugin::carla_lv2_log_printf(%p, %i, \"%s\", ...)", handle, type, fmt);
  2864. CARLA_ASSERT(handle);
  2865. CARLA_ASSERT(type > 0);
  2866. #ifndef DEBUG
  2867. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2868. return 0;
  2869. #endif
  2870. va_list args;
  2871. va_start(args, fmt);
  2872. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  2873. va_end(args);
  2874. return ret;
  2875. }
  2876. static int carla_lv2_log_vprintf(const LV2_Log_Handle handle, const LV2_URID type, const char* const fmt, va_list ap)
  2877. {
  2878. qDebug("Lv2Plugin::carla_lv2_log_vprintf(%p, %i, \"%s\", ...)", handle, type, fmt);
  2879. CARLA_ASSERT(handle);
  2880. CARLA_ASSERT(type > 0);
  2881. #ifndef DEBUG
  2882. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2883. return 0;
  2884. #endif
  2885. char buf[8196];
  2886. vsprintf(buf, fmt, ap);
  2887. if (*buf == 0)
  2888. return 0;
  2889. switch (type)
  2890. {
  2891. case CARLA_URI_MAP_ID_LOG_ERROR:
  2892. qCritical("%s", buf);
  2893. break;
  2894. case CARLA_URI_MAP_ID_LOG_NOTE:
  2895. printf("%s\n", buf);
  2896. break;
  2897. case CARLA_URI_MAP_ID_LOG_TRACE:
  2898. qDebug("%s", buf);
  2899. break;
  2900. case CARLA_URI_MAP_ID_LOG_WARNING:
  2901. qWarning("%s", buf);
  2902. break;
  2903. default:
  2904. break;
  2905. }
  2906. return strlen(buf);
  2907. }
  2908. // ----------------- Programs Feature ------------------------------------------------
  2909. static void carla_lv2_program_changed(const LV2_Programs_Handle handle, const int32_t index)
  2910. {
  2911. qDebug("Lv2Plugin::carla_lv2_program_changed(%p, %i)", handle, index);
  2912. CARLA_ASSERT(handle);
  2913. if (! handle)
  2914. return;
  2915. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  2916. plugin->handleProgramChanged(index);
  2917. }
  2918. // ----------------- State Feature ---------------------------------------------------
  2919. static char* carla_lv2_state_make_path(const LV2_State_Make_Path_Handle handle, const char* const path)
  2920. {
  2921. qDebug("Lv2Plugin::carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  2922. CARLA_ASSERT(handle);
  2923. CARLA_ASSERT(path);
  2924. if (! path)
  2925. return nullptr;
  2926. QDir dir;
  2927. dir.mkpath(path);
  2928. return strdup(path);
  2929. }
  2930. static char* carla_lv2_state_map_abstract_path(const LV2_State_Map_Path_Handle handle, const char* const absolute_path)
  2931. {
  2932. qDebug("Lv2Plugin::carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  2933. CARLA_ASSERT(handle);
  2934. CARLA_ASSERT(absolute_path);
  2935. if (! absolute_path)
  2936. return nullptr;
  2937. QDir dir(absolute_path);
  2938. return strdup(dir.canonicalPath().toUtf8().constData());
  2939. }
  2940. static char* carla_lv2_state_map_absolute_path(const LV2_State_Map_Path_Handle handle, const char* const abstract_path)
  2941. {
  2942. qDebug("Lv2Plugin::carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  2943. CARLA_ASSERT(handle);
  2944. CARLA_ASSERT(abstract_path);
  2945. if (! abstract_path)
  2946. return nullptr;
  2947. QDir dir(abstract_path);
  2948. return strdup(dir.absolutePath().toUtf8().constData());
  2949. }
  2950. 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)
  2951. {
  2952. qDebug("Lv2Plugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  2953. CARLA_ASSERT(handle);
  2954. if (! handle)
  2955. return LV2_STATE_ERR_UNKNOWN;
  2956. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  2957. return plugin->handleStateStore(key, value, size, type, flags);
  2958. }
  2959. 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)
  2960. {
  2961. qDebug("Lv2Plugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  2962. CARLA_ASSERT(handle);
  2963. if (! handle)
  2964. return nullptr;
  2965. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  2966. return plugin->handleStateRetrieve(key, size, type, flags);
  2967. }
  2968. // ----------------- URI-Map Feature -------------------------------------------------
  2969. static uint32_t carla_lv2_uri_to_id(const LV2_URI_Map_Callback_Data data, const char* const map, const char* const uri)
  2970. {
  2971. qDebug("Lv2Plugin::carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  2972. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  2973. }
  2974. // ----------------- URID Feature ----------------------------------------------------
  2975. static LV2_URID carla_lv2_urid_map(const LV2_URID_Map_Handle handle, const char* const uri)
  2976. {
  2977. qDebug("Lv2Plugin::carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  2978. CARLA_ASSERT(handle);
  2979. CARLA_ASSERT(uri);
  2980. if (! uri)
  2981. return CARLA_URI_MAP_ID_NULL;
  2982. // Atom types
  2983. if (strcmp(uri, LV2_ATOM__Chunk) == 0)
  2984. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  2985. if (strcmp(uri, LV2_ATOM__Double) == 0)
  2986. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  2987. if (strcmp(uri, LV2_ATOM__Int) == 0)
  2988. return CARLA_URI_MAP_ID_ATOM_INT;
  2989. if (strcmp(uri, LV2_ATOM__Path) == 0)
  2990. return CARLA_URI_MAP_ID_ATOM_PATH;
  2991. if (strcmp(uri, LV2_ATOM__Sequence) == 0)
  2992. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  2993. if (strcmp(uri, LV2_ATOM__String) == 0)
  2994. return CARLA_URI_MAP_ID_ATOM_STRING;
  2995. if (strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  2996. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  2997. if (strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  2998. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  2999. // BufSize types
  3000. if (strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  3001. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  3002. if (strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  3003. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  3004. if (strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  3005. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  3006. // Log types
  3007. if (strcmp(uri, LV2_LOG__Error) == 0)
  3008. return CARLA_URI_MAP_ID_LOG_ERROR;
  3009. if (strcmp(uri, LV2_LOG__Note) == 0)
  3010. return CARLA_URI_MAP_ID_LOG_NOTE;
  3011. if (strcmp(uri, LV2_LOG__Trace) == 0)
  3012. return CARLA_URI_MAP_ID_LOG_TRACE;
  3013. if (strcmp(uri, LV2_LOG__Warning) == 0)
  3014. return CARLA_URI_MAP_ID_LOG_WARNING;
  3015. // Others
  3016. if (strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  3017. return CARLA_URI_MAP_ID_MIDI_EVENT;
  3018. if (strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  3019. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  3020. if (! handle)
  3021. return CARLA_URI_MAP_ID_NULL;
  3022. // Custom types
  3023. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3024. return plugin->getCustomURID(uri);
  3025. }
  3026. static const char* carla_lv2_urid_unmap(const LV2_URID_Map_Handle handle, const LV2_URID urid)
  3027. {
  3028. qDebug("Lv2Plugin::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  3029. CARLA_ASSERT(handle);
  3030. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  3031. if (urid == CARLA_URI_MAP_ID_NULL)
  3032. return nullptr;
  3033. // Atom types
  3034. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  3035. return LV2_ATOM__Chunk;
  3036. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  3037. return LV2_ATOM__Double;
  3038. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  3039. return LV2_ATOM__Int;
  3040. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  3041. return LV2_ATOM__Path;
  3042. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  3043. return LV2_ATOM__Sequence;
  3044. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  3045. return LV2_ATOM__String;
  3046. if (urid == CARLA_URI_MAP_ID_ATOM_WORKER)
  3047. return nullptr; // not a valid URID, only used internally
  3048. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  3049. return LV2_ATOM__atomTransfer;
  3050. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3051. return LV2_ATOM__eventTransfer;
  3052. // BufSize types
  3053. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  3054. return LV2_BUF_SIZE__maxBlockLength;
  3055. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  3056. return LV2_BUF_SIZE__minBlockLength;
  3057. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  3058. return LV2_BUF_SIZE__sequenceSize;
  3059. // Log types
  3060. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  3061. return LV2_LOG__Error;
  3062. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  3063. return LV2_LOG__Note;
  3064. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  3065. return LV2_LOG__Trace;
  3066. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  3067. return LV2_LOG__Warning;
  3068. // Others
  3069. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  3070. return LV2_MIDI__MidiEvent;
  3071. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  3072. return LV2_PARAMETERS__sampleRate;
  3073. if (! handle)
  3074. return nullptr;
  3075. // Custom types
  3076. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3077. return plugin->getCustomURIString(urid);
  3078. }
  3079. // ----------------- Worker Feature --------------------------------------------------
  3080. static LV2_Worker_Status carla_lv2_worker_schedule(const LV2_Worker_Schedule_Handle handle, const uint32_t size, const void* const data)
  3081. {
  3082. qDebug("Lv2Plugin::carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  3083. CARLA_ASSERT(handle);
  3084. if (! handle)
  3085. return LV2_WORKER_ERR_UNKNOWN;
  3086. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3087. return plugin->handleWorkerSchedule(size, data);
  3088. }
  3089. static LV2_Worker_Status carla_lv2_worker_respond(const LV2_Worker_Respond_Handle handle, const uint32_t size, const void* const data)
  3090. {
  3091. qDebug("Lv2Plugin::carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  3092. CARLA_ASSERT(handle);
  3093. if (! handle)
  3094. return LV2_WORKER_ERR_UNKNOWN;
  3095. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3096. return plugin->handleWorkerRespond(size, data);
  3097. }
  3098. // ----------------- UI Port-Map Feature ---------------------------------------------
  3099. static uint32_t carla_lv2_ui_port_map(const LV2UI_Feature_Handle handle, const char* const symbol)
  3100. {
  3101. qDebug("Lv2Plugin::carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  3102. CARLA_ASSERT(handle);
  3103. if (! handle)
  3104. return LV2UI_INVALID_PORT_INDEX;
  3105. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3106. return plugin->handleUiPortMap(symbol);
  3107. }
  3108. // ----------------- UI Resize Feature -----------------------------------------------
  3109. static int carla_lv2_ui_resize(const LV2UI_Feature_Handle handle, const int width, const int height)
  3110. {
  3111. qDebug("Lv2Plugin::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  3112. CARLA_ASSERT(handle);
  3113. if (! handle)
  3114. return 1;
  3115. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3116. return plugin->handleUiResize(width, height);
  3117. }
  3118. // ----------------- External UI Feature ---------------------------------------------
  3119. static void carla_lv2_external_ui_closed(const LV2UI_Controller controller)
  3120. {
  3121. qDebug("Lv2Plugin::carla_lv2_external_ui_closed(%p)", controller);
  3122. CARLA_ASSERT(controller);
  3123. if (! controller)
  3124. return;
  3125. Lv2Plugin* const plugin = (Lv2Plugin*)controller;
  3126. plugin->handleExternalUiClosed();
  3127. }
  3128. // ----------------- UI Extension ----------------------------------------------------
  3129. 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)
  3130. {
  3131. qDebug("Lv2Plugin::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  3132. CARLA_ASSERT(controller);
  3133. if (! controller)
  3134. return;
  3135. Lv2Plugin* const plugin = (Lv2Plugin*)controller;
  3136. plugin->handleUiWrite(port_index, buffer_size, format, buffer);
  3137. }
  3138. // -------------------------------------------------------------------
  3139. bool uiLibOpen(const char* const filename)
  3140. {
  3141. ui.lib = lib_open(filename);
  3142. return bool(ui.lib);
  3143. }
  3144. bool uiLibClose()
  3145. {
  3146. if (ui.lib)
  3147. return lib_close(ui.lib);
  3148. return false;
  3149. }
  3150. void* uiLibSymbol(const char* const symbol)
  3151. {
  3152. if (ui.lib)
  3153. return lib_symbol(ui.lib, symbol);
  3154. return nullptr;
  3155. }
  3156. // -------------------------------------------------------------------
  3157. bool init(const char* const bundle, const char* const name, const char* const URI)
  3158. {
  3159. // ---------------------------------------------------------------
  3160. // get plugin from lv2_rdf (lilv)
  3161. rdf_descriptor = lv2_rdf_new(URI);
  3162. if (! rdf_descriptor)
  3163. {
  3164. setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3165. return false;
  3166. }
  3167. // ---------------------------------------------------------------
  3168. // open DLL
  3169. if (! libOpen(rdf_descriptor->Binary))
  3170. {
  3171. setLastError(libError(rdf_descriptor->Binary));
  3172. return false;
  3173. }
  3174. // ---------------------------------------------------------------
  3175. // get DLL main entry
  3176. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)libSymbol("lv2_descriptor");
  3177. if (! descFn)
  3178. {
  3179. setLastError("Could not find the LV2 Descriptor in the plugin library");
  3180. return false;
  3181. }
  3182. // ---------------------------------------------------------------
  3183. // get descriptor that matches URI
  3184. uint32_t i = 0;
  3185. while ((descriptor = descFn(i++)))
  3186. {
  3187. if (strcmp(descriptor->URI, URI) == 0)
  3188. break;
  3189. }
  3190. if (! descriptor)
  3191. {
  3192. setLastError("Could not find the requested plugin URI in the plugin library");
  3193. return false;
  3194. }
  3195. // ---------------------------------------------------------------
  3196. // check supported port-types and features
  3197. bool canContinue = true;
  3198. // Check supported ports
  3199. for (i=0; i < rdf_descriptor->PortCount; i++)
  3200. {
  3201. LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  3202. 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)))
  3203. {
  3204. if (! LV2_IS_PORT_OPTIONAL(rdf_descriptor->Ports[i].Properties))
  3205. {
  3206. setLastError("Plugin requires a port that is not currently supported");
  3207. canContinue = false;
  3208. break;
  3209. }
  3210. }
  3211. }
  3212. // Check supported features
  3213. for (i=0; i < rdf_descriptor->FeatureCount && canContinue; i++)
  3214. {
  3215. if (LV2_IS_FEATURE_REQUIRED(rdf_descriptor->Features[i].Type) && ! is_lv2_feature_supported(rdf_descriptor->Features[i].URI))
  3216. {
  3217. QString msg = QString("Plugin requires a feature that is not supported:\n%1").arg(rdf_descriptor->Features[i].URI);
  3218. setLastError(msg.toUtf8().constData());
  3219. canContinue = false;
  3220. break;
  3221. }
  3222. }
  3223. // Check extensions
  3224. for (i=0; i < rdf_descriptor->ExtensionCount; i++)
  3225. {
  3226. if (strcmp(rdf_descriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3227. m_hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3228. else if (strcmp(rdf_descriptor->Extensions[i], LV2_STATE__interface) == 0)
  3229. m_hints |= PLUGIN_HAS_EXTENSION_STATE;
  3230. else if (strcmp(rdf_descriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3231. m_hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3232. else
  3233. qDebug("Plugin has non-supported extension: '%s'", rdf_descriptor->Extensions[i]);
  3234. }
  3235. if (! canContinue)
  3236. {
  3237. // error already set
  3238. return false;
  3239. }
  3240. // ---------------------------------------------------------------
  3241. // initialize features
  3242. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3243. eventFt->callback_data = this;
  3244. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3245. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3246. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3247. logFt->handle = this;
  3248. logFt->printf = carla_lv2_log_printf;
  3249. logFt->vprintf = carla_lv2_log_vprintf;
  3250. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3251. programsFt->handle = this;
  3252. programsFt->program_changed = carla_lv2_program_changed;
  3253. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3254. rtmempool_allocator_init(rtMemPoolFt);
  3255. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3256. stateMakePathFt->handle = this;
  3257. stateMakePathFt->path = carla_lv2_state_make_path;
  3258. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3259. stateMapPathFt->handle = this;
  3260. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3261. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3262. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3263. uriMapFt->callback_data = this;
  3264. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3265. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3266. uridMapFt->handle = this;
  3267. uridMapFt->map = carla_lv2_urid_map;
  3268. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3269. uridUnmapFt->handle = this;
  3270. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3271. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3272. workerFt->handle = this;
  3273. workerFt->schedule_work = carla_lv2_worker_schedule;
  3274. LV2_Options_Option* const optionsFt = new LV2_Options_Option [5];
  3275. optionsFt[0] = lv2Options.oMaxBlockLenth;
  3276. optionsFt[1] = lv2Options.oMinBlockLenth;
  3277. optionsFt[2] = lv2Options.oSequenceSize;
  3278. optionsFt[3] = lv2Options.oSampleRate;
  3279. optionsFt[4] = lv2Options.oNull;
  3280. features[lv2_feature_id_bufsize_bounded] = new LV2_Feature;
  3281. features[lv2_feature_id_bufsize_bounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3282. features[lv2_feature_id_bufsize_bounded]->data = nullptr;
  3283. #ifndef BUILD_BRIDGE
  3284. if (carlaOptions.processHighPrecision)
  3285. {
  3286. features[lv2_feature_id_bufsize_fixed] = new LV2_Feature;
  3287. features[lv2_feature_id_bufsize_fixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3288. features[lv2_feature_id_bufsize_fixed]->data = nullptr;
  3289. features[lv2_feature_id_bufsize_powerof2] = new LV2_Feature;
  3290. features[lv2_feature_id_bufsize_powerof2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3291. features[lv2_feature_id_bufsize_powerof2]->data = nullptr;
  3292. }
  3293. else
  3294. #endif
  3295. {
  3296. // fake, used to keep a valid array
  3297. features[lv2_feature_id_bufsize_fixed] = features[lv2_feature_id_bufsize_bounded];
  3298. features[lv2_feature_id_bufsize_powerof2] = features[lv2_feature_id_bufsize_bounded];
  3299. }
  3300. features[lv2_feature_id_event] = new LV2_Feature;
  3301. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  3302. features[lv2_feature_id_event]->data = eventFt;
  3303. features[lv2_feature_id_logs] = new LV2_Feature;
  3304. features[lv2_feature_id_logs]->URI = LV2_LOG__log;
  3305. features[lv2_feature_id_logs]->data = logFt;
  3306. features[lv2_feature_id_options] = new LV2_Feature;
  3307. features[lv2_feature_id_options]->URI = LV2_OPTIONS__options;
  3308. features[lv2_feature_id_options]->data = optionsFt;
  3309. features[lv2_feature_id_programs] = new LV2_Feature;
  3310. features[lv2_feature_id_programs]->URI = LV2_PROGRAMS__Host;
  3311. features[lv2_feature_id_programs]->data = programsFt;
  3312. features[lv2_feature_id_rtmempool] = new LV2_Feature;
  3313. features[lv2_feature_id_rtmempool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3314. features[lv2_feature_id_rtmempool]->data = rtMemPoolFt;
  3315. features[lv2_feature_id_state_make_path] = new LV2_Feature;
  3316. features[lv2_feature_id_state_make_path]->URI = LV2_STATE__makePath;
  3317. features[lv2_feature_id_state_make_path]->data = stateMakePathFt;
  3318. features[lv2_feature_id_state_map_path] = new LV2_Feature;
  3319. features[lv2_feature_id_state_map_path]->URI = LV2_STATE__mapPath;
  3320. features[lv2_feature_id_state_map_path]->data = stateMapPathFt;
  3321. features[lv2_feature_id_strict_bounds] = new LV2_Feature;
  3322. features[lv2_feature_id_strict_bounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3323. features[lv2_feature_id_strict_bounds]->data = nullptr;
  3324. features[lv2_feature_id_uri_map] = new LV2_Feature;
  3325. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  3326. features[lv2_feature_id_uri_map]->data = uriMapFt;
  3327. features[lv2_feature_id_urid_map] = new LV2_Feature;
  3328. features[lv2_feature_id_urid_map]->URI = LV2_URID__map;
  3329. features[lv2_feature_id_urid_map]->data = uridMapFt;
  3330. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  3331. features[lv2_feature_id_urid_unmap]->URI = LV2_URID__unmap;
  3332. features[lv2_feature_id_urid_unmap]->data = uridUnmapFt;
  3333. features[lv2_feature_id_worker] = new LV2_Feature;
  3334. features[lv2_feature_id_worker]->URI = LV2_WORKER__schedule;
  3335. features[lv2_feature_id_worker]->data = workerFt;
  3336. // ---------------------------------------------------------------
  3337. // initialize plugin
  3338. handle = descriptor->instantiate(descriptor, x_engine->getSampleRate(), rdf_descriptor->Bundle, features);
  3339. if (! handle)
  3340. {
  3341. setLastError("Plugin failed to initialize");
  3342. return false;
  3343. }
  3344. // ---------------------------------------------------------------
  3345. // get info
  3346. m_filename = strdup(bundle);
  3347. if (name)
  3348. m_name = x_engine->getUniqueName(name);
  3349. else
  3350. m_name = x_engine->getUniqueName(rdf_descriptor->Name);
  3351. // ---------------------------------------------------------------
  3352. // register client
  3353. x_client = x_engine->addClient(this);
  3354. if (! x_client->isOk())
  3355. {
  3356. setLastError("Failed to register plugin client");
  3357. return false;
  3358. }
  3359. // ---------------------------------------------------------------
  3360. // gui stuff
  3361. if (rdf_descriptor->UICount == 0)
  3362. return true;
  3363. // -----------------------------------------------------------
  3364. // find more appropriate ui
  3365. int eQt4, eCocoa, eHWND, eX11, eGtk2, eGtk3, iCocoa, iHWND, iX11, iQt4, iExt, iSuil, iFinal;
  3366. eQt4 = eCocoa = eHWND = eX11 = eGtk2 = eGtk3 = iQt4 = iCocoa = iHWND = iX11 = iExt = iSuil = iFinal = -1;
  3367. for (i=0; i < rdf_descriptor->UICount; i++)
  3368. {
  3369. switch (rdf_descriptor->UIs[i].Type)
  3370. {
  3371. case LV2_UI_QT4:
  3372. #ifndef BUILD_BRIDGE
  3373. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3374. eQt4 = i;
  3375. #endif
  3376. iQt4 = i;
  3377. break;
  3378. case LV2_UI_COCOA:
  3379. #ifndef BUILD_BRIDGE
  3380. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3381. eCocoa = i;
  3382. #endif
  3383. iCocoa = i;
  3384. break;
  3385. case LV2_UI_WINDOWS:
  3386. #ifndef BUILD_BRIDGE
  3387. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3388. eHWND = i;
  3389. #endif
  3390. iHWND = i;
  3391. break;
  3392. case LV2_UI_X11:
  3393. #ifndef BUILD_BRIDGE
  3394. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3395. eX11 = i;
  3396. #endif
  3397. iX11 = i;
  3398. break;
  3399. case LV2_UI_GTK2:
  3400. #ifdef BUILD_BRIDGE
  3401. if (false)
  3402. #else
  3403. # ifdef HAVE_SUIL
  3404. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3405. # else
  3406. if (isUiBridgeable(i))
  3407. # endif
  3408. #endif
  3409. eGtk2 = i;
  3410. #ifdef HAVE_SUIL
  3411. iSuil = i;
  3412. #endif
  3413. break;
  3414. #ifndef BUILD_BRIDGE
  3415. case LV2_UI_GTK3:
  3416. if (isUiBridgeable(i))
  3417. eGtk3 = i;
  3418. break;
  3419. #endif
  3420. case LV2_UI_EXTERNAL:
  3421. case LV2_UI_OLD_EXTERNAL:
  3422. iExt = i;
  3423. break;
  3424. default:
  3425. break;
  3426. }
  3427. }
  3428. if (eQt4 >= 0)
  3429. iFinal = eQt4;
  3430. else if (eCocoa >= 0)
  3431. iFinal = eCocoa;
  3432. else if (eHWND >= 0)
  3433. iFinal = eHWND;
  3434. else if (eX11 >= 0)
  3435. iFinal = eX11;
  3436. else if (eGtk2 >= 0)
  3437. iFinal = eGtk2;
  3438. else if (eGtk3 >= 0)
  3439. iFinal = eGtk3;
  3440. else if (iQt4 >= 0)
  3441. iFinal = iQt4;
  3442. else if (iCocoa >= 0)
  3443. iFinal = iCocoa;
  3444. else if (iHWND >= 0)
  3445. iFinal = iHWND;
  3446. else if (iX11 >= 0)
  3447. iFinal = iX11;
  3448. else if (iExt >= 0)
  3449. iFinal = iExt;
  3450. else if (iSuil >= 0)
  3451. iFinal = iSuil;
  3452. #ifndef BUILD_BRIDGE
  3453. const bool isBridged = (iFinal == eQt4 || iFinal == eCocoa || iFinal == eHWND || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3);
  3454. #endif
  3455. #ifdef HAVE_SUIL
  3456. const bool isSuil = (iFinal == iSuil && !isBridged);
  3457. #endif
  3458. if (iFinal < 0)
  3459. {
  3460. qWarning("Failed to find an appropriate LV2 UI for this plugin");
  3461. return true;
  3462. }
  3463. ui.rdf_descriptor = &rdf_descriptor->UIs[iFinal];
  3464. // -----------------------------------------------------------
  3465. // check supported ui features
  3466. canContinue = true;
  3467. for (i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  3468. {
  3469. if (LV2_IS_FEATURE_REQUIRED(ui.rdf_descriptor->Features[i].Type) && is_lv2_ui_feature_supported(ui.rdf_descriptor->Features[i].URI) == false)
  3470. {
  3471. qCritical("Plugin UI requires a feature that is not supported:\n%s", ui.rdf_descriptor->Features[i].URI);
  3472. canContinue = false;
  3473. break;
  3474. }
  3475. }
  3476. if (! canContinue)
  3477. {
  3478. ui.rdf_descriptor = nullptr;
  3479. return true;
  3480. }
  3481. #ifdef HAVE_SUIL
  3482. if (isSuil)
  3483. {
  3484. // -------------------------------------------------------
  3485. // init suil host
  3486. suil.host = suil_host_new(carla_lv2_ui_write_function, carla_lv2_ui_port_map, nullptr, nullptr);
  3487. }
  3488. else
  3489. #endif
  3490. {
  3491. // -------------------------------------------------------
  3492. // open DLL
  3493. if (! uiLibOpen(ui.rdf_descriptor->Binary))
  3494. {
  3495. qCritical("Could not load UI library, error was:\n%s", libError(ui.rdf_descriptor->Binary));
  3496. ui.rdf_descriptor = nullptr;
  3497. return true;
  3498. }
  3499. // -------------------------------------------------------
  3500. // get DLL main entry
  3501. LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor");
  3502. if (! ui_descFn)
  3503. {
  3504. qCritical("Could not find the LV2UI Descriptor in the UI library");
  3505. uiLibClose();
  3506. ui.lib = nullptr;
  3507. ui.rdf_descriptor = nullptr;
  3508. return true;
  3509. }
  3510. // -------------------------------------------------------
  3511. // get descriptor that matches URI
  3512. i = 0;
  3513. while ((ui.descriptor = ui_descFn(i++)))
  3514. {
  3515. if (strcmp(ui.descriptor->URI, ui.rdf_descriptor->URI) == 0)
  3516. break;
  3517. }
  3518. if (! ui.descriptor)
  3519. {
  3520. qCritical("Could not find the requested GUI in the plugin UI library");
  3521. uiLibClose();
  3522. ui.lib = nullptr;
  3523. ui.rdf_descriptor = nullptr;
  3524. return true;
  3525. }
  3526. }
  3527. // -----------------------------------------------------------
  3528. // initialize ui according to type
  3529. const LV2_Property uiType = ui.rdf_descriptor->Type;
  3530. #ifndef BUILD_BRIDGE
  3531. if (isBridged)
  3532. {
  3533. // -------------------------------------------------------
  3534. // initialize ui bridge
  3535. if (const char* const oscBinary = lv2bridge2str(uiType))
  3536. {
  3537. gui.type = GUI_EXTERNAL_OSC;
  3538. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  3539. osc.thread->setOscData(oscBinary, descriptor->URI, ui.descriptor->URI);
  3540. }
  3541. }
  3542. else
  3543. #endif
  3544. {
  3545. // -------------------------------------------------------
  3546. // initialize ui features
  3547. QString guiTitle = QString("%1 (GUI)").arg(m_name);
  3548. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3549. uiDataFt->data_access = descriptor->extension_data;
  3550. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3551. uiPortMapFt->handle = this;
  3552. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3553. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3554. uiResizeFt->handle = this;
  3555. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3556. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3557. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3558. uiExternalHostFt->plugin_human_id = strdup(guiTitle.toUtf8().constData());
  3559. features[lv2_feature_id_data_access] = new LV2_Feature;
  3560. features[lv2_feature_id_data_access]->URI = LV2_DATA_ACCESS_URI;
  3561. features[lv2_feature_id_data_access]->data = uiDataFt;
  3562. features[lv2_feature_id_instance_access] = new LV2_Feature;
  3563. features[lv2_feature_id_instance_access]->URI = LV2_INSTANCE_ACCESS_URI;
  3564. features[lv2_feature_id_instance_access]->data = handle;
  3565. features[lv2_feature_id_ui_parent] = new LV2_Feature;
  3566. features[lv2_feature_id_ui_parent]->URI = LV2_UI__parent;
  3567. features[lv2_feature_id_ui_parent]->data = nullptr;
  3568. features[lv2_feature_id_ui_port_map] = new LV2_Feature;
  3569. features[lv2_feature_id_ui_port_map]->URI = LV2_UI__portMap;
  3570. features[lv2_feature_id_ui_port_map]->data = uiPortMapFt;
  3571. features[lv2_feature_id_ui_resize] = new LV2_Feature;
  3572. features[lv2_feature_id_ui_resize]->URI = LV2_UI__resize;
  3573. features[lv2_feature_id_ui_resize]->data = uiResizeFt;
  3574. features[lv2_feature_id_external_ui] = new LV2_Feature;
  3575. features[lv2_feature_id_external_ui]->URI = LV2_EXTERNAL_UI__Host;
  3576. features[lv2_feature_id_external_ui]->data = uiExternalHostFt;
  3577. features[lv2_feature_id_external_ui_old] = new LV2_Feature;
  3578. features[lv2_feature_id_external_ui_old]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3579. features[lv2_feature_id_external_ui_old]->data = uiExternalHostFt;
  3580. // -------------------------------------------------------
  3581. // initialize ui
  3582. switch (uiType)
  3583. {
  3584. case LV2_UI_QT4:
  3585. qDebug("Will use LV2 Qt4 UI");
  3586. gui.type = GUI_INTERNAL_QT4;
  3587. gui.resizable = isUiResizable();
  3588. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  3589. m_hints |= PLUGIN_USES_SINGLE_THREAD;
  3590. break;
  3591. case LV2_UI_COCOA:
  3592. qDebug("Will use LV2 Cocoa UI");
  3593. gui.type = GUI_INTERNAL_COCOA;
  3594. gui.resizable = isUiResizable();
  3595. break;
  3596. case LV2_UI_WINDOWS:
  3597. qDebug("Will use LV2 Windows UI");
  3598. gui.type = GUI_INTERNAL_HWND;
  3599. gui.resizable = isUiResizable();
  3600. break;
  3601. case LV2_UI_X11:
  3602. qDebug("Will use LV2 X11 UI");
  3603. gui.type = GUI_INTERNAL_X11;
  3604. gui.resizable = isUiResizable();
  3605. break;
  3606. case LV2_UI_GTK2:
  3607. #ifdef HAVE_SUIL
  3608. qDebug("Will use LV2 Gtk2 UI (suil)");
  3609. gui.type = GUI_EXTERNAL_SUIL;
  3610. gui.resizable = isUiResizable();
  3611. 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);
  3612. m_hints |= PLUGIN_USES_SINGLE_THREAD;
  3613. if (suil.handle)
  3614. {
  3615. ui.handle = ((SuilInstanceImpl*)suil.handle)->handle;
  3616. ui.descriptor = ((SuilInstanceImpl*)suil.handle)->descriptor;
  3617. ui.widget = suil_instance_get_widget(suil.handle);
  3618. if (ui.widget)
  3619. {
  3620. QWidget* const widget = (QWidget*)ui.widget;
  3621. widget->setWindowTitle(guiTitle);
  3622. }
  3623. }
  3624. #else
  3625. qDebug("Will use LV2 Gtk2 UI, NOT!");
  3626. #endif
  3627. break;
  3628. case LV2_UI_GTK3:
  3629. qDebug("Will use LV2 Gtk3 UI, NOT!");
  3630. break;
  3631. case LV2_UI_EXTERNAL:
  3632. case LV2_UI_OLD_EXTERNAL:
  3633. qDebug("Will use LV2 External UI");
  3634. gui.type = GUI_EXTERNAL_LV2;
  3635. break;
  3636. }
  3637. }
  3638. if (gui.type != GUI_NONE)
  3639. m_hints |= PLUGIN_HAS_GUI;
  3640. return true;
  3641. }
  3642. private:
  3643. LV2_Handle handle, h2;
  3644. const LV2_Descriptor* descriptor;
  3645. const LV2_RDF_Descriptor* rdf_descriptor;
  3646. LV2_Feature* features[lv2_feature_count+1];
  3647. struct {
  3648. const LV2_State_Interface* state;
  3649. const LV2_Worker_Interface* worker;
  3650. const LV2_Programs_Interface* programs;
  3651. const LV2_Programs_UI_Interface* uiprograms;
  3652. } ext;
  3653. struct {
  3654. void* lib;
  3655. LV2UI_Handle handle;
  3656. LV2UI_Widget widget;
  3657. const LV2UI_Descriptor* descriptor;
  3658. const LV2_RDF_UI* rdf_descriptor;
  3659. } ui;
  3660. struct {
  3661. GuiType type;
  3662. bool resizable;
  3663. int width;
  3664. int height;
  3665. } gui;
  3666. #ifdef HAVE_SUIL
  3667. struct {
  3668. SuilHost* host;
  3669. SuilInstance* handle;
  3670. QByteArray pos;
  3671. } suil;
  3672. #endif
  3673. Lv2AtomQueue atomQueueIn;
  3674. Lv2AtomQueue atomQueueOut;
  3675. Lv2PluginEventData evIn;
  3676. Lv2PluginEventData evOut;
  3677. float* paramBuffers;
  3678. std::vector<const char*> customURIDs;
  3679. bool lastTimePosPlaying;
  3680. uint32_t lastTimePosFrame;
  3681. };
  3682. /**@}*/
  3683. // -------------------------------------------------------------------------------------------------------------------
  3684. int CarlaOsc::handleMsgLv2AtomTransfer(CARLA_OSC_HANDLE_ARGS2)
  3685. {
  3686. qDebug("CarlaOsc::handleMsgLv2AtomTransfer()");
  3687. CARLA_OSC_CHECK_OSC_TYPES(3, "iss");
  3688. const int32_t portIndex = argv[0]->i;
  3689. const char* const typeStr = (const char*)&argv[1]->s;
  3690. const char* const atomBuf = (const char*)&argv[2]->s;
  3691. QByteArray chunk;
  3692. chunk = QByteArray::fromBase64(atomBuf);
  3693. LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  3694. CarlaBackend::Lv2Plugin* const lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3695. atom->type = lv2plugin->getCustomURID(typeStr);
  3696. lv2plugin->handleTransferAtom(portIndex, atom);
  3697. return 0;
  3698. }
  3699. int CarlaOsc::handleMsgLv2EventTransfer(CARLA_OSC_HANDLE_ARGS2)
  3700. {
  3701. qDebug("CarlaOsc::handleMsgLv2EventTransfer()");
  3702. CARLA_OSC_CHECK_OSC_TYPES(3, "iss");
  3703. const int32_t portIndex = argv[0]->i;
  3704. const char* const typeStr = (const char*)&argv[1]->s;
  3705. const char* const atomBuf = (const char*)&argv[2]->s;
  3706. QByteArray chunk;
  3707. chunk = QByteArray::fromBase64(atomBuf);
  3708. LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  3709. CarlaBackend::Lv2Plugin* const lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3710. atom->type = lv2plugin->getCustomURID(typeStr);
  3711. lv2plugin->handleTransferEvent(portIndex, atom);
  3712. return 0;
  3713. }
  3714. CARLA_BACKEND_END_NAMESPACE
  3715. #else // WANT_LV2
  3716. # warning Building without LV2 support
  3717. #endif
  3718. CARLA_BACKEND_START_NAMESPACE
  3719. CarlaPlugin* CarlaPlugin::newLV2(const initializer& init)
  3720. {
  3721. qDebug("CarlaPlugin::newLV2(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  3722. #ifdef WANT_LV2
  3723. short id = init.engine->getNewPluginId();
  3724. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  3725. {
  3726. setLastError("Maximum number of plugins reached");
  3727. return nullptr;
  3728. }
  3729. Lv2Plugin* const plugin = new Lv2Plugin(init.engine, id);
  3730. if (! plugin->init(init.filename, init.name, init.label))
  3731. {
  3732. delete plugin;
  3733. return nullptr;
  3734. }
  3735. plugin->reload();
  3736. # ifndef BUILD_BRIDGE
  3737. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  3738. {
  3739. if (! (plugin->hints() & PLUGIN_CAN_FORCE_STEREO))
  3740. {
  3741. setLastError("Carla's rack mode can only work with Mono (simple) or Stereo LV2 plugins, sorry!");
  3742. delete plugin;
  3743. return nullptr;
  3744. }
  3745. }
  3746. # endif
  3747. plugin->registerToOscControl();
  3748. plugin->updateUi();
  3749. return plugin;
  3750. #else
  3751. setLastError("LV2 support not available");
  3752. return nullptr;
  3753. #endif
  3754. }
  3755. CARLA_BACKEND_END_NAMESPACE