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.

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