Collection of tools useful for audio production
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4649 lines
161KB

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