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.

4654 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. #ifdef BUILD_BRIDGE
  1001. const bool haveUI = (gui.type != GUI_EXTERNAL_OSC && ui.handle && ui.descriptor);
  1002. #else
  1003. const bool haveUI = (gui.type == GUI_EXTERNAL_OSC && osc.data.target) || (ui.handle && ui.descriptor);
  1004. #endif
  1005. if (haveUI)
  1006. {
  1007. // Update event ports
  1008. if (! atomQueueOut.isEmpty())
  1009. {
  1010. static Lv2AtomQueue queue;
  1011. queue.copyDataFrom(&atomQueueOut);
  1012. uint32_t portIndex;
  1013. const LV2_Atom* atom;
  1014. while (queue.get(&portIndex, &atom, false))
  1015. {
  1016. #ifndef BUILD_BRIDGE
  1017. if (gui.type == GUI_EXTERNAL_OSC)
  1018. {
  1019. QByteArray chunk((const char*)atom, sizeof(LV2_Atom) + atom->size);
  1020. osc_send_lv2_transfer_event(&osc.data, portIndex, chunk.toBase64().constData());
  1021. }
  1022. else
  1023. #endif
  1024. {
  1025. if (ui.descriptor->port_event)
  1026. ui.descriptor->port_event(ui.handle, portIndex, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1027. }
  1028. }
  1029. }
  1030. // Update external UI
  1031. if (gui.type == GUI_EXTERNAL_LV2 && ui.widget)
  1032. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)ui.widget);
  1033. }
  1034. CarlaPlugin::idleGui();
  1035. }
  1036. // -------------------------------------------------------------------
  1037. // Plugin state
  1038. void reload()
  1039. {
  1040. qDebug("Lv2Plugin::reload() - start");
  1041. Q_ASSERT(descriptor && rdf_descriptor);
  1042. // Safely disable plugin for reload
  1043. const ScopedDisabler m(this);
  1044. if (x_client->isActive())
  1045. x_client->deactivate();
  1046. // Remove client ports
  1047. removeClientPorts();
  1048. // Delete old data
  1049. deleteBuffers();
  1050. uint32_t aIns, aOuts, cvIns, cvOuts, params, j;
  1051. aIns = aOuts = cvIns = cvOuts = params = 0;
  1052. std::vector<uint32_t> evIns, evOuts;
  1053. const double sampleRate = x_engine->getSampleRate();
  1054. const uint32_t portCount = rdf_descriptor->PortCount;
  1055. bool forcedStereoIn, forcedStereoOut;
  1056. forcedStereoIn = forcedStereoOut = false;
  1057. for (uint32_t i=0; i < portCount; i++)
  1058. {
  1059. const LV2_Property portType = rdf_descriptor->Ports[i].Type;
  1060. if (LV2_IS_PORT_AUDIO(portType))
  1061. {
  1062. if (LV2_IS_PORT_INPUT(portType))
  1063. aIns += 1;
  1064. else if (LV2_IS_PORT_OUTPUT(portType))
  1065. aOuts += 1;
  1066. }
  1067. else if (LV2_IS_PORT_CV(portType))
  1068. {
  1069. if (LV2_IS_PORT_INPUT(portType))
  1070. cvIns += 1;
  1071. else if (LV2_IS_PORT_OUTPUT(portType))
  1072. cvOuts += 1;
  1073. }
  1074. else if (LV2_IS_PORT_ATOM_SEQUENCE(portType))
  1075. {
  1076. if (LV2_IS_PORT_INPUT(portType))
  1077. evIns.push_back(CARLA_EVENT_DATA_ATOM);
  1078. else if (LV2_IS_PORT_OUTPUT(portType))
  1079. evOuts.push_back(CARLA_EVENT_DATA_ATOM);
  1080. }
  1081. else if (LV2_IS_PORT_EVENT(portType))
  1082. {
  1083. if (LV2_IS_PORT_INPUT(portType))
  1084. evIns.push_back(CARLA_EVENT_DATA_EVENT);
  1085. else if (LV2_IS_PORT_OUTPUT(portType))
  1086. evOuts.push_back(CARLA_EVENT_DATA_EVENT);
  1087. }
  1088. else if (LV2_IS_PORT_MIDI_LL(portType))
  1089. {
  1090. if (LV2_IS_PORT_INPUT(portType))
  1091. evIns.push_back(CARLA_EVENT_DATA_MIDI_LL);
  1092. else if (LV2_IS_PORT_OUTPUT(portType))
  1093. evOuts.push_back(CARLA_EVENT_DATA_MIDI_LL);
  1094. }
  1095. else if (LV2_IS_PORT_CONTROL(portType))
  1096. params += 1;
  1097. }
  1098. #ifndef BUILD_BRIDGE
  1099. if (carlaOptions.forceStereo && (aIns == 1 || aOuts == 1) && ! h2)
  1100. {
  1101. h2 = descriptor->instantiate(descriptor, sampleRate, rdf_descriptor->Bundle, features);
  1102. if (aIns == 1)
  1103. {
  1104. aIns = 2;
  1105. forcedStereoIn = true;
  1106. }
  1107. if (aOuts == 1)
  1108. {
  1109. aOuts = 2;
  1110. forcedStereoOut = true;
  1111. }
  1112. }
  1113. #endif
  1114. if (aIns > 0)
  1115. {
  1116. aIn.ports = new CarlaEngineAudioPort*[aIns];
  1117. aIn.rindexes = new uint32_t[aIns];
  1118. }
  1119. if (aOuts > 0)
  1120. {
  1121. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  1122. aOut.rindexes = new uint32_t[aOuts];
  1123. }
  1124. if (evIns.size() > 0)
  1125. {
  1126. const size_t size = evIns.size();
  1127. evIn.data = new Lv2EventData[size];
  1128. for (j=0; j < size; j++)
  1129. {
  1130. evIn.data[j].port = nullptr;
  1131. evIn.data[j].type = 0;
  1132. if (evIns[j] == CARLA_EVENT_DATA_ATOM)
  1133. {
  1134. evIn.data[j].type = CARLA_EVENT_DATA_ATOM;
  1135. evIn.data[j].atom = (LV2_Atom_Sequence*)malloc(sizeof(LV2_Atom_Sequence) + MAX_EVENT_BUFFER);
  1136. evIn.data[j].atom->atom.size = sizeof(LV2_Atom_Sequence_Body);
  1137. evIn.data[j].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1138. evIn.data[j].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1139. evIn.data[j].atom->body.pad = 0;
  1140. }
  1141. else if (evIns[j] == CARLA_EVENT_DATA_EVENT)
  1142. {
  1143. evIn.data[j].type = CARLA_EVENT_DATA_EVENT;
  1144. evIn.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1145. }
  1146. else if (evIns[j] == CARLA_EVENT_DATA_MIDI_LL)
  1147. {
  1148. evIn.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1149. evIn.data[j].midi = new LV2_MIDI;
  1150. evIn.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1151. evIn.data[j].midi->data = new unsigned char [MAX_EVENT_BUFFER];
  1152. }
  1153. }
  1154. }
  1155. if (evOuts.size() > 0)
  1156. {
  1157. const size_t size = evOuts.size();
  1158. evOut.data = new Lv2EventData[size];
  1159. for (j=0; j < size; j++)
  1160. {
  1161. evOut.data[j].port = nullptr;
  1162. evOut.data[j].type = 0;
  1163. if (evOuts[j] == CARLA_EVENT_DATA_ATOM)
  1164. {
  1165. evOut.data[j].type = CARLA_EVENT_DATA_ATOM;
  1166. evOut.data[j].atom = (LV2_Atom_Sequence*)malloc(sizeof(LV2_Atom_Sequence) + MAX_EVENT_BUFFER);
  1167. evOut.data[j].atom->atom.size = sizeof(LV2_Atom_Sequence_Body);
  1168. evOut.data[j].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1169. evOut.data[j].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1170. evOut.data[j].atom->body.pad = 0;
  1171. }
  1172. else if (evOuts[j] == CARLA_EVENT_DATA_EVENT)
  1173. {
  1174. evOut.data[j].type = CARLA_EVENT_DATA_EVENT;
  1175. evOut.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1176. }
  1177. else if (evOuts[j] == CARLA_EVENT_DATA_MIDI_LL)
  1178. {
  1179. evOut.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1180. evOut.data[j].midi = new LV2_MIDI;
  1181. evOut.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1182. evOut.data[j].midi->data = new unsigned char [MAX_EVENT_BUFFER];
  1183. }
  1184. }
  1185. }
  1186. if (params > 0)
  1187. {
  1188. param.data = new ParameterData[params];
  1189. param.ranges = new ParameterRanges[params];
  1190. paramBuffers = new float[params];
  1191. }
  1192. const int portNameSize = CarlaEngine::maxPortNameSize() - 2;
  1193. char portName[portNameSize];
  1194. bool needsCtrlIn = false;
  1195. bool needsCtrlOut = false;
  1196. for (uint32_t i=0; i < portCount; i++)
  1197. {
  1198. const LV2_Property portType = rdf_descriptor->Ports[i].Type;
  1199. 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))
  1200. {
  1201. #ifndef BUILD_BRIDGE
  1202. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  1203. {
  1204. strcpy(portName, m_name);
  1205. strcat(portName, ":");
  1206. strncat(portName, rdf_descriptor->Ports[i].Name, portNameSize/2);
  1207. }
  1208. else
  1209. #endif
  1210. strncpy(portName, rdf_descriptor->Ports[i].Name, portNameSize);
  1211. }
  1212. if (LV2_IS_PORT_AUDIO(portType))
  1213. {
  1214. if (LV2_IS_PORT_INPUT(portType))
  1215. {
  1216. j = aIn.count++;
  1217. aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  1218. aIn.rindexes[j] = i;
  1219. if (forcedStereoIn)
  1220. {
  1221. strcat(portName, "_");
  1222. aIn.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  1223. aIn.rindexes[1] = i;
  1224. }
  1225. }
  1226. else if (LV2_IS_PORT_OUTPUT(portType))
  1227. {
  1228. j = aOut.count++;
  1229. aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  1230. aOut.rindexes[j] = i;
  1231. needsCtrlIn = true;
  1232. if (forcedStereoOut)
  1233. {
  1234. strcat(portName, "_");
  1235. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  1236. aOut.rindexes[1] = i;
  1237. }
  1238. }
  1239. else
  1240. qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
  1241. }
  1242. else if (LV2_IS_PORT_CV(portType))
  1243. {
  1244. if (LV2_IS_PORT_INPUT(portType))
  1245. {
  1246. qWarning("WARNING - CV Ports are not supported yet");
  1247. }
  1248. else if (LV2_IS_PORT_OUTPUT(portType))
  1249. {
  1250. qWarning("WARNING - CV Ports are not supported yet");
  1251. }
  1252. else
  1253. qWarning("WARNING - Got a broken Port (CV, but not input or output)");
  1254. descriptor->connect_port(handle, i, nullptr);
  1255. if (h2) descriptor->connect_port(h2, i, nullptr);
  1256. }
  1257. else if (LV2_IS_PORT_ATOM_SEQUENCE(portType))
  1258. {
  1259. if (LV2_IS_PORT_INPUT(portType))
  1260. {
  1261. j = evIn.count++;
  1262. descriptor->connect_port(handle, i, evIn.data[j].atom);
  1263. if (h2) descriptor->connect_port(h2, i, evIn.data[j].atom);
  1264. evIn.data[j].rindex = i;
  1265. if (portType & LV2_PORT_SUPPORTS_MIDI_EVENT)
  1266. {
  1267. evIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1268. evIn.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  1269. }
  1270. if (portType & LV2_PORT_SUPPORTS_PATCH_MESSAGE)
  1271. {
  1272. evIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1273. }
  1274. }
  1275. else if (LV2_IS_PORT_OUTPUT(portType))
  1276. {
  1277. j = evOut.count++;
  1278. descriptor->connect_port(handle, i, evOut.data[j].atom);
  1279. if (h2) descriptor->connect_port(h2, i, evOut.data[j].atom);
  1280. evOut.data[j].rindex = i;
  1281. if (portType & LV2_PORT_SUPPORTS_MIDI_EVENT)
  1282. {
  1283. evOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1284. evOut.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  1285. }
  1286. if (portType & LV2_PORT_SUPPORTS_PATCH_MESSAGE)
  1287. {
  1288. evOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1289. }
  1290. }
  1291. else
  1292. qWarning("WARNING - Got a broken Port (Atom Sequence, but not input or output)");
  1293. }
  1294. else if (LV2_IS_PORT_EVENT(portType))
  1295. {
  1296. if (LV2_IS_PORT_INPUT(portType))
  1297. {
  1298. j = evIn.count++;
  1299. descriptor->connect_port(handle, i, evIn.data[j].event);
  1300. if (h2) descriptor->connect_port(h2, i, evIn.data[j].event);
  1301. evIn.data[j].rindex = i;
  1302. if (portType & LV2_PORT_SUPPORTS_MIDI_EVENT)
  1303. {
  1304. evIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1305. evIn.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  1306. }
  1307. }
  1308. else if (LV2_IS_PORT_OUTPUT(portType))
  1309. {
  1310. j = evOut.count++;
  1311. descriptor->connect_port(handle, i, evOut.data[j].event);
  1312. if (h2) descriptor->connect_port(h2, i, evOut.data[j].event);
  1313. evOut.data[j].rindex = i;
  1314. if (portType & LV2_PORT_SUPPORTS_MIDI_EVENT)
  1315. {
  1316. evOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1317. evOut.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  1318. }
  1319. }
  1320. else
  1321. qWarning("WARNING - Got a broken Port (Event, but not input or output)");
  1322. }
  1323. else if (LV2_IS_PORT_MIDI_LL(portType))
  1324. {
  1325. if (LV2_IS_PORT_INPUT(portType))
  1326. {
  1327. j = evIn.count++;
  1328. descriptor->connect_port(handle, i, evIn.data[j].midi);
  1329. if (h2) descriptor->connect_port(h2, i, evIn.data[j].midi);
  1330. evIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1331. evIn.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  1332. evIn.data[j].rindex = i;
  1333. }
  1334. else if (LV2_IS_PORT_OUTPUT(portType))
  1335. {
  1336. j = evOut.count++;
  1337. descriptor->connect_port(handle, i, evOut.data[j].midi);
  1338. if (h2) descriptor->connect_port(h2, i, evOut.data[j].midi);
  1339. evOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1340. evOut.data[j].port = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  1341. evOut.data[j].rindex = i;
  1342. }
  1343. else
  1344. qWarning("WARNING - Got a broken Port (Midi, but not input or output)");
  1345. }
  1346. else if (LV2_IS_PORT_CONTROL(portType))
  1347. {
  1348. const LV2_Property portProps = rdf_descriptor->Ports[i].Properties;
  1349. const LV2_Property portDesignation = rdf_descriptor->Ports[i].Designation;
  1350. const LV2_RDF_PortPoints portPoints = rdf_descriptor->Ports[i].Points;
  1351. j = param.count++;
  1352. param.data[j].index = j;
  1353. param.data[j].rindex = i;
  1354. param.data[j].hints = 0;
  1355. param.data[j].midiChannel = 0;
  1356. param.data[j].midiCC = -1;
  1357. double min, max, def, step, stepSmall, stepLarge;
  1358. // min value
  1359. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1360. min = portPoints.Minimum;
  1361. else
  1362. min = 0.0;
  1363. // max value
  1364. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1365. max = portPoints.Maximum;
  1366. else
  1367. max = 1.0;
  1368. if (min > max)
  1369. max = min;
  1370. else if (max < min)
  1371. min = max;
  1372. // stupid hack for ir.lv2 (broken plugin)
  1373. if (strcmp(rdf_descriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && strncmp(rdf_descriptor->Ports[i].Name, "FileHash", 8) == 0)
  1374. {
  1375. min = 0.0;
  1376. max = 16777215.0; // 0xffffff
  1377. }
  1378. if (max - min == 0.0)
  1379. {
  1380. qWarning("Broken plugin parameter: max - min == 0");
  1381. max = min + 0.1;
  1382. }
  1383. // default value
  1384. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1385. {
  1386. def = portPoints.Default;
  1387. }
  1388. else
  1389. {
  1390. // no default value
  1391. if (min < 0.0 && max > 0.0)
  1392. def = 0.0;
  1393. else
  1394. def = min;
  1395. }
  1396. if (def < min)
  1397. def = min;
  1398. else if (def > max)
  1399. def = max;
  1400. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1401. {
  1402. min *= sampleRate;
  1403. max *= sampleRate;
  1404. def *= sampleRate;
  1405. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1406. }
  1407. if (LV2_IS_PORT_TOGGLED(portProps))
  1408. {
  1409. step = max - min;
  1410. stepSmall = step;
  1411. stepLarge = step;
  1412. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1413. }
  1414. else if (LV2_IS_PORT_INTEGER(portProps))
  1415. {
  1416. step = 1.0;
  1417. stepSmall = 1.0;
  1418. stepLarge = 10.0;
  1419. param.data[j].hints |= PARAMETER_IS_INTEGER;
  1420. }
  1421. else
  1422. {
  1423. double range = max - min;
  1424. step = range/100.0;
  1425. stepSmall = range/1000.0;
  1426. stepLarge = range/10.0;
  1427. }
  1428. if (LV2_IS_PORT_INPUT(portType))
  1429. {
  1430. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1431. {
  1432. qWarning("Plugin has latency input port, this should not happen!");
  1433. }
  1434. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1435. {
  1436. def = sampleRate;
  1437. step = 1.0;
  1438. stepSmall = 1.0;
  1439. stepLarge = 1.0;
  1440. param.data[j].type = PARAMETER_SAMPLE_RATE;
  1441. param.data[j].hints = 0;
  1442. }
  1443. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1444. {
  1445. param.data[j].type = PARAMETER_LV2_FREEWHEEL;
  1446. }
  1447. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1448. {
  1449. param.data[j].type = PARAMETER_LV2_TIME;
  1450. }
  1451. else
  1452. {
  1453. param.data[j].type = PARAMETER_INPUT;
  1454. param.data[j].hints |= PARAMETER_IS_ENABLED;
  1455. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1456. needsCtrlIn = true;
  1457. }
  1458. // MIDI CC value
  1459. const LV2_RDF_PortMidiMap* const portMidiMap = &rdf_descriptor->Ports[i].MidiMap;
  1460. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap->Type))
  1461. {
  1462. if (! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap->Number))
  1463. param.data[j].midiCC = portMidiMap->Number;
  1464. }
  1465. }
  1466. else if (LV2_IS_PORT_OUTPUT(portType))
  1467. {
  1468. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1469. {
  1470. min = 0.0;
  1471. max = sampleRate;
  1472. def = 0.0;
  1473. step = 1.0;
  1474. stepSmall = 1.0;
  1475. stepLarge = 1.0;
  1476. param.data[j].type = PARAMETER_LATENCY;
  1477. param.data[j].hints = 0;
  1478. }
  1479. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1480. {
  1481. def = sampleRate;
  1482. step = 1.0;
  1483. stepSmall = 1.0;
  1484. stepLarge = 1.0;
  1485. param.data[j].type = PARAMETER_SAMPLE_RATE;
  1486. param.data[j].hints = 0;
  1487. }
  1488. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1489. {
  1490. qWarning("Plugin has freewheeling output port, this should not happen!");
  1491. }
  1492. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1493. {
  1494. param.data[j].type = PARAMETER_LV2_TIME;
  1495. }
  1496. else
  1497. {
  1498. param.data[j].type = PARAMETER_OUTPUT;
  1499. param.data[j].hints |= PARAMETER_IS_ENABLED;
  1500. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1501. needsCtrlOut = true;
  1502. }
  1503. }
  1504. else
  1505. {
  1506. param.data[j].type = PARAMETER_UNKNOWN;
  1507. qWarning("WARNING - Got a broken Port (Control, but not input or output)");
  1508. }
  1509. // extra parameter hints
  1510. if (LV2_IS_PORT_ENUMERATION(portProps))
  1511. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1512. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1513. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1514. if (LV2_IS_PORT_TRIGGER(portProps))
  1515. param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1516. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1517. param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1518. // check if parameter is not enabled or automable
  1519. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1520. param.data[j].hints &= ~PARAMETER_IS_ENABLED;
  1521. if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1522. param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1523. param.ranges[j].min = min;
  1524. param.ranges[j].max = max;
  1525. param.ranges[j].def = def;
  1526. param.ranges[j].step = step;
  1527. param.ranges[j].stepSmall = stepSmall;
  1528. param.ranges[j].stepLarge = stepLarge;
  1529. // Start parameters in their default values
  1530. paramBuffers[j] = def;
  1531. descriptor->connect_port(handle, i, &paramBuffers[j]);
  1532. if (h2) descriptor->connect_port(h2, i, &paramBuffers[j]);
  1533. }
  1534. else
  1535. {
  1536. // Port Type not supported, but it's optional anyway
  1537. descriptor->connect_port(handle, i, nullptr);
  1538. if (h2) descriptor->connect_port(h2, i, nullptr);
  1539. }
  1540. }
  1541. if (needsCtrlIn)
  1542. {
  1543. #ifndef BUILD_BRIDGE
  1544. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  1545. {
  1546. strcpy(portName, m_name);
  1547. strcat(portName, ":control-in");
  1548. }
  1549. else
  1550. #endif
  1551. strcpy(portName, "control-in");
  1552. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  1553. }
  1554. if (needsCtrlOut)
  1555. {
  1556. #ifndef BUILD_BRIDGE
  1557. if (carlaOptions.processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  1558. {
  1559. strcpy(portName, m_name);
  1560. strcat(portName, ":control-out");
  1561. }
  1562. else
  1563. #endif
  1564. strcpy(portName, "control-out");
  1565. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  1566. }
  1567. aIn.count = aIns;
  1568. aOut.count = aOuts;
  1569. evIn.count = evIns.size();
  1570. evOut.count = evOuts.size();
  1571. param.count = params;
  1572. // plugin checks
  1573. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE);
  1574. if (LV2_IS_GENERATOR(rdf_descriptor->Type))
  1575. m_hints |= PLUGIN_IS_SYNTH;
  1576. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1577. m_hints |= PLUGIN_CAN_DRYWET;
  1578. if (aOuts > 0)
  1579. m_hints |= PLUGIN_CAN_VOLUME;
  1580. if (aOuts >= 2 && aOuts%2 == 0)
  1581. m_hints |= PLUGIN_CAN_BALANCE;
  1582. // check extensions
  1583. ext.state = nullptr;
  1584. ext.worker = nullptr;
  1585. ext.programs = nullptr;
  1586. if (descriptor->extension_data)
  1587. {
  1588. if (m_hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  1589. ext.programs = (const LV2_Programs_Interface*)descriptor->extension_data(LV2_PROGRAMS__Interface);
  1590. if (m_hints & PLUGIN_HAS_EXTENSION_STATE)
  1591. ext.state = (const LV2_State_Interface*)descriptor->extension_data(LV2_STATE__interface);
  1592. if (m_hints & PLUGIN_HAS_EXTENSION_WORKER)
  1593. ext.worker = (const LV2_Worker_Interface*)descriptor->extension_data(LV2_WORKER__interface);
  1594. }
  1595. reloadPrograms(true);
  1596. x_client->activate();
  1597. qDebug("Lv2Plugin::reload() - end");
  1598. }
  1599. void reloadPrograms(const bool init)
  1600. {
  1601. qDebug("Lv2Plugin::reloadPrograms(%s)", bool2str(init));
  1602. uint32_t i, oldCount = midiprog.count;
  1603. // Delete old programs
  1604. if (midiprog.count > 0)
  1605. {
  1606. for (i=0; i < midiprog.count; i++)
  1607. {
  1608. if (midiprog.data[i].name)
  1609. free((void*)midiprog.data[i].name);
  1610. }
  1611. delete[] midiprog.data;
  1612. }
  1613. midiprog.count = 0;
  1614. midiprog.data = nullptr;
  1615. // Query new programs
  1616. if (ext.programs && ext.programs->get_program && ext.programs->select_program)
  1617. {
  1618. while (ext.programs->get_program(handle, midiprog.count))
  1619. midiprog.count += 1;
  1620. }
  1621. if (midiprog.count > 0)
  1622. midiprog.data = new midi_program_t[midiprog.count];
  1623. // Update data
  1624. for (i=0; i < midiprog.count; i++)
  1625. {
  1626. const LV2_Program_Descriptor* const pdesc = ext.programs->get_program(handle, i);
  1627. Q_ASSERT(pdesc);
  1628. Q_ASSERT(pdesc->program < 128);
  1629. Q_ASSERT(pdesc->name);
  1630. midiprog.data[i].bank = pdesc->bank;
  1631. midiprog.data[i].program = pdesc->program;
  1632. midiprog.data[i].name = strdup(pdesc->name);
  1633. }
  1634. #ifndef BUILD_BRIDGE
  1635. // Update OSC Names
  1636. if (x_engine->isOscControllerRegisted())
  1637. {
  1638. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  1639. for (i=0; i < midiprog.count; i++)
  1640. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  1641. }
  1642. #endif
  1643. if (init)
  1644. {
  1645. if (midiprog.count > 0)
  1646. setMidiProgram(0, false, false, false, true);
  1647. }
  1648. else
  1649. {
  1650. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  1651. // Check if current program is invalid
  1652. bool programChanged = false;
  1653. if (midiprog.count == oldCount+1)
  1654. {
  1655. // one midi program added, probably created by user
  1656. midiprog.current = oldCount;
  1657. programChanged = true;
  1658. }
  1659. else if (midiprog.current >= (int32_t)midiprog.count)
  1660. {
  1661. // current midi program > count
  1662. midiprog.current = 0;
  1663. programChanged = true;
  1664. }
  1665. else if (midiprog.current < 0 && midiprog.count > 0)
  1666. {
  1667. // programs exist now, but not before
  1668. midiprog.current = 0;
  1669. programChanged = true;
  1670. }
  1671. else if (midiprog.current >= 0 && midiprog.count == 0)
  1672. {
  1673. // programs existed before, but not anymore
  1674. midiprog.current = -1;
  1675. programChanged = true;
  1676. }
  1677. if (programChanged)
  1678. setMidiProgram(midiprog.current, true, true, true, true);
  1679. }
  1680. }
  1681. void prepareForSave()
  1682. {
  1683. if (ext.state && ext.state->save)
  1684. {
  1685. ext.state->save(handle, carla_lv2_state_store, this, LV2_STATE_IS_POD, features);
  1686. if (h2) ext.state->save(h2, carla_lv2_state_store, this, LV2_STATE_IS_POD, features);
  1687. }
  1688. }
  1689. // -------------------------------------------------------------------
  1690. // Plugin processing
  1691. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  1692. {
  1693. uint32_t i, k;
  1694. uint32_t midiEventCount = 0;
  1695. double aInsPeak[2] = { 0.0 };
  1696. double aOutsPeak[2] = { 0.0 };
  1697. // handle events from different APIs
  1698. uint32_t evInAtomOffsets[evIn.count];
  1699. LV2_Event_Iterator evInEventIters[evIn.count];
  1700. LV2_MIDIState evInMidiStates[evIn.count];
  1701. for (i=0; i < evIn.count; i++)
  1702. {
  1703. if (evIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1704. {
  1705. evInAtomOffsets[i] = 0;
  1706. evIn.data[i].atom->atom.size = 0;
  1707. evIn.data[i].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1708. evIn.data[i].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1709. evIn.data[i].atom->body.pad = 0;
  1710. }
  1711. else if (evIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1712. {
  1713. lv2_event_buffer_reset(evIn.data[i].event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(evIn.data[i].event + 1));
  1714. lv2_event_begin(&evInEventIters[i], evIn.data[i].event);
  1715. }
  1716. else if (evIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1717. {
  1718. evInMidiStates[i].midi = evIn.data[i].midi;
  1719. evInMidiStates[i].frame_count = frames;
  1720. evInMidiStates[i].position = 0;
  1721. evInMidiStates[i].midi->event_count = 0;
  1722. evInMidiStates[i].midi->size = 0;
  1723. }
  1724. }
  1725. for (i=0; i < evOut.count; i++)
  1726. {
  1727. if (evOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1728. {
  1729. evOut.data[i].atom->atom.size = 0;
  1730. evOut.data[i].atom->atom.type = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  1731. evOut.data[i].atom->body.unit = CARLA_URI_MAP_ID_NULL;
  1732. evOut.data[i].atom->body.pad = 0;
  1733. }
  1734. else if (evOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1735. {
  1736. lv2_event_buffer_reset(evOut.data[i].event, LV2_EVENT_AUDIO_STAMP, (uint8_t*)(evOut.data[i].event + 1));
  1737. }
  1738. else if (evOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1739. {
  1740. // not needed
  1741. }
  1742. }
  1743. CARLA_PROCESS_CONTINUE_CHECK;
  1744. // --------------------------------------------------------------------------------------------------------
  1745. // Input VU
  1746. if (aIn.count > 0)
  1747. {
  1748. if (aIn.count == 1)
  1749. {
  1750. for (k=0; k < frames; k++)
  1751. {
  1752. if (abs(inBuffer[0][k]) > aInsPeak[0])
  1753. aInsPeak[0] = abs(inBuffer[0][k]);
  1754. }
  1755. }
  1756. else if (aIn.count > 1)
  1757. {
  1758. for (k=0; k < frames; k++)
  1759. {
  1760. if (abs(inBuffer[0][k]) > aInsPeak[0])
  1761. aInsPeak[0] = abs(inBuffer[0][k]);
  1762. if (abs(inBuffer[1][k]) > aInsPeak[1])
  1763. aInsPeak[1] = abs(inBuffer[1][k]);
  1764. }
  1765. }
  1766. }
  1767. CARLA_PROCESS_CONTINUE_CHECK;
  1768. // --------------------------------------------------------------------------------------------------------
  1769. // Parameters Input [Automation]
  1770. if (param.portCin && m_active && m_activeBefore)
  1771. {
  1772. bool allNotesOffSent = false;
  1773. const CarlaEngineControlEvent* cinEvent;
  1774. uint32_t time, nEvents = param.portCin->getEventCount();
  1775. uint32_t nextBankId = 0;
  1776. if (midiprog.current >= 0 && midiprog.count > 0)
  1777. nextBankId = midiprog.data[midiprog.current].bank;
  1778. for (i=0; i < nEvents; i++)
  1779. {
  1780. cinEvent = param.portCin->getEvent(i);
  1781. if (! cinEvent)
  1782. continue;
  1783. time = cinEvent->time - framesOffset;
  1784. if (time >= frames)
  1785. continue;
  1786. // Control change
  1787. switch (cinEvent->type)
  1788. {
  1789. case CarlaEngineEventNull:
  1790. break;
  1791. case CarlaEngineEventControlChange:
  1792. {
  1793. double value;
  1794. // Control backend stuff
  1795. if (cinEvent->channel == m_ctrlInChannel)
  1796. {
  1797. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  1798. {
  1799. value = cinEvent->value;
  1800. setDryWet(value, false, false);
  1801. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  1802. continue;
  1803. }
  1804. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  1805. {
  1806. value = cinEvent->value*127/100;
  1807. setVolume(value, false, false);
  1808. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  1809. continue;
  1810. }
  1811. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  1812. {
  1813. double left, right;
  1814. value = cinEvent->value/0.5 - 1.0;
  1815. if (value < 0.0)
  1816. {
  1817. left = -1.0;
  1818. right = (value*2)+1.0;
  1819. }
  1820. else if (value > 0.0)
  1821. {
  1822. left = (value*2)-1.0;
  1823. right = 1.0;
  1824. }
  1825. else
  1826. {
  1827. left = -1.0;
  1828. right = 1.0;
  1829. }
  1830. setBalanceLeft(left, false, false);
  1831. setBalanceRight(right, false, false);
  1832. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1833. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1834. continue;
  1835. }
  1836. }
  1837. // Control plugin parameters
  1838. for (k=0; k < param.count; k++)
  1839. {
  1840. if (param.data[k].midiChannel != cinEvent->channel)
  1841. continue;
  1842. if (param.data[k].midiCC != cinEvent->controller)
  1843. continue;
  1844. if (param.data[k].type != PARAMETER_INPUT)
  1845. continue;
  1846. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  1847. {
  1848. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1849. {
  1850. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  1851. }
  1852. else
  1853. {
  1854. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  1855. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  1856. value = rint(value);
  1857. }
  1858. setParameterValue(k, value, false, false, false);
  1859. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  1860. }
  1861. }
  1862. break;
  1863. }
  1864. case CarlaEngineEventMidiBankChange:
  1865. if (cinEvent->channel == m_ctrlInChannel)
  1866. nextBankId = rint(cinEvent->value);
  1867. break;
  1868. case CarlaEngineEventMidiProgramChange:
  1869. if (cinEvent->channel == m_ctrlInChannel)
  1870. {
  1871. uint32_t nextProgramId = rint(cinEvent->value);
  1872. for (k=0; k < midiprog.count; k++)
  1873. {
  1874. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  1875. {
  1876. setMidiProgram(k, false, false, false, false);
  1877. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  1878. break;
  1879. }
  1880. }
  1881. }
  1882. break;
  1883. case CarlaEngineEventAllSoundOff:
  1884. if (cinEvent->channel == m_ctrlInChannel)
  1885. {
  1886. if (midi.portMin && ! allNotesOffSent)
  1887. sendMidiAllNotesOff();
  1888. if (descriptor->deactivate)
  1889. {
  1890. descriptor->deactivate(handle);
  1891. if (h2) descriptor->deactivate(h2);
  1892. }
  1893. if (descriptor->activate)
  1894. {
  1895. descriptor->activate(handle);
  1896. if (h2) descriptor->activate(h2);
  1897. }
  1898. allNotesOffSent = true;
  1899. }
  1900. break;
  1901. case CarlaEngineEventAllNotesOff:
  1902. if (cinEvent->channel == m_ctrlInChannel)
  1903. {
  1904. if (midi.portMin && ! allNotesOffSent)
  1905. sendMidiAllNotesOff();
  1906. allNotesOffSent = true;
  1907. }
  1908. break;
  1909. }
  1910. }
  1911. } // End of Parameters Input
  1912. CARLA_PROCESS_CONTINUE_CHECK;
  1913. // --------------------------------------------------------------------------------------------------------
  1914. // Event Input
  1915. if (evIn.count > 0 && m_active && m_activeBefore)
  1916. {
  1917. // ----------------------------------------------------------------------------------------------------
  1918. // MIDI Input (External)
  1919. {
  1920. engineMidiLock();
  1921. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  1922. {
  1923. if (extMidiNotes[i].channel < 0)
  1924. break;
  1925. uint8_t midiEvent[3] = { 0 };
  1926. midiEvent[0] = m_ctrlInChannel + extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1927. midiEvent[1] = extMidiNotes[i].note;
  1928. midiEvent[2] = extMidiNotes[i].velo;
  1929. // send to all midi inputs
  1930. for (k=0; k < evIn.count; k++)
  1931. {
  1932. if (evIn.data[k].type & CARLA_EVENT_TYPE_MIDI)
  1933. {
  1934. if (evIn.data[k].type & CARLA_EVENT_DATA_ATOM)
  1935. {
  1936. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  1937. aev->time.frames = 0;
  1938. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1939. aev->body.size = 3;
  1940. memcpy(LV2_ATOM_BODY(&aev->body), midiEvent, 3);
  1941. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 3);
  1942. evInAtomOffsets[k] += evInPadSize;
  1943. evIn.data[k].atom->atom.size += evInPadSize;
  1944. }
  1945. else if (evIn.data[k].type & CARLA_EVENT_DATA_EVENT)
  1946. {
  1947. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  1948. }
  1949. else if (evIn.data[k].type & CARLA_EVENT_DATA_MIDI_LL)
  1950. {
  1951. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiEvent);
  1952. }
  1953. }
  1954. }
  1955. extMidiNotes[i].channel = -1; // mark as invalid
  1956. midiEventCount += 1;
  1957. }
  1958. engineMidiUnlock();
  1959. } // End of MIDI Input (External)
  1960. CARLA_PROCESS_CONTINUE_CHECK;
  1961. // ----------------------------------------------------------------------------------------------------
  1962. // MIDI Input (System)
  1963. {
  1964. for (i=0; i < evIn.count; i++)
  1965. {
  1966. if (! evIn.data[i].port)
  1967. continue;
  1968. const CarlaEngineMidiEvent* minEvent;
  1969. uint32_t time, nEvents = evIn.data[i].port->getEventCount();
  1970. for (k=0; k < nEvents && midiEventCount < MAX_MIDI_EVENTS; k++)
  1971. {
  1972. minEvent = evIn.data[i].port->getEvent(k);
  1973. if (! minEvent)
  1974. continue;
  1975. time = minEvent->time - framesOffset;
  1976. if (time >= frames)
  1977. continue;
  1978. uint8_t status = minEvent->data[0];
  1979. uint8_t channel = status & 0x0F;
  1980. // Fix bad note-off
  1981. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  1982. status -= 0x10;
  1983. // only write supported status types
  1984. 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))
  1985. {
  1986. if (evIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1987. {
  1988. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  1989. aev->time.frames = time;
  1990. aev->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  1991. aev->body.size = minEvent->size;
  1992. memcpy(LV2_ATOM_BODY(&aev->body), minEvent->data, minEvent->size);
  1993. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + minEvent->size);
  1994. evInAtomOffsets[i] += evInPadSize;
  1995. evIn.data[i].atom->atom.size += evInPadSize;
  1996. }
  1997. else if (evIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1998. {
  1999. lv2_event_write(&evInEventIters[i], time, 0, CARLA_URI_MAP_ID_MIDI_EVENT, minEvent->size, minEvent->data);
  2000. }
  2001. else if (evIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2002. {
  2003. lv2midi_put_event(&evInMidiStates[i], time, minEvent->size, minEvent->data);
  2004. }
  2005. if (MIDI_IS_STATUS_NOTE_OFF(status))
  2006. postponeEvent(PluginPostEventNoteOff, channel, minEvent->data[1], 0.0);
  2007. else if (MIDI_IS_STATUS_NOTE_ON(status))
  2008. postponeEvent(PluginPostEventNoteOn, channel, minEvent->data[1], minEvent->data[2]);
  2009. }
  2010. midiEventCount += 1;
  2011. }
  2012. }
  2013. } // End of MIDI Input (System)
  2014. // ----------------------------------------------------------------------------------------------------
  2015. // Message Input
  2016. {
  2017. for (i=0; i < evIn.count; i++)
  2018. {
  2019. if (! evIn.data[i].type & CARLA_EVENT_TYPE_MESSAGE)
  2020. continue;
  2021. // messages only supported in Atom type ports
  2022. Q_ASSERT(evIn.data[i].type & CARLA_EVENT_DATA_ATOM);
  2023. #if 0
  2024. // send transport info if changed
  2025. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  2026. if (timeInfo->playing != lastTimePosPlaying || timeInfo->frame != lastTimePosFrame)
  2027. {
  2028. uint8_t posBuf[256];
  2029. LV2_Atom* const lv2Pos = (LV2_Atom*)posBuf;
  2030. LV2_Atom_Forge tempForge;
  2031. LV2_Atom_Forge* const forge = &tempForge;
  2032. lv2_atom_forge_set_buffer(forge, posBuf, sizeof(uint8_t)*256);
  2033. //LV2_Atom_Forge_Frame frame;
  2034. //lv2_atom_forge_blank(forge, &frame, 1, jalv->urids.time_Position);
  2035. //lv2_atom_forge_property_head(forge, jalv->urids.time_frame, 0);
  2036. lv2_atom_forge_long(forge, timeInfo->frame);
  2037. //lv2_atom_forge_property_head(forge, jalv->urids.time_position, 0);
  2038. lv2_atom_forge_long(forge, timeInfo->time);
  2039. //lv2_atom_forge_property_head(forge, jalv->urids.time_speed, 0);
  2040. lv2_atom_forge_float(forge, timeInfo->playing ? 1.0 : 0.0);
  2041. if (timeInfo->valid & CarlaEngineTimeBBT)
  2042. {
  2043. //lv2_atom_forge_property_head(forge, jalv->urids.time_bar, 0);
  2044. lv2_atom_forge_float(forge, timeInfo->bbt.bar - 1);
  2045. //lv2_atom_forge_property_head(forge, jalv->urids.time_barBeat, 0);
  2046. lv2_atom_forge_float(forge, timeInfo->bbt.beat - 1 + (timeInfo->bbt.tick / timeInfo->bbt.ticks_per_beat));
  2047. //lv2_atom_forge_property_head(forge, jalv->urids.time_beat, 0);
  2048. lv2_atom_forge_float(forge, timeInfo->bbt.beat - 1); // FIXME: -1 ?
  2049. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatUnit, 0);
  2050. lv2_atom_forge_float(forge, timeInfo->bbt.beat_type);
  2051. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatsPerBar, 0);
  2052. lv2_atom_forge_float(forge, timeInfo->bbt.beats_per_bar);
  2053. //lv2_atom_forge_property_head(forge, jalv->urids.time_beatsPerMinute, 0);
  2054. lv2_atom_forge_float(forge, timeInfo->bbt.beats_per_minute);
  2055. }
  2056. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  2057. aev->time.frames = framesOffset;
  2058. aev->body.type = lv2Pos->type;
  2059. aev->body.size = lv2Pos->size;
  2060. memcpy(LV2_ATOM_BODY(&aev->body), LV2_ATOM_BODY(lv2Pos), lv2Pos->size);
  2061. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + lv2Pos->size);
  2062. evInAtomOffsets[i] += evInPadSize;
  2063. evIn.data[i].atom->atom.size += evInPadSize;
  2064. lastTimePosPlaying = timeInfo->playing;
  2065. lastTimePosFrame = timeInfo->playing ? timeInfo->frame + frames : timeInfo->frame;
  2066. }
  2067. #endif
  2068. atomQueueIn.lock();
  2069. if (! atomQueueIn.isEmpty())
  2070. {
  2071. uint32_t portIndex;
  2072. const LV2_Atom* atom;
  2073. while (atomQueueIn.get(&portIndex, &atom, false))
  2074. {
  2075. if (atom->type == CARLA_URI_MAP_ID_ATOM_WORKER)
  2076. {
  2077. const LV2_Atom_Worker* const atomWorker = (const LV2_Atom_Worker*)atom;
  2078. ext.worker->work_response(handle, atomWorker->body.size, atomWorker->body.data);
  2079. continue;
  2080. }
  2081. LV2_Atom_Event* const aev = getLv2AtomEvent(evIn.data[i].atom, evInAtomOffsets[i]);
  2082. aev->time.frames = framesOffset;
  2083. aev->body.type = atom->type;
  2084. aev->body.size = atom->size;
  2085. memcpy(LV2_ATOM_BODY(&aev->body), LV2_ATOM_BODY(atom), atom->size);
  2086. const uint32_t evInPadSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + atom->size);
  2087. evInAtomOffsets[i] += evInPadSize;
  2088. evIn.data[i].atom->atom.size += evInPadSize;
  2089. }
  2090. }
  2091. atomQueueIn.unlock();
  2092. }
  2093. }
  2094. } // End of Event Input
  2095. CARLA_PROCESS_CONTINUE_CHECK;
  2096. // --------------------------------------------------------------------------------------------------------
  2097. // Special Parameters
  2098. {
  2099. int32_t rindex;
  2100. const CarlaTimeInfo* const timeInfo = x_engine->getTimeInfo();
  2101. for (k=0; k < param.count; k++)
  2102. {
  2103. if (param.data[k].type == PARAMETER_LATENCY)
  2104. {
  2105. // TODO
  2106. }
  2107. else if (param.data[k].type == PARAMETER_LV2_FREEWHEEL)
  2108. {
  2109. setParameterValue(k, x_engine->isOffline() ? 1.0 : 0.0, false, false, false);
  2110. }
  2111. else if (param.data[k].type == PARAMETER_LV2_TIME)
  2112. {
  2113. rindex = param.data[k].rindex;
  2114. Q_ASSERT(rindex >= 0 && rindex < (int32_t)rdf_descriptor->PortCount);
  2115. switch (rdf_descriptor->Ports[rindex].Designation)
  2116. {
  2117. // Non-BBT
  2118. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2119. setParameterValue(k, timeInfo->frame, false, false, false);
  2120. break;
  2121. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2122. break;
  2123. case LV2_PORT_DESIGNATION_TIME_POSITION:
  2124. setParameterValue(k, timeInfo->time, false, false, false);
  2125. break;
  2126. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2127. setParameterValue(k, timeInfo->playing ? 1.0 : 0.0, false, false, false);
  2128. break;
  2129. // BBT
  2130. case LV2_PORT_DESIGNATION_TIME_BAR:
  2131. if (timeInfo->valid & CarlaEngineTimeBBT)
  2132. setParameterValue(k, timeInfo->bbt.bar - 1, false, false, false);
  2133. break;
  2134. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2135. if (timeInfo->valid & CarlaEngineTimeBBT)
  2136. setParameterValue(k, timeInfo->bbt.beat - 1 + (timeInfo->bbt.tick / timeInfo->bbt.ticks_per_beat), false, false, false);
  2137. break;
  2138. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2139. if (timeInfo->valid & CarlaEngineTimeBBT) // FIXME: -1 ?
  2140. setParameterValue(k, timeInfo->bbt.beat - 1, false, false, false);
  2141. break;
  2142. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2143. if (timeInfo->valid & CarlaEngineTimeBBT)
  2144. setParameterValue(k, timeInfo->bbt.beat_type, false, false, false);
  2145. break;
  2146. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2147. if (timeInfo->valid & CarlaEngineTimeBBT)
  2148. setParameterValue(k, timeInfo->bbt.beats_per_bar, false, false, false);
  2149. break;
  2150. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2151. if (timeInfo->valid & CarlaEngineTimeBBT)
  2152. setParameterValue(k, timeInfo->bbt.beats_per_minute, false, false, false);
  2153. break;
  2154. }
  2155. }
  2156. }
  2157. }
  2158. CARLA_PROCESS_CONTINUE_CHECK;
  2159. // --------------------------------------------------------------------------------------------------------
  2160. // Plugin processing
  2161. if (m_active)
  2162. {
  2163. if (! m_activeBefore)
  2164. {
  2165. if (evIn.count > 0 && m_ctrlInChannel >= 0 && m_ctrlInChannel < 16)
  2166. {
  2167. uint8_t midiEvent1[2] = { 0 };
  2168. midiEvent1[0] = MIDI_STATUS_CONTROL_CHANGE + m_ctrlInChannel;
  2169. midiEvent1[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2170. uint8_t midiEvent2[2] = { 0 };
  2171. midiEvent2[0] = MIDI_STATUS_CONTROL_CHANGE + m_ctrlInChannel;
  2172. midiEvent2[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2173. // send to all midi inputs
  2174. for (k=0; k < evIn.count; k++)
  2175. {
  2176. if (evIn.data[k].type & CARLA_EVENT_TYPE_MIDI)
  2177. {
  2178. if (evIn.data[k].type & CARLA_EVENT_DATA_ATOM)
  2179. {
  2180. // all sound off
  2181. LV2_Atom_Event* const aev1 = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  2182. aev1->time.frames = 0;
  2183. aev1->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2184. aev1->body.size = 2;
  2185. memcpy(LV2_ATOM_BODY(&aev1->body), midiEvent1, 2);
  2186. const uint32_t padSize = lv2_atom_pad_size(sizeof(LV2_Atom_Event) + 2);
  2187. evInAtomOffsets[k] += padSize;
  2188. evIn.data[k].atom->atom.size += padSize;
  2189. // all notes off
  2190. LV2_Atom_Event* const aev2 = getLv2AtomEvent(evIn.data[k].atom, evInAtomOffsets[k]);
  2191. aev2->time.frames = 0;
  2192. aev2->body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2193. aev2->body.size = 2;
  2194. memcpy(LV2_ATOM_BODY(&aev2->body), midiEvent2, 2);
  2195. evInAtomOffsets[k] += padSize;
  2196. evIn.data[k].atom->atom.size += padSize;
  2197. }
  2198. else if (evIn.data[k].type & CARLA_EVENT_DATA_EVENT)
  2199. {
  2200. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiEvent1);
  2201. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 2, midiEvent2);
  2202. }
  2203. else if (evIn.data[k].type & CARLA_EVENT_DATA_MIDI_LL)
  2204. {
  2205. lv2midi_put_event(&evInMidiStates[k], 0, 2, midiEvent1);
  2206. lv2midi_put_event(&evInMidiStates[k], 0, 2, midiEvent2);
  2207. }
  2208. }
  2209. }
  2210. midiEventCount = 2;
  2211. }
  2212. if (descriptor->activate)
  2213. {
  2214. descriptor->activate(handle);
  2215. if (h2) descriptor->activate(h2);
  2216. }
  2217. }
  2218. for (i=0; i < aIn.count; i++)
  2219. {
  2220. if (i == 0 || ! h2) descriptor->connect_port(handle, aIn.rindexes[i], inBuffer[i]);
  2221. if (i == 1 && h2) descriptor->connect_port(h2, aIn.rindexes[i], inBuffer[i]);
  2222. }
  2223. for (i=0; i < aOut.count; i++)
  2224. {
  2225. if (i == 0 || ! h2) descriptor->connect_port(handle, aOut.rindexes[i], outBuffer[i]);
  2226. if (i == 1 && h2) descriptor->connect_port(h2, aOut.rindexes[i], outBuffer[i]);
  2227. }
  2228. descriptor->run(handle, frames);
  2229. if (h2) descriptor->run(h2, frames);
  2230. if (ext.worker && ext.worker->end_run)
  2231. {
  2232. ext.worker->end_run(handle);
  2233. if (h2) ext.worker->end_run(h2);
  2234. }
  2235. }
  2236. else
  2237. {
  2238. if (m_activeBefore)
  2239. {
  2240. if (descriptor->deactivate)
  2241. {
  2242. descriptor->deactivate(handle);
  2243. if (h2) descriptor->deactivate(h2);
  2244. }
  2245. }
  2246. }
  2247. CARLA_PROCESS_CONTINUE_CHECK;
  2248. // --------------------------------------------------------------------------------------------------------
  2249. // Post-processing (dry/wet, volume and balance)
  2250. if (m_active)
  2251. {
  2252. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  2253. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  2254. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  2255. double bal_rangeL, bal_rangeR;
  2256. float oldBufLeft[do_balance ? frames : 0];
  2257. for (i=0; i < aOut.count; i++)
  2258. {
  2259. // Dry/Wet
  2260. if (do_drywet)
  2261. {
  2262. for (k=0; k < frames; k++)
  2263. {
  2264. if (aOut.count == 1)
  2265. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[0][k]*(1.0-x_dryWet));
  2266. else
  2267. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(inBuffer[i][k]*(1.0-x_dryWet));
  2268. }
  2269. }
  2270. // Balance
  2271. if (do_balance)
  2272. {
  2273. if (i%2 == 0)
  2274. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  2275. bal_rangeL = (x_balanceLeft+1.0)/2;
  2276. bal_rangeR = (x_balanceRight+1.0)/2;
  2277. for (k=0; k < frames; k++)
  2278. {
  2279. if (i%2 == 0)
  2280. {
  2281. // left output
  2282. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  2283. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  2284. }
  2285. else
  2286. {
  2287. // right
  2288. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  2289. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  2290. }
  2291. }
  2292. }
  2293. // Volume
  2294. if (do_volume)
  2295. {
  2296. for (k=0; k < frames; k++)
  2297. outBuffer[i][k] *= x_volume;
  2298. }
  2299. // Output VU
  2300. for (k=0; i < 2 && k < frames; k++)
  2301. {
  2302. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  2303. aOutsPeak[i] = abs(outBuffer[i][k]);
  2304. }
  2305. }
  2306. }
  2307. else
  2308. {
  2309. // disable any output sound if not active
  2310. for (i=0; i < aOut.count; i++)
  2311. memset(outBuffer[i], 0.0f, sizeof(float)*frames);
  2312. aOutsPeak[0] = 0.0;
  2313. aOutsPeak[1] = 0.0;
  2314. } // End of Post-processing
  2315. CARLA_PROCESS_CONTINUE_CHECK;
  2316. // --------------------------------------------------------------------------------------------------------
  2317. // Control Output
  2318. if (param.portCout && m_active)
  2319. {
  2320. double value;
  2321. for (k=0; k < param.count; k++)
  2322. {
  2323. if (param.data[k].type == PARAMETER_OUTPUT)
  2324. {
  2325. fixParameterValue(paramBuffers[k], param.ranges[k]);
  2326. if (param.data[k].midiCC > 0)
  2327. {
  2328. value = (paramBuffers[k] - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  2329. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, value);
  2330. }
  2331. }
  2332. }
  2333. } // End of Control Output
  2334. CARLA_PROCESS_CONTINUE_CHECK;
  2335. // --------------------------------------------------------------------------------------------------------
  2336. // Event Output
  2337. if (evOut.count > 0 && m_active)
  2338. {
  2339. atomQueueOut.lock();
  2340. for (i=0; i < evOut.count; i++)
  2341. {
  2342. // midi events need the midi port to send events to
  2343. if ((evOut.data[i].type & CARLA_EVENT_TYPE_MIDI) > 0 && ! evOut.data[i].port)
  2344. continue;
  2345. if (evOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2346. {
  2347. int32_t size = evOut.data[i].atom->atom.size - sizeof(LV2_Atom_Sequence_Body);
  2348. int32_t offset = 0;
  2349. while (offset < size)
  2350. {
  2351. qDebug("output event??, offset:%i, size:%i", offset, size);
  2352. const LV2_Atom_Event* const aev = (LV2_Atom_Event*)((char*)LV2_ATOM_CONTENTS(LV2_Atom_Sequence, evOut.data[i].atom) + offset);
  2353. if ((! aev) || aev->body.type == CARLA_URI_MAP_ID_NULL)
  2354. break;
  2355. qDebug("output event ------------------------------ YES!");
  2356. if (aev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2357. {
  2358. const unsigned char* const data = (unsigned char*)LV2_ATOM_BODY(&aev->body);
  2359. evOut.data[i].port->writeEvent(aev->time.frames, data, aev->body.size);
  2360. }
  2361. else if (aev->body.type == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2362. {
  2363. if (! atomQueueOut.isFull())
  2364. atomQueueOut.put(evOut.data[i].rindex, &aev->body);
  2365. }
  2366. offset += lv2_atom_pad_size(sizeof(LV2_Atom_Event) + aev->body.size);
  2367. }
  2368. }
  2369. else if (evOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2370. {
  2371. const LV2_Event* ev;
  2372. LV2_Event_Iterator iter;
  2373. uint8_t* data;
  2374. lv2_event_begin(&iter, evOut.data[i].event);
  2375. for (k=0; k < iter.buf->event_count; k++)
  2376. {
  2377. ev = lv2_event_get(&iter, &data);
  2378. if (ev && ev->type == CARLA_URI_MAP_ID_MIDI_EVENT && data)
  2379. evOut.data[i].port->writeEvent(ev->frames, data, ev->size);
  2380. lv2_event_increment(&iter);
  2381. }
  2382. }
  2383. else if (evOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2384. {
  2385. LV2_MIDIState state = { evOut.data[i].midi, frames, 0 };
  2386. uint32_t eventSize;
  2387. double eventTime;
  2388. unsigned char* eventData;
  2389. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  2390. {
  2391. evOut.data[i].port->writeEvent(eventTime, eventData, eventSize);
  2392. lv2midi_step(&state);
  2393. }
  2394. }
  2395. }
  2396. atomQueueOut.unlock();
  2397. } // End of Event Output
  2398. CARLA_PROCESS_CONTINUE_CHECK;
  2399. // --------------------------------------------------------------------------------------------------------
  2400. // Peak Values
  2401. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  2402. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  2403. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  2404. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  2405. m_activeBefore = m_active;
  2406. }
  2407. void bufferSizeChanged(const uint32_t newBufferSize)
  2408. {
  2409. lv2Options.bufferSize = newBufferSize;
  2410. }
  2411. // -------------------------------------------------------------------
  2412. // Post-poned events
  2413. void postEventHandleCustom(const int32_t size, const int32_t, const double, const void* const data)
  2414. {
  2415. qDebug("Lv2Plugin::postEventHandleCustom(%i, %p)", size, data);
  2416. Q_ASSERT(ext.worker && ext.worker->work);
  2417. if (ext.worker && ext.worker->work)
  2418. ext.worker->work(handle, carla_lv2_worker_respond, this, size, data);
  2419. }
  2420. void uiParameterChange(const uint32_t index, const double value)
  2421. {
  2422. Q_ASSERT(index < param.count);
  2423. if (index >= param.count)
  2424. return;
  2425. #ifndef BUILD_BRIDGE
  2426. if (gui.type == GUI_EXTERNAL_OSC)
  2427. {
  2428. if (osc.data.target)
  2429. osc_send_control(&osc.data, param.data[index].rindex, value);
  2430. }
  2431. else
  2432. #endif
  2433. {
  2434. if (ui.handle && ui.descriptor && ui.descriptor->port_event)
  2435. {
  2436. float valueF = value;
  2437. ui.descriptor->port_event(ui.handle, param.data[index].rindex, sizeof(float), 0, &valueF);
  2438. }
  2439. }
  2440. }
  2441. void uiMidiProgramChange(const uint32_t index)
  2442. {
  2443. Q_ASSERT(index < midiprog.count);
  2444. if (index >= midiprog.count)
  2445. return;
  2446. #ifndef BUILD_BRIDGE
  2447. if (gui.type == GUI_EXTERNAL_OSC)
  2448. {
  2449. if (osc.data.target)
  2450. osc_send_midi_program(&osc.data, midiprog.data[index].bank, midiprog.data[index].program);
  2451. }
  2452. else
  2453. #endif
  2454. {
  2455. if (ext.uiprograms)
  2456. ext.uiprograms->select_program(ui.handle, midiprog.data[index].bank, midiprog.data[index].program);
  2457. }
  2458. }
  2459. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  2460. {
  2461. Q_ASSERT(channel < 16);
  2462. Q_ASSERT(note < 128);
  2463. Q_ASSERT(velo > 0 && velo < 128);
  2464. #ifndef BUILD_BRIDGE
  2465. if (gui.type == GUI_EXTERNAL_OSC)
  2466. {
  2467. if (osc.data.target)
  2468. {
  2469. uint8_t midiData[4] = { 0 };
  2470. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2471. midiData[2] = note;
  2472. midiData[3] = velo;
  2473. osc_send_midi(&osc.data, midiData);
  2474. }
  2475. }
  2476. else
  2477. #endif
  2478. {
  2479. uiTransferAtom(); // TODO
  2480. }
  2481. }
  2482. void uiNoteOff(const uint8_t channel, const uint8_t note)
  2483. {
  2484. Q_ASSERT(channel < 16);
  2485. Q_ASSERT(note < 128);
  2486. #ifndef BUILD_BRIDGE
  2487. if (gui.type == GUI_EXTERNAL_OSC)
  2488. {
  2489. if (osc.data.target)
  2490. {
  2491. uint8_t midiData[4] = { 0 };
  2492. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2493. midiData[2] = note;
  2494. osc_send_midi(&osc.data, midiData);
  2495. }
  2496. }
  2497. else
  2498. #endif
  2499. {
  2500. uiTransferAtom(); // TODO
  2501. }
  2502. }
  2503. // -------------------------------------------------------------------
  2504. // Cleanup
  2505. void removeClientPorts()
  2506. {
  2507. qDebug("Lv2Plugin::removeClientPorts() - start");
  2508. for (uint32_t i=0; i < evIn.count; i++)
  2509. {
  2510. if (evIn.data[i].port)
  2511. {
  2512. delete evIn.data[i].port;
  2513. evIn.data[i].port = nullptr;
  2514. }
  2515. }
  2516. for (uint32_t i=0; i < evOut.count; i++)
  2517. {
  2518. if (evOut.data[i].port)
  2519. {
  2520. delete evOut.data[i].port;
  2521. evOut.data[i].port = nullptr;
  2522. }
  2523. }
  2524. CarlaPlugin::removeClientPorts();
  2525. qDebug("Lv2Plugin::removeClientPorts() - end");
  2526. }
  2527. void initBuffers()
  2528. {
  2529. uint32_t i;
  2530. for (i=0; i < evIn.count; i++)
  2531. {
  2532. if (evIn.data[i].port)
  2533. evIn.data[i].port->initBuffer(x_engine);
  2534. }
  2535. for (uint32_t i=0; i < evOut.count; i++)
  2536. {
  2537. if (evOut.data[i].port)
  2538. evOut.data[i].port->initBuffer(x_engine);
  2539. }
  2540. CarlaPlugin::initBuffers();
  2541. }
  2542. void deleteBuffers()
  2543. {
  2544. qDebug("Lv2Plugin::deleteBuffers() - start");
  2545. if (evIn.count > 0)
  2546. {
  2547. for (uint32_t i=0; i < evIn.count; i++)
  2548. {
  2549. if (evIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  2550. {
  2551. free(evIn.data[i].atom);
  2552. }
  2553. else if (evIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  2554. {
  2555. free(evIn.data[i].event);
  2556. }
  2557. else if (evIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2558. {
  2559. delete[] evIn.data[i].midi->data;
  2560. delete evIn.data[i].midi;
  2561. }
  2562. }
  2563. delete[] evIn.data;
  2564. }
  2565. if (evOut.count > 0)
  2566. {
  2567. for (uint32_t i=0; i < evOut.count; i++)
  2568. {
  2569. if (evOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  2570. {
  2571. free(evOut.data[i].atom);
  2572. }
  2573. else if (evOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  2574. {
  2575. free(evOut.data[i].event);
  2576. }
  2577. else if (evOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2578. {
  2579. delete[] evOut.data[i].midi->data;
  2580. delete evOut.data[i].midi;
  2581. }
  2582. }
  2583. delete[] evOut.data;
  2584. }
  2585. if (param.count > 0)
  2586. delete[] paramBuffers;
  2587. evIn.count = 0;
  2588. evIn.data = nullptr;
  2589. evOut.count = 0;
  2590. evOut.data = nullptr;
  2591. paramBuffers = nullptr;
  2592. qDebug("Lv2Plugin::deleteBuffers() - end");
  2593. }
  2594. // -------------------------------------------------------------------
  2595. uint32_t getCustomURID(const char* const uri)
  2596. {
  2597. qDebug("Lv2Plugin::getCustomURID(%s)", uri);
  2598. Q_ASSERT(uri);
  2599. if (! uri)
  2600. return CARLA_URI_MAP_ID_NULL;
  2601. for (size_t i=0; i < customURIDs.size(); i++)
  2602. {
  2603. if (customURIDs[i] && strcmp(customURIDs[i], uri) == 0)
  2604. return i;
  2605. }
  2606. customURIDs.push_back(strdup(uri));
  2607. return customURIDs.size()-1;
  2608. }
  2609. const char* getCustomURIString(const LV2_URID urid) const
  2610. {
  2611. qDebug("Lv2Plugin::getCustomURIString(%i)", urid);
  2612. Q_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  2613. if (urid == CARLA_URI_MAP_ID_NULL)
  2614. return nullptr;
  2615. if (urid < customURIDs.size())
  2616. return customURIDs[urid];
  2617. return nullptr;
  2618. }
  2619. // -------------------------------------------------------------------
  2620. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  2621. {
  2622. qDebug("Lv2Plugin::handleAtomTransfer(%i, %p)", portIndex, atom);
  2623. Q_ASSERT(atom);
  2624. // FIXME - is this correct?
  2625. //atomQueueIn.put(portIndex, atom);
  2626. }
  2627. void handleTransferEvent(const uint32_t portIndex, const LV2_Atom* const atom)
  2628. {
  2629. qDebug("Lv2Plugin::handleEventTransfer(%i, %p)", portIndex, atom);
  2630. Q_ASSERT(atom);
  2631. atomQueueIn.put(portIndex, atom);
  2632. }
  2633. // -------------------------------------------------------------------
  2634. void handleProgramChanged(const int32_t index)
  2635. {
  2636. if (index == -1)
  2637. {
  2638. const CarlaPlugin::ScopedDisabler m(this);
  2639. reloadPrograms(false);
  2640. }
  2641. else
  2642. {
  2643. if (index >= 0 && index < (int32_t)prog.count && ext.programs)
  2644. {
  2645. const char* const progName = ext.programs->get_program(handle, index)->name;
  2646. Q_ASSERT(progName);
  2647. if (prog.names[index])
  2648. free((void*)prog.names[index]);
  2649. prog.names[index] = strdup(progName);
  2650. }
  2651. }
  2652. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  2653. }
  2654. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  2655. {
  2656. Q_ASSERT(key > 0);
  2657. Q_ASSERT(value);
  2658. CustomDataType dtype;
  2659. const char* const uriKey = getCustomURIString(key);
  2660. if (type == CARLA_URI_MAP_ID_ATOM_CHUNK)
  2661. dtype = CUSTOM_DATA_CHUNK;
  2662. else if (type == CARLA_URI_MAP_ID_ATOM_PATH)
  2663. dtype = CUSTOM_DATA_PATH;
  2664. else if (type == CARLA_URI_MAP_ID_ATOM_STRING)
  2665. dtype = CUSTOM_DATA_STRING;
  2666. else if (type >= CARLA_URI_MAP_ID_COUNT)
  2667. dtype = CUSTOM_DATA_BINARY;
  2668. else
  2669. dtype = CUSTOM_DATA_INVALID;
  2670. // do basic checks
  2671. if (! uriKey)
  2672. {
  2673. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  2674. return LV2_STATE_ERR_NO_PROPERTY;
  2675. }
  2676. if (! flags & LV2_STATE_IS_POD)
  2677. {
  2678. qWarning("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  2679. return LV2_STATE_ERR_BAD_FLAGS;
  2680. }
  2681. if (dtype == CUSTOM_DATA_INVALID)
  2682. {
  2683. qCritical("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type '%s'", key, value, size, type, flags, CustomDataType2str(dtype));
  2684. return LV2_STATE_ERR_BAD_TYPE;
  2685. }
  2686. // Check if we already have this key
  2687. for (size_t i=0; i < custom.size(); i++)
  2688. {
  2689. if (strcmp(custom[i].key, uriKey) == 0)
  2690. {
  2691. free((void*)custom[i].value);
  2692. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2693. custom[i].value = strdup((const char*)value);
  2694. else
  2695. custom[i].value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2696. return LV2_STATE_SUCCESS;
  2697. }
  2698. }
  2699. // Otherwise store it
  2700. CustomData newData;
  2701. newData.type = dtype;
  2702. newData.key = strdup(uriKey);
  2703. if (dtype == CUSTOM_DATA_STRING || dtype == CUSTOM_DATA_PATH)
  2704. newData.value = strdup((const char*)value);
  2705. else
  2706. newData.value = strdup(QByteArray((const char*)value, size).toBase64().constData());
  2707. custom.push_back(newData);
  2708. return LV2_STATE_SUCCESS;
  2709. }
  2710. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  2711. {
  2712. Q_ASSERT(key > CARLA_URI_MAP_ID_NULL);
  2713. const char* const uriKey = getCustomURIString(key);
  2714. if (! uriKey)
  2715. {
  2716. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  2717. return nullptr;
  2718. }
  2719. const char* stringData = nullptr;
  2720. CustomDataType dtype = CUSTOM_DATA_INVALID;
  2721. for (size_t i=0; i < custom.size(); i++)
  2722. {
  2723. if (strcmp(custom[i].key, uriKey) == 0)
  2724. {
  2725. dtype = custom[i].type;
  2726. stringData = custom[i].value;
  2727. break;
  2728. }
  2729. }
  2730. if (! stringData)
  2731. {
  2732. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  2733. return nullptr;
  2734. }
  2735. *size = 0;
  2736. *type = 0;
  2737. *flags = LV2_STATE_IS_POD;
  2738. if (dtype == CUSTOM_DATA_STRING)
  2739. {
  2740. *size = strlen(stringData);
  2741. *type = CARLA_URI_MAP_ID_ATOM_STRING;
  2742. return stringData;
  2743. }
  2744. if (dtype == CUSTOM_DATA_PATH)
  2745. {
  2746. *size = strlen(stringData);
  2747. *type = CARLA_URI_MAP_ID_ATOM_PATH;
  2748. return stringData;
  2749. }
  2750. if (dtype == CUSTOM_DATA_CHUNK || dtype == CUSTOM_DATA_BINARY)
  2751. {
  2752. static QByteArray chunk;
  2753. chunk = QByteArray::fromBase64(stringData);
  2754. *size = chunk.size();
  2755. *type = (dtype == CUSTOM_DATA_CHUNK) ? CARLA_URI_MAP_ID_ATOM_CHUNK : key;
  2756. return chunk.constData();
  2757. }
  2758. qCritical("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key type '%s'", key, size, type, flags, CustomDataType2str(dtype));
  2759. return nullptr;
  2760. }
  2761. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  2762. {
  2763. if (! ext.worker)
  2764. {
  2765. qWarning("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  2766. return LV2_WORKER_ERR_UNKNOWN;
  2767. }
  2768. if (x_engine->isOffline())
  2769. ext.worker->work(handle, carla_lv2_worker_respond, this, size, data);
  2770. else
  2771. postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  2772. return LV2_WORKER_SUCCESS;
  2773. }
  2774. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  2775. {
  2776. LV2_Atom_Worker workerAtom;
  2777. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  2778. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  2779. workerAtom.body.size = size;
  2780. workerAtom.body.data = data;
  2781. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  2782. return LV2_WORKER_SUCCESS;
  2783. }
  2784. void handleExternalUiClosed()
  2785. {
  2786. if (ui.handle && ui.descriptor && ui.descriptor->cleanup)
  2787. ui.descriptor->cleanup(ui.handle);
  2788. ui.handle = nullptr;
  2789. x_engine->callback(CALLBACK_SHOW_GUI, m_id, 0, 0, 0.0);
  2790. }
  2791. uint32_t handleUiPortMap(const char* const symbol)
  2792. {
  2793. Q_ASSERT(symbol);
  2794. if (! symbol)
  2795. return LV2UI_INVALID_PORT_INDEX;
  2796. for (uint32_t i=0; i < rdf_descriptor->PortCount; i++)
  2797. {
  2798. if (strcmp(rdf_descriptor->Ports[i].Symbol, symbol) == 0)
  2799. return i;
  2800. }
  2801. return LV2UI_INVALID_PORT_INDEX;
  2802. }
  2803. int handleUiResize(const int width, const int height)
  2804. {
  2805. Q_ASSERT(width > 0);
  2806. Q_ASSERT(height > 0);
  2807. if (width <= 0 || height <= 0)
  2808. return 1;
  2809. gui.width = width;
  2810. gui.height = height;
  2811. x_engine->callback(CALLBACK_RESIZE_GUI, m_id, width, height, 0.0);
  2812. return 0;
  2813. }
  2814. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  2815. {
  2816. if (format == 0)
  2817. {
  2818. Q_ASSERT(buffer);
  2819. Q_ASSERT(bufferSize == sizeof(float));
  2820. float value = *(float*)buffer;
  2821. for (uint32_t i=0; i < param.count; i++)
  2822. {
  2823. if (param.data[i].rindex == (int32_t)rindex)
  2824. return setParameterValue(i, value, false, true, true);
  2825. }
  2826. }
  2827. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  2828. {
  2829. Q_ASSERT(buffer);
  2830. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2831. handleTransferAtom(rindex, atom);
  2832. }
  2833. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  2834. {
  2835. Q_ASSERT(buffer);
  2836. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  2837. handleTransferEvent(rindex, atom);
  2838. }
  2839. }
  2840. // -------------------------------------------------------------------
  2841. #ifndef BUILD_BRIDGE
  2842. bool isUiBridgeable(const uint32_t uiId)
  2843. {
  2844. Q_ASSERT(rdf_descriptor);
  2845. Q_ASSERT(uiId < rdf_descriptor->UICount);
  2846. if (uiId >= rdf_descriptor->UICount)
  2847. return false;
  2848. const LV2_RDF_UI* const rdf_ui = &rdf_descriptor->UIs[uiId];
  2849. for (uint32_t i=0; i < rdf_ui->FeatureCount; i++)
  2850. {
  2851. if (strcmp(rdf_ui->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0 || strcmp(rdf_ui->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  2852. return false;
  2853. }
  2854. return true;
  2855. }
  2856. #endif
  2857. bool isUiResizable()
  2858. {
  2859. Q_ASSERT(ui.rdf_descriptor);
  2860. if (! ui.rdf_descriptor)
  2861. return false;
  2862. for (uint32_t i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  2863. {
  2864. if (strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || strcmp(ui.rdf_descriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  2865. return false;
  2866. }
  2867. return true;
  2868. }
  2869. void initExternalUi()
  2870. {
  2871. qDebug("Lv2Plugin::initExternalUi()");
  2872. ui.widget = nullptr;
  2873. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  2874. if (ui.handle && ui.widget)
  2875. {
  2876. updateUi();
  2877. }
  2878. else
  2879. {
  2880. qWarning("Lv2Plugin::initExternalUi() - failed to instantiate UI");
  2881. ui.handle = nullptr;
  2882. ui.widget = nullptr;
  2883. x_engine->callback(CALLBACK_SHOW_GUI, m_id, -1, 0, 0.0);
  2884. }
  2885. }
  2886. void uiTransferAtom()
  2887. {
  2888. // TODO
  2889. }
  2890. void uiTransferEvent(const CustomData* const cdata)
  2891. {
  2892. if (cdata->type == CUSTOM_DATA_INVALID || ! ui.descriptor->port_event)
  2893. return;
  2894. LV2_URID_Map* const URID_Map = (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  2895. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  2896. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  2897. Sratom* const sratom = sratom_new(URID_Map);
  2898. SerdChunk chunk = { nullptr, 0 };
  2899. LV2_Atom_Forge forge;
  2900. lv2_atom_forge_init(&forge, URID_Map);
  2901. lv2_atom_forge_set_sink(&forge, sratom_forge_sink, sratom_forge_deref, &chunk);
  2902. LV2_Atom_Forge_Frame refFrame, bodyFrame;
  2903. LV2_Atom_Forge_Ref ref = lv2_atom_forge_blank(&forge, &refFrame, 1, uridPatchSet);
  2904. lv2_atom_forge_property_head(&forge, uridPatchBody, CARLA_URI_MAP_ID_NULL);
  2905. lv2_atom_forge_blank(&forge, &bodyFrame, 2, CARLA_URI_MAP_ID_NULL);
  2906. lv2_atom_forge_property_head(&forge, getCustomURID(cdata->key), CARLA_URI_MAP_ID_NULL);
  2907. if (cdata->type == CUSTOM_DATA_STRING)
  2908. lv2_atom_forge_string(&forge, cdata->value, strlen(cdata->value));
  2909. else if (cdata->type == CUSTOM_DATA_PATH)
  2910. lv2_atom_forge_path(&forge, cdata->value, strlen(cdata->value));
  2911. else if (cdata->type == CUSTOM_DATA_CHUNK)
  2912. lv2_atom_forge_literal(&forge, cdata->value, strlen(cdata->value), CARLA_URI_MAP_ID_ATOM_CHUNK, CARLA_URI_MAP_ID_NULL);
  2913. else
  2914. lv2_atom_forge_literal(&forge, cdata->value, strlen(cdata->value), getCustomURID(cdata->key), CARLA_URI_MAP_ID_NULL);
  2915. lv2_atom_forge_pop(&forge, &bodyFrame);
  2916. lv2_atom_forge_pop(&forge, &refFrame);
  2917. uint32_t portIndex = evIn.count > 0 ? evIn.data[0].rindex : 0;
  2918. const LV2_Atom* const atom = lv2_atom_forge_deref(&forge, ref);
  2919. ui.descriptor->port_event(ui.handle, portIndex, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  2920. free((void*)chunk.buf);
  2921. sratom_free(sratom);
  2922. }
  2923. void updateUi()
  2924. {
  2925. ext.uiprograms = nullptr;
  2926. if (ui.handle && ui.descriptor)
  2927. {
  2928. if (ui.descriptor->extension_data)
  2929. {
  2930. ext.uiprograms = (const LV2_Programs_UI_Interface*)ui.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  2931. if (ext.uiprograms && ! ext.uiprograms->select_program)
  2932. // invalid
  2933. ext.uiprograms = nullptr;
  2934. if (ext.uiprograms && midiprog.count > 0 && midiprog.current >= 0)
  2935. ext.uiprograms->select_program(ui.handle, midiprog.data[midiprog.current].bank, midiprog.data[midiprog.current].program);
  2936. }
  2937. if (ui.descriptor->port_event)
  2938. {
  2939. // update control ports
  2940. float valueF;
  2941. for (uint32_t i=0; i < param.count; i++)
  2942. {
  2943. valueF = getParameterValue(i);
  2944. ui.descriptor->port_event(ui.handle, param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &valueF);
  2945. }
  2946. }
  2947. }
  2948. }
  2949. // ----------------- Event Feature ---------------------------------------------------
  2950. static uint32_t carla_lv2_event_ref(const LV2_Event_Callback_Data callback_data, LV2_Event* const event)
  2951. {
  2952. qDebug("Lv2Plugin::carla_lv2_event_ref(%p, %p)", callback_data, event);
  2953. Q_ASSERT(callback_data);
  2954. Q_ASSERT(event);
  2955. return 0;
  2956. }
  2957. static uint32_t carla_lv2_event_unref(const LV2_Event_Callback_Data callback_data, LV2_Event* const event)
  2958. {
  2959. qDebug("Lv2Plugin::carla_lv2_event_unref(%p, %p)", callback_data, event);
  2960. Q_ASSERT(callback_data);
  2961. Q_ASSERT(event);
  2962. return 0;
  2963. }
  2964. // ----------------- Logs Feature ----------------------------------------------------
  2965. static int carla_lv2_log_printf(const LV2_Log_Handle handle, const LV2_URID type, const char* const fmt, ...)
  2966. {
  2967. qDebug("Lv2Plugin::carla_lv2_log_printf(%p, %i, \"%s\", ...)", handle, type, fmt);
  2968. Q_ASSERT(handle);
  2969. Q_ASSERT(type > 0);
  2970. #ifndef DEBUG
  2971. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2972. return 0;
  2973. #endif
  2974. va_list args;
  2975. va_start(args, fmt);
  2976. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  2977. va_end(args);
  2978. return ret;
  2979. }
  2980. static int carla_lv2_log_vprintf(const LV2_Log_Handle handle, const LV2_URID type, const char* const fmt, va_list ap)
  2981. {
  2982. qDebug("Lv2Plugin::carla_lv2_log_vprintf(%p, %i, \"%s\", ...)", handle, type, fmt);
  2983. Q_ASSERT(handle);
  2984. Q_ASSERT(type > 0);
  2985. #ifndef DEBUG
  2986. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  2987. return 0;
  2988. #endif
  2989. char buf[8196];
  2990. vsprintf(buf, fmt, ap);
  2991. if (*buf == 0)
  2992. return 0;
  2993. switch (type)
  2994. {
  2995. case CARLA_URI_MAP_ID_LOG_ERROR:
  2996. qCritical("%s", buf);
  2997. break;
  2998. case CARLA_URI_MAP_ID_LOG_NOTE:
  2999. printf("%s\n", buf);
  3000. break;
  3001. case CARLA_URI_MAP_ID_LOG_TRACE:
  3002. qDebug("%s", buf);
  3003. break;
  3004. case CARLA_URI_MAP_ID_LOG_WARNING:
  3005. qWarning("%s", buf);
  3006. break;
  3007. default:
  3008. break;
  3009. }
  3010. return strlen(buf);
  3011. }
  3012. // ----------------- Programs Feature ------------------------------------------------
  3013. static void carla_lv2_program_changed(const LV2_Programs_Handle handle, const int32_t index)
  3014. {
  3015. qDebug("Lv2Plugin::carla_lv2_program_changed(%p, %i)", handle, index);
  3016. Q_ASSERT(handle);
  3017. if (! handle)
  3018. return;
  3019. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3020. plugin->handleProgramChanged(index);
  3021. }
  3022. // ----------------- State Feature ---------------------------------------------------
  3023. static char* carla_lv2_state_make_path(const LV2_State_Make_Path_Handle handle, const char* const path)
  3024. {
  3025. qDebug("Lv2Plugin::carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  3026. Q_ASSERT(handle);
  3027. Q_ASSERT(path);
  3028. if (! path)
  3029. return nullptr;
  3030. QDir dir;
  3031. dir.mkpath(path);
  3032. return strdup(path);
  3033. }
  3034. static char* carla_lv2_state_map_abstract_path(const LV2_State_Map_Path_Handle handle, const char* const absolute_path)
  3035. {
  3036. qDebug("Lv2Plugin::carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  3037. Q_ASSERT(handle);
  3038. Q_ASSERT(absolute_path);
  3039. if (! absolute_path)
  3040. return nullptr;
  3041. QDir dir(absolute_path);
  3042. return strdup(dir.canonicalPath().toUtf8().constData());
  3043. }
  3044. static char* carla_lv2_state_map_absolute_path(const LV2_State_Map_Path_Handle handle, const char* const abstract_path)
  3045. {
  3046. qDebug("Lv2Plugin::carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  3047. Q_ASSERT(handle);
  3048. Q_ASSERT(abstract_path);
  3049. if (! abstract_path)
  3050. return nullptr;
  3051. QDir dir(abstract_path);
  3052. return strdup(dir.absolutePath().toUtf8().constData());
  3053. }
  3054. 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)
  3055. {
  3056. qDebug("Lv2Plugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  3057. Q_ASSERT(handle);
  3058. if (! handle)
  3059. return LV2_STATE_ERR_UNKNOWN;
  3060. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3061. return plugin->handleStateStore(key, value, size, type, flags);
  3062. }
  3063. 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)
  3064. {
  3065. qDebug("Lv2Plugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  3066. Q_ASSERT(handle);
  3067. if (! handle)
  3068. return nullptr;
  3069. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3070. return plugin->handleStateRetrieve(key, size, type, flags);
  3071. }
  3072. // ----------------- URI-Map Feature -------------------------------------------------
  3073. static uint32_t carla_lv2_uri_to_id(const LV2_URI_Map_Callback_Data data, const char* const map, const char* const uri)
  3074. {
  3075. qDebug("Lv2Plugin::carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  3076. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  3077. }
  3078. // ----------------- URID Feature ----------------------------------------------------
  3079. static LV2_URID carla_lv2_urid_map(const LV2_URID_Map_Handle handle, const char* const uri)
  3080. {
  3081. qDebug("Lv2Plugin::carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  3082. Q_ASSERT(handle);
  3083. Q_ASSERT(uri);
  3084. if (! uri)
  3085. return CARLA_URI_MAP_ID_NULL;
  3086. // Atom types
  3087. if (strcmp(uri, LV2_ATOM__Chunk) == 0)
  3088. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  3089. if (strcmp(uri, LV2_ATOM__Double) == 0)
  3090. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  3091. if (strcmp(uri, LV2_ATOM__Int) == 0)
  3092. return CARLA_URI_MAP_ID_ATOM_INT;
  3093. if (strcmp(uri, LV2_ATOM__Path) == 0)
  3094. return CARLA_URI_MAP_ID_ATOM_PATH;
  3095. if (strcmp(uri, LV2_ATOM__Sequence) == 0)
  3096. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  3097. if (strcmp(uri, LV2_ATOM__String) == 0)
  3098. return CARLA_URI_MAP_ID_ATOM_STRING;
  3099. if (strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  3100. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  3101. if (strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  3102. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  3103. // BufSize types
  3104. if (strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  3105. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  3106. if (strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  3107. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  3108. if (strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  3109. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  3110. // Log types
  3111. if (strcmp(uri, LV2_LOG__Error) == 0)
  3112. return CARLA_URI_MAP_ID_LOG_ERROR;
  3113. if (strcmp(uri, LV2_LOG__Note) == 0)
  3114. return CARLA_URI_MAP_ID_LOG_NOTE;
  3115. if (strcmp(uri, LV2_LOG__Trace) == 0)
  3116. return CARLA_URI_MAP_ID_LOG_TRACE;
  3117. if (strcmp(uri, LV2_LOG__Warning) == 0)
  3118. return CARLA_URI_MAP_ID_LOG_WARNING;
  3119. // Others
  3120. if (strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  3121. return CARLA_URI_MAP_ID_MIDI_EVENT;
  3122. if (strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  3123. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  3124. if (! handle)
  3125. return CARLA_URI_MAP_ID_NULL;
  3126. // Custom types
  3127. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3128. return plugin->getCustomURID(uri);
  3129. }
  3130. static const char* carla_lv2_urid_unmap(const LV2_URID_Map_Handle handle, const LV2_URID urid)
  3131. {
  3132. qDebug("Lv2Plugin::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  3133. Q_ASSERT(handle);
  3134. Q_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  3135. if (urid == CARLA_URI_MAP_ID_NULL)
  3136. return nullptr;
  3137. // Atom types
  3138. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  3139. return LV2_ATOM__Chunk;
  3140. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  3141. return LV2_ATOM__Double;
  3142. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  3143. return LV2_ATOM__Int;
  3144. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  3145. return LV2_ATOM__Path;
  3146. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  3147. return LV2_ATOM__Sequence;
  3148. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  3149. return LV2_ATOM__String;
  3150. if (urid == CARLA_URI_MAP_ID_ATOM_WORKER)
  3151. return nullptr; // not a valid URID, only used internally
  3152. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  3153. return LV2_ATOM__atomTransfer;
  3154. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3155. return LV2_ATOM__eventTransfer;
  3156. // BufSize types
  3157. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  3158. return LV2_BUF_SIZE__maxBlockLength;
  3159. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  3160. return LV2_BUF_SIZE__minBlockLength;
  3161. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  3162. return LV2_BUF_SIZE__sequenceSize;
  3163. // Log types
  3164. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  3165. return LV2_LOG__Error;
  3166. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  3167. return LV2_LOG__Note;
  3168. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  3169. return LV2_LOG__Trace;
  3170. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  3171. return LV2_LOG__Warning;
  3172. // Others
  3173. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  3174. return LV2_MIDI__MidiEvent;
  3175. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  3176. return LV2_PARAMETERS__sampleRate;
  3177. if (! handle)
  3178. return nullptr;
  3179. // Custom types
  3180. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3181. return plugin->getCustomURIString(urid);
  3182. }
  3183. // ----------------- Worker Feature --------------------------------------------------
  3184. static LV2_Worker_Status carla_lv2_worker_schedule(const LV2_Worker_Schedule_Handle handle, const uint32_t size, const void* const data)
  3185. {
  3186. qDebug("Lv2Plugin::carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  3187. Q_ASSERT(handle);
  3188. if (! handle)
  3189. return LV2_WORKER_ERR_UNKNOWN;
  3190. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3191. return plugin->handleWorkerSchedule(size, data);
  3192. }
  3193. static LV2_Worker_Status carla_lv2_worker_respond(const LV2_Worker_Respond_Handle handle, const uint32_t size, const void* const data)
  3194. {
  3195. qDebug("Lv2Plugin::carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  3196. Q_ASSERT(handle);
  3197. if (! handle)
  3198. return LV2_WORKER_ERR_UNKNOWN;
  3199. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3200. return plugin->handleWorkerRespond(size, data);
  3201. }
  3202. // ----------------- UI Port-Map Feature ---------------------------------------------
  3203. static uint32_t carla_lv2_ui_port_map(const LV2UI_Feature_Handle handle, const char* const symbol)
  3204. {
  3205. qDebug("Lv2Plugin::carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  3206. Q_ASSERT(handle);
  3207. if (! handle)
  3208. return LV2UI_INVALID_PORT_INDEX;
  3209. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3210. return plugin->handleUiPortMap(symbol);
  3211. }
  3212. // ----------------- UI Resize Feature -----------------------------------------------
  3213. static int carla_lv2_ui_resize(const LV2UI_Feature_Handle handle, const int width, const int height)
  3214. {
  3215. qDebug("Lv2Plugin::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  3216. Q_ASSERT(handle);
  3217. if (! handle)
  3218. return 1;
  3219. Lv2Plugin* const plugin = (Lv2Plugin*)handle;
  3220. return plugin->handleUiResize(width, height);
  3221. }
  3222. // ----------------- External UI Feature ---------------------------------------------
  3223. static void carla_lv2_external_ui_closed(const LV2UI_Controller controller)
  3224. {
  3225. qDebug("Lv2Plugin::carla_lv2_external_ui_closed(%p)", controller);
  3226. Q_ASSERT(controller);
  3227. if (! controller)
  3228. return;
  3229. Lv2Plugin* const plugin = (Lv2Plugin*)controller;
  3230. plugin->handleExternalUiClosed();
  3231. }
  3232. // ----------------- UI Extension ----------------------------------------------------
  3233. 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)
  3234. {
  3235. qDebug("Lv2Plugin::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  3236. Q_ASSERT(controller);
  3237. if (! controller)
  3238. return;
  3239. Lv2Plugin* const plugin = (Lv2Plugin*)controller;
  3240. plugin->handleUiWrite(port_index, buffer_size, format, buffer);
  3241. }
  3242. // -------------------------------------------------------------------
  3243. bool uiLibOpen(const char* const filename)
  3244. {
  3245. ui.lib = lib_open(filename);
  3246. return bool(ui.lib);
  3247. }
  3248. bool uiLibClose()
  3249. {
  3250. if (ui.lib)
  3251. return lib_close(ui.lib);
  3252. return false;
  3253. }
  3254. void* uiLibSymbol(const char* const symbol)
  3255. {
  3256. if (ui.lib)
  3257. return lib_symbol(ui.lib, symbol);
  3258. return nullptr;
  3259. }
  3260. // -------------------------------------------------------------------
  3261. bool init(const char* const bundle, const char* const name, const char* const URI)
  3262. {
  3263. // ---------------------------------------------------------------
  3264. // get plugin from lv2_rdf (lilv)
  3265. rdf_descriptor = lv2_rdf_new(URI);
  3266. if (! rdf_descriptor)
  3267. {
  3268. setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3269. return false;
  3270. }
  3271. // ---------------------------------------------------------------
  3272. // open DLL
  3273. if (! libOpen(rdf_descriptor->Binary))
  3274. {
  3275. setLastError(libError(rdf_descriptor->Binary));
  3276. return false;
  3277. }
  3278. // ---------------------------------------------------------------
  3279. // get DLL main entry
  3280. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)libSymbol("lv2_descriptor");
  3281. if (! descFn)
  3282. {
  3283. setLastError("Could not find the LV2 Descriptor in the plugin library");
  3284. return false;
  3285. }
  3286. // ---------------------------------------------------------------
  3287. // get descriptor that matches URI
  3288. uint32_t i = 0;
  3289. while ((descriptor = descFn(i++)))
  3290. {
  3291. if (strcmp(descriptor->URI, URI) == 0)
  3292. break;
  3293. }
  3294. if (! descriptor)
  3295. {
  3296. setLastError("Could not find the requested plugin URI in the plugin library");
  3297. return false;
  3298. }
  3299. // ---------------------------------------------------------------
  3300. // check supported port-types and features
  3301. bool canContinue = true;
  3302. // Check supported ports
  3303. for (i=0; i < rdf_descriptor->PortCount; i++)
  3304. {
  3305. LV2_Property PortType = rdf_descriptor->Ports[i].Type;
  3306. 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)))
  3307. {
  3308. if (! LV2_IS_PORT_OPTIONAL(rdf_descriptor->Ports[i].Properties))
  3309. {
  3310. setLastError("Plugin requires a port that is not currently supported");
  3311. canContinue = false;
  3312. break;
  3313. }
  3314. }
  3315. }
  3316. // Check supported features
  3317. for (i=0; i < rdf_descriptor->FeatureCount && canContinue; i++)
  3318. {
  3319. if (LV2_IS_FEATURE_REQUIRED(rdf_descriptor->Features[i].Type) && ! is_lv2_feature_supported(rdf_descriptor->Features[i].URI))
  3320. {
  3321. QString msg = QString("Plugin requires a feature that is not supported:\n%1").arg(rdf_descriptor->Features[i].URI);
  3322. setLastError(msg.toUtf8().constData());
  3323. canContinue = false;
  3324. break;
  3325. }
  3326. }
  3327. // Check extensions
  3328. for (i=0; i < rdf_descriptor->ExtensionCount; i++)
  3329. {
  3330. if (strcmp(rdf_descriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3331. m_hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3332. else if (strcmp(rdf_descriptor->Extensions[i], LV2_STATE__interface) == 0)
  3333. m_hints |= PLUGIN_HAS_EXTENSION_STATE;
  3334. else if (strcmp(rdf_descriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3335. m_hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3336. else
  3337. qDebug("Plugin has non-supported extension: '%s'", rdf_descriptor->Extensions[i]);
  3338. }
  3339. if (! canContinue)
  3340. {
  3341. // error already set
  3342. return false;
  3343. }
  3344. // ---------------------------------------------------------------
  3345. // initialize features
  3346. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3347. eventFt->callback_data = this;
  3348. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3349. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3350. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3351. logFt->handle = this;
  3352. logFt->printf = carla_lv2_log_printf;
  3353. logFt->vprintf = carla_lv2_log_vprintf;
  3354. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3355. rtmempool_allocator_init(rtMemPoolFt);
  3356. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3357. programsFt->handle = this;
  3358. programsFt->program_changed = carla_lv2_program_changed;
  3359. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3360. stateMakePathFt->handle = this;
  3361. stateMakePathFt->path = carla_lv2_state_make_path;
  3362. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3363. stateMapPathFt->handle = this;
  3364. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3365. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3366. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3367. uriMapFt->callback_data = this;
  3368. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3369. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3370. uridMapFt->handle = this;
  3371. uridMapFt->map = carla_lv2_urid_map;
  3372. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3373. uridUnmapFt->handle = this;
  3374. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3375. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3376. workerFt->handle = this;
  3377. workerFt->schedule_work = carla_lv2_worker_schedule;
  3378. LV2_Options_Option* const optionsFt = new LV2_Options_Option [5];
  3379. optionsFt[0] = lv2Options.oMaxBlockLenth;
  3380. optionsFt[1] = lv2Options.oMinBlockLenth;
  3381. optionsFt[2] = lv2Options.oSequenceSize;
  3382. optionsFt[3] = lv2Options.oSampleRate;
  3383. optionsFt[4] = lv2Options.oNull;
  3384. features[lv2_feature_id_bufsize_bounded] = new LV2_Feature;
  3385. features[lv2_feature_id_bufsize_bounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3386. features[lv2_feature_id_bufsize_bounded]->data = nullptr;
  3387. #ifndef BUILD_BRIDGE
  3388. if (carlaOptions.processHighPrecision)
  3389. {
  3390. features[lv2_feature_id_bufsize_fixed] = new LV2_Feature;
  3391. features[lv2_feature_id_bufsize_fixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3392. features[lv2_feature_id_bufsize_fixed]->data = nullptr;
  3393. features[lv2_feature_id_bufsize_powerof2] = new LV2_Feature;
  3394. features[lv2_feature_id_bufsize_powerof2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3395. features[lv2_feature_id_bufsize_powerof2]->data = nullptr;
  3396. }
  3397. else
  3398. #endif
  3399. {
  3400. // fake, used to keep a valid array
  3401. features[lv2_feature_id_bufsize_fixed] = features[lv2_feature_id_bufsize_bounded];
  3402. features[lv2_feature_id_bufsize_powerof2] = features[lv2_feature_id_bufsize_bounded];
  3403. }
  3404. features[lv2_feature_id_event] = new LV2_Feature;
  3405. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  3406. features[lv2_feature_id_event]->data = eventFt;
  3407. features[lv2_feature_id_logs] = new LV2_Feature;
  3408. features[lv2_feature_id_logs]->URI = LV2_LOG__log;
  3409. features[lv2_feature_id_logs]->data = logFt;
  3410. features[lv2_feature_id_options] = new LV2_Feature;
  3411. features[lv2_feature_id_options]->URI = LV2_OPTIONS__options;
  3412. features[lv2_feature_id_options]->data = optionsFt;
  3413. features[lv2_feature_id_programs] = new LV2_Feature;
  3414. features[lv2_feature_id_programs]->URI = LV2_PROGRAMS__Host;
  3415. features[lv2_feature_id_programs]->data = programsFt;
  3416. features[lv2_feature_id_rtmempool] = new LV2_Feature;
  3417. features[lv2_feature_id_rtmempool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3418. features[lv2_feature_id_rtmempool]->data = rtMemPoolFt;
  3419. features[lv2_feature_id_state_make_path] = new LV2_Feature;
  3420. features[lv2_feature_id_state_make_path]->URI = LV2_STATE__makePath;
  3421. features[lv2_feature_id_state_make_path]->data = stateMakePathFt;
  3422. features[lv2_feature_id_state_map_path] = new LV2_Feature;
  3423. features[lv2_feature_id_state_map_path]->URI = LV2_STATE__mapPath;
  3424. features[lv2_feature_id_state_map_path]->data = stateMapPathFt;
  3425. features[lv2_feature_id_strict_bounds] = new LV2_Feature;
  3426. features[lv2_feature_id_strict_bounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3427. features[lv2_feature_id_strict_bounds]->data = nullptr;
  3428. features[lv2_feature_id_uri_map] = new LV2_Feature;
  3429. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  3430. features[lv2_feature_id_uri_map]->data = uriMapFt;
  3431. features[lv2_feature_id_urid_map] = new LV2_Feature;
  3432. features[lv2_feature_id_urid_map]->URI = LV2_URID__map;
  3433. features[lv2_feature_id_urid_map]->data = uridMapFt;
  3434. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  3435. features[lv2_feature_id_urid_unmap]->URI = LV2_URID__unmap;
  3436. features[lv2_feature_id_urid_unmap]->data = uridUnmapFt;
  3437. features[lv2_feature_id_worker] = new LV2_Feature;
  3438. features[lv2_feature_id_worker]->URI = LV2_WORKER__schedule;
  3439. features[lv2_feature_id_worker]->data = workerFt;
  3440. // ---------------------------------------------------------------
  3441. // initialize plugin
  3442. handle = descriptor->instantiate(descriptor, x_engine->getSampleRate(), rdf_descriptor->Bundle, features);
  3443. if (! handle)
  3444. {
  3445. setLastError("Plugin failed to initialize");
  3446. return false;
  3447. }
  3448. // ---------------------------------------------------------------
  3449. // get info
  3450. m_filename = strdup(bundle);
  3451. if (name)
  3452. m_name = x_engine->getUniqueName(name);
  3453. else
  3454. m_name = x_engine->getUniqueName(rdf_descriptor->Name);
  3455. // ---------------------------------------------------------------
  3456. // register client
  3457. x_client = x_engine->addClient(this);
  3458. if (! x_client->isOk())
  3459. {
  3460. setLastError("Failed to register plugin client");
  3461. return false;
  3462. }
  3463. // ---------------------------------------------------------------
  3464. // gui stuff
  3465. if (rdf_descriptor->UICount == 0)
  3466. return true;
  3467. // -----------------------------------------------------------
  3468. // find more appropriate ui
  3469. int eQt4, eCocoa, eHWND, eX11, eGtk2, eGtk3, iCocoa, iHWND, iX11, iQt4, iExt, iSuil, iFinal;
  3470. eQt4 = eCocoa = eHWND = eX11 = eGtk2 = eGtk3 = iQt4 = iCocoa = iHWND = iX11 = iExt = iSuil = iFinal = -1;
  3471. for (i=0; i < rdf_descriptor->UICount; i++)
  3472. {
  3473. switch (rdf_descriptor->UIs[i].Type)
  3474. {
  3475. case LV2_UI_QT4:
  3476. #ifndef BUILD_BRIDGE
  3477. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3478. eQt4 = i;
  3479. #endif
  3480. iQt4 = i;
  3481. break;
  3482. case LV2_UI_COCOA:
  3483. #ifndef BUILD_BRIDGE
  3484. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3485. eCocoa = i;
  3486. #endif
  3487. iCocoa = i;
  3488. break;
  3489. case LV2_UI_WINDOWS:
  3490. #ifndef BUILD_BRIDGE
  3491. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3492. eHWND = i;
  3493. #endif
  3494. iHWND = i;
  3495. break;
  3496. case LV2_UI_X11:
  3497. #ifndef BUILD_BRIDGE
  3498. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3499. eX11 = i;
  3500. #endif
  3501. iX11 = i;
  3502. break;
  3503. case LV2_UI_GTK2:
  3504. #ifdef BUILD_BRIDGE
  3505. if (false)
  3506. #else
  3507. # ifdef HAVE_SUIL
  3508. if (isUiBridgeable(i) && carlaOptions.preferUiBridges)
  3509. # else
  3510. if (isUiBridgeable(i))
  3511. # endif
  3512. #endif
  3513. eGtk2 = i;
  3514. #ifdef HAVE_SUIL
  3515. iSuil = i;
  3516. #endif
  3517. break;
  3518. #ifndef BUILD_BRIDGE
  3519. case LV2_UI_GTK3:
  3520. if (isUiBridgeable(i))
  3521. eGtk3 = i;
  3522. break;
  3523. #endif
  3524. case LV2_UI_EXTERNAL:
  3525. case LV2_UI_OLD_EXTERNAL:
  3526. iExt = i;
  3527. break;
  3528. default:
  3529. break;
  3530. }
  3531. }
  3532. if (eQt4 >= 0)
  3533. iFinal = eQt4;
  3534. else if (eCocoa >= 0)
  3535. iFinal = eCocoa;
  3536. else if (eHWND >= 0)
  3537. iFinal = eHWND;
  3538. else if (eX11 >= 0)
  3539. iFinal = eX11;
  3540. else if (eGtk2 >= 0)
  3541. iFinal = eGtk2;
  3542. else if (eGtk3 >= 0)
  3543. iFinal = eGtk3;
  3544. else if (iQt4 >= 0)
  3545. iFinal = iQt4;
  3546. else if (iCocoa >= 0)
  3547. iFinal = iCocoa;
  3548. else if (iHWND >= 0)
  3549. iFinal = iHWND;
  3550. else if (iX11 >= 0)
  3551. iFinal = iX11;
  3552. else if (iExt >= 0)
  3553. iFinal = iExt;
  3554. else if (iSuil >= 0)
  3555. iFinal = iSuil;
  3556. #ifndef BUILD_BRIDGE
  3557. const bool isBridged = (iFinal == eQt4 || iFinal == eCocoa || iFinal == eHWND || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3);
  3558. #endif
  3559. #ifdef HAVE_SUIL
  3560. const bool isSuil = (iFinal == iSuil && !isBridged);
  3561. #endif
  3562. if (iFinal < 0)
  3563. {
  3564. qWarning("Failed to find an appropriate LV2 UI for this plugin");
  3565. return true;
  3566. }
  3567. ui.rdf_descriptor = &rdf_descriptor->UIs[iFinal];
  3568. // -----------------------------------------------------------
  3569. // check supported ui features
  3570. canContinue = true;
  3571. for (i=0; i < ui.rdf_descriptor->FeatureCount; i++)
  3572. {
  3573. if (LV2_IS_FEATURE_REQUIRED(ui.rdf_descriptor->Features[i].Type) && is_lv2_ui_feature_supported(ui.rdf_descriptor->Features[i].URI) == false)
  3574. {
  3575. qCritical("Plugin UI requires a feature that is not supported:\n%s", ui.rdf_descriptor->Features[i].URI);
  3576. canContinue = false;
  3577. break;
  3578. }
  3579. }
  3580. if (! canContinue)
  3581. {
  3582. ui.rdf_descriptor = nullptr;
  3583. return true;
  3584. }
  3585. #ifdef HAVE_SUIL
  3586. if (isSuil)
  3587. {
  3588. // -------------------------------------------------------
  3589. // init suil host
  3590. suil.host = suil_host_new(carla_lv2_ui_write_function, carla_lv2_ui_port_map, nullptr, nullptr);
  3591. }
  3592. else
  3593. #endif
  3594. {
  3595. // -------------------------------------------------------
  3596. // open DLL
  3597. if (! uiLibOpen(ui.rdf_descriptor->Binary))
  3598. {
  3599. qCritical("Could not load UI library, error was:\n%s", libError(ui.rdf_descriptor->Binary));
  3600. ui.rdf_descriptor = nullptr;
  3601. return true;
  3602. }
  3603. // -------------------------------------------------------
  3604. // get DLL main entry
  3605. LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor");
  3606. if (! ui_descFn)
  3607. {
  3608. qCritical("Could not find the LV2UI Descriptor in the UI library");
  3609. uiLibClose();
  3610. ui.lib = nullptr;
  3611. ui.rdf_descriptor = nullptr;
  3612. return true;
  3613. }
  3614. // -------------------------------------------------------
  3615. // get descriptor that matches URI
  3616. i = 0;
  3617. while ((ui.descriptor = ui_descFn(i++)))
  3618. {
  3619. if (strcmp(ui.descriptor->URI, ui.rdf_descriptor->URI) == 0)
  3620. break;
  3621. }
  3622. if (! ui.descriptor)
  3623. {
  3624. qCritical("Could not find the requested GUI in the plugin UI library");
  3625. uiLibClose();
  3626. ui.lib = nullptr;
  3627. ui.rdf_descriptor = nullptr;
  3628. return true;
  3629. }
  3630. }
  3631. // -----------------------------------------------------------
  3632. // initialize ui according to type
  3633. const LV2_Property uiType = ui.rdf_descriptor->Type;
  3634. #ifndef BUILD_BRIDGE
  3635. if (isBridged)
  3636. {
  3637. // -------------------------------------------------------
  3638. // initialize ui bridge
  3639. if (const char* const oscBinary = lv2bridge2str(uiType))
  3640. {
  3641. gui.type = GUI_EXTERNAL_OSC;
  3642. osc.thread = new CarlaPluginThread(x_engine, this, CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  3643. osc.thread->setOscData(oscBinary, descriptor->URI, ui.descriptor->URI);
  3644. }
  3645. }
  3646. else
  3647. #endif
  3648. {
  3649. // -------------------------------------------------------
  3650. // initialize ui features
  3651. QString guiTitle = QString("%1 (GUI)").arg(m_name);
  3652. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3653. uiDataFt->data_access = descriptor->extension_data;
  3654. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3655. uiPortMapFt->handle = this;
  3656. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3657. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3658. uiResizeFt->handle = this;
  3659. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3660. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3661. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3662. uiExternalHostFt->plugin_human_id = strdup(guiTitle.toUtf8().constData());
  3663. features[lv2_feature_id_data_access] = new LV2_Feature;
  3664. features[lv2_feature_id_data_access]->URI = LV2_DATA_ACCESS_URI;
  3665. features[lv2_feature_id_data_access]->data = uiDataFt;
  3666. features[lv2_feature_id_instance_access] = new LV2_Feature;
  3667. features[lv2_feature_id_instance_access]->URI = LV2_INSTANCE_ACCESS_URI;
  3668. features[lv2_feature_id_instance_access]->data = handle;
  3669. features[lv2_feature_id_ui_parent] = new LV2_Feature;
  3670. features[lv2_feature_id_ui_parent]->URI = LV2_UI__parent;
  3671. features[lv2_feature_id_ui_parent]->data = nullptr;
  3672. features[lv2_feature_id_ui_port_map] = new LV2_Feature;
  3673. features[lv2_feature_id_ui_port_map]->URI = LV2_UI__portMap;
  3674. features[lv2_feature_id_ui_port_map]->data = uiPortMapFt;
  3675. features[lv2_feature_id_ui_resize] = new LV2_Feature;
  3676. features[lv2_feature_id_ui_resize]->URI = LV2_UI__resize;
  3677. features[lv2_feature_id_ui_resize]->data = uiResizeFt;
  3678. features[lv2_feature_id_external_ui] = new LV2_Feature;
  3679. features[lv2_feature_id_external_ui]->URI = LV2_EXTERNAL_UI__Host;
  3680. features[lv2_feature_id_external_ui]->data = uiExternalHostFt;
  3681. features[lv2_feature_id_external_ui_old] = new LV2_Feature;
  3682. features[lv2_feature_id_external_ui_old]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3683. features[lv2_feature_id_external_ui_old]->data = uiExternalHostFt;
  3684. // -------------------------------------------------------
  3685. // initialize ui
  3686. switch (uiType)
  3687. {
  3688. case LV2_UI_QT4:
  3689. qDebug("Will use LV2 Qt4 UI");
  3690. gui.type = GUI_INTERNAL_QT4;
  3691. gui.resizable = isUiResizable();
  3692. ui.handle = ui.descriptor->instantiate(ui.descriptor, descriptor->URI, ui.rdf_descriptor->Bundle, carla_lv2_ui_write_function, this, &ui.widget, features);
  3693. m_hints |= PLUGIN_USES_SINGLE_THREAD;
  3694. break;
  3695. case LV2_UI_COCOA:
  3696. qDebug("Will use LV2 Cocoa UI");
  3697. gui.type = GUI_INTERNAL_COCOA;
  3698. gui.resizable = isUiResizable();
  3699. break;
  3700. case LV2_UI_WINDOWS:
  3701. qDebug("Will use LV2 Windows UI");
  3702. gui.type = GUI_INTERNAL_HWND;
  3703. gui.resizable = isUiResizable();
  3704. break;
  3705. case LV2_UI_X11:
  3706. qDebug("Will use LV2 X11 UI");
  3707. gui.type = GUI_INTERNAL_X11;
  3708. gui.resizable = isUiResizable();
  3709. break;
  3710. case LV2_UI_GTK2:
  3711. #ifdef HAVE_SUIL
  3712. qDebug("Will use LV2 Gtk2 UI (suil)");
  3713. gui.type = GUI_EXTERNAL_SUIL;
  3714. gui.resizable = isUiResizable();
  3715. 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);
  3716. m_hints |= PLUGIN_USES_SINGLE_THREAD;
  3717. if (suil.handle)
  3718. {
  3719. ui.handle = ((SuilInstanceImpl*)suil.handle)->handle;
  3720. ui.descriptor = ((SuilInstanceImpl*)suil.handle)->descriptor;
  3721. ui.widget = suil_instance_get_widget(suil.handle);
  3722. if (ui.widget)
  3723. {
  3724. QWidget* const widget = (QWidget*)ui.widget;
  3725. widget->setWindowTitle(guiTitle);
  3726. }
  3727. }
  3728. #else
  3729. qDebug("Will use LV2 Gtk2 UI, NOT!");
  3730. #endif
  3731. break;
  3732. case LV2_UI_GTK3:
  3733. qDebug("Will use LV2 Gtk3 UI, NOT!");
  3734. break;
  3735. case LV2_UI_EXTERNAL:
  3736. case LV2_UI_OLD_EXTERNAL:
  3737. qDebug("Will use LV2 External UI");
  3738. gui.type = GUI_EXTERNAL_LV2;
  3739. break;
  3740. }
  3741. }
  3742. if (gui.type != GUI_NONE)
  3743. m_hints |= PLUGIN_HAS_GUI;
  3744. return true;
  3745. }
  3746. private:
  3747. LV2_Handle handle, h2;
  3748. const LV2_Descriptor* descriptor;
  3749. const LV2_RDF_Descriptor* rdf_descriptor;
  3750. LV2_Feature* features[lv2_feature_count+1];
  3751. struct {
  3752. const LV2_State_Interface* state;
  3753. const LV2_Worker_Interface* worker;
  3754. const LV2_Programs_Interface* programs;
  3755. const LV2_Programs_UI_Interface* uiprograms;
  3756. } ext;
  3757. struct {
  3758. void* lib;
  3759. LV2UI_Handle handle;
  3760. LV2UI_Widget widget;
  3761. const LV2UI_Descriptor* descriptor;
  3762. const LV2_RDF_UI* rdf_descriptor;
  3763. } ui;
  3764. struct {
  3765. GuiType type;
  3766. bool resizable;
  3767. int width;
  3768. int height;
  3769. } gui;
  3770. #ifdef HAVE_SUIL
  3771. struct {
  3772. SuilHost* host;
  3773. SuilInstance* handle;
  3774. QByteArray pos;
  3775. } suil;
  3776. #endif
  3777. Lv2AtomQueue atomQueueIn;
  3778. Lv2AtomQueue atomQueueOut;
  3779. Lv2PluginEventData evIn;
  3780. Lv2PluginEventData evOut;
  3781. float* paramBuffers;
  3782. std::vector<const char*> customURIDs;
  3783. bool lastTimePosPlaying;
  3784. uint32_t lastTimePosFrame;
  3785. };
  3786. CarlaPlugin* CarlaPlugin::newLV2(const initializer& init)
  3787. {
  3788. qDebug("CarlaPlugin::newLV2(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  3789. short id = init.engine->getNewPluginId();
  3790. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  3791. {
  3792. setLastError("Maximum number of plugins reached");
  3793. return nullptr;
  3794. }
  3795. Lv2Plugin* const plugin = new Lv2Plugin(init.engine, id);
  3796. if (! plugin->init(init.filename, init.name, init.label))
  3797. {
  3798. delete plugin;
  3799. return nullptr;
  3800. }
  3801. plugin->reload();
  3802. #ifndef BUILD_BRIDGE
  3803. if (carlaOptions.processMode == PROCESS_MODE_CONTINUOUS_RACK)
  3804. {
  3805. const uint32_t ins = plugin->audioInCount();
  3806. const uint32_t outs = plugin->audioOutCount();
  3807. if (ins > 2 || outs > 2 || (ins != outs && ins != 0 && outs != 0))
  3808. {
  3809. setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  3810. delete plugin;
  3811. return nullptr;
  3812. }
  3813. }
  3814. #endif
  3815. plugin->registerToOsc();
  3816. plugin->updateUi();
  3817. return plugin;
  3818. }
  3819. /**@}*/
  3820. // -------------------------------------------------------------------------------------------------------------------
  3821. int CarlaOsc::handle_lv2_atom_transfer(CARLA_OSC_HANDLE_ARGS2)
  3822. {
  3823. qDebug("CarlaOsc::handle_lv2_atom_transfer()");
  3824. CARLA_OSC_CHECK_OSC_TYPES(2, "ss");
  3825. //const char* const type = (const char*)&argv[0]->s;
  3826. //const char* const value = (const char*)&argv[1]->s;
  3827. //QByteArray chunk;
  3828. //chunk = QByteArray::fromBase64(value);
  3829. //const LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  3830. //CarlaBackend::Lv2Plugin* const lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3831. //lv2plugin->handleTransferAtom(atom, type);
  3832. return 0;
  3833. }
  3834. int CarlaOsc::handle_lv2_event_transfer(CARLA_OSC_HANDLE_ARGS2)
  3835. {
  3836. qDebug("CarlaOsc::handle_lv2_event_transfer()");
  3837. CARLA_OSC_CHECK_OSC_TYPES(2, "ss");
  3838. //const char* const type = (const char*)&argv[0]->s;
  3839. //const char* const value = (const char*)&argv[1]->s;
  3840. //QByteArray chunk;
  3841. //chunk = QByteArray::fromBase64(value);
  3842. //const LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  3843. //CarlaBackend::Lv2Plugin* const lv2plugin = (CarlaBackend::Lv2Plugin*)plugin;
  3844. //lv2plugin->handleTransferEvent(atom, type);
  3845. return 0;
  3846. }
  3847. CARLA_BACKEND_END_NAMESPACE