Audio plugin host https://kx.studio/carla
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.

4721 lines
166KB

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