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.

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