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

5477 lines
198KB

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