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.

5477 lines
198KB

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