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.

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