Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5026 lines
179KB

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