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.

5310 lines
191KB

  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 ScopedDisabler sd(this);
  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 (params > 0)
  1184. {
  1185. kData->param.createNew(params);
  1186. fParamBuffers = new float[params];
  1187. for (uint32_t i=0; i < params; ++i)
  1188. fParamBuffers[i] = 0.0f;
  1189. }
  1190. if (evIns.count() > 0)
  1191. {
  1192. const size_t count(evIns.count());
  1193. fEventsIn.createNew(count);
  1194. for (j=0; j < count; ++j)
  1195. {
  1196. const uint32_t& type(evIns.getAt(j));
  1197. if (type == CARLA_EVENT_DATA_ATOM)
  1198. {
  1199. fEventsIn.data[j].type = CARLA_EVENT_DATA_ATOM;
  1200. fEventsIn.data[j].atom = lv2_atom_buffer_new(MAX_EVENT_BUFFER, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1201. }
  1202. else if (type == CARLA_EVENT_DATA_EVENT)
  1203. {
  1204. fEventsIn.data[j].type = CARLA_EVENT_DATA_EVENT;
  1205. fEventsIn.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1206. }
  1207. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1208. {
  1209. fEventsIn.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1210. fEventsIn.data[j].midi = new LV2_MIDI;
  1211. fEventsIn.data[j].midi->event_count = 0;
  1212. fEventsIn.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1213. fEventsIn.data[j].midi->size = 0;
  1214. fEventsIn.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1215. }
  1216. }
  1217. }
  1218. if (evOuts.count() > 0)
  1219. {
  1220. const size_t count(evOuts.count());
  1221. fEventsOut.createNew(count);
  1222. for (j=0; j < count; ++j)
  1223. {
  1224. const uint32_t& type(evOuts.getAt(j));
  1225. if (type == CARLA_EVENT_DATA_ATOM)
  1226. {
  1227. fEventsOut.data[j].type = CARLA_EVENT_DATA_ATOM;
  1228. fEventsOut.data[j].atom = lv2_atom_buffer_new(MAX_EVENT_BUFFER, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1229. }
  1230. else if (type == CARLA_EVENT_DATA_EVENT)
  1231. {
  1232. fEventsOut.data[j].type = CARLA_EVENT_DATA_EVENT;
  1233. fEventsOut.data[j].event = lv2_event_buffer_new(MAX_EVENT_BUFFER, LV2_EVENT_AUDIO_STAMP);
  1234. }
  1235. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1236. {
  1237. fEventsOut.data[j].type = CARLA_EVENT_DATA_MIDI_LL;
  1238. fEventsOut.data[j].midi = new LV2_MIDI;
  1239. fEventsOut.data[j].midi->event_count = 0;
  1240. fEventsOut.data[j].midi->capacity = MAX_EVENT_BUFFER;
  1241. fEventsOut.data[j].midi->size = 0;
  1242. fEventsOut.data[j].midi->data = new unsigned char[MAX_EVENT_BUFFER];
  1243. }
  1244. }
  1245. }
  1246. const uint portNameSize(kData->engine->maxPortNameSize());
  1247. CarlaString portName;
  1248. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1249. {
  1250. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1251. portName.clear();
  1252. 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))
  1253. {
  1254. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1255. {
  1256. portName = fName;
  1257. portName += ":";
  1258. }
  1259. portName += fRdfDescriptor->Ports[i].Name;
  1260. portName.truncate(portNameSize);
  1261. }
  1262. if (LV2_IS_PORT_AUDIO(portTypes))
  1263. {
  1264. if (LV2_IS_PORT_INPUT(portTypes))
  1265. {
  1266. j = iAudioIn++;
  1267. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  1268. kData->audioIn.ports[j].rindex = i;
  1269. if (forcedStereoIn)
  1270. {
  1271. portName += "_2";
  1272. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  1273. kData->audioIn.ports[1].rindex = i;
  1274. }
  1275. }
  1276. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1277. {
  1278. j = iAudioOut++;
  1279. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  1280. kData->audioOut.ports[j].rindex = i;
  1281. if (forcedStereoOut)
  1282. {
  1283. portName += "_2";
  1284. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  1285. kData->audioOut.ports[1].rindex = i;
  1286. }
  1287. }
  1288. else
  1289. carla_stderr("WARNING - Got a broken Port (Audio, but not input or output)");
  1290. }
  1291. else if (LV2_IS_PORT_CV(portTypes))
  1292. {
  1293. if (LV2_IS_PORT_INPUT(portTypes))
  1294. {
  1295. carla_stderr("WARNING - CV Ports are not supported yet");
  1296. }
  1297. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1298. {
  1299. carla_stderr("WARNING - CV Ports are not supported yet");
  1300. }
  1301. else
  1302. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1303. fDescriptor->connect_port(fHandle, i, nullptr);
  1304. if (fHandle2 != nullptr)
  1305. fDescriptor->connect_port(fHandle2, i, nullptr);
  1306. }
  1307. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1308. {
  1309. if (LV2_IS_PORT_INPUT(portTypes))
  1310. {
  1311. j = iEvIn++;
  1312. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1313. if (fHandle2 != nullptr)
  1314. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1315. fEventsIn.data[j].rindex = i;
  1316. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1317. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1318. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1319. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1320. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1321. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1322. if (evIns.count() == 1)
  1323. {
  1324. fEventsIn.ctrl = &fEventsIn.data[j];
  1325. fEventsIn.ctrlIndex = j;
  1326. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1327. needsCtrlIn = true;
  1328. }
  1329. else
  1330. {
  1331. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1332. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1333. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1334. {
  1335. fEventsIn.ctrl = &fEventsIn.data[j];
  1336. fEventsIn.ctrlIndex = j;
  1337. }
  1338. }
  1339. }
  1340. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1341. {
  1342. j = iEvOut++;
  1343. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1344. if (fHandle2 != nullptr)
  1345. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1346. fEventsOut.data[j].rindex = i;
  1347. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1348. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1349. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1350. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1351. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1352. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1353. if (evOuts.count() == 1)
  1354. {
  1355. fEventsOut.ctrl = &fEventsOut.data[j];
  1356. fEventsOut.ctrlIndex = j;
  1357. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1358. needsCtrlOut = true;
  1359. }
  1360. else
  1361. {
  1362. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1363. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1364. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1365. {
  1366. fEventsOut.ctrl = &fEventsOut.data[j];
  1367. fEventsOut.ctrlIndex = j;
  1368. }
  1369. }
  1370. }
  1371. else
  1372. carla_stderr("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1373. }
  1374. else if (LV2_IS_PORT_EVENT(portTypes))
  1375. {
  1376. if (LV2_IS_PORT_INPUT(portTypes))
  1377. {
  1378. j = iEvIn++;
  1379. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1380. if (fHandle2 != nullptr)
  1381. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1382. fEventsIn.data[j].rindex = i;
  1383. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1384. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1385. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1386. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1387. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1388. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1389. if (evIns.count() == 1)
  1390. {
  1391. fEventsIn.ctrl = &fEventsIn.data[j];
  1392. fEventsIn.ctrlIndex = j;
  1393. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1394. needsCtrlIn = true;
  1395. }
  1396. else
  1397. {
  1398. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1399. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1400. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1401. {
  1402. fEventsIn.ctrl = &fEventsIn.data[j];
  1403. fEventsIn.ctrlIndex = j;
  1404. }
  1405. }
  1406. }
  1407. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1408. {
  1409. j = iEvOut++;
  1410. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1411. if (fHandle2 != nullptr)
  1412. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1413. fEventsOut.data[j].rindex = i;
  1414. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1415. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1416. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1417. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1418. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1419. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1420. if (evOuts.count() == 1)
  1421. {
  1422. fEventsOut.ctrl = &fEventsOut.data[j];
  1423. fEventsOut.ctrlIndex = j;
  1424. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1425. needsCtrlOut = true;
  1426. }
  1427. else
  1428. {
  1429. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1430. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1431. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1432. {
  1433. fEventsOut.ctrl = &fEventsOut.data[j];
  1434. fEventsOut.ctrlIndex = j;
  1435. }
  1436. }
  1437. }
  1438. else
  1439. carla_stderr("WARNING - Got a broken Port (Event, but not input or output)");
  1440. }
  1441. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1442. {
  1443. if (LV2_IS_PORT_INPUT(portTypes))
  1444. {
  1445. j = iEvIn++;
  1446. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].midi);
  1447. if (fHandle2 != nullptr)
  1448. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].midi);
  1449. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1450. fEventsIn.data[j].rindex = i;
  1451. if (evIns.count() == 1)
  1452. {
  1453. needsCtrlIn = true;
  1454. fEventsIn.ctrl = &fEventsIn.data[j];
  1455. fEventsIn.ctrlIndex = j;
  1456. }
  1457. else
  1458. {
  1459. fEventsIn.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1460. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1461. {
  1462. fEventsIn.ctrl = &fEventsIn.data[j];
  1463. fEventsIn.ctrlIndex = j;
  1464. }
  1465. }
  1466. }
  1467. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1468. {
  1469. j = iEvOut++;
  1470. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].midi);
  1471. if (fHandle2 != nullptr)
  1472. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].midi);
  1473. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1474. fEventsOut.data[j].rindex = i;
  1475. if (evOuts.count() == 1)
  1476. {
  1477. needsCtrlOut = true;
  1478. fEventsOut.ctrl = &fEventsOut.data[j];
  1479. fEventsOut.ctrlIndex = j;
  1480. }
  1481. else
  1482. {
  1483. fEventsOut.data[j].port = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1484. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1485. {
  1486. fEventsOut.ctrl = &fEventsOut.data[j];
  1487. fEventsOut.ctrlIndex = j;
  1488. }
  1489. }
  1490. }
  1491. else
  1492. carla_stderr("WARNING - Got a broken Port (MIDI, but not input or output)");
  1493. }
  1494. else if (LV2_IS_PORT_CONTROL(portTypes))
  1495. {
  1496. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1497. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1498. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1499. j = iCtrl++;
  1500. kData->param.data[j].index = j;
  1501. kData->param.data[j].rindex = i;
  1502. kData->param.data[j].hints = 0x0;
  1503. kData->param.data[j].midiChannel = 0;
  1504. kData->param.data[j].midiCC = -1;
  1505. float min, max, def, step, stepSmall, stepLarge;
  1506. // min value
  1507. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1508. min = portPoints.Minimum;
  1509. else
  1510. min = 0.0f;
  1511. // max value
  1512. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1513. max = portPoints.Maximum;
  1514. else
  1515. max = 1.0f;
  1516. if (min > max)
  1517. max = min;
  1518. else if (max < min)
  1519. min = max;
  1520. // stupid hack for ir.lv2 (broken plugin)
  1521. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1522. {
  1523. min = 0.0f;
  1524. max = (float)0xffffff;
  1525. }
  1526. if (max - min == 0.0f)
  1527. {
  1528. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1529. max = min + 0.1f;
  1530. }
  1531. // default value
  1532. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1533. {
  1534. def = portPoints.Default;
  1535. }
  1536. else
  1537. {
  1538. // no default value
  1539. if (min < 0.0f && max > 0.0f)
  1540. def = 0.0f;
  1541. else
  1542. def = min;
  1543. }
  1544. if (def < min)
  1545. def = min;
  1546. else if (def > max)
  1547. def = max;
  1548. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1549. {
  1550. min *= sampleRate;
  1551. max *= sampleRate;
  1552. def *= sampleRate;
  1553. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1554. }
  1555. if (LV2_IS_PORT_TOGGLED(portProps))
  1556. {
  1557. step = max - min;
  1558. stepSmall = step;
  1559. stepLarge = step;
  1560. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1561. }
  1562. else if (LV2_IS_PORT_INTEGER(portProps))
  1563. {
  1564. step = 1.0f;
  1565. stepSmall = 1.0f;
  1566. stepLarge = 10.0f;
  1567. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1568. }
  1569. else
  1570. {
  1571. float range = max - min;
  1572. step = range/100.0f;
  1573. stepSmall = range/1000.0f;
  1574. stepLarge = range/10.0f;
  1575. }
  1576. if (LV2_IS_PORT_INPUT(portTypes))
  1577. {
  1578. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1579. {
  1580. carla_stderr("Plugin has latency input port, this should not happen!");
  1581. }
  1582. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1583. {
  1584. def = sampleRate;
  1585. step = 1.0f;
  1586. stepSmall = 1.0f;
  1587. stepLarge = 1.0f;
  1588. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1589. kData->param.data[j].hints = 0x0;
  1590. }
  1591. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1592. {
  1593. kData->param.data[j].type = PARAMETER_LV2_FREEWHEEL;
  1594. }
  1595. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1596. {
  1597. kData->param.data[j].type = PARAMETER_LV2_TIME;
  1598. }
  1599. else
  1600. {
  1601. kData->param.data[j].type = PARAMETER_INPUT;
  1602. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1603. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1604. needsCtrlIn = true;
  1605. }
  1606. // MIDI CC value
  1607. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1608. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1609. {
  1610. if (! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1611. kData->param.data[j].midiCC = portMidiMap.Number;
  1612. }
  1613. }
  1614. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1615. {
  1616. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1617. {
  1618. min = 0.0f;
  1619. max = sampleRate;
  1620. def = 0.0f;
  1621. step = 1.0f;
  1622. stepSmall = 1.0f;
  1623. stepLarge = 1.0f;
  1624. kData->param.data[j].type = PARAMETER_LATENCY;
  1625. kData->param.data[j].hints = 0x0;
  1626. }
  1627. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1628. {
  1629. def = sampleRate;
  1630. step = 1.0f;
  1631. stepSmall = 1.0f;
  1632. stepLarge = 1.0f;
  1633. kData->param.data[j].type = PARAMETER_SAMPLE_RATE;
  1634. kData->param.data[j].hints = 0x0;
  1635. }
  1636. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1637. {
  1638. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1639. }
  1640. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1641. {
  1642. kData->param.data[j].type = PARAMETER_LV2_TIME;
  1643. }
  1644. else
  1645. {
  1646. kData->param.data[j].type = PARAMETER_OUTPUT;
  1647. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1648. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1649. needsCtrlOut = true;
  1650. }
  1651. }
  1652. else
  1653. {
  1654. kData->param.data[j].type = PARAMETER_UNKNOWN;
  1655. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1656. }
  1657. // extra parameter hints
  1658. if (LV2_IS_PORT_ENUMERATION(portProps))
  1659. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1660. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1661. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1662. if (LV2_IS_PORT_TRIGGER(portProps))
  1663. kData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1664. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1665. kData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1666. // check if parameter is not enabled or automable
  1667. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1668. {
  1669. kData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1670. }
  1671. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1672. kData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1673. kData->param.ranges[j].min = min;
  1674. kData->param.ranges[j].max = max;
  1675. kData->param.ranges[j].def = def;
  1676. kData->param.ranges[j].step = step;
  1677. kData->param.ranges[j].stepSmall = stepSmall;
  1678. kData->param.ranges[j].stepLarge = stepLarge;
  1679. // Start parameters in their default values
  1680. if (kData->param.data[j].type != PARAMETER_LV2_FREEWHEEL)
  1681. fParamBuffers[j] = def;
  1682. else
  1683. fParamBuffers[j] = min;
  1684. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1685. if (fHandle2 != nullptr)
  1686. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1687. }
  1688. else
  1689. {
  1690. // Port Type not supported, but it's optional anyway
  1691. fDescriptor->connect_port(fHandle, i, nullptr);
  1692. if (fHandle2 != nullptr)
  1693. fDescriptor->connect_port(fHandle2, i, nullptr);
  1694. }
  1695. }
  1696. if (needsCtrlIn)
  1697. {
  1698. portName.clear();
  1699. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1700. {
  1701. portName = fName;
  1702. portName += ":";
  1703. }
  1704. portName += "events-in";
  1705. portName.truncate(portNameSize);
  1706. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  1707. }
  1708. if (needsCtrlOut)
  1709. {
  1710. portName.clear();
  1711. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  1712. {
  1713. portName = fName;
  1714. portName += ":";
  1715. }
  1716. portName += "events-out";
  1717. portName.truncate(portNameSize);
  1718. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  1719. }
  1720. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1721. fEventsIn.ctrl->port = kData->event.portIn;
  1722. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1723. fEventsOut.ctrl->port = kData->event.portOut;
  1724. if (forcedStereoIn || forcedStereoOut)
  1725. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1726. else
  1727. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  1728. // plugin hints
  1729. fHints = 0x0;
  1730. if (isRealtimeSafe())
  1731. fHints |= PLUGIN_IS_RTSAFE;
  1732. if (fUi.type != PLUGIN_UI_NULL)
  1733. {
  1734. fHints |= PLUGIN_HAS_GUI;
  1735. if (fUi.type == PLUGIN_UI_QT || fUi.type == PLUGIN_UI_PARENT)
  1736. fHints |= PLUGIN_HAS_SINGLE_THREAD;
  1737. }
  1738. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1739. fHints |= PLUGIN_IS_SYNTH;
  1740. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1741. fHints |= PLUGIN_CAN_DRYWET;
  1742. if (aOuts > 0)
  1743. fHints |= PLUGIN_CAN_VOLUME;
  1744. if (aOuts >= 2 && aOuts % 2 == 0)
  1745. fHints |= PLUGIN_CAN_BALANCE;
  1746. // extra plugin hints
  1747. kData->extraHints &= ~PLUGIN_HINT_CAN_RUN_RACK;
  1748. if (fExt.state != nullptr || fExt.worker != nullptr)
  1749. {
  1750. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1751. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  1752. }
  1753. else
  1754. {
  1755. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1756. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  1757. }
  1758. bufferSizeChanged(kData->engine->getBufferSize());
  1759. reloadPrograms(true);
  1760. if (kData->active)
  1761. activate();
  1762. evIns.clear();
  1763. evOuts.clear();
  1764. carla_debug("Lv2Plugin::reload() - end");
  1765. }
  1766. void reloadPrograms(const bool init) override
  1767. {
  1768. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(init));
  1769. uint32_t i, oldCount = kData->midiprog.count;
  1770. const int32_t current = kData->midiprog.current;
  1771. // special LV2 programs handling
  1772. if (init)
  1773. {
  1774. kData->prog.clear();
  1775. const uint32_t count(fRdfDescriptor->PresetCount);
  1776. if (count > 0)
  1777. {
  1778. kData->prog.createNew(count);
  1779. for (i=0; i < count; ++i)
  1780. kData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1781. }
  1782. }
  1783. // Delete old programs
  1784. kData->midiprog.clear();
  1785. // Query new programs
  1786. uint32_t count = 0;
  1787. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1788. {
  1789. while (fExt.programs->get_program(fHandle, count))
  1790. ++count;
  1791. }
  1792. if (count > 0)
  1793. {
  1794. kData->midiprog.createNew(count);
  1795. // Update data
  1796. for (i=0; i < count; ++i)
  1797. {
  1798. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1799. CARLA_ASSERT(pdesc != nullptr);
  1800. CARLA_ASSERT(pdesc->name != nullptr);
  1801. kData->midiprog.data[i].bank = pdesc->bank;
  1802. kData->midiprog.data[i].program = pdesc->program;
  1803. kData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1804. }
  1805. }
  1806. #ifndef BUILD_BRIDGE
  1807. // Update OSC Names
  1808. if (kData->engine->isOscControlRegistered())
  1809. {
  1810. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  1811. for (i=0; i < count; ++i)
  1812. 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);
  1813. }
  1814. #endif
  1815. if (init)
  1816. {
  1817. if (count > 0)
  1818. {
  1819. setMidiProgram(0, false, false, false);
  1820. }
  1821. else
  1822. {
  1823. // load default state
  1824. if (const LilvState* state = gLv2World.getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1825. {
  1826. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1827. if (fHandle2 != nullptr)
  1828. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1829. }
  1830. }
  1831. }
  1832. else
  1833. {
  1834. // Check if current program is invalid
  1835. bool programChanged = false;
  1836. if (count == oldCount+1)
  1837. {
  1838. // one midi program added, probably created by user
  1839. kData->midiprog.current = oldCount;
  1840. programChanged = true;
  1841. }
  1842. else if (current < 0 && count > 0)
  1843. {
  1844. // programs exist now, but not before
  1845. kData->midiprog.current = 0;
  1846. programChanged = true;
  1847. }
  1848. else if (current >= 0 && count == 0)
  1849. {
  1850. // programs existed before, but not anymore
  1851. kData->midiprog.current = -1;
  1852. programChanged = true;
  1853. }
  1854. else if (current >= static_cast<int32_t>(count))
  1855. {
  1856. // current midi program > count
  1857. kData->midiprog.current = 0;
  1858. programChanged = true;
  1859. }
  1860. else
  1861. {
  1862. // no change
  1863. kData->midiprog.current = current;
  1864. }
  1865. if (programChanged)
  1866. setMidiProgram(kData->midiprog.current, true, true, true);
  1867. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  1868. }
  1869. }
  1870. // -------------------------------------------------------------------
  1871. // Plugin processing
  1872. void activate() override
  1873. {
  1874. CARLA_ASSERT(fDescriptor != nullptr);
  1875. CARLA_ASSERT(fHandle != nullptr);
  1876. if (fDescriptor->activate != nullptr)
  1877. {
  1878. fDescriptor->activate(fHandle);
  1879. if (fHandle2 != nullptr)
  1880. fDescriptor->activate(fHandle2);
  1881. }
  1882. fFirstActive = true;
  1883. }
  1884. void deactivate() override
  1885. {
  1886. CARLA_ASSERT(fDescriptor != nullptr);
  1887. CARLA_ASSERT(fHandle != nullptr);
  1888. if (fDescriptor->deactivate != nullptr)
  1889. {
  1890. fDescriptor->deactivate(fHandle);
  1891. if (fHandle2 != nullptr)
  1892. fDescriptor->deactivate(fHandle2);
  1893. }
  1894. }
  1895. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1896. {
  1897. uint32_t i, k;
  1898. // --------------------------------------------------------------------------------------------------------
  1899. // Check if active
  1900. if (! kData->active)
  1901. {
  1902. // disable any output sound
  1903. for (i=0; i < kData->audioOut.count; ++i)
  1904. carla_zeroFloat(outBuffer[i], frames);
  1905. return;
  1906. }
  1907. // --------------------------------------------------------------------------------------------------------
  1908. // Handle events from different APIs
  1909. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  1910. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1911. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1912. for (i=0; i < fEventsIn.count; ++i)
  1913. {
  1914. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1915. {
  1916. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  1917. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  1918. }
  1919. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1920. {
  1921. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  1922. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1923. }
  1924. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1925. {
  1926. fEventsIn.data[i].midi->event_count = 0;
  1927. fEventsIn.data[i].midi->size = 0;
  1928. evInMidiStates[i].midi = fEventsIn.data[i].midi;
  1929. evInMidiStates[i].frame_count = frames;
  1930. evInMidiStates[i].position = 0;
  1931. }
  1932. }
  1933. for (i=0; i < fEventsOut.count; ++i)
  1934. {
  1935. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1936. {
  1937. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  1938. }
  1939. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1940. {
  1941. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  1942. }
  1943. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1944. {
  1945. // not needed
  1946. }
  1947. }
  1948. CARLA_PROCESS_CONTINUE_CHECK;
  1949. // --------------------------------------------------------------------------------------------------------
  1950. // Check if needs reset
  1951. if (kData->needsReset)
  1952. {
  1953. uint8_t midiData[3] = { 0 };
  1954. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  1955. {
  1956. k = fEventsIn.ctrlIndex;
  1957. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1958. {
  1959. for (i=0; i < MAX_MIDI_CHANNELS; ++i)
  1960. {
  1961. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  1962. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1963. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1964. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1965. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1966. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1967. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1968. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  1969. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  1970. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1971. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1972. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1973. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1974. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1975. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1976. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  1977. }
  1978. }
  1979. else if (kData->ctrlChannel >= 0 && kData->ctrlChannel < MAX_MIDI_CHANNELS)
  1980. {
  1981. for (k=0; k < MAX_MIDI_NOTE; ++k)
  1982. {
  1983. midiData[0] = MIDI_STATUS_NOTE_OFF + kData->ctrlChannel;
  1984. midiData[1] = k;
  1985. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  1986. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1987. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  1988. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  1989. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  1990. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiData);
  1991. }
  1992. }
  1993. }
  1994. //if (kData->latency > 0)
  1995. {
  1996. //for (i=0; i < kData->audioIn.count; ++i)
  1997. // carla_zeroFloat(kData->latencyBuffers[i], kData->latency);
  1998. }
  1999. kData->needsReset = false;
  2000. CARLA_PROCESS_CONTINUE_CHECK;
  2001. }
  2002. // --------------------------------------------------------------------------------------------------------
  2003. // TimeInfo
  2004. const EngineTimeInfo& timeInfo(kData->engine->getTimeInfo());
  2005. if (fFirstActive || fLastTimeInfo != timeInfo)
  2006. {
  2007. bool doPostRt;
  2008. int32_t rindex;
  2009. // update input ports
  2010. for (k=0; k < kData->param.count; ++k)
  2011. {
  2012. if (kData->param.data[k].type != PARAMETER_LV2_TIME)
  2013. continue;
  2014. doPostRt = false;
  2015. rindex = kData->param.data[k].rindex;
  2016. CARLA_ASSERT(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2017. switch (fRdfDescriptor->Ports[rindex].Designation)
  2018. {
  2019. // Non-BBT
  2020. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2021. if (fLastTimeInfo.playing != timeInfo.playing)
  2022. {
  2023. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2024. doPostRt = true;
  2025. }
  2026. break;
  2027. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2028. if (fLastTimeInfo.frame != timeInfo.frame)
  2029. {
  2030. fParamBuffers[k] = timeInfo.frame;
  2031. doPostRt = true;
  2032. }
  2033. break;
  2034. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2035. break;
  2036. // BBT
  2037. case LV2_PORT_DESIGNATION_TIME_BAR:
  2038. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2039. {
  2040. fParamBuffers[k] = timeInfo.bbt.bar - 1;
  2041. doPostRt = true;
  2042. }
  2043. break;
  2044. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2045. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2046. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  2047. {
  2048. fParamBuffers[k] = timeInfo.bbt.beat - 1 + (double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat);
  2049. doPostRt = true;
  2050. }
  2051. break;
  2052. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2053. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2054. {
  2055. fParamBuffers[k] = timeInfo.bbt.beat - 1;
  2056. doPostRt = true;
  2057. }
  2058. break;
  2059. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2060. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  2061. {
  2062. fParamBuffers[k] = timeInfo.bbt.beatType;
  2063. doPostRt = true;
  2064. }
  2065. break;
  2066. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2067. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  2068. {
  2069. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2070. doPostRt = true;
  2071. }
  2072. break;
  2073. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2074. if ((timeInfo.valid & EngineTimeInfo::ValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  2075. {
  2076. fParamBuffers[k] = timeInfo.bbt.beatsPerMinute;
  2077. doPostRt = true;
  2078. }
  2079. break;
  2080. }
  2081. if (doPostRt)
  2082. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2083. }
  2084. for (i = 0; i < fEventsIn.count; ++i)
  2085. {
  2086. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2087. continue;
  2088. uint8_t timeInfoBuf[256] = { 0 };
  2089. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2090. LV2_Atom_Forge_Frame forgeFrame;
  2091. lv2_atom_forge_blank(&fAtomForge, &forgeFrame, 1, CARLA_URI_MAP_ID_TIME_POSITION);
  2092. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED, 0);
  2093. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2094. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME, 0);
  2095. lv2_atom_forge_long(&fAtomForge, timeInfo.frame);
  2096. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  2097. {
  2098. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR, 0);
  2099. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2100. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT, 0);
  2101. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beat - 1 + (double(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2102. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT, 0);
  2103. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.beat -1);
  2104. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT, 0);
  2105. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatType);
  2106. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR, 0);
  2107. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2108. lv2_atom_forge_property_head(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE, 0);
  2109. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerMinute);
  2110. }
  2111. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2112. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2NV_ATOM_BODY_CONST(atom));
  2113. CARLA_ASSERT(atom->size < 256);
  2114. }
  2115. kData->postRtEvents.trySplice();
  2116. std::memcpy(&fLastTimeInfo, &timeInfo, sizeof(EngineTimeInfo));
  2117. CARLA_PROCESS_CONTINUE_CHECK;
  2118. }
  2119. // --------------------------------------------------------------------------------------------------------
  2120. // Event Input and Processing
  2121. if (fEventsIn.ctrl != nullptr)
  2122. {
  2123. // ----------------------------------------------------------------------------------------------------
  2124. // Message Input
  2125. if (fAtomQueueIn.tryLock())
  2126. {
  2127. if (! fAtomQueueIn.isEmpty())
  2128. {
  2129. uint32_t portIndex;
  2130. const LV2_Atom* atom;
  2131. k = fEventsIn.ctrlIndex;
  2132. while (fAtomQueueIn.get(&portIndex, &atom))
  2133. {
  2134. carla_debug("Event input message sent to plugin DSP, type %i:\"%s\", size:%i/%i",
  2135. atom->type, carla_lv2_urid_unmap(this, atom->type),
  2136. atom->size, lv2_atom_total_size(atom)
  2137. );
  2138. if (! lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, atom->type, atom->size, LV2NV_ATOM_BODY_CONST(atom)))
  2139. {
  2140. carla_stdout("Event input buffer full, 1 message lost");
  2141. break;
  2142. }
  2143. }
  2144. }
  2145. fAtomQueueIn.unlock();
  2146. }
  2147. // ----------------------------------------------------------------------------------------------------
  2148. // MIDI Input (External)
  2149. if (kData->extNotes.mutex.tryLock())
  2150. {
  2151. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2152. {
  2153. // does not handle MIDI
  2154. kData->extNotes.data.clear();
  2155. }
  2156. else
  2157. {
  2158. k = fEventsIn.ctrlIndex;
  2159. while (! kData->extNotes.data.isEmpty())
  2160. {
  2161. const ExternalMidiNote& note(kData->extNotes.data.getFirst(true));
  2162. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2163. uint8_t midiEvent[3];
  2164. midiEvent[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  2165. midiEvent[0] += note.channel;
  2166. midiEvent[1] = note.note;
  2167. midiEvent[2] = note.velo;
  2168. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2169. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2170. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2171. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2172. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2173. lv2midi_put_event(&evInMidiStates[k], 0, 3, midiEvent);
  2174. }
  2175. }
  2176. kData->extNotes.mutex.unlock();
  2177. } // End of MIDI Input (External)
  2178. // ----------------------------------------------------------------------------------------------------
  2179. // Event Input (System)
  2180. bool allNotesOffSent = false;
  2181. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  2182. uint32_t time, nEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2183. uint32_t startTime = 0;
  2184. uint32_t timeOffset = 0;
  2185. uint32_t nextBankId = 0;
  2186. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  2187. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  2188. for (i=0; i < nEvents; ++i)
  2189. {
  2190. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2191. time = event.time;
  2192. if (time >= frames)
  2193. continue;
  2194. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  2195. if (time > timeOffset && sampleAccurate)
  2196. {
  2197. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  2198. {
  2199. startTime = 0;
  2200. timeOffset = time;
  2201. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  2202. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  2203. else
  2204. nextBankId = 0;
  2205. // reset iters
  2206. k = fEventsIn.ctrlIndex;
  2207. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2208. {
  2209. lv2_atom_buffer_reset(fEventsIn.data[k].atom, true);
  2210. lv2_atom_buffer_begin(&evInAtomIters[k], fEventsIn.data[k].atom);
  2211. }
  2212. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2213. {
  2214. lv2_event_buffer_reset(fEventsIn.data[k].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[k].event->data);
  2215. lv2_event_begin(&evInEventIters[k], fEventsIn.data[k].event);
  2216. }
  2217. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2218. {
  2219. fEventsIn.data[k].midi->event_count = 0;
  2220. fEventsIn.data[k].midi->size = 0;
  2221. evInMidiStates[k].position = time;
  2222. }
  2223. }
  2224. else
  2225. startTime += timeOffset;
  2226. }
  2227. // Control change
  2228. switch (event.type)
  2229. {
  2230. case kEngineEventTypeNull:
  2231. break;
  2232. case kEngineEventTypeControl:
  2233. {
  2234. const EngineControlEvent& ctrlEvent(event.ctrl);
  2235. switch (ctrlEvent.type)
  2236. {
  2237. case kEngineControlEventTypeNull:
  2238. break;
  2239. case kEngineControlEventTypeParameter:
  2240. {
  2241. #ifndef BUILD_BRIDGE
  2242. // Control backend stuff
  2243. if (event.channel == kData->ctrlChannel)
  2244. {
  2245. float value;
  2246. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  2247. {
  2248. value = ctrlEvent.value;
  2249. setDryWet(value, false, false);
  2250. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2251. }
  2252. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  2253. {
  2254. value = ctrlEvent.value*127.0f/100.0f;
  2255. setVolume(value, false, false);
  2256. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2257. }
  2258. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  2259. {
  2260. float left, right;
  2261. value = ctrlEvent.value/0.5f - 1.0f;
  2262. if (value < 0.0f)
  2263. {
  2264. left = -1.0f;
  2265. right = (value*2.0f)+1.0f;
  2266. }
  2267. else if (value > 0.0f)
  2268. {
  2269. left = (value*2.0f)-1.0f;
  2270. right = 1.0f;
  2271. }
  2272. else
  2273. {
  2274. left = -1.0f;
  2275. right = 1.0f;
  2276. }
  2277. setBalanceLeft(left, false, false);
  2278. setBalanceRight(right, false, false);
  2279. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2280. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2281. }
  2282. }
  2283. #endif
  2284. // Control plugin parameters
  2285. for (k=0; k < kData->param.count; ++k)
  2286. {
  2287. if (kData->param.data[k].midiChannel != event.channel)
  2288. continue;
  2289. if (kData->param.data[k].midiCC != ctrlEvent.param)
  2290. continue;
  2291. if (kData->param.data[k].type != PARAMETER_INPUT)
  2292. continue;
  2293. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2294. continue;
  2295. float value;
  2296. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2297. {
  2298. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  2299. }
  2300. else
  2301. {
  2302. value = kData->param.ranges[i].unnormalizeValue(ctrlEvent.value);
  2303. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2304. value = std::rint(value);
  2305. }
  2306. setParameterValue(k, value, false, false, false);
  2307. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2308. }
  2309. break;
  2310. }
  2311. case kEngineControlEventTypeMidiBank:
  2312. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2313. nextBankId = ctrlEvent.param;
  2314. break;
  2315. case kEngineControlEventTypeMidiProgram:
  2316. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2317. {
  2318. const uint32_t nextProgramId = ctrlEvent.param;
  2319. for (k=0; k < kData->midiprog.count; ++k)
  2320. {
  2321. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  2322. {
  2323. setMidiProgram(k, false, false, false);
  2324. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  2325. break;
  2326. }
  2327. }
  2328. }
  2329. break;
  2330. case kEngineControlEventTypeAllSoundOff:
  2331. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2332. {
  2333. uint8_t midiData[3];
  2334. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2335. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2336. midiData[2] = 0;
  2337. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2338. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2339. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2340. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2341. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2342. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0, 3, midiData);
  2343. }
  2344. break;
  2345. case kEngineControlEventTypeAllNotesOff:
  2346. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2347. {
  2348. if (event.channel == kData->ctrlChannel && ! allNotesOffSent)
  2349. {
  2350. allNotesOffSent = true;
  2351. sendMidiAllNotesOffToCallback();
  2352. }
  2353. uint8_t midiData[3];
  2354. midiData[0] = MIDI_STATUS_CONTROL_CHANGE + i;
  2355. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2356. midiData[2] = 0;
  2357. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2358. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2359. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2360. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2361. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2362. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], 0, 3, midiData);
  2363. }
  2364. break;
  2365. }
  2366. break;
  2367. }
  2368. case kEngineEventTypeMidi:
  2369. {
  2370. const EngineMidiEvent& midiEvent(event.midi);
  2371. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  2372. uint8_t channel = event.channel;
  2373. uint32_t mtime = sampleAccurate ? startTime : time;
  2374. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2375. continue;
  2376. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2377. continue;
  2378. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2379. continue;
  2380. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2381. continue;
  2382. // Fix bad note-off
  2383. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  2384. status -= 0x10;
  2385. k = fEventsIn.ctrlIndex;
  2386. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2387. lv2_atom_buffer_write(&evInAtomIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2388. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2389. lv2_event_write(&evInEventIters[k], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2390. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2391. lv2midi_put_event(&evInMidiStates[k], mtime, midiEvent.size, midiEvent.data);
  2392. if (status == MIDI_STATUS_NOTE_ON)
  2393. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2394. else if (status == MIDI_STATUS_NOTE_OFF)
  2395. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2396. break;
  2397. }
  2398. }
  2399. }
  2400. kData->postRtEvents.trySplice();
  2401. if (frames > timeOffset)
  2402. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  2403. } // End of Event Input and Processing
  2404. // --------------------------------------------------------------------------------------------------------
  2405. // Plugin processing (no events)
  2406. else
  2407. {
  2408. processSingle(inBuffer, outBuffer, frames, 0);
  2409. } // End of Plugin processing (no events)
  2410. CARLA_PROCESS_CONTINUE_CHECK;
  2411. // --------------------------------------------------------------------------------------------------------
  2412. // MIDI Output
  2413. if (fEventsOut.ctrl != nullptr)
  2414. {
  2415. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2416. {
  2417. const uint32_t rindex(fEventsOut.ctrl->rindex);
  2418. const LV2_Atom_Event* ev;
  2419. LV2_Atom_Buffer_Iterator iter;
  2420. uint8_t* data;
  2421. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2422. while (true)
  2423. {
  2424. data = nullptr;
  2425. ev = lv2_atom_buffer_get(&iter, &data);
  2426. if (ev == nullptr || data == nullptr)
  2427. break;
  2428. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT && fEventsOut.ctrl->port != nullptr)
  2429. fEventsOut.ctrl->port->writeMidiEvent(ev->time.frames, data, ev->body.size);
  2430. else if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2431. fAtomQueueOut.put(rindex, &ev->body);
  2432. lv2_atom_buffer_increment(&iter);
  2433. }
  2434. }
  2435. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2436. {
  2437. const LV2_Event* ev;
  2438. LV2_Event_Iterator iter;
  2439. uint8_t* data;
  2440. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2441. while (true)
  2442. {
  2443. data = nullptr;
  2444. ev = lv2_event_get(&iter, &data);
  2445. if (ev == nullptr || data == nullptr)
  2446. break;
  2447. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2448. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, data, ev->size);
  2449. lv2_event_increment(&iter);
  2450. }
  2451. }
  2452. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2453. {
  2454. LV2_MIDIState state = { fEventsOut.ctrl->midi, frames, 0 };
  2455. uint32_t eventSize;
  2456. double eventTime;
  2457. unsigned char* eventData;
  2458. while (lv2midi_get_event(&state, &eventTime, &eventSize, &eventData) < frames)
  2459. {
  2460. if (eventData == nullptr || eventSize == 0)
  2461. break;
  2462. fEventsOut.ctrl->port->writeMidiEvent(eventTime, eventData, eventSize);
  2463. lv2midi_step(&state);
  2464. }
  2465. }
  2466. }
  2467. // --------------------------------------------------------------------------------------------------------
  2468. // Control Output
  2469. if (kData->event.portOut != nullptr)
  2470. {
  2471. uint8_t channel;
  2472. uint16_t param;
  2473. float value;
  2474. for (k=0; k < kData->param.count; ++k)
  2475. {
  2476. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  2477. continue;
  2478. if (kData->param.data[k].hints & PARAMETER_IS_STRICT_BOUNDS)
  2479. kData->param.ranges[k].fixValue(fParamBuffers[k]);
  2480. if (kData->param.data[k].midiCC > 0)
  2481. {
  2482. channel = kData->param.data[k].midiChannel;
  2483. param = static_cast<uint16_t>(kData->param.data[k].midiCC);
  2484. value = kData->param.ranges[k].normalizeValue(fParamBuffers[k]);
  2485. kData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2486. }
  2487. }
  2488. } // End of Control Output
  2489. CARLA_PROCESS_CONTINUE_CHECK;
  2490. #if 0
  2491. // --------------------------------------------------------------------------------------------------------
  2492. // Final work
  2493. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2494. {
  2495. fExt.worker->end_run(fHandle);
  2496. if (fHandle2 != nullptr)
  2497. fExt.worker->end_run(fHandle2);
  2498. }
  2499. #endif
  2500. fFirstActive = false;
  2501. // --------------------------------------------------------------------------------------------------------
  2502. }
  2503. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  2504. {
  2505. CARLA_ASSERT(frames > 0);
  2506. if (frames == 0)
  2507. return false;
  2508. if (kData->audioIn.count > 0)
  2509. {
  2510. CARLA_ASSERT(inBuffer != nullptr);
  2511. if (inBuffer == nullptr)
  2512. return false;
  2513. }
  2514. if (kData->audioOut.count > 0)
  2515. {
  2516. CARLA_ASSERT(outBuffer != nullptr);
  2517. if (outBuffer == nullptr)
  2518. return false;
  2519. }
  2520. uint32_t i, k;
  2521. // --------------------------------------------------------------------------------------------------------
  2522. // Try lock, silence otherwise
  2523. if (kData->engine->isOffline())
  2524. {
  2525. kData->singleMutex.lock();
  2526. }
  2527. else if (! kData->singleMutex.tryLock())
  2528. {
  2529. for (i=0; i < kData->audioOut.count; ++i)
  2530. {
  2531. for (k=0; k < frames; ++k)
  2532. outBuffer[i][k+timeOffset] = 0.0f;
  2533. }
  2534. return false;
  2535. }
  2536. // --------------------------------------------------------------------------------------------------------
  2537. // Reset audio buffers
  2538. for (i=0; i < kData->audioIn.count; ++i)
  2539. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  2540. for (i=0; i < kData->audioOut.count; ++i)
  2541. carla_zeroFloat(fAudioOutBuffers[i], frames);
  2542. // --------------------------------------------------------------------------------------------------------
  2543. // Run plugin
  2544. fDescriptor->run(fHandle, frames);
  2545. if (fHandle2 != nullptr)
  2546. fDescriptor->run(fHandle2, frames);
  2547. // --------------------------------------------------------------------------------------------------------
  2548. // Special Parameters
  2549. for (k=0; k < kData->param.count; ++k)
  2550. {
  2551. if (kData->param.data[k].type != PARAMETER_INPUT)
  2552. continue;
  2553. if (kData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2554. {
  2555. if (fParamBuffers[k] != kData->param.ranges[k].def)
  2556. {
  2557. fParamBuffers[k] = kData->param.ranges[k].def;
  2558. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2559. }
  2560. }
  2561. }
  2562. kData->postRtEvents.trySplice();
  2563. #ifndef BUILD_BRIDGE
  2564. // --------------------------------------------------------------------------------------------------------
  2565. // Post-processing (dry/wet, volume and balance)
  2566. {
  2567. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  2568. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  2569. bool isPair;
  2570. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2571. for (i=0; i < kData->audioOut.count; ++i)
  2572. {
  2573. // Dry/Wet
  2574. if (doDryWet)
  2575. {
  2576. for (k=0; k < frames; ++k)
  2577. {
  2578. // TODO
  2579. //if (k < kData->latency && kData->latency < frames)
  2580. // bufValue = (kData->audioIn.count == 1) ? kData->latencyBuffers[0][k] : kData->latencyBuffers[i][k];
  2581. //else
  2582. // bufValue = (kData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2583. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  2584. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  2585. }
  2586. }
  2587. // Balance
  2588. if (doBalance)
  2589. {
  2590. isPair = (i % 2 == 0);
  2591. if (isPair)
  2592. {
  2593. CARLA_ASSERT(i+1 < kData->audioOut.count);
  2594. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  2595. }
  2596. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  2597. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  2598. for (k=0; k < frames; ++k)
  2599. {
  2600. if (isPair)
  2601. {
  2602. // left
  2603. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2604. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2605. }
  2606. else
  2607. {
  2608. // right
  2609. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2610. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2611. }
  2612. }
  2613. }
  2614. // Volume (and buffer copy)
  2615. {
  2616. for (k=0; k < frames; ++k)
  2617. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  2618. }
  2619. }
  2620. # if 0
  2621. // Latency, save values for next callback, TODO
  2622. if (kData->latency > 0 && kData->latency < frames)
  2623. {
  2624. for (i=0; i < kData->audioIn.count; ++i)
  2625. carla_copyFloat(kData->latencyBuffers[i], inBuffer[i] + (frames - kData->latency), kData->latency);
  2626. }
  2627. # endif
  2628. } // End of Post-processing
  2629. #else
  2630. for (i=0; i < kData->audioOut.count; ++i)
  2631. {
  2632. for (k=0; k < frames; ++k)
  2633. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2634. }
  2635. #endif
  2636. // --------------------------------------------------------------------------------------------------------
  2637. kData->singleMutex.unlock();
  2638. return true;
  2639. }
  2640. void bufferSizeChanged(const uint32_t newBufferSize) override
  2641. {
  2642. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2643. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2644. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2645. {
  2646. if (fAudioInBuffers[i] != nullptr)
  2647. delete[] fAudioInBuffers[i];
  2648. fAudioInBuffers[i] = new float[newBufferSize];
  2649. }
  2650. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2651. {
  2652. if (fAudioOutBuffers[i] != nullptr)
  2653. delete[] fAudioOutBuffers[i];
  2654. fAudioOutBuffers[i] = new float[newBufferSize];
  2655. }
  2656. if (fHandle2 == nullptr)
  2657. {
  2658. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2659. {
  2660. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2661. fDescriptor->connect_port(fHandle, kData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2662. }
  2663. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2664. {
  2665. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2666. fDescriptor->connect_port(fHandle, kData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2667. }
  2668. }
  2669. else
  2670. {
  2671. if (kData->audioIn.count > 0)
  2672. {
  2673. CARLA_ASSERT(kData->audioIn.count == 2);
  2674. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2675. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2676. fDescriptor->connect_port(fHandle, kData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2677. fDescriptor->connect_port(fHandle2, kData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2678. }
  2679. if (kData->audioOut.count > 0)
  2680. {
  2681. CARLA_ASSERT(kData->audioOut.count == 2);
  2682. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2683. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2684. fDescriptor->connect_port(fHandle, kData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2685. fDescriptor->connect_port(fHandle2, kData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2686. }
  2687. }
  2688. if (fLv2Options.maxBufferSize != static_cast<int>(newBufferSize) || (fLv2Options.minBufferSize > 1 && fLv2Options.minBufferSize != static_cast<int>(newBufferSize)))
  2689. {
  2690. fLv2Options.maxBufferSize = newBufferSize;
  2691. if (fLv2Options.minBufferSize > 1)
  2692. fLv2Options.minBufferSize = newBufferSize;
  2693. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2694. {
  2695. fExt.options->set(fHandle, &fLv2Options.optMinBlockLenth);
  2696. fExt.options->set(fHandle, &fLv2Options.optMaxBlockLenth);
  2697. }
  2698. }
  2699. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2700. }
  2701. void sampleRateChanged(const double newSampleRate) override
  2702. {
  2703. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2704. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2705. if (fLv2Options.sampleRate != newSampleRate)
  2706. {
  2707. fLv2Options.sampleRate = newSampleRate;
  2708. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2709. fExt.options->set(fHandle, &fLv2Options.optSampleRate);
  2710. }
  2711. for (uint32_t k=0; k < kData->param.count; ++k)
  2712. {
  2713. if (kData->param.data[k].type == PARAMETER_SAMPLE_RATE)
  2714. {
  2715. fParamBuffers[k] = newSampleRate;
  2716. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2717. }
  2718. }
  2719. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2720. }
  2721. void offlineModeChanged(const bool isOffline) override
  2722. {
  2723. for (uint32_t k=0; k < kData->param.count; ++k)
  2724. {
  2725. if (kData->param.data[k].type == PARAMETER_LV2_FREEWHEEL)
  2726. {
  2727. fParamBuffers[k] = isOffline ? kData->param.ranges[k].max : kData->param.ranges[k].min;
  2728. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2729. }
  2730. }
  2731. }
  2732. // -------------------------------------------------------------------
  2733. // Plugin buffers
  2734. void initBuffers() override
  2735. {
  2736. fEventsIn.initBuffers(kData->engine);
  2737. fEventsOut.initBuffers(kData->engine);
  2738. CarlaPlugin::initBuffers();
  2739. }
  2740. void clearBuffers() override
  2741. {
  2742. carla_debug("Lv2Plugin::clearBuffers() - start");
  2743. if (fAudioInBuffers != nullptr)
  2744. {
  2745. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  2746. {
  2747. if (fAudioInBuffers[i] != nullptr)
  2748. {
  2749. delete[] fAudioInBuffers[i];
  2750. fAudioInBuffers[i] = nullptr;
  2751. }
  2752. }
  2753. delete[] fAudioInBuffers;
  2754. fAudioInBuffers = nullptr;
  2755. }
  2756. if (fAudioOutBuffers != nullptr)
  2757. {
  2758. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  2759. {
  2760. if (fAudioOutBuffers[i] != nullptr)
  2761. {
  2762. delete[] fAudioOutBuffers[i];
  2763. fAudioOutBuffers[i] = nullptr;
  2764. }
  2765. }
  2766. delete[] fAudioOutBuffers;
  2767. fAudioOutBuffers = nullptr;
  2768. }
  2769. if (fParamBuffers != nullptr)
  2770. {
  2771. delete[] fParamBuffers;
  2772. fParamBuffers = nullptr;
  2773. }
  2774. fEventsIn.clear();
  2775. fEventsOut.clear();
  2776. CarlaPlugin::clearBuffers();
  2777. carla_debug("Lv2Plugin::clearBuffers() - end");
  2778. }
  2779. // -------------------------------------------------------------------
  2780. // Post-poned UI Stuff
  2781. void uiParameterChange(const uint32_t index, const float value) override
  2782. {
  2783. CARLA_ASSERT(fDescriptor != nullptr);
  2784. CARLA_ASSERT(fHandle != nullptr);
  2785. CARLA_ASSERT(index < kData->param.count);
  2786. if (fDescriptor == nullptr || fHandle == nullptr)
  2787. return;
  2788. if (index >= kData->param.count)
  2789. return;
  2790. if (fUi.type == PLUGIN_UI_OSC)
  2791. {
  2792. if (kData->osc.data.target != nullptr)
  2793. osc_send_control(&kData->osc.data, kData->param.data[index].rindex, value);
  2794. }
  2795. else
  2796. {
  2797. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2798. fUi.descriptor->port_event(fUi.handle, kData->param.data[index].rindex, sizeof(float), 0, &value);
  2799. }
  2800. }
  2801. void uiMidiProgramChange(const uint32_t index) override
  2802. {
  2803. CARLA_ASSERT(index < kData->midiprog.count);
  2804. if (index >= kData->midiprog.count)
  2805. return;
  2806. if (fUi.type == PLUGIN_UI_OSC)
  2807. {
  2808. if (kData->osc.data.target != nullptr)
  2809. osc_send_midi_program(&kData->osc.data, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2810. }
  2811. else
  2812. {
  2813. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2814. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  2815. }
  2816. }
  2817. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  2818. {
  2819. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2820. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2821. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  2822. if (channel >= MAX_MIDI_CHANNELS)
  2823. return;
  2824. if (note >= MAX_MIDI_NOTE)
  2825. return;
  2826. if (velo >= MAX_MIDI_VALUE)
  2827. return;
  2828. if (fUi.type == PLUGIN_UI_OSC)
  2829. {
  2830. if (kData->osc.data.target != nullptr)
  2831. {
  2832. uint8_t midiData[4] = { 0 };
  2833. midiData[1] = MIDI_STATUS_NOTE_ON + channel;
  2834. midiData[2] = note;
  2835. midiData[3] = velo;
  2836. osc_send_midi(&kData->osc.data, midiData);
  2837. }
  2838. }
  2839. else
  2840. {
  2841. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2842. {
  2843. LV2_Atom_MidiEvent midiEv;
  2844. midiEv.event.time.frames = 0;
  2845. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2846. midiEv.event.body.size = 3;
  2847. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2848. midiEv.data[1] = note;
  2849. midiEv.data[2] = velo;
  2850. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2851. }
  2852. }
  2853. }
  2854. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  2855. {
  2856. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  2857. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  2858. if (channel >= MAX_MIDI_CHANNELS)
  2859. return;
  2860. if (note >= MAX_MIDI_NOTE)
  2861. return;
  2862. if (fUi.type == PLUGIN_UI_OSC)
  2863. {
  2864. if (kData->osc.data.target != nullptr)
  2865. {
  2866. uint8_t midiData[4] = { 0 };
  2867. midiData[1] = MIDI_STATUS_NOTE_OFF + channel;
  2868. midiData[2] = note;
  2869. osc_send_midi(&kData->osc.data, midiData);
  2870. }
  2871. }
  2872. else
  2873. {
  2874. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  2875. {
  2876. LV2_Atom_MidiEvent midiEv;
  2877. midiEv.event.time.frames = 0;
  2878. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  2879. midiEv.event.body.size = 3;
  2880. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  2881. midiEv.data[1] = note;
  2882. midiEv.data[2] = 0;
  2883. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  2884. }
  2885. }
  2886. }
  2887. // -------------------------------------------------------------------
  2888. protected:
  2889. void guiClosedCallback() override
  2890. {
  2891. showGui(false);
  2892. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  2893. }
  2894. // -------------------------------------------------------------------
  2895. LV2_URID getCustomURID(const char* const uri)
  2896. {
  2897. CARLA_ASSERT(uri != nullptr);
  2898. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  2899. if (uri == nullptr)
  2900. return CARLA_URI_MAP_ID_NULL;
  2901. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  2902. {
  2903. const char*& thisUri(fCustomURIDs.getAt(i));
  2904. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  2905. return i;
  2906. }
  2907. fCustomURIDs.append(carla_strdup(uri));
  2908. const LV2_URID urid(fCustomURIDs.count()-1);
  2909. if (fUi.type == PLUGIN_UI_OSC && kData->osc.data.target != nullptr)
  2910. osc_send_lv2_urid_map(&kData->osc.data, urid, uri);
  2911. return urid;
  2912. }
  2913. const char* getCustomURIString(const LV2_URID urid)
  2914. {
  2915. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  2916. CARLA_ASSERT_INT2(urid < fCustomURIDs.count(), urid, fCustomURIDs.count());
  2917. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  2918. if (urid == CARLA_URI_MAP_ID_NULL)
  2919. return nullptr;
  2920. if (urid < fCustomURIDs.count())
  2921. return fCustomURIDs.getAt(urid);
  2922. return nullptr;
  2923. }
  2924. // -------------------------------------------------------------------
  2925. void handleProgramChanged(const int32_t index)
  2926. {
  2927. CARLA_ASSERT_INT(index >= -1, index);
  2928. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  2929. if (index == -1)
  2930. {
  2931. const CarlaPlugin::ScopedDisabler m(this);
  2932. return reloadPrograms(false);
  2933. }
  2934. if (index >= 0 && index < static_cast<int32_t>(kData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  2935. {
  2936. if (const LV2_Program_Descriptor* progDesc = fExt.programs->get_program(fHandle, index))
  2937. {
  2938. CARLA_ASSERT(progDesc->name != nullptr);
  2939. if (kData->midiprog.data[index].name != nullptr)
  2940. delete[] kData->midiprog.data[index].name;
  2941. kData->midiprog.data[index].name = carla_strdup(progDesc->name ? progDesc->name : "");
  2942. if (index == kData->midiprog.current)
  2943. kData->engine->callback(CALLBACK_UPDATE, fId, 0, 0, 0.0, nullptr);
  2944. else
  2945. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0, nullptr);
  2946. }
  2947. }
  2948. }
  2949. // -------------------------------------------------------------------
  2950. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  2951. {
  2952. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  2953. CARLA_ASSERT(value != nullptr);
  2954. CARLA_ASSERT(size > 0);
  2955. carla_debug("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i)", key, value, size, type, flags);
  2956. // basic checks
  2957. if (key == CARLA_URI_MAP_ID_NULL)
  2958. {
  2959. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key", key, value, size, type, flags);
  2960. return LV2_STATE_ERR_NO_PROPERTY;
  2961. }
  2962. if (value == nullptr || size == 0)
  2963. {
  2964. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid value", key, value, size, type, flags);
  2965. return LV2_STATE_ERR_NO_PROPERTY;
  2966. }
  2967. if ((flags & LV2_STATE_IS_POD) == 0)
  2968. {
  2969. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid flags", key, value, size, type, flags);
  2970. return LV2_STATE_ERR_BAD_FLAGS;
  2971. }
  2972. const char* const stype(carla_lv2_urid_unmap(this, type));
  2973. if (stype == nullptr)
  2974. {
  2975. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid type", key, value, size, type, flags);
  2976. return LV2_STATE_ERR_BAD_TYPE;
  2977. }
  2978. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  2979. if (uriKey == nullptr)
  2980. {
  2981. carla_stderr2("Lv2Plugin::handleStateStore(%i, %p, " P_SIZE ", %i, %i) - invalid key URI", key, value, size, type, flags);
  2982. return LV2_STATE_ERR_NO_PROPERTY;
  2983. }
  2984. // Check if we already have this key
  2985. for (NonRtList<CustomData>::Itenerator it = kData->custom.begin(); it.valid(); it.next())
  2986. {
  2987. CustomData& data(*it);
  2988. if (std::strcmp(data.key, uriKey) == 0)
  2989. {
  2990. if (data.value != nullptr)
  2991. delete[] data.value;
  2992. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  2993. data.value = carla_strdup((const char*)value);
  2994. else
  2995. data.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  2996. return LV2_STATE_SUCCESS;
  2997. }
  2998. }
  2999. // Otherwise store it
  3000. CustomData newData;
  3001. newData.type = carla_strdup(stype);
  3002. newData.key = carla_strdup(uriKey);
  3003. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  3004. newData.value = carla_strdup((const char*)value);
  3005. else
  3006. newData.value = carla_strdup(QByteArray((const char*)value, size).toBase64().constData());
  3007. kData->custom.append(newData);
  3008. return LV2_STATE_SUCCESS;
  3009. }
  3010. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3011. {
  3012. CARLA_ASSERT(key != CARLA_URI_MAP_ID_NULL);
  3013. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3014. // basic checks
  3015. if (key == CARLA_URI_MAP_ID_NULL)
  3016. {
  3017. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key", key, size, type, flags);
  3018. return nullptr;
  3019. }
  3020. if (size == nullptr || type == nullptr || flags == nullptr)
  3021. {
  3022. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid data", key, size, type, flags);
  3023. return nullptr;
  3024. }
  3025. const char* const uriKey(carla_lv2_urid_unmap(this, key));
  3026. if (uriKey == nullptr)
  3027. {
  3028. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - failed to find key", key, size, type, flags);
  3029. return nullptr;
  3030. }
  3031. const char* stype = nullptr;
  3032. const char* stringData = nullptr;
  3033. for (NonRtList<CustomData>::Itenerator it = kData->custom.begin(); it.valid(); it.next())
  3034. {
  3035. CustomData& data(*it);
  3036. if (std::strcmp(data.key, uriKey) == 0)
  3037. {
  3038. stype = data.type;
  3039. stringData = data.value;
  3040. break;
  3041. }
  3042. }
  3043. if (stringData == nullptr)
  3044. {
  3045. carla_stderr2("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p) - invalid key '%s'", key, size, type, flags, uriKey);
  3046. return nullptr;
  3047. }
  3048. *type = carla_lv2_urid_map(this, stype);
  3049. *flags = LV2_STATE_IS_POD;
  3050. if (std::strcmp(stype, LV2_ATOM__String) == 0 || std::strcmp(stype, LV2_ATOM__Path) == 0)
  3051. {
  3052. *size = std::strlen(stringData);
  3053. return stringData;
  3054. }
  3055. else
  3056. {
  3057. static QByteArray chunk;
  3058. chunk = QByteArray::fromBase64(stringData);
  3059. *size = chunk.size();
  3060. return chunk.constData();
  3061. }
  3062. }
  3063. // -------------------------------------------------------------------
  3064. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3065. {
  3066. carla_stdout("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3067. if (fExt.worker == nullptr || fExt.worker->work == nullptr)
  3068. {
  3069. carla_stderr("Lv2Plugin::handleWorkerSchedule(%i, %p) - plugin has no worker", size, data);
  3070. return LV2_WORKER_ERR_UNKNOWN;
  3071. }
  3072. //if (kData->engine->isOffline())
  3073. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3074. //else
  3075. // postponeEvent(PluginPostEventCustom, size, 0, 0.0, data);
  3076. return LV2_WORKER_SUCCESS;
  3077. }
  3078. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3079. {
  3080. carla_stdout("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3081. #if 0
  3082. LV2_Atom_Worker workerAtom;
  3083. workerAtom.atom.type = CARLA_URI_MAP_ID_ATOM_WORKER;
  3084. workerAtom.atom.size = sizeof(LV2_Atom_Worker_Body);
  3085. workerAtom.body.size = size;
  3086. workerAtom.body.data = data;
  3087. atomQueueIn.put(0, (const LV2_Atom*)&workerAtom);
  3088. #endif
  3089. return LV2_WORKER_SUCCESS;
  3090. }
  3091. // -------------------------------------------------------------------
  3092. void handleExternalUiClosed()
  3093. {
  3094. CARLA_ASSERT(fUi.type == PLUGIN_UI_EXTERNAL);
  3095. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3096. fUi.descriptor->cleanup(fUi.handle);
  3097. fUi.handle = nullptr;
  3098. fUi.widget = nullptr;
  3099. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  3100. }
  3101. uint32_t handleUiPortMap(const char* const symbol)
  3102. {
  3103. CARLA_ASSERT(symbol != nullptr);
  3104. if (symbol == nullptr)
  3105. return LV2UI_INVALID_PORT_INDEX;
  3106. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3107. {
  3108. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3109. return i;
  3110. }
  3111. return LV2UI_INVALID_PORT_INDEX;
  3112. }
  3113. int handleUiResize(const int width, const int height)
  3114. {
  3115. CARLA_ASSERT(kData->gui != nullptr);
  3116. CARLA_ASSERT(width > 0);
  3117. CARLA_ASSERT(height > 0);
  3118. if (width <= 0 || height <= 0)
  3119. return 1;
  3120. if (kData->gui != nullptr)
  3121. kData->gui->setSize(width, height);
  3122. return 0;
  3123. }
  3124. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3125. {
  3126. if (format == 0)
  3127. {
  3128. CARLA_ASSERT(buffer != nullptr);
  3129. CARLA_ASSERT(bufferSize == sizeof(float));
  3130. if (buffer == nullptr || bufferSize != sizeof(float))
  3131. return;
  3132. const float value(*(const float*)buffer);
  3133. for (uint32_t i=0; i < kData->param.count; ++i)
  3134. {
  3135. if (kData->param.data[i].rindex == static_cast<int32_t>(rindex))
  3136. {
  3137. if (fParamBuffers[i] != value)
  3138. setParameterValue(i, value, false, true, true);
  3139. break;
  3140. }
  3141. }
  3142. }
  3143. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM || format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  3144. {
  3145. CARLA_ASSERT(bufferSize != 0);
  3146. CARLA_ASSERT(buffer != nullptr);
  3147. if (bufferSize == 0 || buffer == nullptr)
  3148. return;
  3149. fAtomQueueIn.put(rindex, (const LV2_Atom*)buffer);
  3150. }
  3151. else
  3152. {
  3153. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3154. }
  3155. }
  3156. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, uint32_t size, uint32_t type)
  3157. {
  3158. CARLA_ASSERT(portSymbol != nullptr);
  3159. CARLA_ASSERT(value != nullptr);
  3160. CARLA_ASSERT(size > 0);
  3161. CARLA_ASSERT(type != CARLA_URI_MAP_ID_NULL);
  3162. if (portSymbol == nullptr)
  3163. return;
  3164. if (value == nullptr)
  3165. return;
  3166. if (size == 0)
  3167. return;
  3168. if (type == CARLA_URI_MAP_ID_NULL)
  3169. return;
  3170. const int32_t rindex(handleUiPortMap(portSymbol));
  3171. if (rindex < 0)
  3172. return;
  3173. if (size == sizeof(float) && type == CARLA_URI_MAP_ID_ATOM_FLOAT)
  3174. {
  3175. const float valuef(*(const float*)value);
  3176. for (uint32_t i=0; i < kData->param.count; ++i)
  3177. {
  3178. if (kData->param.data[i].rindex == rindex)
  3179. {
  3180. setParameterValue(i, valuef, true, true, true);
  3181. break;
  3182. }
  3183. }
  3184. }
  3185. }
  3186. // -------------------------------------------------------------------
  3187. bool isRealtimeSafe() const
  3188. {
  3189. CARLA_ASSERT(fRdfDescriptor != nullptr);
  3190. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3191. {
  3192. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3193. return true;
  3194. }
  3195. return false;
  3196. }
  3197. bool needsFixedBuffer() const
  3198. {
  3199. CARLA_ASSERT(fRdfDescriptor != nullptr);
  3200. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3201. {
  3202. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3203. return true;
  3204. }
  3205. return false;
  3206. }
  3207. // -------------------------------------------------------------------
  3208. const char* getUiBridgePath(const LV2_Property type) const
  3209. {
  3210. const EngineOptions& options(kData->engine->getOptions());
  3211. switch (type)
  3212. {
  3213. case LV2_UI_GTK2:
  3214. return options.bridge_lv2Gtk2;
  3215. case LV2_UI_GTK3:
  3216. return options.bridge_lv2Gtk3;
  3217. case LV2_UI_QT4:
  3218. return options.bridge_lv2Qt4;
  3219. case LV2_UI_QT5:
  3220. return options.bridge_lv2Qt5;
  3221. case LV2_UI_COCOA:
  3222. return options.bridge_lv2Cocoa;
  3223. case LV2_UI_WINDOWS:
  3224. return options.bridge_lv2Win;
  3225. case LV2_UI_X11:
  3226. return options.bridge_lv2X11;
  3227. default:
  3228. return nullptr;
  3229. }
  3230. }
  3231. bool isUiBridgeable(const uint32_t uiId) const
  3232. {
  3233. const LV2_RDF_UI& rdfUi(fRdfDescriptor->UIs[uiId]);
  3234. for (uint32_t i=0; i < rdfUi.FeatureCount; ++i)
  3235. {
  3236. if (std::strcmp(rdfUi.Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3237. return false;
  3238. if (std::strcmp(rdfUi.Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3239. return false;
  3240. }
  3241. return true;
  3242. }
  3243. bool isUiResizable() const
  3244. {
  3245. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3246. {
  3247. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3248. return false;
  3249. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3250. return false;
  3251. }
  3252. return true;
  3253. }
  3254. void updateUi()
  3255. {
  3256. CARLA_ASSERT(fUi.handle != nullptr);
  3257. CARLA_ASSERT(fUi.descriptor != nullptr);
  3258. fExt.uiidle = nullptr;
  3259. fExt.uiprograms = nullptr;
  3260. if (fUi.descriptor->extension_data != nullptr)
  3261. {
  3262. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  3263. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  3264. // check if invalid
  3265. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  3266. fExt.uiidle = nullptr;
  3267. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  3268. fExt.uiprograms = nullptr;
  3269. // update midi program
  3270. if (fExt.uiprograms && kData->midiprog.count > 0 && kData->midiprog.current >= 0)
  3271. fExt.uiprograms->select_program(fUi.handle, kData->midiprog.data[kData->midiprog.current].bank,
  3272. kData->midiprog.data[kData->midiprog.current].program);
  3273. }
  3274. if (fUi.descriptor->port_event != nullptr)
  3275. {
  3276. // update control ports
  3277. float value;
  3278. for (uint32_t i=0; i < kData->param.count; ++i)
  3279. {
  3280. value = getParameterValue(i);
  3281. fUi.descriptor->port_event(fUi.handle, kData->param.data[i].rindex, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3282. }
  3283. }
  3284. }
  3285. // -------------------------------------------------------------------
  3286. public:
  3287. bool init(const char* const name, const char* const uri)
  3288. {
  3289. CARLA_ASSERT(kData->engine != nullptr);
  3290. CARLA_ASSERT(kData->client == nullptr);
  3291. CARLA_ASSERT(uri != nullptr);
  3292. // ---------------------------------------------------------------
  3293. // first checks
  3294. if (kData->engine == nullptr)
  3295. {
  3296. return false;
  3297. }
  3298. if (kData->client != nullptr)
  3299. {
  3300. kData->engine->setLastError("Plugin client is already registered");
  3301. return false;
  3302. }
  3303. if (uri == nullptr)
  3304. {
  3305. kData->engine->setLastError("null uri");
  3306. return false;
  3307. }
  3308. // ---------------------------------------------------------------
  3309. // get plugin from lv2_rdf (lilv)
  3310. gLv2World.init();
  3311. fRdfDescriptor = lv2_rdf_new(uri);
  3312. if (fRdfDescriptor == nullptr)
  3313. {
  3314. kData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3315. return false;
  3316. }
  3317. // ---------------------------------------------------------------
  3318. // open DLL
  3319. if (! kData->libOpen(fRdfDescriptor->Binary))
  3320. {
  3321. kData->engine->setLastError(kData->libError(fRdfDescriptor->Binary));
  3322. return false;
  3323. }
  3324. // ---------------------------------------------------------------
  3325. // initialize options
  3326. fLv2Options.minBufferSize = 1;
  3327. fLv2Options.maxBufferSize = kData->engine->getBufferSize();
  3328. fLv2Options.sampleRate = kData->engine->getSampleRate();
  3329. // ---------------------------------------------------------------
  3330. // initialize features (part 1)
  3331. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3332. eventFt->callback_data = this;
  3333. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3334. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3335. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3336. logFt->handle = this;
  3337. logFt->printf = carla_lv2_log_printf;
  3338. logFt->vprintf = carla_lv2_log_vprintf;
  3339. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3340. stateMakePathFt->handle = this;
  3341. stateMakePathFt->path = carla_lv2_state_make_path;
  3342. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3343. stateMapPathFt->handle = this;
  3344. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3345. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3346. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3347. programsFt->handle = this;
  3348. programsFt->program_changed = carla_lv2_program_changed;
  3349. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3350. lv2_rtmempool_init(rtMemPoolFt);
  3351. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3352. uriMapFt->callback_data = this;
  3353. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3354. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3355. uridMapFt->handle = this;
  3356. uridMapFt->map = carla_lv2_urid_map;
  3357. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3358. uridUnmapFt->handle = this;
  3359. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3360. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3361. workerFt->handle = this;
  3362. workerFt->schedule_work = carla_lv2_worker_schedule;
  3363. // ---------------------------------------------------------------
  3364. // initialize features (part 2)
  3365. for (uint32_t i=0; i < kFeatureIdWorker+1; ++i)
  3366. fFeatures[i] = new LV2_Feature;
  3367. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3368. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3369. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3370. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3371. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3372. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3373. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3374. fFeatures[kFeatureIdEvent]->data = eventFt;
  3375. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3376. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3377. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3378. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3379. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3380. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3381. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3382. fFeatures[kFeatureIdLogs]->data = logFt;
  3383. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3384. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3385. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3386. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3387. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3388. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3389. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3390. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3391. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3392. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3393. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3394. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3395. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3396. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3397. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3398. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3399. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3400. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3401. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3402. fFeatures[kFeatureIdWorker]->data = workerFt;
  3403. // check if it's possible to use non-fixed buffer size
  3404. if (! needsFixedBuffer())
  3405. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3406. // ---------------------------------------------------------------
  3407. // get DLL main entry
  3408. #if 0
  3409. const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)kData->libSymbol("lv2_lib_descriptor");
  3410. if (libDescFn != nullptr)
  3411. {
  3412. // -----------------------------------------------------------
  3413. // get lib descriptor
  3414. const LV2_Lib_Descriptor* libFn = nullptr; //descLibFn(fRdfDescriptor->Bundle, features);
  3415. if (libFn == nullptr || libFn->get_plugin == nullptr)
  3416. {
  3417. kData->engine->setLastError("Plugin failed to return library descriptor");
  3418. return false;
  3419. }
  3420. // -----------------------------------------------------------
  3421. // get descriptor that matches URI
  3422. uint32_t i = 0;
  3423. while ((fDescriptor = libFn->get_plugin(libFn->handle, i++)))
  3424. {
  3425. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3426. break;
  3427. }
  3428. if (fDescriptor == nullptr)
  3429. libFn->cleanup(libFn->handle);
  3430. else
  3431. #endif
  3432. {
  3433. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)kData->libSymbol("lv2_descriptor");
  3434. if (descFn == nullptr)
  3435. {
  3436. kData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3437. return false;
  3438. }
  3439. // -----------------------------------------------------------
  3440. // get descriptor that matches URI
  3441. uint32_t i = 0;
  3442. while ((fDescriptor = descFn(i++)))
  3443. {
  3444. carla_debug("LV2 Init @%i => '%s' vs '%s'", i, fDescriptor->URI, uri);
  3445. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3446. break;
  3447. }
  3448. }
  3449. if (fDescriptor == nullptr)
  3450. {
  3451. kData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3452. return false;
  3453. }
  3454. // ---------------------------------------------------------------
  3455. // check supported port-types and features
  3456. bool canContinue = true;
  3457. // Check supported ports
  3458. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3459. {
  3460. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  3461. if (! (LV2_IS_PORT_AUDIO(portTypes) || LV2_IS_PORT_CONTROL(portTypes) || LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes)))
  3462. {
  3463. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[i].Properties))
  3464. {
  3465. kData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3466. canContinue = false;
  3467. break;
  3468. }
  3469. }
  3470. }
  3471. // Check supported features
  3472. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount && canContinue; ++i)
  3473. {
  3474. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[i].Type) && ! is_lv2_feature_supported(fRdfDescriptor->Features[i].URI))
  3475. {
  3476. QString msg(QString("Plugin requires a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[i].URI));
  3477. kData->engine->setLastError(msg.toUtf8().constData());
  3478. canContinue = false;
  3479. break;
  3480. }
  3481. }
  3482. // Check extensions
  3483. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3484. {
  3485. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3486. kData->extraHints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3487. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3488. kData->extraHints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3489. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3490. kData->extraHints |= PLUGIN_HAS_EXTENSION_STATE;
  3491. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3492. kData->extraHints |= PLUGIN_HAS_EXTENSION_WORKER;
  3493. else
  3494. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3495. }
  3496. if (! canContinue)
  3497. {
  3498. // error already set
  3499. return false;
  3500. }
  3501. // ---------------------------------------------------------------
  3502. // get info
  3503. if (name != nullptr)
  3504. fName = kData->engine->getUniquePluginName(name);
  3505. else
  3506. fName = kData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3507. // ---------------------------------------------------------------
  3508. // register client
  3509. kData->client = kData->engine->addClient(this);
  3510. if (kData->client == nullptr || ! kData->client->isOk())
  3511. {
  3512. kData->engine->setLastError("Failed to register plugin client");
  3513. return false;
  3514. }
  3515. // ---------------------------------------------------------------
  3516. // initialize plugin
  3517. fHandle = fDescriptor->instantiate(fDescriptor, kData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3518. if (fHandle == nullptr)
  3519. {
  3520. kData->engine->setLastError("Plugin failed to initialize");
  3521. return false;
  3522. }
  3523. // ---------------------------------------------------------------
  3524. // load plugin settings
  3525. {
  3526. // set default options
  3527. fOptions = 0x0;
  3528. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3529. if (midiInCount() > 0 || needsFixedBuffer())
  3530. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3531. if (kData->engine->getOptions().forceStereo)
  3532. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  3533. if (midiInCount() > 0)
  3534. {
  3535. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3536. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3537. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  3538. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3539. }
  3540. // load settings
  3541. kData->idStr = "LV2/";
  3542. kData->idStr += uri;
  3543. fOptions = kData->loadSettings(fOptions, availableOptions());
  3544. // ignore settings, we need this anyway
  3545. if (midiInCount() > 0 || needsFixedBuffer())
  3546. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  3547. }
  3548. // ---------------------------------------------------------------
  3549. // gui stuff
  3550. if (fRdfDescriptor->UICount == 0)
  3551. return true;
  3552. // -----------------------------------------------------------
  3553. // find more appropriate ui
  3554. int eQt4, eQt5, eCocoa, eWindows, eX11, eGtk2, eGtk3, iCocoa, iWindows, iX11, iQt4, iQt5, iExt, iFinal;
  3555. eQt4 = eQt5 = eCocoa = eWindows = eX11 = eGtk2 = eGtk3 = iQt4 = iQt5 = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3556. #ifdef BUILD_BRIDGE
  3557. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges);
  3558. #else
  3559. const bool preferUiBridges(kData->engine->getOptions().preferUiBridges && (fHints & PLUGIN_IS_BRIDGE) == 0);
  3560. #endif
  3561. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3562. {
  3563. CARLA_ASSERT(fRdfDescriptor->UIs[i].URI != nullptr);
  3564. if (fRdfDescriptor->UIs[i].URI == nullptr)
  3565. {
  3566. carla_stderr("Plugin has an UI without a valid URI");
  3567. continue;
  3568. }
  3569. switch (fRdfDescriptor->UIs[i].Type)
  3570. {
  3571. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3572. case LV2_UI_QT4:
  3573. if (isUiBridgeable(i))
  3574. eQt4 = i;
  3575. break;
  3576. #else
  3577. case LV2_UI_QT4:
  3578. if (isUiBridgeable(i) && preferUiBridges)
  3579. eQt4 = i;
  3580. iQt4 = i;
  3581. break;
  3582. #endif
  3583. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3584. case LV2_UI_QT5:
  3585. if (isUiBridgeable(i) && preferUiBridges)
  3586. eQt5 = i;
  3587. iQt5 = i;
  3588. break;
  3589. #else
  3590. case LV2_UI_QT5:
  3591. if (isUiBridgeable(i) && preferUiBridges)
  3592. eQt5 = i;
  3593. break;
  3594. #endif
  3595. #ifdef CARLA_OS_MAC
  3596. case LV2_UI_COCOA:
  3597. if (isUiBridgeable(i) && preferUiBridges)
  3598. eCocoa = i;
  3599. iCocoa = i;
  3600. break;
  3601. #endif
  3602. #ifdef CARLA_OS_WIN
  3603. case LV2_UI_WINDOWS:
  3604. if (isUiBridgeable(i) && preferUiBridges)
  3605. eWindows = i;
  3606. iWindows = i;
  3607. break;
  3608. #endif
  3609. case LV2_UI_X11:
  3610. if (isUiBridgeable(i) && preferUiBridges)
  3611. eX11 = i;
  3612. #ifdef Q_WS_X11
  3613. iX11 = i;
  3614. #endif
  3615. break;
  3616. case LV2_UI_GTK2:
  3617. if (isUiBridgeable(i))
  3618. eGtk2 = i;
  3619. break;
  3620. case LV2_UI_GTK3:
  3621. if (isUiBridgeable(i))
  3622. eGtk3 = i;
  3623. break;
  3624. case LV2_UI_EXTERNAL:
  3625. case LV2_UI_OLD_EXTERNAL:
  3626. iExt = i;
  3627. break;
  3628. default:
  3629. break;
  3630. }
  3631. }
  3632. if (eQt4 >= 0)
  3633. iFinal = eQt4;
  3634. else if (eQt5 >= 0)
  3635. iFinal = eQt5;
  3636. else if (eCocoa >= 0)
  3637. iFinal = eCocoa;
  3638. else if (eWindows >= 0)
  3639. iFinal = eWindows;
  3640. else if (eX11 >= 0)
  3641. iFinal = eX11;
  3642. else if (iQt4 >= 0)
  3643. iFinal = iQt4;
  3644. else if (iQt5 >= 0)
  3645. iFinal = iQt5;
  3646. else if (iCocoa >= 0)
  3647. iFinal = iCocoa;
  3648. else if (iWindows >= 0)
  3649. iFinal = iWindows;
  3650. else if (iX11 >= 0)
  3651. iFinal = iX11;
  3652. else if (iExt >= 0)
  3653. iFinal = iExt;
  3654. else if (eGtk2 >= 0)
  3655. iFinal = eGtk2;
  3656. else if (eGtk3 >= 0)
  3657. iFinal = eGtk3;
  3658. if (iFinal < 0)
  3659. {
  3660. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3661. return true;
  3662. }
  3663. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3664. // -----------------------------------------------------------
  3665. // check supported ui features
  3666. canContinue = true;
  3667. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3668. {
  3669. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type) && ! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3670. {
  3671. carla_stderr2("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3672. canContinue = false;
  3673. break;
  3674. }
  3675. }
  3676. if (! canContinue)
  3677. {
  3678. fUi.rdfDescriptor = nullptr;
  3679. return true;
  3680. }
  3681. // -----------------------------------------------------------
  3682. // initialize ui according to type
  3683. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3684. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eGtk2 || iFinal == eGtk3)
  3685. {
  3686. // -------------------------------------------------------
  3687. // initialize ui bridge
  3688. if (const char* const oscBinary = getUiBridgePath(uiType))
  3689. {
  3690. fUi.type = PLUGIN_UI_OSC;
  3691. kData->osc.thread.setOscData(oscBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3692. }
  3693. }
  3694. else
  3695. {
  3696. // -------------------------------------------------------
  3697. // open UI DLL
  3698. if (! kData->uiLibOpen(fUi.rdfDescriptor->Binary))
  3699. {
  3700. carla_stderr2("Could not load UI library, error was:\n%s", kData->libError(fUi.rdfDescriptor->Binary));
  3701. fUi.rdfDescriptor = nullptr;
  3702. return true;
  3703. }
  3704. // -------------------------------------------------------
  3705. // get UI DLL main entry
  3706. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)kData->uiLibSymbol("lv2ui_descriptor");
  3707. if (uiDescFn == nullptr)
  3708. {
  3709. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3710. kData->uiLibClose();
  3711. fUi.rdfDescriptor = nullptr;
  3712. return true;
  3713. }
  3714. // -------------------------------------------------------
  3715. // get UI descriptor that matches UI URI
  3716. uint32_t i = 0;
  3717. while ((fUi.descriptor = uiDescFn(i++)))
  3718. {
  3719. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  3720. break;
  3721. }
  3722. if (fUi.descriptor == nullptr)
  3723. {
  3724. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  3725. kData->uiLibClose();
  3726. fUi.rdfDescriptor = nullptr;
  3727. return true;
  3728. }
  3729. // -------------------------------------------------------
  3730. // check if ui is usable
  3731. switch (uiType)
  3732. {
  3733. #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
  3734. case LV2_UI_QT5:
  3735. carla_debug("Will use LV2 Qt5 UI");
  3736. fUi.type = PLUGIN_UI_QT;
  3737. break;
  3738. #else
  3739. case LV2_UI_QT4:
  3740. carla_debug("Will use LV2 Qt4 UI");
  3741. fUi.type = PLUGIN_UI_QT;
  3742. break;
  3743. #endif
  3744. #ifdef CARLA_OS_MAC
  3745. case LV2_UI_COCOA:
  3746. carla_debug("Will use LV2 Cocoa UI");
  3747. fUi.type = PLUGIN_UI_PARENT;
  3748. break;
  3749. #endif
  3750. #ifdef CARLA_OS_WIN
  3751. case LV2_UI_WINDOWS:
  3752. carla_debug("Will use LV2 Windows UI");
  3753. fUi.type = PLUGIN_UI_PARENT;
  3754. break;
  3755. #endif
  3756. #ifdef Q_WS_X11
  3757. case LV2_UI_X11:
  3758. carla_debug("Will use LV2 X11 UI");
  3759. fUi.type = PLUGIN_UI_PARENT;
  3760. break;
  3761. #endif
  3762. case LV2_UI_GTK2:
  3763. carla_debug("Will use LV2 Gtk2 UI, NOT!");
  3764. break;
  3765. case LV2_UI_GTK3:
  3766. carla_debug("Will use LV2 Gtk3 UI, NOT!");
  3767. break;
  3768. case LV2_UI_EXTERNAL:
  3769. case LV2_UI_OLD_EXTERNAL:
  3770. carla_debug("Will use LV2 External UI");
  3771. fUi.type = PLUGIN_UI_EXTERNAL;
  3772. break;
  3773. }
  3774. if (fUi.type == PLUGIN_UI_NULL)
  3775. {
  3776. kData->uiLibClose();
  3777. fUi.descriptor = nullptr;
  3778. fUi.rdfDescriptor = nullptr;
  3779. return true;
  3780. }
  3781. // -------------------------------------------------------
  3782. // initialize ui features (part 1)
  3783. QString guiTitle(QString("%1 (GUI)").arg((const char*)fName));
  3784. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  3785. uiDataFt->data_access = fDescriptor->extension_data;
  3786. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  3787. uiPortMapFt->handle = this;
  3788. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  3789. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  3790. uiResizeFt->handle = this;
  3791. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  3792. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  3793. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  3794. uiExternalHostFt->plugin_human_id = carla_strdup(guiTitle.toUtf8().constData());
  3795. // -------------------------------------------------------
  3796. // initialize ui features (part 2)
  3797. for (uint32_t i=kFeatureIdUiDataAccess; i < kFeatureCount; ++i)
  3798. fFeatures[i] = new LV2_Feature;
  3799. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  3800. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  3801. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  3802. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  3803. fFeatures[kFeatureIdUiIdle]->URI = LV2_UI__idle;
  3804. fFeatures[kFeatureIdUiIdle]->data = nullptr;
  3805. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  3806. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  3807. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  3808. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  3809. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  3810. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  3811. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  3812. fFeatures[kFeatureIdUiParent]->data = nullptr;
  3813. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  3814. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  3815. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  3816. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  3817. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  3818. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  3819. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  3820. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  3821. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  3822. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  3823. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  3824. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  3825. }
  3826. return true;
  3827. }
  3828. // -------------------------------------------------------------------
  3829. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  3830. {
  3831. CARLA_ASSERT(atom != nullptr);
  3832. carla_debug("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  3833. fAtomQueueIn.put(portIndex, atom);
  3834. }
  3835. void handleUridMap(const LV2_URID urid, const char* const uri)
  3836. {
  3837. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  3838. CARLA_ASSERT(uri != nullptr);
  3839. carla_debug("Lv2Plugin::handleUridMap(%i, \"%s\")", urid, uri);
  3840. CARLA_SAFE_ASSERT_INT2(urid == fCustomURIDs.count(), urid, fCustomURIDs.count());
  3841. if (urid != fCustomURIDs.count())
  3842. return;
  3843. fCustomURIDs.append(carla_strdup(uri));
  3844. }
  3845. // -------------------------------------------------------------------
  3846. private:
  3847. LV2_Handle fHandle;
  3848. LV2_Handle fHandle2;
  3849. LV2_Feature* fFeatures[kFeatureCount+1];
  3850. const LV2_Descriptor* fDescriptor;
  3851. const LV2_RDF_Descriptor* fRdfDescriptor;
  3852. float** fAudioInBuffers;
  3853. float** fAudioOutBuffers;
  3854. float* fParamBuffers;
  3855. Lv2AtomQueue fAtomQueueIn;
  3856. Lv2AtomQueue fAtomQueueOut;
  3857. LV2_Atom_Forge fAtomForge;
  3858. Lv2PluginEventData fEventsIn;
  3859. Lv2PluginEventData fEventsOut;
  3860. Lv2PluginOptions fLv2Options;
  3861. NonRtList<const char*> fCustomURIDs;
  3862. bool fFirstActive; // first process() call after activate()
  3863. EngineTimeInfo fLastTimeInfo;
  3864. struct Extensions {
  3865. const LV2_Options_Interface* options;
  3866. const LV2_State_Interface* state;
  3867. const LV2_Worker_Interface* worker;
  3868. const LV2_Programs_Interface* programs;
  3869. const LV2UI_Idle_Interface* uiidle;
  3870. const LV2_Programs_UI_Interface* uiprograms;
  3871. Extensions()
  3872. : options(nullptr),
  3873. state(nullptr),
  3874. worker(nullptr),
  3875. programs(nullptr),
  3876. uiidle(nullptr),
  3877. uiprograms(nullptr) {}
  3878. } fExt;
  3879. struct UI {
  3880. Lv2PluginGuiType type;
  3881. LV2UI_Handle handle;
  3882. LV2UI_Widget widget;
  3883. const LV2UI_Descriptor* descriptor;
  3884. const LV2_RDF_UI* rdfDescriptor;
  3885. UI()
  3886. : type(PLUGIN_UI_NULL),
  3887. handle(nullptr),
  3888. widget(nullptr),
  3889. descriptor(nullptr),
  3890. rdfDescriptor(nullptr) {}
  3891. ~UI()
  3892. {
  3893. CARLA_ASSERT(handle == nullptr);
  3894. CARLA_ASSERT(widget == nullptr);
  3895. CARLA_ASSERT(descriptor == nullptr);
  3896. CARLA_ASSERT(rdfDescriptor == nullptr);
  3897. }
  3898. } fUi;
  3899. // -------------------------------------------------------------------
  3900. // Event Feature
  3901. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3902. {
  3903. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  3904. CARLA_ASSERT(callback_data != nullptr);
  3905. CARLA_ASSERT(event != nullptr);
  3906. return 0;
  3907. }
  3908. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  3909. {
  3910. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  3911. CARLA_ASSERT(callback_data != nullptr);
  3912. CARLA_ASSERT(event != nullptr);
  3913. return 0;
  3914. }
  3915. // -------------------------------------------------------------------
  3916. // Logs Feature
  3917. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  3918. {
  3919. CARLA_ASSERT(handle != nullptr);
  3920. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3921. #ifndef DEBUG
  3922. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3923. return 0;
  3924. #endif
  3925. va_list args;
  3926. va_start(args, fmt);
  3927. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  3928. va_end(args);
  3929. return ret;
  3930. }
  3931. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  3932. {
  3933. CARLA_ASSERT(handle != nullptr);
  3934. CARLA_ASSERT(type > CARLA_URI_MAP_ID_NULL);
  3935. #ifndef DEBUG
  3936. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  3937. return 0;
  3938. #endif
  3939. int ret = 0;
  3940. switch (type)
  3941. {
  3942. case CARLA_URI_MAP_ID_LOG_ERROR:
  3943. #ifndef CARLA_OS_WIN
  3944. std::fprintf(stderr, "\x1b[31m");
  3945. #endif
  3946. ret = std::vfprintf(stderr, fmt, ap);
  3947. #ifndef CARLA_OS_WIN
  3948. std::fprintf(stderr, "\x1b[0m");
  3949. #endif
  3950. break;
  3951. case CARLA_URI_MAP_ID_LOG_NOTE:
  3952. ret = std::vfprintf(stdout, fmt, ap);
  3953. break;
  3954. case CARLA_URI_MAP_ID_LOG_TRACE:
  3955. #ifdef DEBUG
  3956. # ifndef CARLA_OS_WIN
  3957. std::fprintf(stdout, "\x1b[30;1m");
  3958. # endif
  3959. ret = std::vfprintf(stdout, fmt, ap);
  3960. # ifndef CARLA_OS_WIN
  3961. std::fprintf(stdout, "\x1b[0m");
  3962. # endif
  3963. #endif
  3964. break;
  3965. case CARLA_URI_MAP_ID_LOG_WARNING:
  3966. ret = std::vfprintf(stderr, fmt, ap);
  3967. break;
  3968. default:
  3969. break;
  3970. }
  3971. return ret;
  3972. }
  3973. // -------------------------------------------------------------------
  3974. // Programs Feature
  3975. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  3976. {
  3977. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  3978. CARLA_ASSERT(handle != nullptr);
  3979. if (handle == nullptr)
  3980. return;
  3981. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  3982. }
  3983. // -------------------------------------------------------------------
  3984. // State Feature
  3985. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  3986. {
  3987. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  3988. CARLA_ASSERT(handle != nullptr);
  3989. CARLA_ASSERT(path != nullptr);
  3990. if (path == nullptr)
  3991. return nullptr;
  3992. QDir dir;
  3993. dir.mkpath(path);
  3994. return strdup(path);
  3995. }
  3996. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  3997. {
  3998. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  3999. CARLA_ASSERT(handle != nullptr);
  4000. CARLA_ASSERT(absolute_path != nullptr);
  4001. if (absolute_path == nullptr)
  4002. return nullptr;
  4003. QDir dir(absolute_path);
  4004. return strdup(dir.canonicalPath().toUtf8().constData());
  4005. }
  4006. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4007. {
  4008. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4009. CARLA_ASSERT(handle != nullptr);
  4010. CARLA_ASSERT(abstract_path != nullptr);
  4011. if (abstract_path == nullptr)
  4012. return nullptr;
  4013. QDir dir(abstract_path);
  4014. return strdup(dir.absolutePath().toUtf8().constData());
  4015. }
  4016. 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)
  4017. {
  4018. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4019. CARLA_ASSERT(handle != nullptr);
  4020. if (handle == nullptr)
  4021. return LV2_STATE_ERR_UNKNOWN;
  4022. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4023. }
  4024. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4025. {
  4026. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4027. CARLA_ASSERT(handle != nullptr);
  4028. if (handle == nullptr)
  4029. return nullptr;
  4030. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4031. }
  4032. // -------------------------------------------------------------------
  4033. // URI-Map Feature
  4034. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4035. {
  4036. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4037. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4038. // unused
  4039. (void)map;
  4040. }
  4041. // -------------------------------------------------------------------
  4042. // URID Feature
  4043. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4044. {
  4045. CARLA_ASSERT(handle != nullptr);
  4046. CARLA_ASSERT(uri != nullptr);
  4047. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4048. if (uri == nullptr)
  4049. return CARLA_URI_MAP_ID_NULL;
  4050. // Atom types
  4051. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4052. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4053. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4054. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4055. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4056. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4057. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4058. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4059. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4060. return CARLA_URI_MAP_ID_ATOM_INT;
  4061. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4062. return CARLA_URI_MAP_ID_ATOM_PATH;
  4063. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4064. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4065. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4066. return CARLA_URI_MAP_ID_ATOM_STRING;
  4067. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4068. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4069. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4070. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4071. // BufSize types
  4072. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4073. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4074. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4075. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4076. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4077. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4078. // Log types
  4079. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4080. return CARLA_URI_MAP_ID_LOG_ERROR;
  4081. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4082. return CARLA_URI_MAP_ID_LOG_NOTE;
  4083. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4084. return CARLA_URI_MAP_ID_LOG_TRACE;
  4085. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4086. return CARLA_URI_MAP_ID_LOG_WARNING;
  4087. // Time types
  4088. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4089. return CARLA_URI_MAP_ID_TIME_POSITION;
  4090. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4091. return CARLA_URI_MAP_ID_TIME_BAR;
  4092. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4093. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4094. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4095. return CARLA_URI_MAP_ID_TIME_BEAT;
  4096. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4097. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4098. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4099. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4100. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4101. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4102. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4103. return CARLA_URI_MAP_ID_TIME_FRAME;
  4104. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4105. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4106. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4107. return CARLA_URI_MAP_ID_TIME_SPEED;
  4108. // Others
  4109. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4110. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4111. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4112. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4113. if (handle == nullptr)
  4114. return CARLA_URI_MAP_ID_NULL;
  4115. // Custom types
  4116. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4117. }
  4118. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4119. {
  4120. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4121. CARLA_ASSERT(handle != nullptr);
  4122. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  4123. if (urid == CARLA_URI_MAP_ID_NULL)
  4124. return nullptr;
  4125. // Atom types
  4126. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4127. return LV2_ATOM__Blank;
  4128. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4129. return LV2_ATOM__Chunk;
  4130. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4131. return LV2_ATOM__Double;
  4132. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4133. return LV2_ATOM__Float;
  4134. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4135. return LV2_ATOM__Int;
  4136. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4137. return LV2_ATOM__Path;
  4138. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4139. return LV2_ATOM__Sequence;
  4140. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4141. return LV2_ATOM__String;
  4142. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4143. return LV2_ATOM__atomTransfer;
  4144. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4145. return LV2_ATOM__eventTransfer;
  4146. // BufSize types
  4147. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4148. return LV2_BUF_SIZE__maxBlockLength;
  4149. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4150. return LV2_BUF_SIZE__minBlockLength;
  4151. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4152. return LV2_BUF_SIZE__sequenceSize;
  4153. // Log types
  4154. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4155. return LV2_LOG__Error;
  4156. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4157. return LV2_LOG__Note;
  4158. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4159. return LV2_LOG__Trace;
  4160. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4161. return LV2_LOG__Warning;
  4162. // Time types
  4163. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4164. return LV2_TIME__Position;
  4165. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4166. return LV2_TIME__bar;
  4167. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4168. return LV2_TIME__barBeat;
  4169. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4170. return LV2_TIME__beat;
  4171. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4172. return LV2_TIME__beatUnit;
  4173. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4174. return LV2_TIME__beatsPerBar;
  4175. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4176. return LV2_TIME__beatsPerMinute;
  4177. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4178. return LV2_TIME__frame;
  4179. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4180. return LV2_TIME__framesPerSecond;
  4181. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4182. return LV2_TIME__speed;
  4183. // Others
  4184. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4185. return LV2_MIDI__MidiEvent;
  4186. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4187. return LV2_PARAMETERS__sampleRate;
  4188. if (handle == nullptr)
  4189. return nullptr;
  4190. // Custom types
  4191. return ((Lv2Plugin*)handle)->getCustomURIString(urid);
  4192. }
  4193. // -------------------------------------------------------------------
  4194. // Worker Feature
  4195. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4196. {
  4197. CARLA_ASSERT(handle != nullptr);
  4198. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4199. if (handle == nullptr)
  4200. return LV2_WORKER_ERR_UNKNOWN;
  4201. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4202. }
  4203. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4204. {
  4205. CARLA_ASSERT(handle != nullptr);
  4206. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4207. if (handle == nullptr)
  4208. return LV2_WORKER_ERR_UNKNOWN;
  4209. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4210. }
  4211. // -------------------------------------------------------------------
  4212. // UI Port-Map Feature
  4213. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4214. {
  4215. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4216. CARLA_ASSERT(handle);
  4217. if (handle == nullptr)
  4218. return LV2UI_INVALID_PORT_INDEX;
  4219. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4220. }
  4221. // -------------------------------------------------------------------
  4222. // UI Resize Feature
  4223. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4224. {
  4225. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4226. CARLA_ASSERT(handle != nullptr);
  4227. if (handle == nullptr)
  4228. return 1;
  4229. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4230. }
  4231. // -------------------------------------------------------------------
  4232. // External UI Feature
  4233. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4234. {
  4235. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4236. CARLA_ASSERT(controller != nullptr);
  4237. if (controller == nullptr)
  4238. return;
  4239. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4240. }
  4241. // -------------------------------------------------------------------
  4242. // UI Extension
  4243. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4244. {
  4245. CARLA_ASSERT(controller != nullptr);
  4246. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4247. if (controller == nullptr)
  4248. return;
  4249. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4250. }
  4251. // -------------------------------------------------------------------
  4252. // Lilv State
  4253. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4254. {
  4255. CARLA_ASSERT(user_data != nullptr);
  4256. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4257. if (user_data == nullptr)
  4258. return;
  4259. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4260. }
  4261. // -------------------------------------------------------------------
  4262. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4263. };
  4264. // -------------------------------------------------------------------------------------------------------------------
  4265. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4266. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4267. {
  4268. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4269. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4270. const int32_t portIndex = argv[0]->i;
  4271. const char* const atomBuf = (const char*)&argv[1]->s;
  4272. if (portIndex < 0)
  4273. return 0;
  4274. QByteArray chunk;
  4275. chunk = QByteArray::fromBase64(atomBuf);
  4276. LV2_Atom* const atom = (LV2_Atom*)chunk.data();
  4277. lv2PluginPtr->handleTransferAtom(portIndex, atom);
  4278. return 0;
  4279. }
  4280. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4281. {
  4282. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4283. carla_debug("CarlaOsc::handleMsgLv2EventTransfer()");
  4284. const int32_t urid = argv[0]->i;
  4285. const char* const uri = (const char*)&argv[1]->s;
  4286. if (urid <= 0)
  4287. return 0;
  4288. lv2PluginPtr->handleUridMap(urid, uri);
  4289. return 0;
  4290. }
  4291. #undef lv2PluginPtr
  4292. CARLA_BACKEND_END_NAMESPACE
  4293. #else // WANT_VST
  4294. # warning Building without LV2 support
  4295. #endif
  4296. CARLA_BACKEND_START_NAMESPACE
  4297. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4298. {
  4299. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\"})", init.engine, init.name, init.label);
  4300. #ifdef WANT_LV2
  4301. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4302. if (! plugin->init(init.name, init.label))
  4303. {
  4304. delete plugin;
  4305. return nullptr;
  4306. }
  4307. plugin->reload();
  4308. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  4309. {
  4310. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4311. delete plugin;
  4312. return nullptr;
  4313. }
  4314. return plugin;
  4315. #else
  4316. init.engine->setLastError("LV2 support not available");
  4317. return nullptr;
  4318. #endif
  4319. }
  4320. CARLA_BACKEND_END_NAMESPACE