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.

5313 lines
191KB

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