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.

5265 lines
190KB

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