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.

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