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.

4650 lines
163KB

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