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.

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