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.

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