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.

5486 lines
199KB

  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. showCustomUI(false);
  347. if (fUi.type == PLUGIN_UI_OSC)
  348. {
  349. pData->osc.thread.stop(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 (List<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(pData->name);
  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 getOptionsAvailable() 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() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  531. {
  532. if (pData->options & 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*)pData->name));
  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 showCustomUI(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.start();
  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.stop(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(ENGINE_CALLBACK_ERROR, pData->id, 0, 0, 0.0f, "Plugin refused to open its own UI");
  937. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 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, pData->id, 0, 0, 0.0f, "Plugin refused to open its own UI");
  988. pData->engine->callback(CALLBACK_SHOW_GUI, pData->id, -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*)pData->name));
  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 idle() override
  1014. {
  1015. //if (fUi.type == PLUGIN_UI_NULL)
  1016. // return CarlaPlugin::idleGui();
  1017. //if (! fAtomQueueOut.isEmpty())
  1018. {
  1019. Lv2AtomQueue tmpQueue;
  1020. tmpQueue.copyDataFromQueue(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. showCustomUI(false);
  1049. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1050. }
  1051. }
  1052. CarlaPlugin::idle();
  1053. }
  1054. // -------------------------------------------------------------------
  1055. // Plugin state
  1056. void reload() override
  1057. {
  1058. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1059. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1060. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1061. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1062. carla_debug("Lv2Plugin::reload() - start");
  1063. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1064. // Safely disable plugin for reload
  1065. const ScopedDisabler sd(this);
  1066. if (pData->active)
  1067. deactivate();
  1068. clearBuffers();
  1069. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1070. const uint32_t portCount(static_cast<uint32_t>(fRdfDescriptor->PortCount));
  1071. uint32_t aIns, aOuts, cvIns, cvOuts, params, j;
  1072. aIns = aOuts = cvIns = cvOuts = params = 0;
  1073. List<unsigned int> evIns, evOuts;
  1074. bool forcedStereoIn, forcedStereoOut;
  1075. forcedStereoIn = forcedStereoOut = false;
  1076. bool needsCtrlIn, needsCtrlOut;
  1077. needsCtrlIn = needsCtrlOut = false;
  1078. for (uint32_t i=0; i < portCount; ++i)
  1079. {
  1080. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1081. if (LV2_IS_PORT_AUDIO(portTypes))
  1082. {
  1083. if (LV2_IS_PORT_INPUT(portTypes))
  1084. aIns += 1;
  1085. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1086. aOuts += 1;
  1087. }
  1088. else if (LV2_IS_PORT_CV(portTypes))
  1089. {
  1090. if (LV2_IS_PORT_INPUT(portTypes))
  1091. cvIns += 1;
  1092. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1093. cvOuts += 1;
  1094. }
  1095. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1096. {
  1097. if (LV2_IS_PORT_INPUT(portTypes))
  1098. evIns.append(CARLA_EVENT_DATA_ATOM);
  1099. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1100. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1101. }
  1102. else if (LV2_IS_PORT_EVENT(portTypes))
  1103. {
  1104. if (LV2_IS_PORT_INPUT(portTypes))
  1105. evIns.append(CARLA_EVENT_DATA_EVENT);
  1106. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1107. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1108. }
  1109. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1110. {
  1111. if (LV2_IS_PORT_INPUT(portTypes))
  1112. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1113. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1114. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1115. }
  1116. else if (LV2_IS_PORT_CONTROL(portTypes))
  1117. params += 1;
  1118. }
  1119. // check extensions
  1120. fExt.options = nullptr;
  1121. fExt.programs = nullptr;
  1122. fExt.state = nullptr;
  1123. fExt.worker = nullptr;
  1124. if (fDescriptor->extension_data != nullptr)
  1125. {
  1126. if (pData->extraHints & PLUGIN_HAS_EXTENSION_OPTIONS)
  1127. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  1128. if (pData->extraHints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  1129. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  1130. if (pData->extraHints & PLUGIN_HAS_EXTENSION_STATE)
  1131. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  1132. if (pData->extraHints & PLUGIN_HAS_EXTENSION_WORKER)
  1133. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  1134. }
  1135. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1136. {
  1137. if (fHandle2 == nullptr)
  1138. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1139. if (fHandle2 != nullptr)
  1140. {
  1141. if (aIns == 1)
  1142. {
  1143. aIns = 2;
  1144. forcedStereoIn = true;
  1145. }
  1146. if (aOuts == 1)
  1147. {
  1148. aOuts = 2;
  1149. forcedStereoOut = true;
  1150. }
  1151. }
  1152. }
  1153. if (aIns > 0)
  1154. {
  1155. pData->audioIn.createNew(aIns);
  1156. fAudioInBuffers = new float*[aIns];
  1157. for (uint32_t i=0; i < aIns; ++i)
  1158. fAudioInBuffers[i] = nullptr;
  1159. }
  1160. if (aOuts > 0)
  1161. {
  1162. pData->audioOut.createNew(aOuts);
  1163. fAudioOutBuffers = new float*[aOuts];
  1164. needsCtrlIn = true;
  1165. for (uint32_t i=0; i < aOuts; ++i)
  1166. fAudioOutBuffers[i] = nullptr;
  1167. }
  1168. if (cvIns > 0)
  1169. {
  1170. fCvIn.createNew(cvIns);
  1171. needsCtrlIn = true;
  1172. }
  1173. if (cvOuts > 0)
  1174. {
  1175. fCvOut.createNew(cvOuts);
  1176. needsCtrlOut = true;
  1177. }
  1178. if (params > 0)
  1179. {
  1180. pData->param.createNew(params+cvIns+cvOuts);
  1181. fParamBuffers = new float[params+cvIns+cvOuts];
  1182. #ifdef HAVE_JUCE
  1183. FloatVectorOperations::clear(fParamBuffers, params+cvIns+cvOuts);
  1184. #else
  1185. #endif
  1186. }
  1187. if (evIns.count() > 0)
  1188. {
  1189. const size_t count(evIns.count());
  1190. fEventsIn.createNew(count);
  1191. for (j=0; j < count; ++j)
  1192. {
  1193. const uint32_t& type(evIns.getAt(j));
  1194. if (type == CARLA_EVENT_DATA_ATOM)
  1195. {
  1196. fEventsIn.data[j].type = CARLA_EVENT_DATA_ATOM;
  1197. fEventsIn.data[j].atom = lv2_atom_buffer_new(MAX_EVENT_BUFFER, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1198. }
  1199. else if (type == CARLA_EVENT_DATA_EVENT)
  1200. {
  1201. fEventsIn.data[j].type = CARLA_EVENT_DATA_EVENT;
  1202. fEventsIn.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1203. }
  1204. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1205. {
  1206. fEventsIn.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1207. fEventsIn.data[j].midi = new LV2_MIDI;
  1208. fEventsIn.data[j].midi->event_count = 0;
  1209. fEventsIn.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1210. fEventsIn.data[j].midi->size = 0;
  1211. fEventsIn.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1212. }
  1213. }
  1214. }
  1215. if (evOuts.count() > 0)
  1216. {
  1217. const size_t count(evOuts.count());
  1218. fEventsOut.createNew(count);
  1219. for (j=0; j < count; ++j)
  1220. {
  1221. const uint32_t& type(evOuts.getAt(j));
  1222. if (type == CARLA_EVENT_DATA_ATOM)
  1223. {
  1224. fEventsOut.data[j].type = CARLA_EVENT_DATA_ATOM;
  1225. fEventsOut.data[j].atom = lv2_atom_buffer_new(MAX_EVENT_BUFFER, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1226. }
  1227. else if (type == CARLA_EVENT_DATA_EVENT)
  1228. {
  1229. fEventsOut.data[j].type = CARLA_EVENT_DATA_EVENT;
  1230. fEventsOut.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1231. }
  1232. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1233. {
  1234. fEventsOut.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1235. fEventsOut.data[j].midi = new LV2_MIDI;
  1236. fEventsOut.data[j].midi->event_count = 0;
  1237. fEventsOut.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1238. fEventsOut.data[j].midi->size = 0;
  1239. fEventsOut.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1240. }
  1241. }
  1242. }
  1243. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1244. CarlaString portName;
  1245. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1246. {
  1247. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1248. portName.clear();
  1249. 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))
  1250. {
  1251. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1252. {
  1253. portName = pData->name;
  1254. portName += ":";
  1255. }
  1256. portName += fRdfDescriptor->Ports[i].Name;
  1257. portName.truncate(portNameSize);
  1258. }
  1259. if (LV2_IS_PORT_AUDIO(portTypes))
  1260. {
  1261. if (LV2_IS_PORT_INPUT(portTypes))
  1262. {
  1263. j = iAudioIn++;
  1264. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1265. pData->audioIn.ports[j].rindex = i;
  1266. if (forcedStereoIn)
  1267. {
  1268. portName += "_2";
  1269. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1270. pData->audioIn.ports[1].rindex = i;
  1271. }
  1272. }
  1273. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1274. {
  1275. j = iAudioOut++;
  1276. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1277. pData->audioOut.ports[j].rindex = i;
  1278. if (forcedStereoOut)
  1279. {
  1280. portName += "_2";
  1281. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1282. pData->audioOut.ports[1].rindex = i;
  1283. }
  1284. }
  1285. else
  1286. carla_stderr("WARNING - Got a broken Port (Audio, but not input or output)");
  1287. }
  1288. else if (LV2_IS_PORT_CV(portTypes))
  1289. {
  1290. if (LV2_IS_PORT_INPUT(portTypes))
  1291. {
  1292. j = iCvIn++;
  1293. fCvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true);
  1294. fCvIn.ports[j].rindex = i;
  1295. fCvIn.ports[j].param = params + j;
  1296. fDescriptor->connect_port(fHandle, i, fCvIn.ports[j].port->getBuffer());
  1297. if (fHandle2 != nullptr)
  1298. fDescriptor->connect_port(fHandle2, i, fCvIn.ports[j].port->getBuffer());
  1299. j = fCvIn.ports[j].param;
  1300. pData->param.data[j].index = j;
  1301. pData->param.data[j].rindex = i;
  1302. pData->param.data[j].hints = PARAMETER_IS_INPUT|PARAMETER_IS_ENABLED|PARAMETER_IS_READ_ONLY;
  1303. pData->param.data[j].midiChannel = 0;
  1304. pData->param.data[j].midiCC = -1;
  1305. pData->param.ranges[j].min = -1.0f;
  1306. pData->param.ranges[j].max = 1.0f;
  1307. pData->param.ranges[j].def = 0.0f;
  1308. pData->param.ranges[j].step = 0.01f;
  1309. pData->param.ranges[j].stepSmall = 0.001f;
  1310. pData->param.ranges[j].stepLarge = 0.1f;
  1311. fParamBuffers[j] = 0.0f;
  1312. }
  1313. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1314. {
  1315. j = iCvOut++;
  1316. fCvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false);
  1317. fCvOut.ports[j].rindex = i;
  1318. fCvOut.ports[j].param = params + cvIns + j;
  1319. fDescriptor->connect_port(fHandle, i, fCvOut.ports[j].port->getBuffer());
  1320. if (fHandle2 != nullptr)
  1321. fDescriptor->connect_port(fHandle2, i, fCvOut.ports[j].port->getBuffer());
  1322. j = fCvOut.ports[j].param;
  1323. pData->param.data[j].index = j;
  1324. pData->param.data[j].rindex = i;
  1325. pData->param.data[j].hints = PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE;
  1326. pData->param.data[j].midiChannel = 0;
  1327. pData->param.data[j].midiCC = -1;
  1328. pData->param.ranges[j].min = -1.0f;
  1329. pData->param.ranges[j].max = 1.0f;
  1330. pData->param.ranges[j].def = 0.0f;
  1331. pData->param.ranges[j].step = 0.01f;
  1332. pData->param.ranges[j].stepSmall = 0.001f;
  1333. pData->param.ranges[j].stepLarge = 0.1f;
  1334. fParamBuffers[j] = 0.0f;
  1335. }
  1336. else
  1337. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1338. }
  1339. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1340. {
  1341. if (LV2_IS_PORT_INPUT(portTypes))
  1342. {
  1343. j = iEvIn++;
  1344. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1345. if (fHandle2 != nullptr)
  1346. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1347. fEventsIn.data[j].rindex = i;
  1348. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1349. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1350. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1351. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1352. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1353. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1354. if (evIns.count() == 1)
  1355. {
  1356. fEventsIn.ctrl = &fEventsIn.data[j];
  1357. fEventsIn.ctrlIndex = j;
  1358. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1359. needsCtrlIn = true;
  1360. }
  1361. else
  1362. {
  1363. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1364. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1365. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1366. {
  1367. fEventsIn.ctrl = &fEventsIn.data[j];
  1368. fEventsIn.ctrlIndex = j;
  1369. }
  1370. }
  1371. }
  1372. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1373. {
  1374. j = iEvOut++;
  1375. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1376. if (fHandle2 != nullptr)
  1377. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1378. fEventsOut.data[j].rindex = i;
  1379. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1380. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1381. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1382. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1383. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1384. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1385. if (evOuts.count() == 1)
  1386. {
  1387. fEventsOut.ctrl = &fEventsOut.data[j];
  1388. fEventsOut.ctrlIndex = j;
  1389. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1390. needsCtrlOut = true;
  1391. }
  1392. else
  1393. {
  1394. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1395. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1396. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1397. {
  1398. fEventsOut.ctrl = &fEventsOut.data[j];
  1399. fEventsOut.ctrlIndex = j;
  1400. }
  1401. }
  1402. }
  1403. else
  1404. carla_stderr("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1405. }
  1406. else if (LV2_IS_PORT_EVENT(portTypes))
  1407. {
  1408. if (LV2_IS_PORT_INPUT(portTypes))
  1409. {
  1410. j = iEvIn++;
  1411. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1412. if (fHandle2 != nullptr)
  1413. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1414. fEventsIn.data[j].rindex = i;
  1415. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1416. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1417. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1418. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1419. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1420. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1421. if (evIns.count() == 1)
  1422. {
  1423. fEventsIn.ctrl = &fEventsIn.data[j];
  1424. fEventsIn.ctrlIndex = j;
  1425. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1426. needsCtrlIn = true;
  1427. }
  1428. else
  1429. {
  1430. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1431. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1432. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1433. {
  1434. fEventsIn.ctrl = &fEventsIn.data[j];
  1435. fEventsIn.ctrlIndex = j;
  1436. }
  1437. }
  1438. }
  1439. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1440. {
  1441. j = iEvOut++;
  1442. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1443. if (fHandle2 != nullptr)
  1444. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1445. fEventsOut.data[j].rindex = i;
  1446. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1447. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1448. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1449. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1450. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1451. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1452. if (evOuts.count() == 1)
  1453. {
  1454. fEventsOut.ctrl = &fEventsOut.data[j];
  1455. fEventsOut.ctrlIndex = j;
  1456. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1457. needsCtrlOut = true;
  1458. }
  1459. else
  1460. {
  1461. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1462. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1463. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1464. {
  1465. fEventsOut.ctrl = &fEventsOut.data[j];
  1466. fEventsOut.ctrlIndex = j;
  1467. }
  1468. }
  1469. }
  1470. else
  1471. carla_stderr("WARNING - Got a broken Port (Event, but not input or output)");
  1472. }
  1473. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1474. {
  1475. if (LV2_IS_PORT_INPUT(portTypes))
  1476. {
  1477. j = iEvIn++;
  1478. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].midi);
  1479. if (fHandle2 != nullptr)
  1480. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].midi);
  1481. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1482. fEventsIn.data[j].rindex = i;
  1483. if (evIns.count() == 1)
  1484. {
  1485. needsCtrlIn = true;
  1486. fEventsIn.ctrl = &fEventsIn.data[j];
  1487. fEventsIn.ctrlIndex = j;
  1488. }
  1489. else
  1490. {
  1491. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1492. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1493. {
  1494. fEventsIn.ctrl = &fEventsIn.data[j];
  1495. fEventsIn.ctrlIndex = j;
  1496. }
  1497. }
  1498. }
  1499. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1500. {
  1501. j = iEvOut++;
  1502. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].midi);
  1503. if (fHandle2 != nullptr)
  1504. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].midi);
  1505. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1506. fEventsOut.data[j].rindex = i;
  1507. if (evOuts.count() == 1)
  1508. {
  1509. needsCtrlOut = true;
  1510. fEventsOut.ctrl = &fEventsOut.data[j];
  1511. fEventsOut.ctrlIndex = j;
  1512. }
  1513. else
  1514. {
  1515. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1516. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1517. {
  1518. fEventsOut.ctrl = &fEventsOut.data[j];
  1519. fEventsOut.ctrlIndex = j;
  1520. }
  1521. }
  1522. }
  1523. else
  1524. carla_stderr("WARNING - Got a broken Port (MIDI, but not input or output)");
  1525. }
  1526. else if (LV2_IS_PORT_CONTROL(portTypes))
  1527. {
  1528. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1529. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1530. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1531. j = iCtrl++;
  1532. pData->param.data[j].index = j;
  1533. pData->param.data[j].rindex = i;
  1534. pData->param.data[j].hints = 0x0;
  1535. pData->param.data[j].midiChannel = 0;
  1536. pData->param.data[j].midiCC = -1;
  1537. float min, max, def, step, stepSmall, stepLarge;
  1538. // min value
  1539. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1540. min = portPoints.Minimum;
  1541. else
  1542. min = 0.0f;
  1543. // max value
  1544. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1545. max = portPoints.Maximum;
  1546. else
  1547. max = 1.0f;
  1548. if (min > max)
  1549. max = min;
  1550. else if (max < min)
  1551. min = max;
  1552. // stupid hack for ir.lv2 (broken plugin)
  1553. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1554. {
  1555. min = 0.0f;
  1556. max = (float)0xffffff;
  1557. }
  1558. if (max - min == 0.0f)
  1559. {
  1560. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1561. max = min + 0.1f;
  1562. }
  1563. // default value
  1564. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1565. {
  1566. def = portPoints.Default;
  1567. }
  1568. else
  1569. {
  1570. // no default value
  1571. if (min < 0.0f && max > 0.0f)
  1572. def = 0.0f;
  1573. else
  1574. def = min;
  1575. }
  1576. if (def < min)
  1577. def = min;
  1578. else if (def > max)
  1579. def = max;
  1580. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1581. {
  1582. min *= sampleRate;
  1583. max *= sampleRate;
  1584. def *= sampleRate;
  1585. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1586. }
  1587. if (LV2_IS_PORT_TOGGLED(portProps))
  1588. {
  1589. step = max - min;
  1590. stepSmall = step;
  1591. stepLarge = step;
  1592. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1593. }
  1594. else if (LV2_IS_PORT_INTEGER(portProps))
  1595. {
  1596. step = 1.0f;
  1597. stepSmall = 1.0f;
  1598. stepLarge = 10.0f;
  1599. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1600. }
  1601. else
  1602. {
  1603. float range = max - min;
  1604. step = range/100.0f;
  1605. stepSmall = range/1000.0f;
  1606. stepLarge = range/10.0f;
  1607. }
  1608. if (LV2_IS_PORT_INPUT(portTypes))
  1609. {
  1610. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1611. {
  1612. carla_stderr("Plugin has latency input port, this should not happen!");
  1613. }
  1614. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1615. {
  1616. def = sampleRate;
  1617. step = 1.0f;
  1618. stepSmall = 1.0f;
  1619. stepLarge = 1.0f;
  1620. //pData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1621. pData->param.data[j].hints = 0x0;
  1622. }
  1623. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1624. {
  1625. //pData->param.data[j].type = PARAMETER_LV2_FREEWHEEL;
  1626. }
  1627. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1628. {
  1629. //pData->param.data[j].type = PARAMETER_LV2_TIME;
  1630. }
  1631. else
  1632. {
  1633. pData->param.data[j].hints |= PARAMETER_IS_INPUT;
  1634. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1635. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1636. needsCtrlIn = true;
  1637. }
  1638. // MIDI CC value
  1639. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1640. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1641. {
  1642. if (! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1643. pData->param.data[j].midiCC = portMidiMap.Number;
  1644. }
  1645. }
  1646. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1647. {
  1648. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1649. {
  1650. min = 0.0f;
  1651. max = sampleRate;
  1652. def = 0.0f;
  1653. step = 1.0f;
  1654. stepSmall = 1.0f;
  1655. stepLarge = 1.0f;
  1656. //pData->param.data[j].type = PARAMETER_LATENCY;
  1657. pData->param.data[j].hints = 0x0;
  1658. }
  1659. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1660. {
  1661. def = sampleRate;
  1662. step = 1.0f;
  1663. stepSmall = 1.0f;
  1664. stepLarge = 1.0f;
  1665. //pData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1666. pData->param.data[j].hints = 0x0;
  1667. }
  1668. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1669. {
  1670. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1671. }
  1672. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1673. {
  1674. //pData->param.data[j].type = PARAMETER_LV2_TIME;
  1675. }
  1676. else
  1677. {
  1678. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1679. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1680. needsCtrlOut = true;
  1681. }
  1682. }
  1683. else
  1684. {
  1685. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1686. }
  1687. // extra parameter hints
  1688. if (LV2_IS_PORT_ENUMERATION(portProps))
  1689. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1690. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1691. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1692. if (LV2_IS_PORT_TRIGGER(portProps))
  1693. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1694. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1695. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1696. // check if parameter is not enabled or automable
  1697. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1698. {
  1699. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1700. }
  1701. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1702. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1703. pData->param.ranges[j].min = min;
  1704. pData->param.ranges[j].max = max;
  1705. pData->param.ranges[j].def = def;
  1706. pData->param.ranges[j].step = step;
  1707. pData->param.ranges[j].stepSmall = stepSmall;
  1708. pData->param.ranges[j].stepLarge = stepLarge;
  1709. // Start parameters in their default values
  1710. //if (pData->param.data[j].type != PARAMETER_LV2_FREEWHEEL)
  1711. fParamBuffers[j] = def;
  1712. //else
  1713. // fParamBuffers[j] = min;
  1714. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1715. if (fHandle2 != nullptr)
  1716. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1717. }
  1718. else
  1719. {
  1720. // Port Type not supported, but it's optional anyway
  1721. fDescriptor->connect_port(fHandle, i, nullptr);
  1722. if (fHandle2 != nullptr)
  1723. fDescriptor->connect_port(fHandle2, i, nullptr);
  1724. }
  1725. }
  1726. if (needsCtrlIn)
  1727. {
  1728. portName.clear();
  1729. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1730. {
  1731. portName = pData->name;
  1732. portName += ":";
  1733. }
  1734. portName += "events-in";
  1735. portName.truncate(portNameSize);
  1736. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1737. }
  1738. if (needsCtrlOut)
  1739. {
  1740. portName.clear();
  1741. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1742. {
  1743. portName = pData->name;
  1744. portName += ":";
  1745. }
  1746. portName += "events-out";
  1747. portName.truncate(portNameSize);
  1748. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1749. }
  1750. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1751. fEventsIn.ctrl->port = pData->event.portIn;
  1752. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1753. fEventsOut.ctrl->port = pData->event.portOut;
  1754. if (forcedStereoIn || forcedStereoOut)
  1755. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1756. else
  1757. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1758. // plugin hints
  1759. pData->hints = 0x0;
  1760. if (isRealtimeSafe())
  1761. pData->hints |= PLUGIN_IS_RTSAFE;
  1762. if (fUi.type != PLUGIN_UI_NULL)
  1763. {
  1764. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1765. if (fUi.type == PLUGIN_UI_QT || fUi.type == PLUGIN_UI_PARENT)
  1766. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  1767. }
  1768. //if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1769. // pData->hints |= PLUGIN_IS_SYNTH;
  1770. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1771. pData->hints |= PLUGIN_CAN_DRYWET;
  1772. if (aOuts > 0)
  1773. pData->hints |= PLUGIN_CAN_VOLUME;
  1774. if (aOuts >= 2 && aOuts % 2 == 0)
  1775. pData->hints |= PLUGIN_CAN_BALANCE;
  1776. // extra plugin hints
  1777. pData->extraHints &= ~PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1778. if (fExt.state != nullptr || fExt.worker != nullptr)
  1779. {
  1780. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1781. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1782. }
  1783. else
  1784. {
  1785. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1786. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1787. }
  1788. bufferSizeChanged(pData->engine->getBufferSize());
  1789. reloadPrograms(true);
  1790. if (pData->active)
  1791. activate();
  1792. evIns.clear();
  1793. evOuts.clear();
  1794. carla_debug("Lv2Plugin::reload() - end");
  1795. }
  1796. void reloadPrograms(const bool init) override
  1797. {
  1798. carla_debug("Lv2Plugin::reloadPrograms(%s)", bool2str(init));
  1799. uint32_t i, oldCount = pData->midiprog.count;
  1800. const int32_t current = pData->midiprog.current;
  1801. // special LV2 programs handling
  1802. if (init)
  1803. {
  1804. pData->prog.clear();
  1805. const uint32_t count(fRdfDescriptor->PresetCount);
  1806. if (count > 0)
  1807. {
  1808. pData->prog.createNew(count);
  1809. for (i=0; i < count; ++i)
  1810. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1811. }
  1812. }
  1813. // Delete old programs
  1814. pData->midiprog.clear();
  1815. // Query new programs
  1816. uint32_t count = 0;
  1817. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1818. {
  1819. while (fExt.programs->get_program(fHandle, count))
  1820. ++count;
  1821. }
  1822. if (count > 0)
  1823. {
  1824. pData->midiprog.createNew(count);
  1825. // Update data
  1826. for (i=0; i < count; ++i)
  1827. {
  1828. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1829. CARLA_ASSERT(pdesc != nullptr);
  1830. CARLA_ASSERT(pdesc->name != nullptr);
  1831. pData->midiprog.data[i].bank = pdesc->bank;
  1832. pData->midiprog.data[i].program = pdesc->program;
  1833. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1834. }
  1835. }
  1836. #ifndef BUILD_BRIDGE
  1837. // Update OSC Names
  1838. if (pData->engine->isOscControlRegistered())
  1839. {
  1840. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  1841. for (i=0; i < count; ++i)
  1842. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  1843. }
  1844. #endif
  1845. if (init)
  1846. {
  1847. if (count > 0)
  1848. {
  1849. setMidiProgram(0, false, false, false);
  1850. }
  1851. else
  1852. {
  1853. // load default state
  1854. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  1855. if (const LilvState* state = lv2World.getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1856. {
  1857. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1858. if (fHandle2 != nullptr)
  1859. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1860. }
  1861. }
  1862. }
  1863. else
  1864. {
  1865. // Check if current program is invalid
  1866. bool programChanged = false;
  1867. if (count == oldCount+1)
  1868. {
  1869. // one midi program added, probably created by user
  1870. pData->midiprog.current = oldCount;
  1871. programChanged = true;
  1872. }
  1873. else if (current < 0 && count > 0)
  1874. {
  1875. // programs exist now, but not before
  1876. pData->midiprog.current = 0;
  1877. programChanged = true;
  1878. }
  1879. else if (current >= 0 && count == 0)
  1880. {
  1881. // programs existed before, but not anymore
  1882. pData->midiprog.current = -1;
  1883. programChanged = true;
  1884. }
  1885. else if (current >= static_cast<int32_t>(count))
  1886. {
  1887. // current midi program > count
  1888. pData->midiprog.current = 0;
  1889. programChanged = true;
  1890. }
  1891. else
  1892. {
  1893. // no change
  1894. pData->midiprog.current = current;
  1895. }
  1896. if (programChanged)
  1897. setMidiProgram(pData->midiprog.current, true, true, true);
  1898. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1899. }
  1900. }
  1901. // -------------------------------------------------------------------
  1902. // Plugin processing
  1903. void activate() override
  1904. {
  1905. CARLA_ASSERT(fDescriptor != nullptr);
  1906. CARLA_ASSERT(fHandle != nullptr);
  1907. if (fDescriptor->activate != nullptr)
  1908. {
  1909. fDescriptor->activate(fHandle);
  1910. if (fHandle2 != nullptr)
  1911. fDescriptor->activate(fHandle2);
  1912. }
  1913. fFirstActive = true;
  1914. }
  1915. void deactivate() override
  1916. {
  1917. CARLA_ASSERT(fDescriptor != nullptr);
  1918. CARLA_ASSERT(fHandle != nullptr);
  1919. if (fDescriptor->deactivate != nullptr)
  1920. {
  1921. fDescriptor->deactivate(fHandle);
  1922. if (fHandle2 != nullptr)
  1923. fDescriptor->deactivate(fHandle2);
  1924. }
  1925. }
  1926. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1927. {
  1928. uint32_t i, k;
  1929. // --------------------------------------------------------------------------------------------------------
  1930. // Check if active
  1931. if (! pData->active)
  1932. {
  1933. // disable any output sound
  1934. for (i=0; i < pData->audioOut.count; ++i)
  1935. {
  1936. #ifdef HAVE_JUCE
  1937. FloatVectorOperations::clear(outBuffer[i], frames);
  1938. #else
  1939. #endif
  1940. }
  1941. return;
  1942. }
  1943. // --------------------------------------------------------------------------------------------------------
  1944. // Handle events from different APIs
  1945. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  1946. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1947. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1948. for (i=0; i < fEventsIn.count; ++i)
  1949. {
  1950. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1951. {
  1952. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  1953. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  1954. }
  1955. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1956. {
  1957. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  1958. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1959. }
  1960. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1961. {
  1962. fEventsIn.data[i].midi->event_count = 0;
  1963. fEventsIn.data[i].midi->size = 0;
  1964. evInMidiStates[i].midi = fEventsIn.data[i].midi;
  1965. evInMidiStates[i].frame_count = frames;
  1966. evInMidiStates[i].position = 0;
  1967. }
  1968. }
  1969. for (i=0; i < fEventsOut.count; ++i)
  1970. {
  1971. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1972. {
  1973. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  1974. }
  1975. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1976. {
  1977. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  1978. }
  1979. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1980. {
  1981. // not needed
  1982. }
  1983. }
  1984. CARLA_PROCESS_CONTINUE_CHECK;
  1985. // --------------------------------------------------------------------------------------------------------
  1986. // Check if needs reset
  1987. if (pData->needsReset)
  1988. {
  1989. uint8_t midiData[3] = { 0 };
  1990. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  1991. {
  1992. k = fEventsIn.ctrlIndex;
  1993. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1994. {
  1995. for (i=0; i < MAX_MIDI_CHANNELS; ++i)
  1996. {
  1997. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  1998. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1999. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2000. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2001. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2002. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2003. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2004. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  2005. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2006. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2007. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2008. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2009. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2010. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2011. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2012. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  2013. }
  2014. }
  2015. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  2016. {
  2017. for (k=0; k < MAX_MIDI_NOTE; ++k)
  2018. {
  2019. midiData[0] = MIDI_STATUS_NOTE_OFF + pData->ctrlChannel;
  2020. midiData[1] = k;
  2021. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2022. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2023. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2024. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2025. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2026. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  2027. }
  2028. }
  2029. }
  2030. //if (pData->latency > 0)
  2031. {
  2032. //for (i=0; i < pData->audioIn.count; ++i)
  2033. // FloatVectorOperations::clear(pData->latencyBuffers[i], pData->latency);
  2034. }
  2035. pData->needsReset = false;
  2036. CARLA_PROCESS_CONTINUE_CHECK;
  2037. }
  2038. // --------------------------------------------------------------------------------------------------------
  2039. // TimeInfo
  2040. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  2041. if (fFirstActive || fLastTimeInfo != timeInfo)
  2042. {
  2043. bool doPostRt;
  2044. int32_t rindex;
  2045. // update input ports
  2046. for (k=0; k < pData->param.count; ++k)
  2047. {
  2048. //if (pData->param.data[k].type != PARAMETER_LV2_TIME)
  2049. continue;
  2050. doPostRt = false;
  2051. rindex = pData->param.data[k].rindex;
  2052. CARLA_ASSERT(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2053. switch (fRdfDescriptor->Ports[rindex].Designation)
  2054. {
  2055. // Non-BBT
  2056. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2057. if (fLastTimeInfo.playing != timeInfo.playing)
  2058. {
  2059. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2060. doPostRt = true;
  2061. }
  2062. break;
  2063. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2064. if (fLastTimeInfo.frame != timeInfo.frame)
  2065. {
  2066. fParamBuffers[k] = timeInfo.frame;
  2067. doPostRt = true;
  2068. }
  2069. break;
  2070. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2071. break;
  2072. // BBT
  2073. case LV2_PORT_DESIGNATION_TIME_BAR:
  2074. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2075. {
  2076. fParamBuffers[k] = timeInfo.bbt.bar - 1;
  2077. doPostRt = true;
  2078. }
  2079. break;
  2080. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2081. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2082. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  2083. {
  2084. fParamBuffers[k] = timeInfo.bbt.beat - 1 + (double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat);
  2085. doPostRt = true;
  2086. }
  2087. break;
  2088. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2089. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2090. {
  2091. fParamBuffers[k] = timeInfo.bbt.beat - 1;
  2092. doPostRt = true;
  2093. }
  2094. break;
  2095. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2096. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  2097. {
  2098. fParamBuffers[k] = timeInfo.bbt.beatType;
  2099. doPostRt = true;
  2100. }
  2101. break;
  2102. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2103. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  2104. {
  2105. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2106. doPostRt = true;
  2107. }
  2108. break;
  2109. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2110. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  2111. {
  2112. fParamBuffers[k] = timeInfo.bbt.beatsPerMinute;
  2113. doPostRt = true;
  2114. }
  2115. break;
  2116. }
  2117. if (doPostRt)
  2118. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2119. }
  2120. for (i = 0; i < fEventsIn.count; ++i)
  2121. {
  2122. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2123. continue;
  2124. uint8_t timeInfoBuf[256] = { 0 };
  2125. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2126. LV2_Atom_Forge_Frame forgeFrame;
  2127. lv2_atom_forge_blank(&fAtomForge, &forgeFrame, 1, CARLA_URI_MAP_ID_TIME_POSITION);
  2128. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED, 0);
  2129. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2130. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME, 0);
  2131. lv2_atom_forge_long(&fAtomForge, timeInfo.frame);
  2132. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2133. {
  2134. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR, 0);
  2135. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2136. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT, 0);
  2137. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beat - 1 + (double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2138. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT, 0);
  2139. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.beat -1);
  2140. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT, 0);
  2141. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatType);
  2142. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR, 0);
  2143. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2144. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE, 0);
  2145. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerMinute);
  2146. }
  2147. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2148. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2149. CARLA_ASSERT(atom->size < 256);
  2150. }
  2151. pData->postRtEvents.trySplice();
  2152. std::memcpy(&fLastTimeInfo, &timeInfo, sizeof(EngineTimeInfo));
  2153. CARLA_PROCESS_CONTINUE_CHECK;
  2154. }
  2155. // --------------------------------------------------------------------------------------------------------
  2156. // Event Input and Processing
  2157. if (fEventsIn.ctrl != nullptr)
  2158. {
  2159. // ----------------------------------------------------------------------------------------------------
  2160. // Message Input
  2161. if (fAtomQueueIn.tryLock())
  2162. {
  2163. //if (! fAtomQueueIn.isEmpty())
  2164. {
  2165. uint32_t portIndex;
  2166. const LV2_Atom* atom;
  2167. k = fEventsIn.ctrlIndex;
  2168. while (fAtomQueueIn.get(&atom, &portIndex))
  2169. {
  2170. carla_debug("Event input message sent to plugin DSP, type %i:\"%s\", size:%i/%i",
  2171. atom->type, carla_lv2_urid_unmap(this, atom->type),
  2172. atom->size, lv2_atom_total_size(atom)
  2173. );
  2174. if (! lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2175. {
  2176. carla_stdout("Event input buffer full, 1 message lost");
  2177. break;
  2178. }
  2179. }
  2180. }
  2181. fAtomQueueIn.unlock();
  2182. }
  2183. // ----------------------------------------------------------------------------------------------------
  2184. // MIDI Input (External)
  2185. if (pData->extNotes.mutex.tryLock())
  2186. {
  2187. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2188. {
  2189. // does not handle MIDI
  2190. pData->extNotes.data.clear();
  2191. }
  2192. else
  2193. {
  2194. k = fEventsIn.ctrlIndex;
  2195. while (! pData->extNotes.data.isEmpty())
  2196. {
  2197. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  2198. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2199. uint8_t midiEvent[3];
  2200. midiEvent[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  2201. midiEvent[0] += note.channel;
  2202. midiEvent[1] = note.note;
  2203. midiEvent[2] = note.velo;
  2204. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2205. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2206. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2207. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2208. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2209. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiEvent);
  2210. }
  2211. }
  2212. pData->extNotes.mutex.unlock();
  2213. } // End of MIDI Input (External)
  2214. // ----------------------------------------------------------------------------------------------------
  2215. // Event Input (System)
  2216. bool allNotesOffSent = false;
  2217. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2218. uint32_t time, nEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2219. uint32_t startTime = 0;
  2220. uint32_t timeOffset = 0;
  2221. uint32_t nextBankId = 0;
  2222. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2223. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2224. for (i=0; i < nEvents; ++i)
  2225. {
  2226. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2227. time = event.time;
  2228. if (time >= frames)
  2229. continue;
  2230. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  2231. if (time > timeOffset && sampleAccurate)
  2232. {
  2233. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  2234. {
  2235. startTime = 0;
  2236. timeOffset = time;
  2237. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2238. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2239. else
  2240. nextBankId = 0;
  2241. // reset iters
  2242. k = fEventsIn.ctrlIndex;
  2243. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2244. {
  2245. lv2_atom_buffer_reset(fEventsIn.data[k].atom, true);
  2246. lv2_atom_buffer_begin(&evInAtomIters[k], fEventsIn.data[k].atom);
  2247. }
  2248. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2249. {
  2250. lv2_event_buffer_reset(fEventsIn.data[k].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[k].event->data);
  2251. lv2_event_begin(&evInEventIters[k], fEventsIn.data[k].event);
  2252. }
  2253. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2254. {
  2255. fEventsIn.data[k].midi->event_count = 0;
  2256. fEventsIn.data[k].midi->size = 0;
  2257. evInMidiStates[k].position = time;
  2258. }
  2259. }
  2260. else
  2261. startTime += timeOffset;
  2262. }
  2263. // Control change
  2264. switch (event.type)
  2265. {
  2266. case kEngineEventTypeNull:
  2267. break;
  2268. case kEngineEventTypeControl:
  2269. {
  2270. const EngineControlEvent& ctrlEvent(event.ctrl);
  2271. switch (ctrlEvent.type)
  2272. {
  2273. case kEngineControlEventTypeNull:
  2274. break;
  2275. case kEngineControlEventTypeParameter:
  2276. {
  2277. #ifndef BUILD_BRIDGE
  2278. // Control backend stuff
  2279. if (event.channel == pData->ctrlChannel)
  2280. {
  2281. float value;
  2282. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  2283. {
  2284. value = ctrlEvent.value;
  2285. setDryWet(value, false, false);
  2286. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2287. }
  2288. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  2289. {
  2290. value = ctrlEvent.value*127.0f/100.0f;
  2291. setVolume(value, false, false);
  2292. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2293. }
  2294. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  2295. {
  2296. float left, right;
  2297. value = ctrlEvent.value/0.5f - 1.0f;
  2298. if (value < 0.0f)
  2299. {
  2300. left = -1.0f;
  2301. right = (value*2.0f)+1.0f;
  2302. }
  2303. else if (value > 0.0f)
  2304. {
  2305. left = (value*2.0f)-1.0f;
  2306. right = 1.0f;
  2307. }
  2308. else
  2309. {
  2310. left = -1.0f;
  2311. right = 1.0f;
  2312. }
  2313. setBalanceLeft(left, false, false);
  2314. setBalanceRight(right, false, false);
  2315. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2316. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2317. }
  2318. }
  2319. #endif
  2320. // Control plugin parameters
  2321. for (k=0; k < pData->param.count; ++k)
  2322. {
  2323. if (pData->param.data[k].midiChannel != event.channel)
  2324. continue;
  2325. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2326. continue;
  2327. if ((pData->param.data[k].hints & PARAMETER_IS_INPUT) == 0)
  2328. continue;
  2329. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2330. continue;
  2331. float value;
  2332. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2333. {
  2334. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2335. }
  2336. else
  2337. {
  2338. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2339. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2340. value = std::rint(value);
  2341. }
  2342. setParameterValue(k, value, false, false, false);
  2343. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2344. }
  2345. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  2346. {
  2347. uint8_t midiData[3];
  2348. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2349. midiData[1] = ctrlEvent.param;
  2350. midiData[2] = ctrlEvent.value*127.0f;
  2351. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2352. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2353. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2354. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2355. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2356. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0, 3, midiData);
  2357. }
  2358. break;
  2359. }
  2360. case kEngineControlEventTypeMidiBank:
  2361. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2362. nextBankId = ctrlEvent.param;
  2363. break;
  2364. case kEngineControlEventTypeMidiProgram:
  2365. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2366. {
  2367. const uint32_t nextProgramId = ctrlEvent.param;
  2368. for (k=0; k < pData->midiprog.count; ++k)
  2369. {
  2370. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2371. {
  2372. setMidiProgram(k, false, false, false);
  2373. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  2374. break;
  2375. }
  2376. }
  2377. }
  2378. break;
  2379. case kEngineControlEventTypeAllSoundOff:
  2380. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2381. {
  2382. const uint32_t mtime(sampleAccurate ? startTime : time);
  2383. uint8_t midiData[3];
  2384. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2385. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2386. midiData[2] = 0;
  2387. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2388. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2389. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2390. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2391. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2392. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2393. }
  2394. break;
  2395. case kEngineControlEventTypeAllNotesOff:
  2396. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2397. {
  2398. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2399. {
  2400. allNotesOffSent = true;
  2401. sendMidiAllNotesOffToCallback();
  2402. }
  2403. const uint32_t mtime(sampleAccurate ? startTime : time);
  2404. uint8_t midiData[3];
  2405. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2406. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2407. midiData[2] = 0;
  2408. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2409. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2410. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2411. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2412. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2413. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2414. }
  2415. break;
  2416. }
  2417. break;
  2418. }
  2419. case kEngineEventTypeMidi:
  2420. {
  2421. const EngineMidiEvent& midiEvent(event.midi);
  2422. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  2423. uint8_t channel = event.channel;
  2424. uint32_t mtime = sampleAccurate ? startTime : time;
  2425. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2426. continue;
  2427. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2428. continue;
  2429. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2430. continue;
  2431. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2432. continue;
  2433. // Fix bad note-off
  2434. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  2435. status -= 0x10;
  2436. k = fEventsIn.ctrlIndex;
  2437. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2438. lv2_atom_buffer_write(&evInAtomIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2439. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2440. lv2_event_write(&evInEventIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2441. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2442. lv2midi_put_event(&evInMidiStates[k], mtime, midiEvent.size, midiEvent.data);
  2443. if (status == MIDI_STATUS_NOTE_ON)
  2444. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2445. else if (status == MIDI_STATUS_NOTE_OFF)
  2446. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2447. break;
  2448. }
  2449. }
  2450. }
  2451. pData->postRtEvents.trySplice();
  2452. if (frames > timeOffset)
  2453. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  2454. } // End of Event Input and Processing
  2455. // --------------------------------------------------------------------------------------------------------
  2456. // Plugin processing (no events)
  2457. else
  2458. {
  2459. processSingle(inBuffer, outBuffer, frames, 0);
  2460. } // End of Plugin processing (no events)
  2461. CARLA_PROCESS_CONTINUE_CHECK;
  2462. // --------------------------------------------------------------------------------------------------------
  2463. // MIDI Output
  2464. if (fEventsOut.ctrl != nullptr)
  2465. {
  2466. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2467. {
  2468. const uint32_t rindex(fEventsOut.ctrl->rindex);
  2469. const LV2_Atom_Event* ev;
  2470. LV2_Atom_Buffer_Iterator iter;
  2471. uint8_t* data;
  2472. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2473. while (true)
  2474. {
  2475. data = nullptr;
  2476. ev = lv2_atom_buffer_get(&iter, &data);
  2477. if (ev == nullptr || data == nullptr)
  2478. break;
  2479. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT && fEventsOut.ctrl->port != nullptr)
  2480. fEventsOut.ctrl->port->writeMidiEvent(ev->time.frames, ev->body.size, data);
  2481. else if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2482. fAtomQueueOut.put(&ev->body, rindex);
  2483. lv2_atom_buffer_increment(&iter);
  2484. }
  2485. }
  2486. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2487. {
  2488. const LV2_Event* ev;
  2489. LV2_Event_Iterator iter;
  2490. uint8_t* data;
  2491. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2492. while (true)
  2493. {
  2494. data = nullptr;
  2495. ev = lv2_event_get(&iter, &data);
  2496. if (ev == nullptr || data == nullptr)
  2497. break;
  2498. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2499. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, ev->size, data);
  2500. lv2_event_increment(&iter);
  2501. }
  2502. }
  2503. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2504. {
  2505. LV2_MIDIState state = { fEventsOut.ctrl->midi, frames, 0 };
  2506. uint32_t eventSize;
  2507. double eventTime;
  2508. unsigned char* eventData;
  2509. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  2510. {
  2511. if (eventData == nullptr || eventSize == 0)
  2512. break;
  2513. fEventsOut.ctrl->port->writeMidiEvent(eventTime, eventSize, eventData);
  2514. lv2midi_step(&state);
  2515. }
  2516. }
  2517. }
  2518. // --------------------------------------------------------------------------------------------------------
  2519. // Control Output
  2520. if (pData->event.portOut != nullptr)
  2521. {
  2522. uint8_t channel;
  2523. uint16_t param;
  2524. float value;
  2525. for (k=0; k < pData->param.count; ++k)
  2526. {
  2527. if (pData->param.data[k].hints & PARAMETER_IS_INPUT)
  2528. continue;
  2529. if (pData->param.data[k].hints & PARAMETER_IS_STRICT_BOUNDS)
  2530. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2531. if (pData->param.data[k].midiCC > 0)
  2532. {
  2533. channel = pData->param.data[k].midiChannel;
  2534. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2535. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2536. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2537. }
  2538. }
  2539. } // End of Control Output
  2540. CARLA_PROCESS_CONTINUE_CHECK;
  2541. #if 0
  2542. // --------------------------------------------------------------------------------------------------------
  2543. // Final work
  2544. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2545. {
  2546. fExt.worker->end_run(fHandle);
  2547. if (fHandle2 != nullptr)
  2548. fExt.worker->end_run(fHandle2);
  2549. }
  2550. #endif
  2551. fFirstActive = false;
  2552. // --------------------------------------------------------------------------------------------------------
  2553. }
  2554. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  2555. {
  2556. CARLA_ASSERT(frames > 0);
  2557. if (frames == 0)
  2558. return false;
  2559. if (pData->audioIn.count > 0)
  2560. {
  2561. CARLA_ASSERT(inBuffer != nullptr);
  2562. if (inBuffer == nullptr)
  2563. return false;
  2564. }
  2565. if (pData->audioOut.count > 0)
  2566. {
  2567. CARLA_ASSERT(outBuffer != nullptr);
  2568. if (outBuffer == nullptr)
  2569. return false;
  2570. }
  2571. uint32_t i, k;
  2572. // --------------------------------------------------------------------------------------------------------
  2573. // Try lock, silence otherwise
  2574. if (pData->engine->isOffline())
  2575. {
  2576. pData->singleMutex.lock();
  2577. }
  2578. else if (! pData->singleMutex.tryLock())
  2579. {
  2580. for (i=0; i < pData->audioOut.count; ++i)
  2581. {
  2582. for (k=0; k < frames; ++k)
  2583. outBuffer[i][k+timeOffset] = 0.0f;
  2584. }
  2585. return false;
  2586. }
  2587. // --------------------------------------------------------------------------------------------------------
  2588. // Reset audio buffers
  2589. for (i=0; i < pData->audioIn.count; ++i)
  2590. {
  2591. #ifdef HAVE_JUCE
  2592. FloatVectorOperations::copy(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  2593. #else
  2594. #endif
  2595. }
  2596. for (i=0; i < pData->audioOut.count; ++i)
  2597. {
  2598. #ifdef HAVE_JUCE
  2599. FloatVectorOperations::clear(fAudioOutBuffers[i], frames);
  2600. #else
  2601. #endif
  2602. }
  2603. // --------------------------------------------------------------------------------------------------------
  2604. // Set CV input buffers
  2605. for (i=0; i < fCvIn.count; ++i)
  2606. {
  2607. const uint32_t cvIndex(fCvIn.ports[i].param);
  2608. const float cvValue((fCvIn.ports[i].port->getBuffer()+timeOffset)[0]);
  2609. if (fParamBuffers[cvIndex] != cvValue)
  2610. {
  2611. fParamBuffers[cvIndex] = cvValue;
  2612. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(cvIndex), 0, cvValue);
  2613. }
  2614. }
  2615. // --------------------------------------------------------------------------------------------------------
  2616. // Run plugin
  2617. fDescriptor->run(fHandle, frames);
  2618. if (fHandle2 != nullptr)
  2619. fDescriptor->run(fHandle2, frames);
  2620. // --------------------------------------------------------------------------------------------------------
  2621. // Special Parameters
  2622. for (k=0; k < pData->param.count; ++k)
  2623. {
  2624. if ((pData->param.data[k].hints & PARAMETER_IS_INPUT) == 0)
  2625. continue;
  2626. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2627. {
  2628. if (fParamBuffers[k] != pData->param.ranges[k].def)
  2629. {
  2630. fParamBuffers[k] = pData->param.ranges[k].def;
  2631. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2632. }
  2633. }
  2634. }
  2635. pData->postRtEvents.trySplice();
  2636. #ifndef BUILD_BRIDGE
  2637. // --------------------------------------------------------------------------------------------------------
  2638. // Post-processing (dry/wet, volume and balance)
  2639. {
  2640. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  2641. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  2642. bool isPair;
  2643. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2644. for (i=0; i < pData->audioOut.count; ++i)
  2645. {
  2646. // Dry/Wet
  2647. if (doDryWet)
  2648. {
  2649. for (k=0; k < frames; ++k)
  2650. {
  2651. // TODO
  2652. //if (k < pData->latency && pData->latency < frames)
  2653. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  2654. //else
  2655. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2656. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  2657. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2658. }
  2659. }
  2660. // Balance
  2661. if (doBalance)
  2662. {
  2663. isPair = (i % 2 == 0);
  2664. if (isPair)
  2665. {
  2666. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2667. #ifdef HAVE_JUCE
  2668. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], frames);
  2669. #else
  2670. #endif
  2671. }
  2672. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2673. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2674. for (k=0; k < frames; ++k)
  2675. {
  2676. if (isPair)
  2677. {
  2678. // left
  2679. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2680. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2681. }
  2682. else
  2683. {
  2684. // right
  2685. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2686. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2687. }
  2688. }
  2689. }
  2690. // Volume (and buffer copy)
  2691. {
  2692. for (k=0; k < frames; ++k)
  2693. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2694. }
  2695. }
  2696. # if 0
  2697. // Latency, save values for next callback, TODO
  2698. if (pData->latency > 0 && pData->latency < frames)
  2699. {
  2700. for (i=0; i < pData->audioIn.count; ++i)
  2701. FloatVectorOperations::copy(pData->latencyBuffers[i], inBuffer[i] + (frames - pData->latency), pData->latency);
  2702. }
  2703. # endif
  2704. } // End of Post-processing
  2705. #else
  2706. for (i=0; i < pData->audioOut.count; ++i)
  2707. {
  2708. for (k=0; k < frames; ++k)
  2709. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2710. }
  2711. #endif
  2712. // --------------------------------------------------------------------------------------------------------
  2713. // Set CV output buffers
  2714. for (i=0; i < fCvOut.count; ++i)
  2715. {
  2716. const uint32_t cvIndex(fCvOut.ports[i].param);
  2717. const float cvValue(fCvOut.ports[i].port->getBuffer()[0]);
  2718. #if 0
  2719. fCvOut.ports[i].port->writeBuffer(frames, timeOffset);
  2720. #endif
  2721. fParamBuffers[cvIndex] = cvValue;
  2722. }
  2723. // --------------------------------------------------------------------------------------------------------
  2724. pData->singleMutex.unlock();
  2725. return true;
  2726. }
  2727. void bufferSizeChanged(const uint32_t newBufferSize) override
  2728. {
  2729. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2730. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2731. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2732. {
  2733. if (fAudioInBuffers[i] != nullptr)
  2734. delete[] fAudioInBuffers[i];
  2735. fAudioInBuffers[i] = new float[newBufferSize];
  2736. }
  2737. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2738. {
  2739. if (fAudioOutBuffers[i] != nullptr)
  2740. delete[] fAudioOutBuffers[i];
  2741. fAudioOutBuffers[i] = new float[newBufferSize];
  2742. }
  2743. for (uint32_t i=0; i < fCvIn.count; ++i)
  2744. {
  2745. if (CarlaEngineCVPort* const port = fCvIn.ports[i].port)
  2746. {
  2747. //port->setBufferSize(newBufferSize);
  2748. fDescriptor->connect_port(fHandle, fCvIn.ports[i].rindex, port->getBuffer());
  2749. if (fHandle2 != nullptr)
  2750. fDescriptor->connect_port(fHandle2, fCvIn.ports[i].rindex, port->getBuffer());
  2751. }
  2752. }
  2753. for (uint32_t i=0; i < fCvOut.count; ++i)
  2754. {
  2755. if (CarlaEngineCVPort* const port = fCvOut.ports[i].port)
  2756. {
  2757. //port->setBufferSize(newBufferSize);
  2758. fDescriptor->connect_port(fHandle, fCvOut.ports[i].rindex, port->getBuffer());
  2759. if (fHandle2 != nullptr)
  2760. fDescriptor->connect_port(fHandle2, fCvOut.ports[i].rindex, port->getBuffer());
  2761. }
  2762. }
  2763. if (fHandle2 == nullptr)
  2764. {
  2765. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2766. {
  2767. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2768. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2769. }
  2770. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2771. {
  2772. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2773. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2774. }
  2775. }
  2776. else
  2777. {
  2778. if (pData->audioIn.count > 0)
  2779. {
  2780. CARLA_ASSERT(pData->audioIn.count == 2);
  2781. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2782. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2783. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2784. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2785. }
  2786. if (pData->audioOut.count > 0)
  2787. {
  2788. CARLA_ASSERT(pData->audioOut.count == 2);
  2789. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2790. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2791. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2792. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2793. }
  2794. }
  2795. if (fLv2Options.maxBufferSize != static_cast<int>(newBufferSize) || (fLv2Options.minBufferSize > 1 && fLv2Options.minBufferSize != static_cast<int>(newBufferSize)))
  2796. {
  2797. fLv2Options.maxBufferSize = newBufferSize;
  2798. if (fLv2Options.minBufferSize > 1)
  2799. fLv2Options.minBufferSize = newBufferSize;
  2800. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2801. {
  2802. fExt.options->set(fHandle, &fLv2Options.optMinBlockLenth);
  2803. fExt.options->set(fHandle, &fLv2Options.optMaxBlockLenth);
  2804. }
  2805. }
  2806. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2807. }
  2808. void sampleRateChanged(const double newSampleRate) override
  2809. {
  2810. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2811. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2812. if (fLv2Options.sampleRate != newSampleRate)
  2813. {
  2814. fLv2Options.sampleRate = newSampleRate;
  2815. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2816. fExt.options->set(fHandle, &fLv2Options.optSampleRate);
  2817. }
  2818. for (uint32_t k=0; k < pData->param.count; ++k)
  2819. {
  2820. //if (pData->param.data[k].type == PARAMETER_SAMPLE_RATE)
  2821. {
  2822. fParamBuffers[k] = newSampleRate;
  2823. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2824. }
  2825. }
  2826. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2827. }
  2828. void offlineModeChanged(const bool isOffline) override
  2829. {
  2830. for (uint32_t k=0; k < pData->param.count; ++k)
  2831. {
  2832. //if (pData->param.data[k].type == PARAMETER_LV2_FREEWHEEL)
  2833. {
  2834. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  2835. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2836. }
  2837. }
  2838. }
  2839. // -------------------------------------------------------------------
  2840. // Plugin buffers
  2841. void initBuffers() override
  2842. {
  2843. fEventsIn.initBuffers();
  2844. fEventsOut.initBuffers();
  2845. fCvIn.initBuffers();
  2846. fCvOut.initBuffers();
  2847. CarlaPlugin::initBuffers();
  2848. }
  2849. void clearBuffers() override
  2850. {
  2851. carla_debug("Lv2Plugin::clearBuffers() - start");
  2852. if (fAudioInBuffers != nullptr)
  2853. {
  2854. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2855. {
  2856. if (fAudioInBuffers[i] != nullptr)
  2857. {
  2858. delete[] fAudioInBuffers[i];
  2859. fAudioInBuffers[i] = nullptr;
  2860. }
  2861. }
  2862. delete[] fAudioInBuffers;
  2863. fAudioInBuffers = nullptr;
  2864. }
  2865. if (fAudioOutBuffers != nullptr)
  2866. {
  2867. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2868. {
  2869. if (fAudioOutBuffers[i] != nullptr)
  2870. {
  2871. delete[] fAudioOutBuffers[i];
  2872. fAudioOutBuffers[i] = nullptr;
  2873. }
  2874. }
  2875. delete[] fAudioOutBuffers;
  2876. fAudioOutBuffers = nullptr;
  2877. }
  2878. if (fParamBuffers != nullptr)
  2879. {
  2880. delete[] fParamBuffers;
  2881. fParamBuffers = nullptr;
  2882. }
  2883. fEventsIn.clear();
  2884. fEventsOut.clear();
  2885. fCvIn.clear();
  2886. fCvOut.clear();
  2887. CarlaPlugin::clearBuffers();
  2888. carla_debug("Lv2Plugin::clearBuffers() - end");
  2889. }
  2890. // -------------------------------------------------------------------
  2891. // Post-poned UI Stuff
  2892. void uiParameterChange(const uint32_t index, const float value) override
  2893. {
  2894. CARLA_ASSERT(fDescriptor != nullptr);
  2895. CARLA_ASSERT(fHandle != nullptr);
  2896. CARLA_ASSERT(index < pData->param.count);
  2897. if (fDescriptor == nullptr || fHandle == nullptr)
  2898. return;
  2899. if (index >= pData->param.count)
  2900. return;
  2901. if (fUi.type == PLUGIN_UI_OSC)
  2902. {
  2903. if (pData->osc.data.target != nullptr)
  2904. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  2905. }
  2906. else
  2907. {
  2908. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2909. fUi.descriptor->port_event(fUi.handle, pData->param.data[index].rindex, sizeof(float), 0, &value);
  2910. }
  2911. }
  2912. void uiMidiProgramChange(const uint32_t index) override
  2913. {
  2914. CARLA_ASSERT(index < pData->midiprog.count);
  2915. if (index >= pData->midiprog.count)
  2916. return;
  2917. if (fUi.type == PLUGIN_UI_OSC)
  2918. {
  2919. if (pData->osc.data.target != nullptr)
  2920. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2921. }
  2922. else
  2923. {
  2924. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2925. fExt.uiprograms->select_program(fUi.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2926. }
  2927. }
  2928. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  2929. {
  2930. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2931. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2932. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  2933. if (channel >= MAX_MIDI_CHANNELS)
  2934. return;
  2935. if (note >= MAX_MIDI_NOTE)
  2936. return;
  2937. if (velo >= MAX_MIDI_VALUE)
  2938. return;
  2939. if (fUi.type == PLUGIN_UI_OSC)
  2940. {
  2941. if (pData->osc.data.target != nullptr)
  2942. {
  2943. uint8_t midiData[4] = { 0 };
  2944. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2945. midiData[2] = note;
  2946. midiData[3] = velo;
  2947. osc_send_midi(pData->osc.data, midiData);
  2948. }
  2949. }
  2950. else
  2951. {
  2952. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2953. {
  2954. LV2_Atom_MidiEvent midiEv;
  2955. midiEv.event.time.frames = 0;
  2956. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2957. midiEv.event.body.size = 3;
  2958. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2959. midiEv.data[1] = note;
  2960. midiEv.data[2] = velo;
  2961. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2962. }
  2963. }
  2964. }
  2965. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  2966. {
  2967. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2968. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2969. if (channel >= MAX_MIDI_CHANNELS)
  2970. return;
  2971. if (note >= MAX_MIDI_NOTE)
  2972. return;
  2973. if (fUi.type == PLUGIN_UI_OSC)
  2974. {
  2975. if (pData->osc.data.target != nullptr)
  2976. {
  2977. uint8_t midiData[4] = { 0 };
  2978. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2979. midiData[2] = note;
  2980. osc_send_midi(pData->osc.data, midiData);
  2981. }
  2982. }
  2983. else
  2984. {
  2985. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2986. {
  2987. LV2_Atom_MidiEvent midiEv;
  2988. midiEv.event.time.frames = 0;
  2989. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2990. midiEv.event.body.size = 3;
  2991. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2992. midiEv.data[1] = note;
  2993. midiEv.data[2] = 0;
  2994. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2995. }
  2996. }
  2997. }
  2998. // -------------------------------------------------------------------
  2999. protected:
  3000. // void guiClosedCallback() override
  3001. // {
  3002. // showGui(false);
  3003. // pData->engine->callback(CALLBACK_SHOW_GUI, pData->id, 0, 0, 0.0f, nullptr);
  3004. // }
  3005. // -------------------------------------------------------------------
  3006. LV2_URID getCustomURID(const char* const uri)
  3007. {
  3008. CARLA_ASSERT(uri != nullptr);
  3009. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  3010. if (uri == nullptr)
  3011. return CARLA_URI_MAP_ID_NULL;
  3012. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  3013. {
  3014. const char*& thisUri(fCustomURIDs.getAt(i));
  3015. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  3016. return i;
  3017. }
  3018. fCustomURIDs.append(carla_strdup(uri));
  3019. const LV2_URID urid(fCustomURIDs.count()-1);
  3020. if (fUi.type == PLUGIN_UI_OSC && pData->osc.data.target != nullptr)
  3021. osc_send_lv2_urid_map(pData->osc.data, urid, uri);
  3022. return urid;
  3023. }
  3024. const char* getCustomURIString(const LV2_URID urid)
  3025. {
  3026. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  3027. CARLA_ASSERT_INT2(urid < fCustomURIDs.count(), urid, fCustomURIDs.count());
  3028. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  3029. if (urid == CARLA_URI_MAP_ID_NULL)
  3030. return nullptr;
  3031. if (urid < fCustomURIDs.count())
  3032. return fCustomURIDs.getAt(urid);
  3033. return nullptr;
  3034. }
  3035. // -------------------------------------------------------------------
  3036. void handleProgramChanged(const int32_t index)
  3037. {
  3038. CARLA_ASSERT_INT(index >= -1, index);
  3039. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  3040. if (index == -1)
  3041. {
  3042. const ScopedSingleProcessLocker spl(this, true);
  3043. return reloadPrograms(false);
  3044. }
  3045. if (index >= 0 && index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3046. {
  3047. if (const LV2_Program_Descriptor* progDesc = fExt.programs->get_program(fHandle, index))
  3048. {
  3049. CARLA_ASSERT(progDesc->name != nullptr);
  3050. if (pData->midiprog.data[index].name != nullptr)
  3051. delete[] pData->midiprog.data[index].name;
  3052. pData->midiprog.data[index].name = carla_strdup(progDesc->name ? progDesc->name : "");
  3053. if (index == pData->midiprog.current)
  3054. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3055. else
  3056. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3057. }
  3058. }
  3059. }
  3060. // -------------------------------------------------------------------
  3061. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3062. {
  3063. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  3064. CARLA_ASSERT(value != nullptr);
  3065. CARLA_ASSERT(size > 0);
  3066. carla_debug("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i)", key, value, size, type, flags);
  3067. // basic checks
  3068. if (key == CARLA_URI_MAP_ID_NULL)
  3069. {
  3070. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  3071. return LV2_STATE_ERR_NO_PROPERTY;
  3072. }
  3073. if (value == nullptr || size == 0)
  3074. {
  3075. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid value", key, value, size, type, flags);
  3076. return LV2_STATE_ERR_NO_PROPERTY;
  3077. }
  3078. if ((flags & LV2_STATE_IS_POD) == 0)
  3079. {
  3080. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  3081. return LV2_STATE_ERR_BAD_FLAGS;
  3082. }
  3083. const char* const stype(carla_lv2_urid_unmap(this, type));
  3084. if (stype == nullptr)
  3085. {
  3086. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type", key, value, size, type, flags);
  3087. return LV2_STATE_ERR_BAD_TYPE;
  3088. }
  3089. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  3090. if (uriKey == nullptr)
  3091. {
  3092. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key URI", key, value, size, type, flags);
  3093. return LV2_STATE_ERR_NO_PROPERTY;
  3094. }
  3095. // Check if we already have this key
  3096. for (List<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3097. {
  3098. CustomData& data(*it);
  3099. if (std::strcmp(data.key, uriKey) == 0)
  3100. {
  3101. if (data.value != nullptr)
  3102. delete[] data.value;
  3103. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  3104. data.value = carla_strdup((const char*)value);
  3105. //else
  3106. //data.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  3107. return LV2_STATE_SUCCESS;
  3108. }
  3109. }
  3110. // Otherwise store it
  3111. CustomData newData;
  3112. newData.type = carla_strdup(stype);
  3113. newData.key = carla_strdup(uriKey);
  3114. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  3115. newData.value = carla_strdup((const char*)value);
  3116. //else
  3117. //newData.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  3118. pData->custom.append(newData);
  3119. return LV2_STATE_SUCCESS;
  3120. }
  3121. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3122. {
  3123. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  3124. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3125. // basic checks
  3126. if (key == CARLA_URI_MAP_ID_NULL)
  3127. {
  3128. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key", key, size, type, flags);
  3129. return nullptr;
  3130. }
  3131. if (size == nullptr || type == nullptr || flags == nullptr)
  3132. {
  3133. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid data", key, size, type, flags);
  3134. return nullptr;
  3135. }
  3136. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  3137. if (uriKey == nullptr)
  3138. {
  3139. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  3140. return nullptr;
  3141. }
  3142. const char* stype = nullptr;
  3143. const char* stringData = nullptr;
  3144. for (List<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3145. {
  3146. CustomData& data(*it);
  3147. if (std::strcmp(data.key, uriKey) == 0)
  3148. {
  3149. stype = data.type;
  3150. stringData = data.value;
  3151. break;
  3152. }
  3153. }
  3154. if (stringData == nullptr)
  3155. {
  3156. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  3157. return nullptr;
  3158. }
  3159. *type = carla_lv2_urid_map(this, stype);
  3160. *flags = LV2_STATE_IS_POD;
  3161. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  3162. {
  3163. *size = std::strlen(stringData);
  3164. return stringData;
  3165. }
  3166. else
  3167. {
  3168. // static QByteArray chunk;
  3169. // chunk = QByteArray::fromBase64(stringData);
  3170. // *size = chunk.size();
  3171. // return chunk.constData();
  3172. return nullptr;
  3173. }
  3174. }
  3175. // -------------------------------------------------------------------
  3176. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3177. {
  3178. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3179. if (fExt.worker == nullptr || fExt.worker->work == nullptr)
  3180. {
  3181. carla_stderr("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  3182. return LV2_WORKER_ERR_UNKNOWN;
  3183. }
  3184. //if (pData->engine->isOffline())
  3185. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3186. //else
  3187. // postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  3188. return LV2_WORKER_SUCCESS;
  3189. }
  3190. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3191. {
  3192. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3193. #if 0
  3194. LV2_Atom_Worker workerAtom;
  3195. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3196. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  3197. workerAtom.body.size = size;
  3198. workerAtom.body.data = data;
  3199. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  3200. #endif
  3201. return LV2_WORKER_SUCCESS;
  3202. }
  3203. // -------------------------------------------------------------------
  3204. void handleExternalUiClosed()
  3205. {
  3206. CARLA_ASSERT(fUi.type == PLUGIN_UI_EXTERNAL);
  3207. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3208. fUi.descriptor->cleanup(fUi.handle);
  3209. fUi.handle = nullptr;
  3210. fUi.widget = nullptr;
  3211. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3212. }
  3213. uint32_t handleUiPortMap(const char* const symbol)
  3214. {
  3215. CARLA_ASSERT(symbol != nullptr);
  3216. if (symbol == nullptr)
  3217. return LV2UI_INVALID_PORT_INDEX;
  3218. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3219. {
  3220. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3221. return i;
  3222. }
  3223. return LV2UI_INVALID_PORT_INDEX;
  3224. }
  3225. int handleUiResize(const int width, const int height)
  3226. {
  3227. CARLA_ASSERT(width > 0);
  3228. CARLA_ASSERT(height > 0);
  3229. if (width <= 0 || height <= 0)
  3230. return 1;
  3231. //if (pData->gui != nullptr)
  3232. // pData->gui->setSize(width, height);
  3233. return 0;
  3234. }
  3235. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3236. {
  3237. if (format == 0)
  3238. {
  3239. CARLA_ASSERT(buffer != nullptr);
  3240. CARLA_ASSERT(bufferSize == sizeof(float));
  3241. if (buffer == nullptr || bufferSize != sizeof(float))
  3242. return;
  3243. const float value(*(const float*)buffer);
  3244. for (uint32_t i=0; i < pData->param.count; ++i)
  3245. {
  3246. if (pData->param.data[i].rindex == static_cast<int32_t>(rindex))
  3247. {
  3248. if (fParamBuffers[i] != value)
  3249. setParameterValue(i, value, false, true, true);
  3250. break;
  3251. }
  3252. }
  3253. }
  3254. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM || format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3255. {
  3256. CARLA_ASSERT(bufferSize != 0);
  3257. CARLA_ASSERT(buffer != nullptr);
  3258. if (bufferSize == 0 || buffer == nullptr)
  3259. return;
  3260. fAtomQueueIn.put((const LV2_Atom*)buffer, rindex);
  3261. }
  3262. else
  3263. {
  3264. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3265. }
  3266. }
  3267. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, uint32_t size, uint32_t type)
  3268. {
  3269. CARLA_ASSERT(portSymbol != nullptr);
  3270. CARLA_ASSERT(value != nullptr);
  3271. CARLA_ASSERT(size > 0);
  3272. CARLA_ASSERT(type != CARLA_URI_MAP_ID_NULL);
  3273. if (portSymbol == nullptr)
  3274. return;
  3275. if (value == nullptr)
  3276. return;
  3277. if (size == 0)
  3278. return;
  3279. if (type == CARLA_URI_MAP_ID_NULL)
  3280. return;
  3281. const int32_t rindex(handleUiPortMap(portSymbol));
  3282. if (rindex < 0)
  3283. return;
  3284. if (size == sizeof(float) && type == CARLA_URI_MAP_ID_ATOM_FLOAT)
  3285. {
  3286. const float valuef(*(const float*)value);
  3287. for (uint32_t i=0; i < pData->param.count; ++i)
  3288. {
  3289. if (pData->param.data[i].rindex == rindex)
  3290. {
  3291. setParameterValue(i, valuef, true, true, true);
  3292. break;
  3293. }
  3294. }
  3295. }
  3296. }
  3297. // -------------------------------------------------------------------
  3298. bool isRealtimeSafe() const
  3299. {
  3300. CARLA_ASSERT(fRdfDescriptor != nullptr);
  3301. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3302. {
  3303. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3304. return true;
  3305. }
  3306. return false;
  3307. }
  3308. bool needsFixedBuffer() const
  3309. {
  3310. CARLA_ASSERT(fRdfDescriptor != nullptr);
  3311. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3312. {
  3313. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3314. return true;
  3315. }
  3316. return false;
  3317. }
  3318. // -------------------------------------------------------------------
  3319. const char* getUiBridgePath(const LV2_Property type) const
  3320. {
  3321. const EngineOptions& options(pData->engine->getOptions());
  3322. switch (type)
  3323. {
  3324. // case LV2_UI_GTK2:
  3325. // return options.bridge_lv2Gtk2;
  3326. // case LV2_UI_GTK3:
  3327. // return options.bridge_lv2Gtk3;
  3328. // case LV2_UI_QT4:
  3329. // return options.bridge_lv2Qt4;
  3330. // case LV2_UI_QT5:
  3331. // return options.bridge_lv2Qt5;
  3332. // case LV2_UI_COCOA:
  3333. // return options.bridge_lv2Cocoa;
  3334. // case LV2_UI_WINDOWS:
  3335. // return options.bridge_lv2Win;
  3336. // case LV2_UI_X11:
  3337. // return options.bridge_lv2X11;
  3338. default:
  3339. return nullptr;
  3340. }
  3341. }
  3342. bool isUiBridgeable(const uint32_t uiId) const
  3343. {
  3344. const LV2_RDF_UI& rdfUi(fRdfDescriptor->UIs[uiId]);
  3345. for (uint32_t i=0; i < rdfUi.FeatureCount; ++i)
  3346. {
  3347. if (std::strcmp(rdfUi.Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3348. return false;
  3349. if (std::strcmp(rdfUi.Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3350. return false;
  3351. }
  3352. return true;
  3353. }
  3354. bool isUiResizable() const
  3355. {
  3356. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3357. {
  3358. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3359. return false;
  3360. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3361. return false;
  3362. }
  3363. return true;
  3364. }
  3365. void updateUi()
  3366. {
  3367. CARLA_ASSERT(fUi.handle != nullptr);
  3368. CARLA_ASSERT(fUi.descriptor != nullptr);
  3369. fExt.uiidle = nullptr;
  3370. fExt.uiprograms = nullptr;
  3371. if (fUi.descriptor->extension_data != nullptr)
  3372. {
  3373. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  3374. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  3375. // check if invalid
  3376. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  3377. fExt.uiidle = nullptr;
  3378. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  3379. fExt.uiprograms = nullptr;
  3380. // update midi program
  3381. if (fExt.uiprograms && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3382. fExt.uiprograms->select_program(fUi.handle, pData->midiprog.data[pData->midiprog.current].bank,
  3383. pData->midiprog.data[pData->midiprog.current].program);
  3384. }
  3385. if (fUi.descriptor->port_event != nullptr)
  3386. {
  3387. // update control ports
  3388. float value;
  3389. for (uint32_t i=0; i < pData->param.count; ++i)
  3390. {
  3391. value = getParameterValue(i);
  3392. fUi.descriptor->port_event(fUi.handle, pData->param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3393. }
  3394. }
  3395. }
  3396. // -------------------------------------------------------------------
  3397. public:
  3398. bool init(const char* const name, const char* const uri)
  3399. {
  3400. CARLA_ASSERT(pData->engine != nullptr);
  3401. CARLA_ASSERT(pData->client == nullptr);
  3402. CARLA_ASSERT(uri != nullptr);
  3403. // ---------------------------------------------------------------
  3404. // first checks
  3405. if (pData->engine == nullptr)
  3406. {
  3407. return false;
  3408. }
  3409. if (pData->client != nullptr)
  3410. {
  3411. pData->engine->setLastError("Plugin client is already registered");
  3412. return false;
  3413. }
  3414. if (uri == nullptr)
  3415. {
  3416. pData->engine->setLastError("null uri");
  3417. return false;
  3418. }
  3419. // ---------------------------------------------------------------
  3420. // get plugin from lv2_rdf (lilv)
  3421. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  3422. lv2World.init();
  3423. fRdfDescriptor = lv2_rdf_new(uri, true);
  3424. if (fRdfDescriptor == nullptr)
  3425. {
  3426. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3427. return false;
  3428. }
  3429. // ---------------------------------------------------------------
  3430. // open DLL
  3431. if (! pData->libOpen(fRdfDescriptor->Binary))
  3432. {
  3433. pData->engine->setLastError(lib_error(fRdfDescriptor->Binary));
  3434. return false;
  3435. }
  3436. // ---------------------------------------------------------------
  3437. // initialize options
  3438. fLv2Options.minBufferSize = 1;
  3439. fLv2Options.maxBufferSize = pData->engine->getBufferSize();
  3440. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3441. // ---------------------------------------------------------------
  3442. // initialize features (part 1)
  3443. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3444. eventFt->callback_data = this;
  3445. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3446. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3447. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3448. logFt->handle = this;
  3449. logFt->printf = carla_lv2_log_printf;
  3450. logFt->vprintf = carla_lv2_log_vprintf;
  3451. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3452. stateMakePathFt->handle = this;
  3453. stateMakePathFt->path = carla_lv2_state_make_path;
  3454. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3455. stateMapPathFt->handle = this;
  3456. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3457. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3458. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3459. programsFt->handle = this;
  3460. programsFt->program_changed = carla_lv2_program_changed;
  3461. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3462. lv2_rtmempool_init(rtMemPoolFt);
  3463. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3464. uriMapFt->callback_data = this;
  3465. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3466. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3467. uridMapFt->handle = this;
  3468. uridMapFt->map = carla_lv2_urid_map;
  3469. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3470. uridUnmapFt->handle = this;
  3471. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3472. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3473. workerFt->handle = this;
  3474. workerFt->schedule_work = carla_lv2_worker_schedule;
  3475. // ---------------------------------------------------------------
  3476. // initialize features (part 2)
  3477. for (uint32_t i=0; i < kFeatureIdWorker+1; ++i)
  3478. fFeatures[i] = new LV2_Feature;
  3479. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3480. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3481. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3482. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3483. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3484. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3485. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3486. fFeatures[kFeatureIdEvent]->data = eventFt;
  3487. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3488. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3489. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3490. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3491. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3492. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3493. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3494. fFeatures[kFeatureIdLogs]->data = logFt;
  3495. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3496. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3497. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3498. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3499. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3500. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3501. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3502. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3503. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3504. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3505. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3506. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3507. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3508. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3509. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3510. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3511. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3512. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3513. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3514. fFeatures[kFeatureIdWorker]->data = workerFt;
  3515. // check if it's possible to use non-fixed buffer size
  3516. if (! needsFixedBuffer())
  3517. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3518. // ---------------------------------------------------------------
  3519. // get DLL main entry
  3520. #if 0
  3521. const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)pData->libSymbol("lv2_lib_descriptor");
  3522. if (libDescFn != nullptr)
  3523. {
  3524. // -----------------------------------------------------------
  3525. // get lib descriptor
  3526. const LV2_Lib_Descriptor* libFn = nullptr; //descLibFn(fRdfDescriptor->Bundle, features);
  3527. if (libFn == nullptr || libFn->get_plugin == nullptr)
  3528. {
  3529. pData->engine->setLastError("Plugin failed to return library descriptor");
  3530. return false;
  3531. }
  3532. // -----------------------------------------------------------
  3533. // get descriptor that matches URI
  3534. uint32_t i = 0;
  3535. while ((fDescriptor = libFn->get_plugin(libFn->handle, i++)))
  3536. {
  3537. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3538. break;
  3539. }
  3540. if (fDescriptor == nullptr)
  3541. libFn->cleanup(libFn->handle);
  3542. else
  3543. #endif
  3544. {
  3545. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  3546. if (descFn == nullptr)
  3547. {
  3548. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3549. return false;
  3550. }
  3551. // -----------------------------------------------------------
  3552. // get descriptor that matches URI
  3553. uint32_t i = 0;
  3554. while ((fDescriptor = descFn(i++)))
  3555. {
  3556. carla_debug("LV2 Init @%i => '%s' vs '%s'", i, fDescriptor->URI, uri);
  3557. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3558. break;
  3559. }
  3560. }
  3561. if (fDescriptor == nullptr)
  3562. {
  3563. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3564. return false;
  3565. }
  3566. // ---------------------------------------------------------------
  3567. // check supported port-types and features
  3568. bool canContinue = true;
  3569. // Check supported ports
  3570. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3571. {
  3572. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  3573. if (! is_lv2_port_supported(portTypes))
  3574. {
  3575. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[i].Properties))
  3576. {
  3577. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3578. canContinue = false;
  3579. break;
  3580. }
  3581. }
  3582. }
  3583. // Check supported features
  3584. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount && canContinue; ++i)
  3585. {
  3586. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[i].Type) && ! is_lv2_feature_supported(fRdfDescriptor->Features[i].URI))
  3587. {
  3588. // QString msg(QString("Plugin requires a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[i].URI));
  3589. // pData->engine->setLastError(msg.toUtf8().constData());
  3590. canContinue = false;
  3591. break;
  3592. }
  3593. }
  3594. // Check extensions
  3595. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3596. {
  3597. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3598. pData->extraHints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3599. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3600. pData->extraHints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3601. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3602. pData->extraHints |= PLUGIN_HAS_EXTENSION_STATE;
  3603. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3604. pData->extraHints |= PLUGIN_HAS_EXTENSION_WORKER;
  3605. else
  3606. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3607. }
  3608. if (! canContinue)
  3609. {
  3610. // error already set
  3611. return false;
  3612. }
  3613. // ---------------------------------------------------------------
  3614. // get info
  3615. if (name != nullptr)
  3616. pData->name = pData->engine->getUniquePluginName(name);
  3617. else
  3618. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3619. // ---------------------------------------------------------------
  3620. // register client
  3621. pData->client = pData->engine->addClient(this);
  3622. if (pData->client == nullptr || ! pData->client->isOk())
  3623. {
  3624. pData->engine->setLastError("Failed to register plugin client");
  3625. return false;
  3626. }
  3627. // ---------------------------------------------------------------
  3628. // initialize plugin
  3629. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3630. if (fHandle == nullptr)
  3631. {
  3632. pData->engine->setLastError("Plugin failed to initialize");
  3633. return false;
  3634. }
  3635. // ---------------------------------------------------------------
  3636. // load plugin settings
  3637. {
  3638. // set default options
  3639. pData->options = 0x0;
  3640. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3641. if (getMidiInCount() > 0 || needsFixedBuffer())
  3642. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3643. if (pData->engine->getOptions().forceStereo)
  3644. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  3645. if (getMidiInCount() > 0)
  3646. {
  3647. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3648. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3649. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  3650. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3651. }
  3652. // load settings
  3653. pData->idStr = "LV2/";
  3654. pData->idStr += uri;
  3655. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  3656. // ignore settings, we need this anyway
  3657. if (getMidiInCount() > 0 || needsFixedBuffer())
  3658. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3659. }
  3660. // ---------------------------------------------------------------
  3661. // gui stuff
  3662. if (fRdfDescriptor->UICount == 0)
  3663. return true;
  3664. // -----------------------------------------------------------
  3665. // find more appropriate ui
  3666. int eQt4, eQt5, eCocoa, eWindows, eX11, eGtk2, eGtk3, iCocoa, iWindows, iX11, iQt4, iQt5, iExt, iFinal;
  3667. eQt4 = eQt5 = eCocoa = eWindows = eX11 = eGtk2 = eGtk3 = iQt4 = iQt5 = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3668. const bool preferUiBridges(true);
  3669. //#ifdef BUILD_BRIDGE
  3670. // const bool preferUiBridges(pData->engine->getOptions().preferUiBridges);
  3671. //#else
  3672. // const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  3673. //#endif
  3674. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3675. {
  3676. CARLA_ASSERT(fRdfDescriptor->UIs[i].URI != nullptr);
  3677. if (fRdfDescriptor->UIs[i].URI == nullptr)
  3678. {
  3679. carla_stderr("Plugin has an UI without a valid URI");
  3680. continue;
  3681. }
  3682. switch (fRdfDescriptor->UIs[i].Type)
  3683. {
  3684. #if 0//(QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3685. case LV2_UI_QT4:
  3686. if (isUiBridgeable(i))
  3687. eQt4 = i;
  3688. break;
  3689. #else
  3690. case LV2_UI_QT4:
  3691. if (isUiBridgeable(i) && preferUiBridges)
  3692. eQt4 = i;
  3693. iQt4 = i;
  3694. break;
  3695. #endif
  3696. #if 0//(QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3697. case LV2_UI_QT5:
  3698. if (isUiBridgeable(i) && preferUiBridges)
  3699. eQt5 = i;
  3700. iQt5 = i;
  3701. break;
  3702. #else
  3703. case LV2_UI_QT5:
  3704. if (isUiBridgeable(i) && preferUiBridges)
  3705. eQt5 = i;
  3706. break;
  3707. #endif
  3708. #ifdef CARLA_OS_MAC
  3709. case LV2_UI_COCOA:
  3710. if (isUiBridgeable(i) && preferUiBridges)
  3711. eCocoa = i;
  3712. iCocoa = i;
  3713. break;
  3714. #endif
  3715. #ifdef CARLA_OS_WIN
  3716. case LV2_UI_WINDOWS:
  3717. if (isUiBridgeable(i) && preferUiBridges)
  3718. eWindows = i;
  3719. iWindows = i;
  3720. break;
  3721. #endif
  3722. case LV2_UI_X11:
  3723. if (isUiBridgeable(i) && preferUiBridges)
  3724. eX11 = i;
  3725. #ifdef Q_WS_X11
  3726. iX11 = i;
  3727. #endif
  3728. break;
  3729. case LV2_UI_GTK2:
  3730. if (isUiBridgeable(i))
  3731. eGtk2 = i;
  3732. break;
  3733. case LV2_UI_GTK3:
  3734. if (isUiBridgeable(i))
  3735. eGtk3 = i;
  3736. break;
  3737. case LV2_UI_EXTERNAL:
  3738. case LV2_UI_OLD_EXTERNAL:
  3739. iExt = i;
  3740. break;
  3741. default:
  3742. break;
  3743. }
  3744. }
  3745. if (eQt4 >= 0)
  3746. iFinal = eQt4;
  3747. else if (eQt5 >= 0)
  3748. iFinal = eQt5;
  3749. else if (eCocoa >= 0)
  3750. iFinal = eCocoa;
  3751. else if (eWindows >= 0)
  3752. iFinal = eWindows;
  3753. else if (eX11 >= 0)
  3754. iFinal = eX11;
  3755. else if (iQt4 >= 0)
  3756. iFinal = iQt4;
  3757. else if (iQt5 >= 0)
  3758. iFinal = iQt5;
  3759. else if (iCocoa >= 0)
  3760. iFinal = iCocoa;
  3761. else if (iWindows >= 0)
  3762. iFinal = iWindows;
  3763. else if (iX11 >= 0)
  3764. iFinal = iX11;
  3765. else if (iExt >= 0)
  3766. iFinal = iExt;
  3767. else if (eGtk2 >= 0)
  3768. iFinal = eGtk2;
  3769. else if (eGtk3 >= 0)
  3770. iFinal = eGtk3;
  3771. if (iFinal < 0)
  3772. {
  3773. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3774. return true;
  3775. }
  3776. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3777. // -----------------------------------------------------------
  3778. // check supported ui features
  3779. canContinue = true;
  3780. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3781. {
  3782. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type) && ! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3783. {
  3784. carla_stderr2("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3785. canContinue = false;
  3786. break;
  3787. }
  3788. }
  3789. if (! canContinue)
  3790. {
  3791. fUi.rdfDescriptor = nullptr;
  3792. return true;
  3793. }
  3794. // -----------------------------------------------------------
  3795. // initialize ui according to type
  3796. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3797. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3)
  3798. {
  3799. // -------------------------------------------------------
  3800. // initialize ui bridge
  3801. if (const char* const oscBinary = getUiBridgePath(uiType))
  3802. {
  3803. fUi.type = PLUGIN_UI_OSC;
  3804. pData->osc.thread.setOscData(oscBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3805. }
  3806. }
  3807. else
  3808. {
  3809. // -------------------------------------------------------
  3810. // open UI DLL
  3811. if (! pData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3812. {
  3813. carla_stderr2("Could not load UI library, error was:\n%s", lib_error(fUi.rdfDescriptor->Binary));
  3814. fUi.rdfDescriptor = nullptr;
  3815. return true;
  3816. }
  3817. // -------------------------------------------------------
  3818. // get UI DLL main entry
  3819. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor");
  3820. if (uiDescFn == nullptr)
  3821. {
  3822. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3823. pData->uiLibClose();
  3824. fUi.rdfDescriptor = nullptr;
  3825. return true;
  3826. }
  3827. // -------------------------------------------------------
  3828. // get UI descriptor that matches UI URI
  3829. uint32_t i = 0;
  3830. while ((fUi.descriptor = uiDescFn(i++)))
  3831. {
  3832. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3833. break;
  3834. }
  3835. if (fUi.descriptor == nullptr)
  3836. {
  3837. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3838. pData->uiLibClose();
  3839. fUi.rdfDescriptor = nullptr;
  3840. return true;
  3841. }
  3842. // -------------------------------------------------------
  3843. // check if ui is usable
  3844. switch (uiType)
  3845. {
  3846. #if 0//(QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3847. case LV2_UI_QT5:
  3848. carla_debug("Will use LV2 Qt5 UI");
  3849. fUi.type = PLUGIN_UI_QT;
  3850. break;
  3851. #else
  3852. case LV2_UI_QT4:
  3853. carla_debug("Will use LV2 Qt4 UI");
  3854. fUi.type = PLUGIN_UI_QT;
  3855. break;
  3856. #endif
  3857. #ifdef CARLA_OS_MAC
  3858. case LV2_UI_COCOA:
  3859. carla_debug("Will use LV2 Cocoa UI");
  3860. fUi.type = PLUGIN_UI_PARENT;
  3861. break;
  3862. #endif
  3863. #ifdef CARLA_OS_WIN
  3864. case LV2_UI_WINDOWS:
  3865. carla_debug("Will use LV2 Windows UI");
  3866. fUi.type = PLUGIN_UI_PARENT;
  3867. break;
  3868. #endif
  3869. #ifdef Q_WS_X11
  3870. case LV2_UI_X11:
  3871. carla_debug("Will use LV2 X11 UI");
  3872. fUi.type = PLUGIN_UI_PARENT;
  3873. break;
  3874. #endif
  3875. case LV2_UI_GTK2:
  3876. carla_debug("Will use LV2 Gtk2 UI, NOT!");
  3877. break;
  3878. case LV2_UI_GTK3:
  3879. carla_debug("Will use LV2 Gtk3 UI, NOT!");
  3880. break;
  3881. case LV2_UI_EXTERNAL:
  3882. case LV2_UI_OLD_EXTERNAL:
  3883. carla_debug("Will use LV2 External UI");
  3884. fUi.type = PLUGIN_UI_EXTERNAL;
  3885. break;
  3886. }
  3887. if (fUi.type == PLUGIN_UI_NULL)
  3888. {
  3889. pData->uiLibClose();
  3890. fUi.descriptor = nullptr;
  3891. fUi.rdfDescriptor = nullptr;
  3892. return true;
  3893. }
  3894. // -------------------------------------------------------
  3895. // initialize ui features (part 1)
  3896. //QString guiTitle(QString("%1 (GUI)").arg((const char*)pData->name));
  3897. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3898. uiDataFt->data_access = fDescriptor->extension_data;
  3899. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3900. uiPortMapFt->handle = this;
  3901. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3902. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3903. uiResizeFt->handle = this;
  3904. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3905. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3906. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3907. uiExternalHostFt->plugin_human_id = nullptr; //carla_strdup(guiTitle.toUtf8().constData());
  3908. // -------------------------------------------------------
  3909. // initialize ui features (part 2)
  3910. for (uint32_t i=kFeatureIdUiDataAccess; i < kFeatureCount; ++i)
  3911. fFeatures[i] = new LV2_Feature;
  3912. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3913. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3914. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3915. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3916. fFeatures[kFeatureIdUiIdle]->URI = LV2_UI__idle;
  3917. fFeatures[kFeatureIdUiIdle]->data = nullptr;
  3918. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  3919. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  3920. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  3921. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  3922. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  3923. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  3924. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3925. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3926. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3927. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3928. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  3929. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  3930. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3931. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3932. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  3933. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  3934. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3935. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3936. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3937. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3938. }
  3939. return true;
  3940. }
  3941. // -------------------------------------------------------------------
  3942. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  3943. {
  3944. CARLA_ASSERT(atom != nullptr);
  3945. carla_debug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  3946. fAtomQueueIn.put(atom, portIndex);
  3947. }
  3948. void handleUridMap(const LV2_URID urid, const char* const uri)
  3949. {
  3950. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  3951. CARLA_ASSERT(uri != nullptr);
  3952. carla_debug("Lv2Plugin::handleUridMap(%i, \"%s\")", urid, uri);
  3953. CARLA_SAFE_ASSERT_INT2(urid == fCustomURIDs.count(), urid, fCustomURIDs.count());
  3954. if (urid != fCustomURIDs.count())
  3955. return;
  3956. fCustomURIDs.append(carla_strdup(uri));
  3957. }
  3958. // -------------------------------------------------------------------
  3959. private:
  3960. LV2_Handle fHandle;
  3961. LV2_Handle fHandle2;
  3962. LV2_Feature* fFeatures[kFeatureCount+1];
  3963. const LV2_Descriptor* fDescriptor;
  3964. const LV2_RDF_Descriptor* fRdfDescriptor;
  3965. float** fAudioInBuffers;
  3966. float** fAudioOutBuffers;
  3967. float* fParamBuffers;
  3968. Lv2AtomQueue fAtomQueueIn;
  3969. Lv2AtomQueue fAtomQueueOut;
  3970. LV2_Atom_Forge fAtomForge;
  3971. PluginCVData fCvIn;
  3972. PluginCVData fCvOut;
  3973. Lv2PluginEventData fEventsIn;
  3974. Lv2PluginEventData fEventsOut;
  3975. Lv2PluginOptions fLv2Options;
  3976. List<const char*> fCustomURIDs;
  3977. bool fFirstActive; // first process() call after activate()
  3978. EngineTimeInfo fLastTimeInfo;
  3979. struct Extensions {
  3980. const LV2_Options_Interface* options;
  3981. const LV2_State_Interface* state;
  3982. const LV2_Worker_Interface* worker;
  3983. const LV2_Programs_Interface* programs;
  3984. const LV2UI_Idle_Interface* uiidle;
  3985. const LV2_Programs_UI_Interface* uiprograms;
  3986. Extensions()
  3987. : options(nullptr),
  3988. state(nullptr),
  3989. worker(nullptr),
  3990. programs(nullptr),
  3991. uiidle(nullptr),
  3992. uiprograms(nullptr) {}
  3993. } fExt;
  3994. struct UI {
  3995. Lv2PluginGuiType type;
  3996. LV2UI_Handle handle;
  3997. LV2UI_Widget widget;
  3998. const LV2UI_Descriptor* descriptor;
  3999. const LV2_RDF_UI* rdfDescriptor;
  4000. UI()
  4001. : type(PLUGIN_UI_NULL),
  4002. handle(nullptr),
  4003. widget(nullptr),
  4004. descriptor(nullptr),
  4005. rdfDescriptor(nullptr) {}
  4006. ~UI()
  4007. {
  4008. CARLA_ASSERT(handle == nullptr);
  4009. CARLA_ASSERT(widget == nullptr);
  4010. CARLA_ASSERT(descriptor == nullptr);
  4011. CARLA_ASSERT(rdfDescriptor == nullptr);
  4012. }
  4013. } fUi;
  4014. // -------------------------------------------------------------------
  4015. // Event Feature
  4016. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4017. {
  4018. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  4019. CARLA_ASSERT(callback_data != nullptr);
  4020. CARLA_ASSERT(event != nullptr);
  4021. return 0;
  4022. }
  4023. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4024. {
  4025. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  4026. CARLA_ASSERT(callback_data != nullptr);
  4027. CARLA_ASSERT(event != nullptr);
  4028. return 0;
  4029. }
  4030. // -------------------------------------------------------------------
  4031. // Logs Feature
  4032. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  4033. {
  4034. CARLA_ASSERT(handle != nullptr);
  4035. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  4036. #ifndef DEBUG
  4037. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4038. return 0;
  4039. #endif
  4040. va_list args;
  4041. va_start(args, fmt);
  4042. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  4043. va_end(args);
  4044. return ret;
  4045. }
  4046. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  4047. {
  4048. CARLA_ASSERT(handle != nullptr);
  4049. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  4050. #ifndef DEBUG
  4051. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4052. return 0;
  4053. #endif
  4054. int ret = 0;
  4055. switch (type)
  4056. {
  4057. case CARLA_URI_MAP_ID_LOG_ERROR:
  4058. #ifndef CARLA_OS_WIN
  4059. std::fprintf(stderr, "\x1b[31m");
  4060. #endif
  4061. ret = std::vfprintf(stderr, fmt, ap);
  4062. #ifndef CARLA_OS_WIN
  4063. std::fprintf(stderr, "\x1b[0m");
  4064. #endif
  4065. break;
  4066. case CARLA_URI_MAP_ID_LOG_NOTE:
  4067. ret = std::vfprintf(stdout, fmt, ap);
  4068. break;
  4069. case CARLA_URI_MAP_ID_LOG_TRACE:
  4070. #ifdef DEBUG
  4071. # ifndef CARLA_OS_WIN
  4072. std::fprintf(stdout, "\x1b[30;1m");
  4073. # endif
  4074. ret = std::vfprintf(stdout, fmt, ap);
  4075. # ifndef CARLA_OS_WIN
  4076. std::fprintf(stdout, "\x1b[0m");
  4077. # endif
  4078. #endif
  4079. break;
  4080. case CARLA_URI_MAP_ID_LOG_WARNING:
  4081. ret = std::vfprintf(stderr, fmt, ap);
  4082. break;
  4083. default:
  4084. break;
  4085. }
  4086. return ret;
  4087. }
  4088. // -------------------------------------------------------------------
  4089. // Programs Feature
  4090. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  4091. {
  4092. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  4093. CARLA_ASSERT(handle != nullptr);
  4094. if (handle == nullptr)
  4095. return;
  4096. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  4097. }
  4098. // -------------------------------------------------------------------
  4099. // State Feature
  4100. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  4101. {
  4102. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4103. CARLA_ASSERT(handle != nullptr);
  4104. CARLA_ASSERT(path != nullptr);
  4105. if (path == nullptr)
  4106. return nullptr;
  4107. //QDir dir;
  4108. //dir.mkpath(path);
  4109. //return strdup(path);
  4110. return nullptr;
  4111. }
  4112. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4113. {
  4114. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4115. CARLA_ASSERT(handle != nullptr);
  4116. CARLA_ASSERT(absolute_path != nullptr);
  4117. if (absolute_path == nullptr)
  4118. return nullptr;
  4119. //QDir dir(absolute_path);
  4120. //return strdup(dir.canonicalPath().toUtf8().constData());
  4121. return nullptr;
  4122. }
  4123. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4124. {
  4125. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4126. CARLA_ASSERT(handle != nullptr);
  4127. CARLA_ASSERT(abstract_path != nullptr);
  4128. if (abstract_path == nullptr)
  4129. return nullptr;
  4130. //QDir dir(abstract_path);
  4131. //return strdup(dir.absolutePath().toUtf8().constData());
  4132. return nullptr;
  4133. }
  4134. 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)
  4135. {
  4136. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4137. CARLA_ASSERT(handle != nullptr);
  4138. if (handle == nullptr)
  4139. return LV2_STATE_ERR_UNKNOWN;
  4140. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4141. }
  4142. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4143. {
  4144. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4145. CARLA_ASSERT(handle != nullptr);
  4146. if (handle == nullptr)
  4147. return nullptr;
  4148. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4149. }
  4150. // -------------------------------------------------------------------
  4151. // URI-Map Feature
  4152. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4153. {
  4154. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4155. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4156. // unused
  4157. (void)map;
  4158. }
  4159. // -------------------------------------------------------------------
  4160. // URID Feature
  4161. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4162. {
  4163. CARLA_ASSERT(handle != nullptr);
  4164. CARLA_ASSERT(uri != nullptr);
  4165. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4166. if (uri == nullptr)
  4167. return CARLA_URI_MAP_ID_NULL;
  4168. // Atom types
  4169. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4170. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4171. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4172. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4173. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4174. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4175. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4176. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4177. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4178. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4179. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4180. return CARLA_URI_MAP_ID_ATOM_INT;
  4181. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4182. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4183. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4184. return CARLA_URI_MAP_ID_ATOM_LONG;
  4185. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4186. return CARLA_URI_MAP_ID_ATOM_PATH;
  4187. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4188. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4189. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4190. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4191. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4192. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4193. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4194. return CARLA_URI_MAP_ID_ATOM_STRING;
  4195. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4196. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4197. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4198. return CARLA_URI_MAP_ID_ATOM_URI;
  4199. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4200. return CARLA_URI_MAP_ID_ATOM_URID;
  4201. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4202. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4203. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4204. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4205. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4206. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4207. // BufSize types
  4208. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4209. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4210. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4211. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4212. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4213. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4214. // Log types
  4215. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4216. return CARLA_URI_MAP_ID_LOG_ERROR;
  4217. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4218. return CARLA_URI_MAP_ID_LOG_NOTE;
  4219. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4220. return CARLA_URI_MAP_ID_LOG_TRACE;
  4221. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4222. return CARLA_URI_MAP_ID_LOG_WARNING;
  4223. // Time types
  4224. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4225. return CARLA_URI_MAP_ID_TIME_POSITION;
  4226. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4227. return CARLA_URI_MAP_ID_TIME_BAR;
  4228. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4229. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4230. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4231. return CARLA_URI_MAP_ID_TIME_BEAT;
  4232. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4233. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4234. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4235. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4236. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4237. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4238. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4239. return CARLA_URI_MAP_ID_TIME_FRAME;
  4240. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4241. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4242. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4243. return CARLA_URI_MAP_ID_TIME_SPEED;
  4244. // Others
  4245. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4246. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4247. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4248. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4249. if (handle == nullptr)
  4250. return CARLA_URI_MAP_ID_NULL;
  4251. // Custom types
  4252. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4253. }
  4254. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4255. {
  4256. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4257. CARLA_ASSERT(handle != nullptr);
  4258. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  4259. if (urid == CARLA_URI_MAP_ID_NULL)
  4260. return nullptr;
  4261. // Atom types
  4262. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4263. return LV2_ATOM__Blank;
  4264. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4265. return LV2_ATOM__Bool;
  4266. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4267. return LV2_ATOM__Chunk;
  4268. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4269. return LV2_ATOM__Double;
  4270. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4271. return LV2_ATOM__Float;
  4272. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4273. return LV2_ATOM__Int;
  4274. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4275. return LV2_ATOM__Literal;
  4276. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4277. return LV2_ATOM__Long;
  4278. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4279. return LV2_ATOM__Path;
  4280. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4281. return LV2_ATOM__Property;
  4282. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4283. return LV2_ATOM__Resource;
  4284. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4285. return LV2_ATOM__Sequence;
  4286. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4287. return LV2_ATOM__String;
  4288. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4289. return LV2_ATOM__Tuple;
  4290. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4291. return LV2_ATOM__URI;
  4292. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4293. return LV2_ATOM__URID;
  4294. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4295. return LV2_ATOM__Vector;
  4296. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4297. return LV2_ATOM__atomTransfer;
  4298. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4299. return LV2_ATOM__eventTransfer;
  4300. // BufSize types
  4301. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4302. return LV2_BUF_SIZE__maxBlockLength;
  4303. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4304. return LV2_BUF_SIZE__minBlockLength;
  4305. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4306. return LV2_BUF_SIZE__sequenceSize;
  4307. // Log types
  4308. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4309. return LV2_LOG__Error;
  4310. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4311. return LV2_LOG__Note;
  4312. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4313. return LV2_LOG__Trace;
  4314. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4315. return LV2_LOG__Warning;
  4316. // Time types
  4317. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4318. return LV2_TIME__Position;
  4319. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4320. return LV2_TIME__bar;
  4321. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4322. return LV2_TIME__barBeat;
  4323. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4324. return LV2_TIME__beat;
  4325. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4326. return LV2_TIME__beatUnit;
  4327. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4328. return LV2_TIME__beatsPerBar;
  4329. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4330. return LV2_TIME__beatsPerMinute;
  4331. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4332. return LV2_TIME__frame;
  4333. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4334. return LV2_TIME__framesPerSecond;
  4335. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4336. return LV2_TIME__speed;
  4337. // Others
  4338. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4339. return LV2_MIDI__MidiEvent;
  4340. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4341. return LV2_PARAMETERS__sampleRate;
  4342. if (handle == nullptr)
  4343. return nullptr;
  4344. // Custom types
  4345. return ((Lv2Plugin*)handle)->getCustomURIString(urid);
  4346. }
  4347. // -------------------------------------------------------------------
  4348. // Worker Feature
  4349. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4350. {
  4351. CARLA_ASSERT(handle != nullptr);
  4352. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4353. if (handle == nullptr)
  4354. return LV2_WORKER_ERR_UNKNOWN;
  4355. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4356. }
  4357. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4358. {
  4359. CARLA_ASSERT(handle != nullptr);
  4360. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4361. if (handle == nullptr)
  4362. return LV2_WORKER_ERR_UNKNOWN;
  4363. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4364. }
  4365. // -------------------------------------------------------------------
  4366. // UI Port-Map Feature
  4367. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4368. {
  4369. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4370. CARLA_ASSERT(handle);
  4371. if (handle == nullptr)
  4372. return LV2UI_INVALID_PORT_INDEX;
  4373. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4374. }
  4375. // -------------------------------------------------------------------
  4376. // UI Resize Feature
  4377. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4378. {
  4379. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4380. CARLA_ASSERT(handle != nullptr);
  4381. if (handle == nullptr)
  4382. return 1;
  4383. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4384. }
  4385. // -------------------------------------------------------------------
  4386. // External UI Feature
  4387. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4388. {
  4389. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4390. CARLA_ASSERT(controller != nullptr);
  4391. if (controller == nullptr)
  4392. return;
  4393. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4394. }
  4395. // -------------------------------------------------------------------
  4396. // UI Extension
  4397. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4398. {
  4399. CARLA_ASSERT(controller != nullptr);
  4400. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4401. if (controller == nullptr)
  4402. return;
  4403. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4404. }
  4405. // -------------------------------------------------------------------
  4406. // Lilv State
  4407. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4408. {
  4409. CARLA_ASSERT(user_data != nullptr);
  4410. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i)", port_symbol, user_data, value, size, type);
  4411. if (user_data == nullptr)
  4412. return;
  4413. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4414. }
  4415. // -------------------------------------------------------------------
  4416. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4417. };
  4418. // -------------------------------------------------------------------------------------------------------------------
  4419. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4420. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4421. {
  4422. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4423. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4424. const int32_t portIndex = argv[0]->i;
  4425. const char* const atomBuf = (const char*)&argv[1]->s;
  4426. if (portIndex < 0)
  4427. return 0;
  4428. // QByteArray chunk;
  4429. // chunk = QByteArray::fromBase64(atomBuf);
  4430. //
  4431. // LV2_Atom* const atom = (LV2_Atom*)chunk.data();
  4432. // lv2PluginPtr->handleTransferAtom(portIndex, atom);
  4433. return 0;
  4434. }
  4435. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4436. {
  4437. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4438. carla_debug("CarlaOsc::handleMsgLv2EventTransfer()");
  4439. const int32_t urid = argv[0]->i;
  4440. const char* const uri = (const char*)&argv[1]->s;
  4441. if (urid <= 0)
  4442. return 0;
  4443. lv2PluginPtr->handleUridMap(urid, uri);
  4444. return 0;
  4445. }
  4446. #undef lv2PluginPtr
  4447. CARLA_BACKEND_END_NAMESPACE
  4448. #endif // WANT_LV2
  4449. CARLA_BACKEND_START_NAMESPACE
  4450. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4451. {
  4452. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  4453. #ifdef WANT_LV2
  4454. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4455. if (! plugin->init(init.name, init.label))
  4456. {
  4457. delete plugin;
  4458. return nullptr;
  4459. }
  4460. plugin->reload();
  4461. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  4462. {
  4463. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4464. delete plugin;
  4465. return nullptr;
  4466. }
  4467. return plugin;
  4468. #else
  4469. init.engine->setLastError("LV2 support not available");
  4470. return nullptr;
  4471. #endif
  4472. }
  4473. CARLA_BACKEND_END_NAMESPACE