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.

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