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.

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