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.

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