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.

5420 lines
196KB

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