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.

4707 lines
164KB

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